Change the command handler to update its own list of entities
This commit is contained in:
parent
823c0b37c6
commit
94cbd111e8
|
@ -302,7 +302,7 @@ int main(int argc, char *argv[]) {
|
|||
auto snake_food = std::make_shared<SnakeFoodEntity>(window_size, snake_player);
|
||||
game.AddEvent(new simpleengine::EntityEvent(game.GetWindow(), snake_food));
|
||||
game.AddEvent(new simpleengine::EntityEvent(game.GetWindow(), snake_player));
|
||||
game.AddEvent(new simpleengine::CollisionHandler(game.GetWindow(), { snake_player, snake_food } ));
|
||||
game.AddEvent(new simpleengine::CollisionHandler(game));
|
||||
|
||||
return game.Run();
|
||||
}
|
|
@ -11,16 +11,20 @@
|
|||
#include "../entity.h"
|
||||
|
||||
namespace simpleengine {
|
||||
class Game;
|
||||
class CollisionHandler : public Event {
|
||||
public:
|
||||
explicit CollisionHandler(sf::RenderWindow* window, std::vector<std::shared_ptr<Entity>> entities) : simpleengine::Event(window), entities(entities) {
|
||||
explicit CollisionHandler(simpleengine::Game& game) : simpleengine::Event(nullptr), game(game) {
|
||||
|
||||
}
|
||||
|
||||
void Update(const float& delta_time) override;
|
||||
void Render(sf::RenderTarget* target) override { }
|
||||
|
||||
void UpdateHandledEntities();
|
||||
private:
|
||||
std::vector<std::shared_ptr<Entity>> entities;
|
||||
std::vector<std::shared_ptr<Entity>> handled_entities;
|
||||
simpleengine::Game& game;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -16,12 +16,15 @@
|
|||
#include <functional>
|
||||
|
||||
#include "entity.h"
|
||||
#include "events/collision_handler.h"
|
||||
|
||||
namespace simpleengine {
|
||||
class Event;
|
||||
|
||||
class Game {
|
||||
public:
|
||||
friend class CollisionHandler;
|
||||
|
||||
Game(int w, int h, const std::string& window_name);
|
||||
Game(const sf::Vector2u& window_size, const std::string& window_name);
|
||||
virtual ~Game();
|
||||
|
|
|
@ -6,13 +6,34 @@
|
|||
|
||||
#include "events/collision_handler.h"
|
||||
#include "components/collision_component.h"
|
||||
#include "entity.h"
|
||||
#include "events/entity_event.h"
|
||||
#include "game.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
void simpleengine::CollisionHandler::Update(const float& delta_time) {
|
||||
for (std::shared_ptr<Entity> entity : entities) {
|
||||
// Update list of handled entities.
|
||||
handled_entities.erase(std::remove_if(handled_entities.begin(), handled_entities.end(),
|
||||
[](std::shared_ptr<simpleengine::Entity> ptr) { return ptr == nullptr; }), handled_entities.end());
|
||||
|
||||
for (simpleengine::Event* event : game.events) {
|
||||
auto* entity_event = dynamic_cast<simpleengine::EntityEvent*>(event);
|
||||
|
||||
if (entity_event) {
|
||||
std::shared_ptr<simpleengine::Entity> entity = entity_event->GetEntity();
|
||||
if (std::count(handled_entities.begin(), handled_entities.end(), entity) == 0) {
|
||||
handled_entities.push_back(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check for collisions in all entities.
|
||||
for (std::shared_ptr<Entity> entity : handled_entities) {
|
||||
std::shared_ptr<CollisionComponent> entity_col = entity->GetComponent<simpleengine::CollisionComponent>();
|
||||
|
||||
if (entity_col) {
|
||||
for (std::shared_ptr<Entity> two : entities) {
|
||||
for (std::shared_ptr<Entity> two : handled_entities) {
|
||||
if (two == entity) continue;
|
||||
|
||||
std::shared_ptr<CollisionComponent> two_col = two->GetComponent<simpleengine::CollisionComponent>();
|
||||
|
|
Loading…
Reference in New Issue