Change the command handler to update its own list of entities

This commit is contained in:
SeanOMik 2021-04-03 22:27:44 -05:00
parent 823c0b37c6
commit 94cbd111e8
No known key found for this signature in database
GPG Key ID: CA09E5BE1F32728A
4 changed files with 33 additions and 5 deletions

View File

@ -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();
}

View File

@ -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;
};
}

View File

@ -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();

View File

@ -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>();