begin applying tick events to players

This commit is contained in:
Josh Holtrop 2013-03-10 22:31:08 -04:00
parent 89c88b3469
commit f3c0f42689
2 changed files with 53 additions and 7 deletions

View File

@ -2,6 +2,7 @@ import inputs;
static enum uint TICK_MS = 50; static enum uint TICK_MS = 50;
static enum int NUM_PLAYERS = 2; static enum int NUM_PLAYERS = 2;
static double MOVE_SPEED = 200.0;
struct Player struct Player
{ {
@ -16,16 +17,35 @@ public:
{ {
for (int i = 0; i < NUM_PLAYERS; i++) for (int i = 0; i < NUM_PLAYERS; i++)
{ {
m_events[i] = new InputEventList(); m_event_lists[i] = new InputEventList();
} }
} }
void change_player() void change_player()
{ {
m_ticks[m_pindex] = m_tick;
m_pindex ^= 1; m_pindex ^= 1;
uint min_tick = m_tick;
for (int i = 0; i < NUM_PLAYERS; i++)
{
if (m_ticks[i] < min_tick)
min_tick = m_ticks[i];
}
for (int i = 0; i < NUM_PLAYERS; i++)
{
m_event_lists[i].remove_older_than(min_tick);
}
for (int i = 0; i < NUM_PLAYERS; i++)
{
m_events[i] = m_event_lists[i].get_first_event();
}
} }
void handle_move_event(int type, bool active) void handle_move_event(int type, bool active)
{ {
m_events[m_pindex].add_event(m_tick + 1, type, active); m_event_lists[m_pindex].add_event(m_tick + 1, type, active);
if (m_events[m_pindex] is null)
{
m_events[m_pindex] = m_event_lists[m_pindex].get_first_event();
}
} }
void update(uint ms) void update(uint ms)
{ {
@ -33,24 +53,50 @@ public:
{ {
m_last_tick_ms += TICK_MS; m_last_tick_ms += TICK_MS;
m_tick++; m_tick++;
uint min_tick = m_tick;
for (int i = 0; i < NUM_PLAYERS; i++) for (int i = 0; i < NUM_PLAYERS; i++)
{ {
apply_tick(m_players[i]); apply_tick(i);
} }
} }
} }
Player[] get_players() { return m_players; } Player[] get_players() { return m_players; }
protected: protected:
void apply_tick(Player p) void apply_tick(int pindex)
{ {
while ((m_events[pindex] !is null) &&
(m_events[pindex].next !is null) &&
(m_events[pindex].next.tick <= m_tick))
{
m_events[pindex] = m_events[pindex].next;
}
if (m_events[pindex] !is null)
{
InputEvent ie = m_events[pindex];
if (ie.inputs[INPUT_LEFT])
{
m_players[pindex].x -= MOVE_SPEED * TICK_MS / 1000;
}
if (ie.inputs[INPUT_RIGHT])
{
m_players[pindex].x += MOVE_SPEED * TICK_MS / 1000;
}
if (ie.inputs[INPUT_UP])
{
m_players[pindex].y += MOVE_SPEED * TICK_MS / 1000;
}
if (ie.inputs[INPUT_DOWN])
{
m_players[pindex].y -= MOVE_SPEED * TICK_MS / 1000;
}
}
} }
uint m_tick; uint m_tick;
uint m_last_tick_ms; uint m_last_tick_ms;
Player[NUM_PLAYERS] m_players; Player[NUM_PLAYERS] m_players;
InputEventList[NUM_PLAYERS] m_events; InputEventList[NUM_PLAYERS] m_event_lists;
InputEvent[NUM_PLAYERS] m_events;
uint m_ticks[NUM_PLAYERS]; uint m_ticks[NUM_PLAYERS];
int m_pindex; int m_pindex;
}; };

View File

@ -44,7 +44,7 @@ public:
InputEvent get_first_event() { return m_events; } InputEvent get_first_event() { return m_events; }
void remove_older_than(uint tick) void remove_older_than(uint tick)
{ {
while ((m_events !is null) && (m_events.tick <= tick)) while ((m_events !is null) && (m_events.tick < tick))
{ {
m_events = m_events.next; m_events = m_events.next;
} }