added a few more commands.

This commit is contained in:
Andreas Ortmann 2016-09-27 22:35:16 +02:00
parent 6a33c4abc6
commit 67f43de22c
8 changed files with 147 additions and 24 deletions

View file

@ -9,7 +9,7 @@
Game::Game() Game::Game()
{ {
// advance simulation with 100 Hz // advance simulation with 100 Hz
m_time_step = 1.0 / 100.0; m_time_step = 1.0 / 100.0;
m_time_for_next_step = 0.0; m_time_for_next_step = 0.0;
m_state = new game::State(); m_state = new game::State();
@ -33,13 +33,9 @@ bool Game::cycle(float dt)
float a = 2.0 * M_PI * util::randf_0_1(); float a = 2.0 * M_PI * util::randf_0_1();
float speed = 0.005; float speed = 0.005;
m_state->players[0]->addCommand(new game::ShootCommand(a, speed));
}
#if 1 m_state->players[0]->addCommand(new game::SetSpeedCommand(speed));
if (dt >= 10.0) { m_state->players[0]->addCommand(new game::ShootCommand(a));
//std::cout<<"time to big: " << dt << std::endl;
dt = m_time_step;
} }
//std::cout<<"adding dt: " << dt << std::endl; //std::cout<<"adding dt: " << dt << std::endl;
@ -57,11 +53,4 @@ bool Game::cycle(float dt)
//std::cout << m_time_for_next_step << " s remaining time, " << steps << " steps taken." << std::endl; //std::cout << m_time_for_next_step << " s remaining time, " << steps << " steps taken." << std::endl;
return true; return true;
#else
(void) dt;
m_state->advance(dt);
return true;
#endif
} }

View file

@ -10,23 +10,55 @@ namespace game {
// TODO: idea // TODO: idea
// shoot multiple rockets at once or from different positions after // shoot multiple rockets at once or from different positions after
// level up / upgrade ... // level up / upgrade ...
Missile *missile = new Missile(player, player->ship->position, m_angle, m_speed); Missile *missile = new Missile(player, player->ship->position, m_angle, player->speed);
Trace *trace = new Trace(missile); Trace *trace = new Trace(missile);
missile->trace = trace; missile->trace = trace;
player->energy -= m_speed; player->energy -= player->speed;
player->missiles.push_back(missile); player->missiles.push_back(missile);
state->addTrace(trace); state->addTrace(trace);
} }
bool ShootCommand::allowed(const Player *player, const State *state) const bool ShootCommand::ready(const Player *player, const State *state) const
{ {
(void) state; (void) state;
// TODO // TODO
return player->alive && player->energy >= m_speed; return player->alive && player->energy >= player->speed;
//return player->alive; //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
{
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++;
}
}
}
} }

View file

@ -9,6 +9,10 @@
#include <iostream> #include <iostream>
namespace game { namespace game {
/**
* Base class for commands.
* must derive from this.
*/
class Command { class Command {
public: public:
Command() Command()
@ -19,7 +23,11 @@ namespace game {
{ {
} }
virtual bool allowed(const Player *player, const State *state) const // check whether the command is ready to execute.
// if not, wait.
// note: stuff like for admin should be done in execute() to discard the
// command and not block the queue.
virtual bool ready(const Player *player, const State *state) const
{ {
(void) player; (void) player;
(void) state; (void) state;
@ -38,19 +46,74 @@ namespace game {
virtual std::string name() const { return "<unnamed>"; } virtual std::string name() const { return "<unnamed>"; }
}; };
/**********************************************************************/
/* Implemented commands */
/**********************************************************************/
/*
* Shoot at angle and optional speed.
* TODO
*/
class ShootCommand : public Command { class ShootCommand : public Command {
public: public:
ShootCommand(float angle, float speed) : m_angle(angle), m_speed(speed) ShootCommand(float angle) : m_angle(angle)
{ {
} }
std::string name() const { return "<shoot>"; } std::string name() const { return "<shoot>"; }
void apply( Player *player, State *state) const; void apply( Player *player, State *state) const;
bool allowed(const Player *player, const State *state) const; bool ready(const Player *player, const State *state) const;
private: private:
float m_angle; float m_angle;
};
/**
* Change the name of the player if it will be uniuqe.
*/
class ChangeNameCommand : public Command {
public:
ChangeNameCommand(const std::string &name) : m_name(name)
{
}
std::string name() const { return "<change name>"; }
void apply(Player *player, State *state) const;
private:
std::string m_name;
};
/**
* clear all traces of this player that are done
*/
class ClearTracesCommand : public Command {
public:
ClearTracesCommand()
{
}
std::string name() const { return "<clear traces>"; }
void apply(Player *player, State *state) const;
};
/**
* Set default speed of next shots for this player.
*/
class SetSpeedCommand : public Command {
public:
SetSpeedCommand(float speed) : m_speed(speed)
{
}
std::string name() const { return "<set speed>"; }
void apply(Player *player, State *state) const;
private:
float m_speed; float m_speed;
}; };
} }

View file

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <string>
#include <deque> #include <deque>
#include <vector> #include <vector>
@ -15,12 +16,14 @@ namespace game {
public: public:
int id; int id;
bool alive; bool alive;
float speed;
float energy; float energy;
float deadTimeCounter; float deadTimeCounter;
Ship *ship; Ship *ship;
std::string name;
std::vector<Missile*> missiles; std::vector<Missile*> missiles;
Player(int id) : id(id), alive(true), energy(0.0), ship(nullptr) Player(int id) : id(id), alive(true), speed(0.01), energy(0.0), ship(nullptr), name("<unnamed>")
{ {
} }

View file

@ -108,7 +108,7 @@ namespace game {
// try to execute as much queued commands as possible. // try to execute as much queued commands as possible.
while (player->hasCommandInQueue()) { while (player->hasCommandInQueue()) {
Command *command = player->peekCommand(); Command *command = player->peekCommand();
if (!command->allowed(player, this)) { if (!command->ready(player, this)) {
break; break;
} }
@ -290,6 +290,20 @@ namespace game {
traces.push_back(trace); traces.push_back(trace);
} }
void State::deleteTrace(Trace *trace)
{
// TODO: if traces of running missile is deleted it is dangerous
size_t i = 0;
while(i < traces.size()) {
if (traces[i] == trace) {
delete(trace);
traces.erase(traces.begin()+i);
break;
}
}
}
void State::addExplosionFromHit(const Missile::Event *evt) void State::addExplosionFromHit(const Missile::Event *evt)
{ {
if (evt->hit == Hit::Nothing || evt->hit == Hit::BorderOfUniverse) { if (evt->hit == Hit::Nothing || evt->hit == Hit::BorderOfUniverse) {

View file

@ -9,6 +9,18 @@
#include "missile.hpp" #include "missile.hpp"
// TODO:
// give points for equipment / better weapons / more energy when:
// - player discovers the universe
//
// - the shot which made a kill was much longer than the direkt line between
// player and his target
//
// - add wormholes
// - add blackholes
// - shoot through suns which add a fire mantle to the rocket to make it more
// thick and dangerous but it gets destroyed after some time.
namespace game { namespace game {
// forward declarations // forward declarations
class Command; class Command;
@ -56,6 +68,7 @@ namespace game {
// add a trace to the list of traces. // add a trace to the list of traces.
void addTrace(Trace *trace); void addTrace(Trace *trace);
void deleteTrace(Trace *trace);
/*************************************************************************/ /*************************************************************************/
/* Rendering */ /* Rendering */

View file

@ -24,4 +24,9 @@ namespace game {
points.push_back(TracePoint(missile)); points.push_back(TracePoint(missile));
} }
} }
void Trace::finish()
{
missile = nullptr;
}
} }

View file

@ -21,6 +21,10 @@ namespace game {
// the missile) in case the fidelityCounter would skip the current position. // the missile) in case the fidelityCounter would skip the current position.
void addPointFromMissile(bool forceAdd=false); void addPointFromMissile(bool forceAdd=false);
// call this to mark the trace as finish and disconnect it from the
// missile.
void finish();
// TODO: add extendLastPointToPosition() method for saving points / // TODO: add extendLastPointToPosition() method for saving points /
// optimization later on // optimization later on