2020-07-03 18:31:17 +00:00
|
|
|
//
|
|
|
|
// Created by SeanOMik on 7/2/2020.
|
|
|
|
// Github: https://github.com/SeanOMik
|
|
|
|
// Email: seanomik@gmail.com
|
|
|
|
//
|
|
|
|
|
|
|
|
#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>
|
|
|
|
|
2020-07-04 18:55:40 +00:00
|
|
|
#include <chrono>
|
|
|
|
#include <iostream>
|
|
|
|
|
2020-07-03 18:31:17 +00:00
|
|
|
class SnakeMovementComponent : public simpleengine::Component {
|
2020-07-04 18:55:40 +00:00
|
|
|
private:
|
|
|
|
float movement_speed;
|
|
|
|
sf::Vector2f movement_direction;
|
|
|
|
std::chrono::high_resolution_clock::time_point last_movement;
|
2020-07-03 18:31:17 +00:00
|
|
|
public:
|
2020-07-03 20:26:44 +00:00
|
|
|
explicit SnakeMovementComponent(simpleengine::Entity& owning_entity, float movement_speed) : simpleengine::Component(owning_entity), movement_speed(movement_speed) {
|
2020-07-03 18:31:17 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-06 05:45:24 +00:00
|
|
|
void Update(const float& delta_time) override {
|
2020-07-03 18:31:17 +00:00
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
|
2020-07-04 18:55:40 +00:00
|
|
|
movement_direction.x = -movement_speed;
|
2020-07-03 18:31:17 +00:00
|
|
|
movement_direction.y = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
|
|
|
|
movement_direction.x = 0;
|
2020-07-04 18:55:40 +00:00
|
|
|
movement_direction.y = -movement_speed;
|
2020-07-03 18:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
|
2020-07-04 18:55:40 +00:00
|
|
|
movement_direction.x = movement_speed;
|
2020-07-03 18:31:17 +00:00
|
|
|
movement_direction.y = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
|
|
|
|
movement_direction.x = 0;
|
2020-07-04 18:55:40 +00:00
|
|
|
movement_direction.y = movement_speed;
|
2020-07-03 18:31:17 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 18:55:40 +00:00
|
|
|
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();
|
|
|
|
}
|
2020-07-03 18:31:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class SnakePlayerEntity : public simpleengine::Entity {
|
|
|
|
private:
|
|
|
|
sf::RectangleShape shape;
|
2020-07-04 18:55:40 +00:00
|
|
|
float movement_speed = 15;
|
2020-07-03 18:31:17 +00:00
|
|
|
sf::Vector2u window_size;
|
|
|
|
public:
|
|
|
|
explicit SnakePlayerEntity(sf::Vector2u window_size) : window_size(window_size) {
|
|
|
|
shape = sf::RectangleShape(sf::Vector2f(15, 15));
|
|
|
|
shape.setFillColor(sf::Color::White);
|
|
|
|
|
2020-07-04 18:55:40 +00:00
|
|
|
AddComponent(std::make_unique<SnakeMovementComponent>(*this, movement_speed));
|
|
|
|
}
|
|
|
|
|
|
|
|
~SnakePlayerEntity() override {
|
|
|
|
std::cout << "Destroying" << std::endl;
|
|
|
|
|
|
|
|
simpleengine::Entity::~Entity();
|
2020-07-03 18:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Move(const float& delta_time, const float& x, const float& y) override {
|
2020-07-04 18:55:40 +00:00
|
|
|
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);
|
2020-07-03 18:31:17 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 18:34:22 +00:00
|
|
|
void Render(sf::RenderTarget* target) override {
|
2020-07-03 18:31:17 +00:00
|
|
|
target->draw(shape);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2020-07-04 20:21:44 +00:00
|
|
|
simpleengine::Game game(500, 500, "SimpleEngine - Snake Example");
|
2020-07-03 20:26:44 +00:00
|
|
|
game.AddEvent(new simpleengine::EntityEvent(game.GetWindow(), std::make_unique<SnakePlayerEntity>(game.GetWindow()->getSize())));
|
2020-07-03 18:31:17 +00:00
|
|
|
|
|
|
|
return game.Run();
|
|
|
|
}
|