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 <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>
|
|
|
|
|
|
|
|
class SnakeMovementComponent : public simpleengine::Component {
|
|
|
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Update(const float &delta_time) override {
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
|
|
|
|
movement_direction.x = -1;
|
|
|
|
movement_direction.y = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
|
|
|
|
movement_direction.x = 0;
|
|
|
|
movement_direction.y = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
|
|
|
|
movement_direction.x = 1;
|
|
|
|
movement_direction.y = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
|
|
|
|
movement_direction.x = 0;
|
|
|
|
movement_direction.y = 1;
|
|
|
|
}
|
|
|
|
|
2020-07-03 20:26:44 +00:00
|
|
|
owning_entity.Move(delta_time, movement_direction.x, movement_direction.y);
|
2020-07-03 18:31:17 +00:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
float movement_speed;
|
|
|
|
sf::Vector2f movement_direction;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SnakePlayerEntity : public simpleengine::Entity {
|
|
|
|
private:
|
|
|
|
sf::RectangleShape shape;
|
|
|
|
float movement_speed = 100;
|
|
|
|
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);
|
|
|
|
|
2020-07-03 20:26:44 +00:00
|
|
|
this->AddComponent(std::make_unique<SnakeMovementComponent>(*this, movement_speed));
|
2020-07-03 18:31:17 +00:00
|
|
|
loc = sf::Vector2f(0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Update(const float& delta_time) override {
|
|
|
|
UpdateComponents(delta_time);
|
|
|
|
|
|
|
|
if (loc.x >= window_size.x) {
|
2020-07-03 20:26:44 +00:00
|
|
|
DestroyLater();
|
2020-07-03 18:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (loc.y >= window_size.y) {
|
2020-07-03 20:26:44 +00:00
|
|
|
DestroyLater();
|
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[]) {
|
|
|
|
simpleengine::Game game(500, 500, "First 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();
|
|
|
|
}
|