KlassischeKeplerKriege/game/state/commands.cpp

84 lines
2.2 KiB
C++

#include "commands.hpp"
#include <iostream>
#include "trace.hpp"
#include "util.hpp"
namespace game {
void ShootCommand::apply(Player *player, State *state) const
{
//std::cout<<"apply command " << name() << std::endl;
// TODO: idea
// shoot multiple rockets at once or from different positions after
// level up / upgrade ...
Missile *missile = new Missile(player, player->ship->position, util::deg2rad(m_angle), player->speed);
Trace *trace = new Trace(missile);
missile->trace = trace;
player->energy -= player->speed;
player->missiles.push_back(missile);
state->addTrace(trace);
}
bool ShootCommand::ready(const Player *player, const State *state) const
{
(void) state;
// TODO
return player->alive && player->energy >= player->speed;
//return player->alive;
}
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
{
std::cout<<"clearing traces!!!" << std::endl;
size_t i = 0;
while(i < state->traces.size()) {
Trace *trace = state->traces[i];
if (trace->missile == nullptr && trace->player == player) {
state->deleteTrace(i);
} else {
i++;
}
}
}
#if 0
bool TakeOverPlayerCommand::ready(const Player *player, const State *state) const
{
(void) state;
return state->havePlayerGrantFor(player->id, m_otherPlayerId);
}
void TakeOverPlayerCommand::apply(Player *player, State *state) const
{
if (
state->playerGrant(player->id, m_otherPlayerId);
}
#endif
}