KlassischeKeplerKriege/game/state/state.hpp
2016-09-27 21:16:55 +02:00

99 lines
3.2 KiB
C++

#pragma once
#include <vector>
#include <queue>
#include <iostream>
#include <glm/vec2.hpp>
#include "missile.hpp"
namespace game {
// forward declarations
class Command;
class Player;
class Planet;
class Ship;
class Trace;
class Explosion;
class State {
public:
/*************************************************************************/
/* State management */
/*************************************************************************/
// called to setup the state (randomize planets, kill
// traces/missiles/ships etc.)
void init();
// main method to advance the simulation by the given timestamp in
// seconds.
void advance(float dt);
/*************************************************************************/
/* Network / Input */
/*************************************************************************/
// The upper layer (network/renderer) calling these three functions
// should keep id's unique and give one (network) input an id.
void addPlayer(int playerId);
void playerLeft(int playerId);
void commandForPlayer(int playerId, Command *cmd);
// lookup. return nullptr on invalid playerId
Player *playerForId(int playerId);
/*************************************************************************/
/* Mixed stuff */
// distance after which missiles get lost in space (and explode)
float maxMissileDistance() const { return m_maxMissileDistance; }
// each ship has the same radius
float shipRadius() const { return m_shipRadius; }
// add a trace to the list of traces.
void addTrace(Trace *trace);
/*************************************************************************/
/* Rendering */
/*************************************************************************/
// Game items which should be rendered are here:
// (access missiles by iterating over player's missiles attribute)
std::vector<Planet*> planets;
std::vector<Ship*> ships;
std::vector<Player*> players;
std::vector<Trace*> traces;
std::vector<Explosion*> explosions;
private:
/*************************************************************************/
/* Internal */
/*************************************************************************/
void playerKillsPlayer(Player *killer, Player *victim);
void addExplosionFromHit(const Missile::Event *evt);
void advanceExplosions(float dt);
void advancePlayerShipSpawns(float dt);
void advancePlayerCommands(float dt);
void advancePlayerMissiles(float dt);
// try to spawn a ship for this player.
// return true on success, false on failure to find a spot.
bool spawnShipForPlayer(Player *player);
// find some place where nothing is placed nearby (ships/planets).
// usefule for spanwing things
bool findFreePositionWithRadius(float r, glm::vec2 &pos);
float m_maxMissileDistance;
float m_playerRespawnTime;
float m_shipRadius;
float m_defaultEnergy;
};
};