2016-09-27 16:23:58 +00:00
|
|
|
#include "commands.hpp"
|
|
|
|
|
2016-09-27 18:13:09 +00:00
|
|
|
#include "trace.hpp"
|
|
|
|
|
2016-09-27 16:23:58 +00:00
|
|
|
namespace game {
|
|
|
|
void ShootCommand::apply(Player *player, State *state) const
|
|
|
|
{
|
|
|
|
//std::cout<<"apply command " << name() << std::endl;
|
|
|
|
|
2016-09-27 18:13:09 +00:00
|
|
|
// TODO: idea
|
|
|
|
// shoot multiple rockets at once or from different positions after
|
|
|
|
// level up / upgrade ...
|
2016-09-27 20:35:16 +00:00
|
|
|
Missile *missile = new Missile(player, player->ship->position, m_angle, player->speed);
|
2016-09-27 18:13:09 +00:00
|
|
|
|
|
|
|
Trace *trace = new Trace(missile);
|
|
|
|
missile->trace = trace;
|
|
|
|
|
2016-09-27 20:35:16 +00:00
|
|
|
player->energy -= player->speed;
|
2016-09-27 18:13:09 +00:00
|
|
|
player->missiles.push_back(missile);
|
|
|
|
|
|
|
|
state->addTrace(trace);
|
2016-09-27 16:23:58 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 20:35:16 +00:00
|
|
|
bool ShootCommand::ready(const Player *player, const State *state) const
|
2016-09-27 16:23:58 +00:00
|
|
|
{
|
|
|
|
(void) state;
|
|
|
|
|
|
|
|
// TODO
|
2016-09-27 20:35:16 +00:00
|
|
|
return player->alive && player->energy >= player->speed;
|
2016-09-27 16:23:58 +00:00
|
|
|
//return player->alive;
|
|
|
|
}
|
2016-09-27 20:35:16 +00:00
|
|
|
|
|
|
|
void ChangeNameCommand::apply(Player *player, State *state) const
|
|
|
|
{
|
|
|
|
// discard if not unique
|
|
|
|
for (const Player *other : state->players) {
|
|
|
|
if (m_name == other->name) {
|
|
|
|
std::cout << "name '" << m_name << "' already given to player #" << other->id << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
player->name = m_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetSpeedCommand::apply(Player *player, State *state) const
|
|
|
|
{
|
|
|
|
(void) state;
|
|
|
|
player->speed = m_speed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearTracesCommand::apply(Player *player, State *state) const
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
while(i < state->traces.size()) {
|
|
|
|
Trace *trace = state->traces[i];
|
|
|
|
if (trace->missile == nullptr && trace->missile->player == player) {
|
|
|
|
state->deleteTrace(trace);
|
|
|
|
} else {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-27 16:23:58 +00:00
|
|
|
}
|