KlassischeKeplerKriege/game/state/commands.cpp

93 lines
2.3 KiB
C++
Raw Normal View History

#include "commands.hpp"
2016-09-27 21:07:11 +00:00
#include <iostream>
#include "trace.hpp"
#include "util.hpp"
namespace game {
void ShootCommand::apply(Player *player, State *state) const
{
// TODO: idea
// shoot multiple rockets at once or from different positions after
// level up / upgrade ...
// angles are supplied in degrees and are CCW
Missile *missile = new Missile(
player,
player->ship->position,
-util::deg2rad(m_angle),
0.005*player->speed);
Trace *trace = new Trace(missile);
missile->trace = trace;
2016-09-27 20:35:16 +00:00
player->energy -= player->speed;
player->missiles.push_back(missile);
state->addTrace(trace);
}
2016-09-27 20:35:16 +00:00
bool ShootCommand::ready(const Player *player, const State *state) const
{
(void) state;
2016-09-27 20:35:16 +00:00
return player->alive && player->energy >= player->speed;
}
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;
if (m_speed < 0.0) {
return;
}
2016-09-27 20:35:16 +00:00
player->speed = m_speed;
}
void ClearTracesCommand::apply(Player *player, State *state) const
{
2016-09-27 21:07:11 +00:00
std::cout<<"clearing traces!!!" << std::endl;
std::vector<Trace*> rm;
2016-09-27 21:07:11 +00:00
for (Trace *trace : state->traces) {
if (trace->missile == nullptr && trace->player == player) {
rm.push_back(trace);
2016-09-27 20:35:16 +00:00
}
}
for (Trace *trace : rm) {
state->traces.remove(trace);
}
2016-09-27 20:35:16 +00:00
}
#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
}