commit d6aafe61abef841b65217c212df68249fe3a5dc2
parent 359cee0b96a22775d22ca9c07c6c2a671c57bc94
Author: amin <dev@aminmesbah.com>
Date: Wed, 12 Jun 2019 02:42:26 +0000
Add a very simple player jump
FossilOrigin-Name: fb8f6b8d1f23ad53adc5cb01cf6de4963cce3d2cadc4a1e9f42f18ba1ee499a2
Diffstat:
7 files changed, 80 insertions(+), 9 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,5 +1,5 @@
CC = clang
-CFLAGS = -std=c11 -Ilib -Wall -Wextra -Wshadow -Wswitch-enum -Wno-unused-parameter -Wno-unused-function -Wno-missing-braces
+CFLAGS = -std=c11 -Ilib -Wall -Wextra -Wshadow -Wno-unused-parameter -Wno-unused-function -Wno-missing-braces
LDFLAGS = -ldl -lglfw -lGL -lm
SRC_FILES = platform_linux.c
diff --git a/build.bat b/build.bat
@@ -9,7 +9,6 @@ set CFLAGS= ^
-Wall ^
-Wextra ^
-Wshadow ^
- -Wswitch-enum ^
-Wno-unused-parameter ^
-Wno-unused-function ^
-Wno-missing-braces
diff --git a/src/game.c b/src/game.c
@@ -6,6 +6,28 @@
#include "render.c"
#include "world.c"
+internal void physical_status_print(enum PhysicalStatus s)
+{
+ switch(s)
+ {
+ case PHYSICAL_STATUS_FALLING:
+ printf("FALLING\n");
+ break;
+ case PHYSICAL_STATUS_GROUNDED:
+ printf("GROUNDED\n");
+ break;
+ case PHYSICAL_STATUS_JUMPING:
+ printf("JUMPING\n");
+ break;
+ case PHYSICAL_STATUS_CLIMBING:
+ printf("CLIMBING\n");
+ break;
+ default:
+ printf("INVALID\n");
+ break;
+ }
+}
+
internal void game_init(struct GameMemory *game_memory, v2u framebuffer)
{
assert(sizeof(struct GameState) <= game_memory->buffer_size);
@@ -20,6 +42,7 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer)
// In Knytt, the player is 9 by 14 texels and a tile is 24 by 24 texels.
// These dimensions are relative to a square 'meter', one tile
game_state->player.dimensions = (v2) {0.375f, 0.583f};
+ game_state->player.physical_status = PHYSICAL_STATUS_FALLING;
mem_st_init(
&game_state->world_allocator,
@@ -171,6 +194,7 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga
// game_update_player
{
struct Entity *player = &game_state->player;
+ physical_status_print(player->physical_status);
f32 dt = game_input->dt;
if (dt >= 0.5f)
{
@@ -185,18 +209,37 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga
f32 acceleration_rate = 50.0f;
f32 friction = 0.7f;
- if (game_input->key_up)
+ if (player->physical_status == PHYSICAL_STATUS_FALLING)
{
- player->acceleration.y = acceleration_rate;
+ player->acceleration.y = -acceleration_rate;
}
- else if (game_input->key_down)
+ else if (player->physical_status == PHYSICAL_STATUS_CLIMBING)
{
- player->acceleration.y = -acceleration_rate;
+ if (game_input->key_up)
+ {
+ player->acceleration.y = acceleration_rate;
+ }
+ else if (game_input->key_down)
+ {
+ player->acceleration.y = -acceleration_rate;
+ }
+ else
+ {
+ player->acceleration.y = 0.0f;
+ player->velocity.y = player->velocity.y * friction;
+ }
}
- else
+ else if (player->physical_status == PHYSICAL_STATUS_GROUNDED || player->physical_status == PHYSICAL_STATUS_JUMPING)
{
- player->acceleration.y = 0.0f;
- player->velocity.y = player->velocity.y * friction;
+ if (game_input->key_jump)
+ {
+ player->acceleration.y = acceleration_rate;
+ player->physical_status = PHYSICAL_STATUS_JUMPING;
+ }
+ else
+ {
+ player->physical_status = PHYSICAL_STATUS_FALLING;
+ }
}
if (game_input->key_left)
@@ -250,6 +293,8 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga
{
v2 wall_normal = {0};
f32 smallest_distance_scale_factor = 1.0f;
+ bool collision_occurred = false;
+ enum MathRectEdge collided_edge = 0;
for (i32 tile_y = tile_search_range.min.y; tile_y < tile_search_range.max.y; tile_y++)
{
for (i32 tile_x = tile_search_range.min.x; tile_x < tile_search_range.max.x; tile_x++)
@@ -298,9 +343,11 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga
struct WallCollision c = get_wall_collision(player->pos.local, new_p, player_sum_wall, normal);
if (c.collision_occurred)
{
+ collision_occurred = true;
if (smallest_distance_scale_factor > c.distance_scale_factor)
{
smallest_distance_scale_factor = c.distance_scale_factor;
+ collided_edge = edge;
}
wall_normal = normal;
RENDER_COLLISION_DEBUG_QUAD(
@@ -316,6 +363,13 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga
}
}
+ if (collision_occurred
+ && collided_edge == RECT_EDGE_TOP
+ && player->physical_status == PHYSICAL_STATUS_FALLING)
+ {
+ player->physical_status = PHYSICAL_STATUS_GROUNDED;
+ }
+
v2 delta_to_nearest_collision = math_v2f_m(player_delta, smallest_distance_scale_factor);
v2 nearest_collision_p = math_v2_a(player->pos.local, delta_to_nearest_collision);
diff --git a/src/game.h b/src/game.h
@@ -37,12 +37,23 @@ struct AbsolutePos
v2 local;
};
+enum PhysicalStatus
+{
+ PHYSICAL_STATUS_FALLING,
+ PHYSICAL_STATUS_GROUNDED,
+ PHYSICAL_STATUS_JUMPING,
+ PHYSICAL_STATUS_CLIMBING,
+
+ MAX_PHYSICAL_STATUS,
+};
+
struct Entity
{
struct AbsolutePos pos;
v2 acceleration;
v2 velocity;
v2 dimensions;
+ enum PhysicalStatus physical_status;
};
struct GameState
diff --git a/src/platform.h b/src/platform.h
@@ -21,6 +21,7 @@ struct GameInput
enum InputKeyAction key_right;
enum InputKeyAction key_up;
enum InputKeyAction key_down;
+ enum InputKeyAction key_jump;
};
#define PLATFORM_READ_ENTIRE_FILE(name) char *(name)(char *file_path)
diff --git a/src/platform_linux.c b/src/platform_linux.c
@@ -178,6 +178,9 @@ internal void key_callback(GLFWwindow* window, int key, int scancode, int action
case GLFW_KEY_DOWN:
game_key = &(game_input->key_down);
break;
+ case GLFW_KEY_S:
+ game_key = &(game_input->key_jump);
+ break;
default:
break;
}
diff --git a/src/platform_windows.c b/src/platform_windows.c
@@ -158,6 +158,9 @@ internal void key_callback(GLFWwindow* window, int key, int scancode, int action
case GLFW_KEY_DOWN:
game_key = &(game_input->key_down);
break;
+ case GLFW_KEY_S:
+ game_key = &(game_input->key_jump);
+ break;
default:
break;
}