commit 9dcb2e7bd8f5f0e2651daec0f7a8705e07dda8d3
parent e1f5747d5b32081cc29bc8a2a8072c77ee508a1f
Author: amin <dev@aminmesbah.com>
Date: Sat, 9 Nov 2019 07:21:27 +0000
Add a rudimentary wall jump
The player is allowed 100 ms after letting go of a wall or walking off a
ledge where they can still jump.
FossilOrigin-Name: a51676db8a02688b4f25aabc41231793663478fc19bbcce2f3c54d7cca68833d
Diffstat:
2 files changed, 38 insertions(+), 0 deletions(-)
diff --git a/src/game.c b/src/game.c
@@ -14,6 +14,24 @@
#include "w_tutorial.h"
+internal inline bool timer_is_expired(struct Timer t)
+{
+ return t.elapsed_ms >= t.limit_ms;
+}
+
+internal inline void timer_init(struct Timer *t, i64 limit_ms)
+{
+ *t = (struct Timer) {
+ .elapsed_ms = 0,
+ .limit_ms = limit_ms,
+ };
+}
+
+internal inline void timer_update(struct Timer *t, i64 elapsed_ms)
+{
+ t->elapsed_ms += elapsed_ms;
+}
+
internal void move_mode_print(enum MoveMode s)
{
switch(s)
@@ -162,6 +180,9 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga
struct GameState *game_state = (struct GameState *)game_memory->buffer;
f32 dt = game_input->dt;
+ // TODO: pass total elapsed ms in as an i64. Use that for the timer.
+ timer_update(&game_state->jump_allowance_timer, (i64)(dt * 1000.0f));
+
rect viewport = {0};
f32 ppm = 0.0f;
// game_update_viewport
@@ -277,6 +298,13 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga
switch(player->move_mode)
{
case MOVE_MODE_FALLING:
+ if (!timer_is_expired(game_state->jump_allowance_timer))
+ {
+ if (btn_was_just_pressed(game_input, BTN_JUMP))
+ {
+ player->move_mode = MOVE_MODE_JUMPING;
+ }
+ }
break;
case MOVE_MODE_GROUNDED:
if(entity_is_adjacent_to_solid_tiles(game_state->world, player->pos, player_rect, DIR_DOWN))
@@ -288,6 +316,7 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga
}
else
{
+ timer_init(&game_state->jump_allowance_timer, JUMP_ALLOWANCE_MS);
player->move_mode = MOVE_MODE_FALLING;
}
break;
@@ -338,6 +367,7 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga
{
player->acceleration.y = 0.0f;
player->velocity.y = 0.0f;
+ timer_init(&game_state->jump_allowance_timer, JUMP_ALLOWANCE_MS);
player->move_mode = MOVE_MODE_FALLING;
}
break;
diff --git a/src/game.h b/src/game.h
@@ -89,6 +89,7 @@ static_assert(-1 == ~0, "Implementation doesn't use two's complement");
// One tile is one square meter
#define TILE_SIZE 1.0f
+#define JUMP_ALLOWANCE_MS 100
// TODO: Move this to world.h
struct AbsolutePos
@@ -125,12 +126,19 @@ struct Entity
f32 jump_timeout;
};
+struct Timer
+{
+ i64 elapsed_ms;
+ i64 limit_ms;
+};
+
struct GameState
{
struct RendererState renderer;
struct Entity player;
struct StackAllocator temp_allocator;
struct StackAllocator world_allocator;
+ struct Timer jump_allowance_timer;
struct World *world;
};