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

source/TargetManager.cpp

Committer:
Maxime Dupuis
Date:
2017-03-30
Revision:
35:7ee45f6d6484
Parent:
34:85994e0501fb
Child:
36:f55ce07292c9

File content as of revision 35:7ee45f6d6484:

#include "TargetManager.hpp"

TargetManager::TargetManager(std::vector<TargetInterface*>& targets,
		NerfusTickerInterface& nerfus_ticker) :
	targets(targets),
	nerfus_ticker(nerfus_ticker)
{
	for(int i=0; i<targets.size(); i++)
	{
		is_active_target.push_back(false);
	}
}

void TargetManager::execute(const TargetInfo& target_info)
{
	TargetInterface& target = *(targets[target_info.id]);

	if(target_info.type == TARGET_TYPE_ALLY)
	{
		target.ally_command();
	}
	else
	{
		target.enemy_command();
	}

	is_active_target[target_info.id] = true;

	nerfus_ticker.start(target_info.timeout_ms);
}

void TargetManager::execute(const std::vector<uint8_t>& target_info_bytes)
{
	const int id = target_info_bytes[1];
	const TargetType type = (target_info_bytes[2] == 0) ? TARGET_TYPE_ALLY : TARGET_TYPE_ENEMY;
	const int timeout_msb = target_info_bytes[3];
	const int timeout_lsb = target_info_bytes[4];
	const int timeout = (timeout_msb << 8) + timeout_lsb;
	const TargetInfo target_info = make_TargetInfo(id, type, timeout);
	execute(target_info);
}

void TargetManager::target_hit(int target_number)
{
	if(is_active_target[target_number])
	{
		targets[target_number]->hit(nerfus_ticker.get_time_ms());
		nerfus_ticker.stop();
		is_active_target[target_number] = false;
	}
}

void TargetManager::target_missed(int target_number)
{
	if(is_active_target[target_number])
	{
		targets[target_number]->timeout(nerfus_ticker.get_time_ms());
		nerfus_ticker.stop();
		is_active_target[target_number] = false;
	}
}