Make EntityEvent update entities components - Add simple snake movement

This commit is contained in:
SeanOMik 2020-07-04 13:55:40 -05:00
parent 0c525ae9e6
commit 98fdde324f
No known key found for this signature in database
GPG Key ID: FA4D55AC05268A88
3 changed files with 44 additions and 30 deletions

View File

@ -4,15 +4,20 @@
// Email: seanomik@gmail.com
//
#include <iostream>
#include <simpleengine/game.h>
#include <simpleengine/event.h>
#include <simpleengine/entity.h>
#include <simpleengine/components/movement/movement_component.h>
#include <simpleengine/events/entity_event.h>
#include <chrono>
#include <iostream>
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;
}
int duration = std::chrono::duration_cast<std::chrono::milliseconds>(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<SnakeMovementComponent>(*this, movement_speed));
loc = sf::Vector2f(0, 0);
AddComponent(std::make_unique<SnakeMovementComponent>(*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 {

View File

@ -46,12 +46,7 @@ namespace simpleengine {
std::shared_ptr<Entity> 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<std::unique_ptr<Component>> components;
bool destroying = false;
};

View File

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