Coordinator v2

Dependencies:   NerfUSXbee PinDetect EthernetInterface JSON MFRC522 WebSocketClient mbed-rtos mbed

src/GameCoordinator.cpp

Committer:
Ismael Balafrej
Date:
2017-04-10
Revision:
1:e1c5259b7d9a
Child:
2:019d8848cf7e

File content as of revision 1:e1c5259b7d9a:

#include "GameCoordinator.hpp"

GameCoordinator gameCoordinator;

//TODO: move that to .cpp 
GameCoordinator::GameCoordinator()
{
    //Adding all the game modes to the game_modes vector
    //TODO: can we avoid new operator ?
    ReflexMode *reflex_mode = new ReflexMode(&targets);
    SpeedMode *speed_mode = new SpeedMode(&targets);
    game_modes.push_back(reflex_mode);
    game_modes.push_back(speed_mode);

    //Starting internal thread
    Tget_next_round = Thread(osPriorityNormal, 800);
    Tget_next_round.start(this, &GameCoordinator::get_next_round);
}

void GameCoordinator::start_game(ServerData *configs)
{
    current_game = game_modes[configs->game_id];
    target_timeout = configs->max_reflex_time;
    number_of_targets = configs->number_of_target;
    ticker.attach(callback(this, &GameCoordinator::ticker_callback), current_game->getTimeBetweenTargets() / 1000.0);
}

void GameCoordinator::stop_game()
{
    ticker.detach();
    LPC_TIM3->TC = 0;
    //TODO: send stats back to server
}

void GameCoordinator::ticker_callback()
{
    Tget_next_round.signal_set(0x1);
}

//Thread
void GameCoordinator::get_next_round()
{
    while (1)
    {
        Thread::signal_wait(0x1);
        Target *nextTarget = current_game->GetNextTarget();
        if (nextTarget == NULL || number_of_targets-- <= 0)
        {
            stop_game();
        } 
        else
        {
            //TODO: how to select enemy or ally ?
            nextTarget->rise(rand() % 2, target_timeout);
        }
        Thread::yield();
    }
}