From a29acd2619fdf88ca55ca4b6364f7179ac13061a Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 18 Feb 2013 22:28:34 -0500 Subject: [PATCH] handle movement events more smoothly --- src/game.d | 41 ++++++++++++++++++++++++++++++++++++++--- src/main.d | 1 + 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/game.d b/src/game.d index 2e8e97f..34be5a3 100644 --- a/src/game.d +++ b/src/game.d @@ -1,4 +1,5 @@ static enum uint TICK_MS = 50; +static enum int MOVE_PER_TICK = 1; enum { @@ -26,16 +27,50 @@ class Game public: void change_player() { - m_pindex = (m_pindex + 1) % 2; + m_pindex ^= 1; } void handle_move_event(int type, bool stop) { - m_players[m_pindex].x += [-10, 10, 0, 0][type]; - m_players[m_pindex].y += [0, 0, 10, -10][type]; + final switch (type) + { + case MOVE_LEFT: + m_left = !stop; + break; + case MOVE_RIGHT: + m_right = !stop; + break; + case MOVE_UP: + m_up = !stop; + break; + case MOVE_DOWN: + m_down = !stop; + break; + } + } + void update(uint ms) + { + while (ms > (m_last_tick_ms + TICK_MS)) + { + if (m_left) + m_players[m_pindex].x -= MOVE_PER_TICK; + if (m_right) + m_players[m_pindex].x += MOVE_PER_TICK; + if (m_up) + m_players[m_pindex].y += MOVE_PER_TICK; + if (m_down) + m_players[m_pindex].y -= MOVE_PER_TICK; + m_last_tick_ms += TICK_MS; + m_tick++; + } } Player[] get_players() { return m_players; } protected: + bool m_left; + bool m_right; + bool m_up; + bool m_down; uint m_tick; + uint m_last_tick_ms; Player[2] m_players; int m_pindex; }; diff --git a/src/main.d b/src/main.d index 1fa79fa..b66b324 100644 --- a/src/main.d +++ b/src/main.d @@ -137,6 +137,7 @@ int main(char[][] args) } if (exit) break; + game.update(SDL_GetTicks()); display(game); }