From 3c716decdd7a286ed7a935c3adddf926c5c0548e Mon Sep 17 00:00:00 2001 From: Andreas Ortmann Date: Tue, 27 Sep 2016 19:01:21 +0200 Subject: [PATCH 1/8] renamed --- game/CMakeLists.txt | 9 +- game/config.cpp | 2 - game/config.h | 33 ------- game/{game_window.h => game_window.hpp} | 4 +- game/glclasses.cpp | 2 +- game/{glclasses.h => glclasses.hpp} | 0 game/main.cpp | 6 +- game/opengl.cpp | 2 +- game/{opengl.h => opengl.hpp} | 0 game/renderer.cpp | 2 +- game/{renderer.h => renderer.hpp} | 2 +- game/simulation.cpp | 2 - game/simulation.h | 50 ----------- ...{triangle_window.h => triangle_window.hpp} | 2 +- game/vector.h | 90 ------------------- 15 files changed, 13 insertions(+), 193 deletions(-) delete mode 100644 game/config.cpp delete mode 100644 game/config.h rename game/{game_window.h => game_window.hpp} (98%) rename game/{glclasses.h => glclasses.hpp} (100%) rename game/{opengl.h => opengl.hpp} (100%) rename game/{renderer.h => renderer.hpp} (93%) delete mode 100644 game/simulation.cpp delete mode 100644 game/simulation.h rename game/{triangle_window.h => triangle_window.hpp} (97%) delete mode 100644 game/vector.h diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index 05fc7d1..c157538 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -24,12 +24,9 @@ set(GAME_SRC ) set(GAME_HEADERS - opengl.h - glclasses.h - vector.h - config.h - simulation.h - renderer.h + opengl.hpp + glclasses.hpp + renderer.hpp ) include_directories(${CMAKE_CURRENT_BINARY_DIR}) diff --git a/game/config.cpp b/game/config.cpp deleted file mode 100644 index 968f9d4..0000000 --- a/game/config.cpp +++ /dev/null @@ -1,2 +0,0 @@ -#include "config.h" - diff --git a/game/config.h b/game/config.h deleted file mode 100644 index aacc673..0000000 --- a/game/config.h +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once - -struct Config { - int maxPlayers; - int numPlanets; - int maxSegments; - int segmentSteps; - int numShots; - int fastmode; - int fullscreen; - int timeout; - int margintop; - int marginleft; - int marginright; - int marginbottom; - double playerSize; - int energy; - int realtime; - int debug; - double battlefieldW; - double battlefieldH; - int throttle; - char* ip; - char* message; - int pot; - int area; -}; - -class ConfigParser{ - private: - protected: - public: -}; diff --git a/game/game_window.h b/game/game_window.hpp similarity index 98% rename from game/game_window.h rename to game/game_window.hpp index a5659a5..b3072ce 100644 --- a/game/game_window.h +++ b/game/game_window.hpp @@ -1,7 +1,7 @@ #pragma once -#include "opengl.h" -#include "renderer.h" +#include "opengl.hpp" +#include "renderer.hpp" #include "game.hpp" diff --git a/game/glclasses.cpp b/game/glclasses.cpp index 4746bbd..2d6e618 100644 --- a/game/glclasses.cpp +++ b/game/glclasses.cpp @@ -1,4 +1,4 @@ -#include "glclasses.h" +#include "glclasses.hpp" endofthejedi::VAO::VAO() { glGenVertexArrays(1, &m_name); } diff --git a/game/glclasses.h b/game/glclasses.hpp similarity index 100% rename from game/glclasses.h rename to game/glclasses.hpp diff --git a/game/main.cpp b/game/main.cpp index a63380e..5714cc3 100644 --- a/game/main.cpp +++ b/game/main.cpp @@ -1,8 +1,8 @@ -#include "opengl.h" +#include "opengl.hpp" + #include -//#include "triangle_window.h" -#include "game_window.h" +#include "game_window.hpp" int main(int argc, const char *argv[]) { GameWindow window(500, 500); diff --git a/game/opengl.cpp b/game/opengl.cpp index ecf3336..7eecce7 100644 --- a/game/opengl.cpp +++ b/game/opengl.cpp @@ -1,4 +1,4 @@ -#include "opengl.h" +#include "opengl.hpp" #include #include diff --git a/game/opengl.h b/game/opengl.hpp similarity index 100% rename from game/opengl.h rename to game/opengl.hpp diff --git a/game/renderer.cpp b/game/renderer.cpp index 93b4dc0..862b9f3 100644 --- a/game/renderer.cpp +++ b/game/renderer.cpp @@ -1,4 +1,4 @@ -#include "renderer.h" +#include "renderer.hpp" endofthejedi::Renderer::Renderer() { diff --git a/game/renderer.h b/game/renderer.hpp similarity index 93% rename from game/renderer.h rename to game/renderer.hpp index 53a4058..387d606 100644 --- a/game/renderer.h +++ b/game/renderer.hpp @@ -5,7 +5,7 @@ #include #include -#include "glclasses.h" +#include "glclasses.hpp" namespace endofthejedi { diff --git a/game/simulation.cpp b/game/simulation.cpp deleted file mode 100644 index 54776d1..0000000 --- a/game/simulation.cpp +++ /dev/null @@ -1,2 +0,0 @@ -#include "simulation.h" - diff --git a/game/simulation.h b/game/simulation.h deleted file mode 100644 index bba7992..0000000 --- a/game/simulation.h +++ /dev/null @@ -1,50 +0,0 @@ -#pragma once - -#include "vector.h" - -typedef struct -{ - Vec2d position; - Vec2d speed; - int live; - int leftSource; - int stale; -} SimMissile; - -typedef struct -{ - Vec2d* dot; - SimMissile missile; - int length; - int player; - double angle; - double velocity; -} SimShot; - -typedef struct -{ - SimShot* shot; - int currentShot; - Vec2d position; - double angle; - double velocity; - double energy; - double oldVelocity; - int watch; - int deaths; - int kills; - int shots; - int active; - int valid; - int didShoot; - int timeout; - int timeoutcnt; - char name[16]; -} SimPlayer; - -typedef struct -{ - Vec2d position; - double radius; - double mass; -} SimPlanet; diff --git a/game/triangle_window.h b/game/triangle_window.hpp similarity index 97% rename from game/triangle_window.h rename to game/triangle_window.hpp index 84a0636..e35d2e4 100644 --- a/game/triangle_window.h +++ b/game/triangle_window.hpp @@ -1,6 +1,6 @@ #pragma once -#include "opengl.h" +#include "opengl.hpp" class TriangleWindow : public endofthejedi::GLWindow { private: diff --git a/game/vector.h b/game/vector.h deleted file mode 100644 index 15a2e6c..0000000 --- a/game/vector.h +++ /dev/null @@ -1,90 +0,0 @@ -#pragma once - -#include - -struct Vec2d -{ - double x; - double y; - - double length() { - return sqrt(x*x + y*y); - } - double distance(Vec2d other) { - Vec2d tmp = (*this - other); - return tmp.length(); - } - - Vec2d & operator+=(const Vec2d& rhs) { - x+=rhs.x; - y+=rhs.y; - return *this; - } - Vec2d & operator-=(const Vec2d& rhs) { - x-=rhs.x; - y-=rhs.y; - return *this; - } - Vec2d & operator*=(const double& rhs) { - x*=rhs; - y*=rhs; - return *this; - } - Vec2d & operator/=(const double& rhs) { - x/=rhs; - y/=rhs; - return *this; - } - const Vec2d operator+(const Vec2d& rhs) const { - Vec2d result = *this; - result += rhs; - return result; - } - const Vec2d operator-(const Vec2d& rhs) const { - Vec2d result = *this; - result -= rhs; - return result; - } - const double operator*(const Vec2d& rhs) const { - return (this->x * rhs.x) + (this->y * rhs.y); - } - const Vec2d operator*(const double& rhs) const { - Vec2d result = *this; - result *= rhs; - return result; - } - const Vec2d operator/(const double& rhs) const { - Vec2d result = *this; - result /= rhs; - return result; - } -}; -/* -struct Mat4d { - std::array data; - - const Mat4d operator*(const Mat4d& rhs) const { - Mat4d result; - result[0] = result[0]*rhs[0] + result[1]*result[5] + result[2]*result[9] + result[3]*result[13]; - result[1] = result[0]*rhs[1] + result[1]*result[6] + result[2]*result[10] + result[3]*result[14]; - result[2] = result[0]*rhs[2] + result[1]*result[7] + result[2]*result[11] + result[3]*result[15]; - result[3] = result[0]*rhs[3] + result[1]*result[8] + result[2]*result[12] + result[3]*result[16]; - - result[4] = result[4]*rhs[0] + result[5]*result[5] + result[6]*result[9] + result[7]*result[13]; - result[5] = result[4]*rhs[1] + result[5]*result[6] + result[6]*result[10] + result[7]*result[14]; - result[6] = result[4]*rhs[2] + result[5]*result[7] + result[6]*result[11] + result[7]*result[15]; - result[7] = result[4]*rhs[3] + result[5]*result[8] + result[6]*result[12] + result[7]*result[16]; - - result[8] = result[8]*rhs[0] + result[9]*result[5] + result[10]*result[9] + result[11]*result[13]; - result[9] = result[8]*rhs[1] + result[9]*result[6] + result[10]*result[10] + result[11]*result[14]; - result[10] = result[8]*rhs[2] + result[9]*result[7] + result[10]*result[11] + result[11]*result[15]; - result[11] = result[8]*rhs[3] + result[9]*result[8] + result[10]*result[12] + result[11]*result[16]; - - result[12] = result[12]*rhs[0] + result[13]*result[5] + result[14]*result[9] + result[15]*result[13]; - result[13] = result[12]*rhs[1] + result[13]*result[6] + result[14]*result[10] + result[15]*result[14]; - result[14] = result[12]*rhs[2] + result[13]*result[7] + result[14]*result[11] + result[15]*result[15]; - result[15] = result[12]*rhs[3] + result[13]*result[8] + result[14]*result[12] + result[15]*result[16]; - return result; - } -}; -*/ From 56ccdfb1fdf4f1d9370deac0eee8a6bcf9ffaee1 Mon Sep 17 00:00:00 2001 From: Andreas Ortmann Date: Tue, 27 Sep 2016 19:26:16 +0200 Subject: [PATCH 2/8] adding explosion stuff, adding trace file. --- game/state/explosion.hpp | 23 +++++++++++++++++------ game/state/trace.cpp | 0 game/state/trace.hpp | 0 3 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 game/state/trace.cpp create mode 100644 game/state/trace.hpp diff --git a/game/state/explosion.hpp b/game/state/explosion.hpp index 2cf92ef..9a37296 100644 --- a/game/state/explosion.hpp +++ b/game/state/explosion.hpp @@ -1,17 +1,28 @@ #pragma once -#if 0 +/** + * Explosion: just an effect which looks good. + */ class Explosion { public: enum class Kind { - Normal, - MissileWithShip + MissileExplodesInSpace, // missile explode in free space + MissileAgainstPlanet, // explosion of missile when it hits a planet + MissileAgainstShip // bigger explosion: missile hits a ship which explodes with it }; - glm::vec2 position; + Explosion(const glm::vec2 &pos, Kind kind, float maxAge=1.0) + : position(pos), kind(kind), age(0.0), maxAge(maxAge) + { + } - Explosion(Kind kind); + const Kind kind; // kind of the explosion + const glm::vec2 position; // position where it starts + float age; // age (in seconsd) of the explosion -public: + // age (in seconds) when the explosion is not visible + // anymore and will disappear afterwards + const float maxAge; }; + #endif diff --git a/game/state/trace.cpp b/game/state/trace.cpp new file mode 100644 index 0000000..e69de29 diff --git a/game/state/trace.hpp b/game/state/trace.hpp new file mode 100644 index 0000000..e69de29 From 64428d2068be102fd48fc89c2ab88b16bba0b090 Mon Sep 17 00:00:00 2001 From: Andreas Ortmann Date: Tue, 27 Sep 2016 19:32:12 +0200 Subject: [PATCH 3/8] implementing trace class. --- game/CMakeLists.txt | 2 ++ game/state/explosion.hpp | 50 ++++++++++++++++++++++------------------ game/state/trace.hpp | 24 +++++++++++++++++++ 3 files changed, 53 insertions(+), 23 deletions(-) diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index c157538..b2f36de 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -15,6 +15,8 @@ set(GAME_SRC util.cpp game.cpp state/object.cpp + state/explosion.cpp + state/trace.cpp state/state.cpp state/player.cpp state/planet.cpp diff --git a/game/state/explosion.hpp b/game/state/explosion.hpp index 9a37296..66c234f 100644 --- a/game/state/explosion.hpp +++ b/game/state/explosion.hpp @@ -1,28 +1,32 @@ #pragma once -/** - * Explosion: just an effect which looks good. - */ -class Explosion { -public: - enum class Kind { - MissileExplodesInSpace, // missile explode in free space - MissileAgainstPlanet, // explosion of missile when it hits a planet - MissileAgainstShip // bigger explosion: missile hits a ship which explodes with it +#include + +namespace game { + /** + * Explosion: just an effect which looks good. + */ + class Explosion { + public: + enum class Kind { + MissileExplodesInSpace, // missile explode in free space + MissileAgainstPlanet, // explosion of missile when it hits a planet + MissileAgainstShip // bigger explosion: missile hits a ship which explodes with it + }; + + Explosion(const glm::vec2 &pos, Kind kind, float maxAge=1.0) + : position(pos), kind(kind), age(0.0), maxAge(maxAge) + { + } + + const Kind kind; // kind of the explosion + const glm::vec2 position; // position where it starts + float age; // age (in seconsd) of the explosion + + // age (in seconds) when the explosion is not visible + // anymore and will disappear afterwards + const float maxAge; }; - - Explosion(const glm::vec2 &pos, Kind kind, float maxAge=1.0) - : position(pos), kind(kind), age(0.0), maxAge(maxAge) - { - } - - const Kind kind; // kind of the explosion - const glm::vec2 position; // position where it starts - float age; // age (in seconsd) of the explosion - - // age (in seconds) when the explosion is not visible - // anymore and will disappear afterwards - const float maxAge; -}; +} #endif diff --git a/game/state/trace.hpp b/game/state/trace.hpp index e69de29..5c7f050 100644 --- a/game/state/trace.hpp +++ b/game/state/trace.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include + +namespace game { + /* + * Trace of a missile through the space. + * Useful for rendering sth. like a smoke trail to follow the rocket. + */ + class Trace { + public: + Trace(const glm::vec2 &startPoint) + { + points.push_back(startPoint); + } + + void addPoint(const glm::vec2 &p) + { + points.push_back(p); + } + + std::vector points; + }; +} From dfa1288aa50fc1b5cffa05fdc2a6a20aebeaa743 Mon Sep 17 00:00:00 2001 From: end Date: Tue, 27 Sep 2016 19:42:42 +0200 Subject: [PATCH 4/8] * fixed CMakeLists, now using the glm submodule --- game/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index c157538..35f1f0a 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -32,6 +32,7 @@ set(GAME_HEADERS include_directories(${CMAKE_CURRENT_BINARY_DIR}) include_directories(${CMAKE_CURRENT_SOURCE_DIR}) include_directories(${OPENGL_INCLUDE_DIR}) +include_directories(${CMAKE_SOURCE_DIR}/libs/glm/) add_executable(game ${GAME_SRC} ${GAME_HEADERS}) setup_target(game) From 1b6c8d4fbac2f872653480fdb0f1a38cdb92ee5f Mon Sep 17 00:00:00 2001 From: Andreas Ortmann Date: Tue, 27 Sep 2016 19:43:40 +0200 Subject: [PATCH 5/8] added trace implemention --- game/state/trace.cpp | 22 ++++++++++++++++++++++ game/state/trace.hpp | 28 +++++++++++++++++++--------- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/game/state/trace.cpp b/game/state/trace.cpp index e69de29..16f6a4f 100644 --- a/game/state/trace.cpp +++ b/game/state/trace.cpp @@ -0,0 +1,22 @@ +#include "trace.hpp" + +#include + +#include "missile.hpp" + +namespace game { + Trace::TracePoint::TracePoint(const Missile *missile) + : position(missile->position), speed(glm::length(missile->velocity)) + { + } + + Trace::Trace(const Missile *missile) + { + points.push_back(TracePoint(missile)); + } + + void Trace::addPointFromMissile(const Missile *missile) + { + points.push_back(TracePoint(missile)); + } +} diff --git a/game/state/trace.hpp b/game/state/trace.hpp index 5c7f050..0ceb0b3 100644 --- a/game/state/trace.hpp +++ b/game/state/trace.hpp @@ -1,24 +1,34 @@ #pragma once #include +#include namespace game { + class Missile; + /* * Trace of a missile through the space. * Useful for rendering sth. like a smoke trail to follow the rocket. */ class Trace { public: - Trace(const glm::vec2 &startPoint) - { - points.push_back(startPoint); - } + Trace(const Missile *missile); - void addPoint(const glm::vec2 &p) - { - points.push_back(p); - } + // TODO: add velocity of the rocket or age at the points too becasue + // that enables nice rendering of speed etc. at different points + // TODO: give missile to this + void addPointFromMissile(const Missile *missile); - std::vector points; + /* + * Trace point data to be used when rendering. + */ + struct TracePoint { + TracePoint(const Missile *missile); + + const glm::vec2 position; + const float speed; + }; + + std::vector points; }; } From fd8d56bf9576beac041d90dca541dc8b27d4f762 Mon Sep 17 00:00:00 2001 From: Andreas Ortmann Date: Tue, 27 Sep 2016 20:13:09 +0200 Subject: [PATCH 6/8] traces are now managed by state/missile code. --- CMakeLists.txt | 1 + game/main.cpp | 3 +++ game/state/commands.cpp | 16 ++++++++++++--- game/state/missile.cpp | 11 ++++++---- game/state/missile.hpp | 24 ++++++++++------------ game/state/state.cpp | 11 ++++++++++ game/state/state.hpp | 45 +++++++++++++++++++++++++++++++++-------- game/state/trace.cpp | 13 ++++++++---- game/state/trace.hpp | 23 +++++++++++++++------ 9 files changed, 109 insertions(+), 38 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 64bcd0f..a2823b4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,7 @@ function(setup_target NAME) set_property(TARGET ${NAME} PROPERTY CXX_STANDARD 14) set_property(TARGET ${NAME} PROPERTY CXX_STANDARD_REQUIRED ON) target_compile_options(${NAME} PRIVATE -Wall -Wextra) + target_compile_options(${NAME} PRIVATE -fdiagnostics-color=always) target_compile_options(${NAME} PRIVATE $<$:-ggdb -O2>) target_compile_options(${NAME} PRIVATE $<$:-O3 -NDEBUG>) endfunction(setup_target) diff --git a/game/main.cpp b/game/main.cpp index 5714cc3..10d615e 100644 --- a/game/main.cpp +++ b/game/main.cpp @@ -5,6 +5,9 @@ #include "game_window.hpp" int main(int argc, const char *argv[]) { + (void) argv; + (void) argc; + GameWindow window(500, 500); window.set_maxfps(60.0); window.loop(); diff --git a/game/state/commands.cpp b/game/state/commands.cpp index f656dc6..b84a214 100644 --- a/game/state/commands.cpp +++ b/game/state/commands.cpp @@ -1,14 +1,24 @@ #include "commands.hpp" +#include "trace.hpp" + namespace game { void ShootCommand::apply(Player *player, State *state) const { - (void) state; - // TODO spawn missile if alive and enough energy //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, m_angle, m_speed); + + Trace *trace = new Trace(missile); + missile->trace = trace; + player->energy -= m_speed; - player->missiles.push_back(new Missile(player->id, player->ship->position, m_angle, m_speed)); + player->missiles.push_back(missile); + + state->addTrace(trace); } bool ShootCommand::allowed(const Player *player, const State *state) const diff --git a/game/state/missile.cpp b/game/state/missile.cpp index b060ca3..b789b5f 100644 --- a/game/state/missile.cpp +++ b/game/state/missile.cpp @@ -4,12 +4,15 @@ #include "state.hpp" #include "player.hpp" +#include "ship.hpp" +#include "planet.hpp" +#include "trace.hpp" #include namespace game { - Missile::Missile(int playerId, const glm::vec2 &pos, float angle, float speed) - : playerId(playerId) + Missile::Missile(Player *player, const glm::vec2 &pos, float angle, float speed) + : player(player) , position(pos) { velocity = speed * glm::vec2(std::sin(angle), std::cos(angle)); @@ -28,13 +31,13 @@ namespace game { glm::vec2 gravityForce = glm::vec2(0.0, 0.0); for (const Player *other : state->players) { - if (other->ship != nullptr && other->id != playerId) { + if (other->ship != nullptr && other != player) { glm::vec2 diff = other->ship->position - position; float dist = glm::length(diff); if (dist <= other->ship->radius) { // TODO: collect all hits and return the first one only // TODO: find exact hit position! - return Missile::Event(position, playerId, other->id); + return Missile::Event(position, player->id, other->id); } } } diff --git a/game/state/missile.hpp b/game/state/missile.hpp index b1f0f96..c68706d 100644 --- a/game/state/missile.hpp +++ b/game/state/missile.hpp @@ -3,13 +3,13 @@ #include #include -#include "state.hpp" - -#include "ship.hpp" -#include "planet.hpp" - namespace game { class State; + class Ship; + class Planet; + class Player; + class State; + class Trace; // missile belongs to a player and optionally fills a trace behind it. // trace then belongs to the player. @@ -50,19 +50,17 @@ namespace game { int playerIdVictim; }; - // XXX - int playerId; // owner won't be hit by own missiles - glm::vec2 position; - glm::vec2 velocity; - //Trace *trace; - - Missile(int playerId, const glm::vec2 &pos, float angle, float speed); - + Missile(Player *player, const glm::vec2 &pos, float angle, float speed); ~Missile(); // try to advance. if something will be hit, return the first hit in // time. Missile::Event advance(const game::State *state, float dt); + + Player *player; // owner won't be hit by own missiles + glm::vec2 position; + glm::vec2 velocity; + Trace *trace; }; } diff --git a/game/state/state.cpp b/game/state/state.cpp index 462f62a..bc39594 100644 --- a/game/state/state.cpp +++ b/game/state/state.cpp @@ -12,6 +12,7 @@ #include "planet.hpp" #include "ship.hpp" #include "commands.hpp" +#include "trace.hpp" #include "util.hpp" @@ -161,6 +162,11 @@ namespace game { //std::cout<<"missile: " << (long unsigned int) missile << std::endl; const Missile::Event evt = missile->advance(this, dt); + missile->trace->addPointFromMissile(); + + // TODO: + // spawn just if the path differs + if (evt.hit == Missile::HitObject::Nothing) { i++; @@ -259,4 +265,9 @@ namespace game { player->addCommand(cmd); } } + + void State::addTrace(Trace *trace) + { + traces.push_back(trace); + } } diff --git a/game/state/state.hpp b/game/state/state.hpp index 1182147..0df4313 100644 --- a/game/state/state.hpp +++ b/game/state/state.hpp @@ -8,25 +8,34 @@ #include namespace game { + // forward declarations class Command; class Missile; class Player; class Planet; class Ship; - - // trace of a missile. exists without a missile at player. - //class Trace { - //public: - // std::vector points; - //}; + class Trace; 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); - // the (network) layer calling these three functions should keep id's - // unique and give one (network) input an id. + /*************************************************************************/ + /* 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); @@ -34,14 +43,34 @@ namespace game { // 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 planets; std::vector ships; std::vector players; + std::vector traces; private: + /*************************************************************************/ + /* Internal */ + /*************************************************************************/ + void playerKillsPlayer(Player *killer, Player *victim); void advancePlayerShipSpawns(float dt); diff --git a/game/state/trace.cpp b/game/state/trace.cpp index 16f6a4f..1509967 100644 --- a/game/state/trace.cpp +++ b/game/state/trace.cpp @@ -6,17 +6,22 @@ namespace game { Trace::TracePoint::TracePoint(const Missile *missile) - : position(missile->position), speed(glm::length(missile->velocity)) + : position(missile->position) + , speed(glm::length(missile->velocity)) { } - Trace::Trace(const Missile *missile) + Trace::Trace(const Missile *missile) : missile(missile), fidelityCounter(0) { points.push_back(TracePoint(missile)); } - void Trace::addPointFromMissile(const Missile *missile) + void Trace::addPointFromMissile(bool forceAdd) { - points.push_back(TracePoint(missile)); + fidelityCounter++; + if (forceAdd || fidelityCounter >= 20) { + fidelityCounter = 0; + points.push_back(TracePoint(missile)); + } } } diff --git a/game/state/trace.hpp b/game/state/trace.hpp index 0ceb0b3..ab33b2f 100644 --- a/game/state/trace.hpp +++ b/game/state/trace.hpp @@ -14,10 +14,15 @@ namespace game { public: Trace(const Missile *missile); - // TODO: add velocity of the rocket or age at the points too becasue - // that enables nice rendering of speed etc. at different points - // TODO: give missile to this - void addPointFromMissile(const Missile *missile); + // Add the current position of the missile as a new point on the + // trace. + // Parameters: + // forceAdd: set to true to add this point (good for the endpoint of + // the missile) in case the fidelityCounter would skip the current position. + void addPointFromMissile(bool forceAdd=false); + + // TODO: add extendLastPointToPosition() method for saving points / + // optimization later on /* * Trace point data to be used when rendering. @@ -25,10 +30,16 @@ namespace game { struct TracePoint { TracePoint(const Missile *missile); - const glm::vec2 position; - const float speed; + glm::vec2 position; + float speed; }; std::vector points; + const Missile *missile; // missile which creates this path. + + // counter which is incremented each time addPointFromMissile() is called. + // when reaching a certain value the point is saved for + // optimization. + int fidelityCounter; }; } From 2b85397674216977526f853c5af0dd083213c4cf Mon Sep 17 00:00:00 2001 From: Andreas Ortmann Date: Tue, 27 Sep 2016 20:23:33 +0200 Subject: [PATCH 7/8] traces working and nice (on simple renderer) --- game/game.cpp | 2 +- game/game_window.hpp | 16 +++++++++++++++- game/opengl.cpp | 2 +- game/renderer.cpp | 16 ++++++++-------- game/state/trace.cpp | 2 +- 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/game/game.cpp b/game/game.cpp index 36ffc64..38fcc0c 100644 --- a/game/game.cpp +++ b/game/game.cpp @@ -32,7 +32,7 @@ bool Game::cycle(float dt) acc -= spawnInterval; float a = 2.0 * M_PI * util::randf_0_1(); - float speed = 0.002; + float speed = 0.005; m_state->players[0]->addCommand(new game::ShootCommand(a, speed)); } diff --git a/game/game_window.hpp b/game/game_window.hpp index b3072ce..8bd7c4f 100644 --- a/game/game_window.hpp +++ b/game/game_window.hpp @@ -5,6 +5,7 @@ #include "game.hpp" +#include "state/trace.hpp" #include "state/object.hpp" #include "state/missile.hpp" #include "state/player.hpp" @@ -41,6 +42,10 @@ protected: drawPlanet(planet->position, planet->radius); } + for (const game::Trace *trace : m_game.state()->traces) { + drawTrace(trace); + } + for (const game::Ship *ship : m_game.state()->ships) { drawShip(ship->position); } @@ -73,9 +78,18 @@ protected: } void drawMissile(const glm::vec2 &pos) + { glm::vec3 color = glm::vec3(1.0, 1.0, 0.3); - m_renderer.drawCircle(pos.x, pos.y, 0.005, color.x, color.y, color.z, 5); + m_renderer.drawCircle(pos.x, pos.y, 0.01, color.x, color.y, color.z, 6); + } + + void drawTrace(const game::Trace *trace) + { + for (const game::Trace::TracePoint &p : trace->points) { + glm::vec3 color = glm::vec3(0.1, 0.3, 1.0) / (1.0f + 500.0f*p.speed); + m_renderer.drawCircle(p.position.x, p.position.y, 0.005, color.x, color.y, color.z, 3); + } } public: diff --git a/game/opengl.cpp b/game/opengl.cpp index 7eecce7..fde9da1 100644 --- a/game/opengl.cpp +++ b/game/opengl.cpp @@ -130,7 +130,7 @@ void endofthejedi::GLWindow::loop() { prev = current; if(delta > 0.0) { m_fps = (1000000000.0/delta); - std::cout << m_fps << "\n"; + //std::cout << m_fps << "\n"; } } } diff --git a/game/renderer.cpp b/game/renderer.cpp index 862b9f3..6a7b4ba 100644 --- a/game/renderer.cpp +++ b/game/renderer.cpp @@ -6,13 +6,13 @@ endofthejedi::Renderer::Renderer() { endofthejedi::Renderer::~Renderer() {} -void endofthejedi::Renderer::drawCircle(float x, float y, float radius, float r, - float g, float b, int numSides) { - glBegin(GL_TRIANGLE_FAN); - glVertex2f(x, y); // center of circle - for (int i = 0; i <= numSides; i++) { +void endofthejedi::Renderer::drawCircle(float x, float y, float radius, float r, float g, float b, int numSides) +{ + glBegin(GL_TRIANGLE_FAN); glColor3f(r,g,b); - glVertex2f(x + (radius * cos(i * 2 * M_PI / numSides)), y + (radius * sin(i * 2 * M_PI / numSides))); - } - glEnd(); + glVertex2f(x, y); // center of circle + for (int i = 0; i <= numSides; i++) { + glVertex2f(x + (radius * cos(i * 2 * M_PI / numSides)), y + (radius * sin(i * 2 * M_PI / numSides))); + } + glEnd(); } diff --git a/game/state/trace.cpp b/game/state/trace.cpp index 1509967..4d93d2c 100644 --- a/game/state/trace.cpp +++ b/game/state/trace.cpp @@ -19,7 +19,7 @@ namespace game { void Trace::addPointFromMissile(bool forceAdd) { fidelityCounter++; - if (forceAdd || fidelityCounter >= 20) { + if (forceAdd || fidelityCounter >= 10) { fidelityCounter = 0; points.push_back(TracePoint(missile)); } From c96637c6e94a8a2e7053afb757d85705bae5bc3e Mon Sep 17 00:00:00 2001 From: /jedi/ Date: Tue, 27 Sep 2016 20:56:57 +0200 Subject: [PATCH 8/8] fixes --- game/main.cpp | 1 + game/options.hpp | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/game/main.cpp b/game/main.cpp index 1459bd4..87648c9 100644 --- a/game/main.cpp +++ b/game/main.cpp @@ -6,6 +6,7 @@ #include "game_window.h" #include "options.hpp" +uint64_t optionsFlags; using namespace std; diff --git a/game/options.hpp b/game/options.hpp index 779e32e..8243311 100644 --- a/game/options.hpp +++ b/game/options.hpp @@ -9,7 +9,7 @@ #define SET_FLAG(x,y) optionsFlags&=(~((1)<