NerfUS mobile node that manages a target for the Nerf gun firing range

Dependencies:   LedController mbed-rtos mbed NerfUSXbee Servomotor TargetManager

Fork of NerfUS by NerfUS

Committer:
Maxime Dupuis
Date:
Mon Mar 27 20:39:08 2017 -0400
Revision:
32:50b777bab5a4
Parent:
30:412a779cf607
Child:
34:85994e0501fb
Implement TargetManager

A TargetManager manages a group of targets, listens for
events (hit/miss) and dispatch them to the appropriate target

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Maxime Dupuis 28:8fc53a870691 1 #include "TargetManager.hpp"
Maxime Dupuis 28:8fc53a870691 2
Maxime Dupuis 30:412a779cf607 3 TargetManager::TargetManager(std::vector<TargetInterface*>& targets,
Maxime Dupuis 30:412a779cf607 4 NerfusTickerInterface& nerfus_ticker) :
Maxime Dupuis 30:412a779cf607 5 targets(targets),
Maxime Dupuis 30:412a779cf607 6 nerfus_ticker(nerfus_ticker)
Maxime Dupuis 30:412a779cf607 7 {
Maxime Dupuis 32:50b777bab5a4 8 for(int i=0; i<targets.size(); i++)
Maxime Dupuis 32:50b777bab5a4 9 {
Maxime Dupuis 32:50b777bab5a4 10 is_active_target.push_back(false);
Maxime Dupuis 32:50b777bab5a4 11 }
Maxime Dupuis 32:50b777bab5a4 12 }
Maxime Dupuis 30:412a779cf607 13
Maxime Dupuis 32:50b777bab5a4 14 void TargetManager::execute(const std::vector<TargetInfo> sequence)
Maxime Dupuis 32:50b777bab5a4 15 {
Maxime Dupuis 32:50b777bab5a4 16 for(int i=0; i<sequence.size(); i++)
Maxime Dupuis 32:50b777bab5a4 17 {
Maxime Dupuis 32:50b777bab5a4 18 const TargetInfo& target_info = sequence[i];
Maxime Dupuis 32:50b777bab5a4 19 TargetInterface& target = *(targets[target_info.id]);
Maxime Dupuis 32:50b777bab5a4 20
Maxime Dupuis 32:50b777bab5a4 21 if(target_info.type == TARGET_TYPE_ALLY)
Maxime Dupuis 32:50b777bab5a4 22 {
Maxime Dupuis 32:50b777bab5a4 23 target.ally_command();
Maxime Dupuis 32:50b777bab5a4 24 }
Maxime Dupuis 32:50b777bab5a4 25 else
Maxime Dupuis 32:50b777bab5a4 26 {
Maxime Dupuis 32:50b777bab5a4 27 target.enemy_command();
Maxime Dupuis 32:50b777bab5a4 28 }
Maxime Dupuis 32:50b777bab5a4 29
Maxime Dupuis 32:50b777bab5a4 30 is_active_target[target_info.id] = true;
Maxime Dupuis 32:50b777bab5a4 31
Maxime Dupuis 32:50b777bab5a4 32 nerfus_ticker.start(target_info.timeout_ms);
Maxime Dupuis 32:50b777bab5a4 33 }
Maxime Dupuis 30:412a779cf607 34 }
Maxime Dupuis 30:412a779cf607 35
Maxime Dupuis 28:8fc53a870691 36 void TargetManager::target_hit(int target_number)
Maxime Dupuis 28:8fc53a870691 37 {
Maxime Dupuis 32:50b777bab5a4 38 if(is_active_target[target_number])
Maxime Dupuis 32:50b777bab5a4 39 {
Maxime Dupuis 32:50b777bab5a4 40 targets[target_number]->hit(nerfus_ticker.get_time_ms());
Maxime Dupuis 32:50b777bab5a4 41 nerfus_ticker.stop();
Maxime Dupuis 32:50b777bab5a4 42 is_active_target[target_number] = false;
Maxime Dupuis 32:50b777bab5a4 43 }
Maxime Dupuis 28:8fc53a870691 44 }
Maxime Dupuis 28:8fc53a870691 45
Maxime Dupuis 28:8fc53a870691 46 void TargetManager::target_missed(int target_number)
Maxime Dupuis 28:8fc53a870691 47 {
Maxime Dupuis 32:50b777bab5a4 48 if(is_active_target[target_number])
Maxime Dupuis 32:50b777bab5a4 49 {
Maxime Dupuis 32:50b777bab5a4 50 targets[target_number]->timeout(nerfus_ticker.get_time_ms());
Maxime Dupuis 32:50b777bab5a4 51 nerfus_ticker.stop();
Maxime Dupuis 32:50b777bab5a4 52 is_active_target[target_number] = false;
Maxime Dupuis 32:50b777bab5a4 53 }
Maxime Dupuis 28:8fc53a870691 54 }
Maxime Dupuis 28:8fc53a870691 55