diff --git a/examples/snake/src/main.cpp b/examples/snake/src/main.cpp index 6765d12..3c6fb28 100644 --- a/examples/snake/src/main.cpp +++ b/examples/snake/src/main.cpp @@ -4,15 +4,20 @@ // Email: seanomik@gmail.com // -#include - #include #include #include #include #include +#include +#include + class SnakeMovementComponent : public simpleengine::Component { +private: + float movement_speed; + sf::Vector2f movement_direction; + std::chrono::high_resolution_clock::time_point last_movement; public: explicit SnakeMovementComponent(simpleengine::Entity& owning_entity, float movement_speed) : simpleengine::Component(owning_entity), movement_speed(movement_speed) { @@ -20,61 +25,76 @@ public: void Update(const float &delta_time) override { if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) { - movement_direction.x = -1; + movement_direction.x = -movement_speed; movement_direction.y = 0; } if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) { movement_direction.x = 0; - movement_direction.y = -1; + movement_direction.y = -movement_speed; } if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) { - movement_direction.x = 1; + movement_direction.x = movement_speed; movement_direction.y = 0; } if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) { movement_direction.x = 0; - movement_direction.y = 1; + movement_direction.y = movement_speed; } - owning_entity.Move(delta_time, movement_direction.x, movement_direction.y); + int duration = std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - last_movement).count(); + + if (duration >= 115 && (movement_direction.x != 0 || movement_direction.y != 0)) { + owning_entity.Move(delta_time, movement_direction.x, movement_direction.y); + last_movement = std::chrono::high_resolution_clock::now(); + } } -private: - float movement_speed; - sf::Vector2f movement_direction; }; class SnakePlayerEntity : public simpleengine::Entity { private: sf::RectangleShape shape; - float movement_speed = 100; + float movement_speed = 15; sf::Vector2u window_size; - sf::Vector2f loc; public: explicit SnakePlayerEntity(sf::Vector2u window_size) : window_size(window_size) { shape = sf::RectangleShape(sf::Vector2f(15, 15)); shape.setFillColor(sf::Color::White); - this->AddComponent(std::make_unique(*this, movement_speed)); - loc = sf::Vector2f(0, 0); + AddComponent(std::make_unique(*this, movement_speed)); + } + + ~SnakePlayerEntity() override { + std::cout << "Destroying" << std::endl; + + simpleengine::Entity::~Entity(); } void Move(const float& delta_time, const float& x, const float& y) override { - shape.move(x * movement_speed * delta_time, y * movement_speed * delta_time); + /*float move_x = shape.getSize().x * x; + float move_y = shape.getSize().y * y;*/ + + shape.move(x, y); + } + + void Move(const float& delta_time, const sf::Vector2f& offset) override { + shape.move(offset * delta_time); + } + + void Move(const sf::Vector2f& offset) override { + shape.move(offset); } void Update(const float& delta_time) override { - UpdateComponents(delta_time); + /*UpdateComponents(delta_time); - if (loc.x >= window_size.x) { + if (shape.getPosition().x - shape.getSize().x < 0 || shape.getPosition().x + shape.getSize().x > window_size.x) { DestroyLater(); - } - - if (loc.y >= window_size.y) { + } else if (shape.getPosition().y - shape.getSize().y < 0 || shape.getPosition().y + shape.getSize().y > window_size.y) { DestroyLater(); - } + }*/ } void Render(sf::RenderTarget* target) override { diff --git a/include/simpleengine/entity.h b/include/simpleengine/entity.h index ce8232c..25bcb16 100644 --- a/include/simpleengine/entity.h +++ b/include/simpleengine/entity.h @@ -46,12 +46,7 @@ namespace simpleengine { std::shared_ptr GetShared() { return shared_from_this(); } - private: - // This is ran from class `Game` and `Event`. It runs the `UpdateComponents` method and then the `Update` method. - void UpdateEntity(const float& delta_time) { - UpdateComponents(delta_time); - } - + protected: std::vector> components; bool destroying = false; }; diff --git a/include/simpleengine/events/entity_event.h b/include/simpleengine/events/entity_event.h index 30dfd02..95f00e3 100644 --- a/include/simpleengine/events/entity_event.h +++ b/include/simpleengine/events/entity_event.h @@ -18,13 +18,12 @@ namespace simpleengine { } void CheckForQuit() override { - /*if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) { - quit = true; - }*/ + } void Update(const float& delta_time) override { entity->Update(delta_time); + entity->UpdateComponents(delta_time); if (entity->IsGettingDestroyed()) { quit = true;