handle movement events more smoothly
This commit is contained in:
parent
ba0fea06fe
commit
a29acd2619
41
src/game.d
41
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;
|
||||
};
|
||||
|
@ -137,6 +137,7 @@ int main(char[][] args)
|
||||
}
|
||||
if (exit)
|
||||
break;
|
||||
game.update(SDL_GetTicks());
|
||||
display(game);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user