Coordinator v2
Dependencies: NerfUSXbee PinDetect EthernetInterface JSON MFRC522 WebSocketClient mbed-rtos mbed
src/GameCoordinator.cpp@1:e1c5259b7d9a, 2017-04-10 (annotated)
- Committer:
- Ismael Balafrej
- Date:
- Mon Apr 10 15:02:24 2017 -0400
- Revision:
- 1:e1c5259b7d9a
- Child:
- 2:019d8848cf7e
V1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ismael Balafrej |
1:e1c5259b7d9a | 1 | #include "GameCoordinator.hpp" |
Ismael Balafrej |
1:e1c5259b7d9a | 2 | |
Ismael Balafrej |
1:e1c5259b7d9a | 3 | GameCoordinator gameCoordinator; |
Ismael Balafrej |
1:e1c5259b7d9a | 4 | |
Ismael Balafrej |
1:e1c5259b7d9a | 5 | //TODO: move that to .cpp |
Ismael Balafrej |
1:e1c5259b7d9a | 6 | GameCoordinator::GameCoordinator() |
Ismael Balafrej |
1:e1c5259b7d9a | 7 | { |
Ismael Balafrej |
1:e1c5259b7d9a | 8 | //Adding all the game modes to the game_modes vector |
Ismael Balafrej |
1:e1c5259b7d9a | 9 | //TODO: can we avoid new operator ? |
Ismael Balafrej |
1:e1c5259b7d9a | 10 | ReflexMode *reflex_mode = new ReflexMode(&targets); |
Ismael Balafrej |
1:e1c5259b7d9a | 11 | SpeedMode *speed_mode = new SpeedMode(&targets); |
Ismael Balafrej |
1:e1c5259b7d9a | 12 | game_modes.push_back(reflex_mode); |
Ismael Balafrej |
1:e1c5259b7d9a | 13 | game_modes.push_back(speed_mode); |
Ismael Balafrej |
1:e1c5259b7d9a | 14 | |
Ismael Balafrej |
1:e1c5259b7d9a | 15 | //Starting internal thread |
Ismael Balafrej |
1:e1c5259b7d9a | 16 | Tget_next_round = Thread(osPriorityNormal, 800); |
Ismael Balafrej |
1:e1c5259b7d9a | 17 | Tget_next_round.start(this, &GameCoordinator::get_next_round); |
Ismael Balafrej |
1:e1c5259b7d9a | 18 | } |
Ismael Balafrej |
1:e1c5259b7d9a | 19 | |
Ismael Balafrej |
1:e1c5259b7d9a | 20 | void GameCoordinator::start_game(ServerData *configs) |
Ismael Balafrej |
1:e1c5259b7d9a | 21 | { |
Ismael Balafrej |
1:e1c5259b7d9a | 22 | current_game = game_modes[configs->game_id]; |
Ismael Balafrej |
1:e1c5259b7d9a | 23 | target_timeout = configs->max_reflex_time; |
Ismael Balafrej |
1:e1c5259b7d9a | 24 | number_of_targets = configs->number_of_target; |
Ismael Balafrej |
1:e1c5259b7d9a | 25 | ticker.attach(callback(this, &GameCoordinator::ticker_callback), current_game->getTimeBetweenTargets() / 1000.0); |
Ismael Balafrej |
1:e1c5259b7d9a | 26 | } |
Ismael Balafrej |
1:e1c5259b7d9a | 27 | |
Ismael Balafrej |
1:e1c5259b7d9a | 28 | void GameCoordinator::stop_game() |
Ismael Balafrej |
1:e1c5259b7d9a | 29 | { |
Ismael Balafrej |
1:e1c5259b7d9a | 30 | ticker.detach(); |
Ismael Balafrej |
1:e1c5259b7d9a | 31 | LPC_TIM3->TC = 0; |
Ismael Balafrej |
1:e1c5259b7d9a | 32 | //TODO: send stats back to server |
Ismael Balafrej |
1:e1c5259b7d9a | 33 | } |
Ismael Balafrej |
1:e1c5259b7d9a | 34 | |
Ismael Balafrej |
1:e1c5259b7d9a | 35 | void GameCoordinator::ticker_callback() |
Ismael Balafrej |
1:e1c5259b7d9a | 36 | { |
Ismael Balafrej |
1:e1c5259b7d9a | 37 | Tget_next_round.signal_set(0x1); |
Ismael Balafrej |
1:e1c5259b7d9a | 38 | } |
Ismael Balafrej |
1:e1c5259b7d9a | 39 | |
Ismael Balafrej |
1:e1c5259b7d9a | 40 | //Thread |
Ismael Balafrej |
1:e1c5259b7d9a | 41 | void GameCoordinator::get_next_round() |
Ismael Balafrej |
1:e1c5259b7d9a | 42 | { |
Ismael Balafrej |
1:e1c5259b7d9a | 43 | while (1) |
Ismael Balafrej |
1:e1c5259b7d9a | 44 | { |
Ismael Balafrej |
1:e1c5259b7d9a | 45 | Thread::signal_wait(0x1); |
Ismael Balafrej |
1:e1c5259b7d9a | 46 | Target *nextTarget = current_game->GetNextTarget(); |
Ismael Balafrej |
1:e1c5259b7d9a | 47 | if (nextTarget == NULL || number_of_targets-- <= 0) |
Ismael Balafrej |
1:e1c5259b7d9a | 48 | { |
Ismael Balafrej |
1:e1c5259b7d9a | 49 | stop_game(); |
Ismael Balafrej |
1:e1c5259b7d9a | 50 | } |
Ismael Balafrej |
1:e1c5259b7d9a | 51 | else |
Ismael Balafrej |
1:e1c5259b7d9a | 52 | { |
Ismael Balafrej |
1:e1c5259b7d9a | 53 | //TODO: how to select enemy or ally ? |
Ismael Balafrej |
1:e1c5259b7d9a | 54 | nextTarget->rise(rand() % 2, target_timeout); |
Ismael Balafrej |
1:e1c5259b7d9a | 55 | } |
Ismael Balafrej |
1:e1c5259b7d9a | 56 | Thread::yield(); |
Ismael Balafrej |
1:e1c5259b7d9a | 57 | } |
Ismael Balafrej |
1:e1c5259b7d9a | 58 | } |