dtimegame/src/game.d

77 lines
1.4 KiB
D

static enum uint TICK_MS = 50;
static enum int MOVE_PER_TICK = 1;
enum
{
MOVE_LEFT,
MOVE_RIGHT,
MOVE_UP,
MOVE_DOWN
}
struct Player
{
int x;
int y;
};
struct MoveEvent
{
int type;
bool active;
uint tick;
};
class Game
{
public:
void change_player()
{
m_pindex ^= 1;
}
void handle_move_event(int type, bool active)
{
final switch (type)
{
case MOVE_LEFT:
m_left = active;
break;
case MOVE_RIGHT:
m_right = active;
break;
case MOVE_UP:
m_up = active;
break;
case MOVE_DOWN:
m_down = active;
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;
};