diff --git a/game/main.cpp b/game/main.cpp index 1e4afd1..d8b0e24 100644 --- a/game/main.cpp +++ b/game/main.cpp @@ -107,9 +107,19 @@ int main(int argc, char *argv[]) window.poll(); io_service.poll(); + size_t numEvents = game.state()->currentStateUpdateEvents().size(); + if (numEvents != 0) { + std::cout<<"game state update events: " << numEvents << std::endl; + for (game::StateUpdateEvent *evt : game.state()->currentStateUpdateEvents()) { + std::cout<< evt->description() << std::endl; + } + } + //if (sounds != nullptr) { // sounds.advance(game->state()); //} + + game.state()->applyAndClearAllOldStateUpdates(); } return 0; diff --git a/game/renderer_polygon_3d/renderer_polygon_3d.cpp b/game/renderer_polygon_3d/renderer_polygon_3d.cpp index 247ab85..fc56aa9 100644 --- a/game/renderer_polygon_3d/renderer_polygon_3d.cpp +++ b/game/renderer_polygon_3d/renderer_polygon_3d.cpp @@ -2,6 +2,8 @@ #include +#include "state/events/explosion_event.hpp" + namespace endofthejedi { void RendererPolygon3d::setup() { @@ -250,6 +252,7 @@ namespace endofthejedi { void RendererPolygon3d::advanceGraphicObjects(float dt) { +#if 0 for (const game::Explosion *expl : m_state->explosions) { bool gotIt = false; for (ParticleBatch *batch : m_particles) { @@ -267,6 +270,24 @@ namespace endofthejedi { 1000, 1.0); } } +#endif + + for (game::StateUpdateEvent *evt : m_state->currentStateUpdateEvents()) { + if (evt->eventType() == game::StateUpdateEvent::EventType::Explosion) { + auto cycle = evt->lifeCycle(); + if (cycle == game::StateUpdateEvent::LifeCycle::Create) { + game::ExplosionEvent *ee = static_cast(evt); + const game::Explosion *expl = ee->explosion; + addExplosionEffect( + expl->id, expl->position, + expl->missileVelocity, + (expl->hit == game::Hit::Planet), + 1000, 1.0); + + std::cout<<"adding [graphic] explosion for #" << expl->id << std::endl; + } + } + } //if (m_particles.size() == 0) { // addExplosionEffect(0, glm::vec2(0.0, 0.0), glm::vec2(0.0, 0.0), false, 10000, 2.0); diff --git a/game/state/events/explosion_event.hpp b/game/state/events/explosion_event.hpp index 6c081d2..7af76d7 100644 --- a/game/state/events/explosion_event.hpp +++ b/game/state/events/explosion_event.hpp @@ -16,6 +16,6 @@ namespace game { } public: - Explosion *explosion; + const Explosion *explosion; }; } diff --git a/game/state/state.cpp b/game/state/state.cpp index 935e2cc..855c0b3 100644 --- a/game/state/state.cpp +++ b/game/state/state.cpp @@ -366,7 +366,11 @@ namespace game { //std::cout<<"[state] (before move) cycle: update events length is " << m_nextEvents.size() << std::endl; // put collected events into that list. - m_allEvents.push_back(std::move(m_nextEvents)); + //m_allEvents.push_back(std::move(m_nextEvents)); + for (StateUpdateEvent *evt : m_nextEvents) { + m_allEvents.push_back(evt); + } + m_nextEvents.clear(); //std::cout<<"[state] (after move) cycle: update events length is " << m_nextEvents.size() << std::endl; @@ -511,12 +515,39 @@ namespace game { // TODO: delete the items for events that are to be removed in proper // way - for (std::list list : m_allEvents) { - for (StateUpdateEvent *evt : list) { - delete(evt); + //for (std::list list : m_allEvents) { + // for (StateUpdateEvent *evt : list) { + // delete(evt); + // } + // list.clear(); + //} + + for (StateUpdateEvent *evt : m_allEvents) { + if (evt->lifeCycle() == StateUpdateEvent::LifeCycle::Destroy) { + switch(evt->eventType()) { + case StateUpdateEvent::EventType::Explosion: + { + ExplosionEvent *ee = static_cast(evt); + std::cout<<"got explosion delete event, finally deleting explosion #" + << ee->explosion->id << std::endl; + + delete(ee->explosion); + } + break; + + default: + std::cout<<"warning: unhandled deletion event for: event type " + << (int) evt->eventType() << std::endl; + } } - list.clear(); + delete(evt); } + m_allEvents.clear(); } + + std::list State::currentStateUpdateEvents() const + { + return m_allEvents; + } } diff --git a/game/state/state.hpp b/game/state/state.hpp index 8aed6fb..ddaab60 100644 --- a/game/state/state.hpp +++ b/game/state/state.hpp @@ -105,6 +105,8 @@ namespace game { void applyAndClearAllOldStateUpdates(); + std::list currentStateUpdateEvents() const; + /*************************************************************************/ /* Rendering */ @@ -161,6 +163,7 @@ namespace game { glm::vec2 m_playingFieldSize; std::list m_nextEvents; - std::vector> m_allEvents; + std::list m_allEvents; + //std::vector> m_allEvents; }; } diff --git a/game/state/state_update_event.hpp b/game/state/state_update_event.hpp index 0db7486..b350466 100644 --- a/game/state/state_update_event.hpp +++ b/game/state/state_update_event.hpp @@ -31,16 +31,24 @@ namespace game { std::string description() const { // TODO - return ""; + return "StateUpdateEvent(" + lifeCycleToString(m_lifeCycle) + ", " + eventTypeToString(m_eventType) + ")"; } static std::string lifeCycleToString(LifeCycle lifeCycle) { switch(lifeCycle) { - case StateUpdateEvent::LifeCycle::Create: return "create"; - case StateUpdateEvent::LifeCycle::Modify: return "modify"; - case StateUpdateEvent::LifeCycle::Destroy: return "destroy"; - default: return ""; + case LifeCycle::Create: return "create"; + case LifeCycle::Modify: return "modify"; + case LifeCycle::Destroy: return "destroy"; + default: return ""; + } + } + + static std::string eventTypeToString(EventType eventType) + { + switch(eventType) { + case EventType::Explosion: return "explosion"; + default: return ""; } }