From 12c3c7bdc87bbddb4454f9bc95e271100a3cdc7e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nils=20Forss=C3=A9n?= Date: Fri, 10 Nov 2023 01:28:05 +0100 Subject: [PATCH] Added movement to player --- src/Moving_object.h | 4 +++- src/Object.h | 8 +++----- src/Player.cc | 42 +++++++++++++++++++++++++++++++++++++----- src/Player.h | 21 ++++++++++++++------- src/Static_object.cc | 0 5 files changed, 57 insertions(+), 18 deletions(-) delete mode 100644 src/Static_object.cc diff --git a/src/Moving_object.h b/src/Moving_object.h index 959a195..aa26add 100644 --- a/src/Moving_object.h +++ b/src/Moving_object.h @@ -6,9 +6,11 @@ class Moving_object : public Object { public: - Moving_object() = default; + Moving_object() : direction{} {}; ~Moving_object() = default; protected: + sf::Vector2f direction; // Treat like unit-vector for movement + private: }; #endif \ No newline at end of file diff --git a/src/Object.h b/src/Object.h index 80f1400..91f1b39 100644 --- a/src/Object.h +++ b/src/Object.h @@ -2,22 +2,20 @@ #define OBJECT_H #include -#include class Object { public: - Object() : x_pos{0}, y_pos{0}, sprite{}, texture{} {}; + Object() : position{}, sprite{}, texture{} {}; virtual ~Object() = default; virtual bool collides(Object& other) = 0; virtual void collision(Object& other) = 0; virtual void update() = 0; - virtual void render(sf::RenderWindow& window) = 0; + virtual void render(sf::RenderWindow& window) const = 0; protected: - double x_pos; - double y_pos; + sf::Vector2f position; sf::Sprite sprite; sf::Texture texture; private: diff --git a/src/Player.cc b/src/Player.cc index ee8a77c..0f3b2f0 100644 --- a/src/Player.cc +++ b/src/Player.cc @@ -1,12 +1,14 @@ #include "Player.h" -Player::Player() : collected{0} +Player::Player() : collected{0}, max_speed{10} { texture.loadFromFile("assets/4V_figur.png"); sprite.setTexture(texture); sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF); - sprite.setPosition(S_WIDTH / 2, S_HEIGHT / 2); + sf::FloatRect gb {sprite.getGlobalBounds()}; + sprite.setOrigin(gb.width / 2, gb.height / 2); + sprite.setPosition(position); } bool Player::collides(Object& other) @@ -21,12 +23,42 @@ void Player::collision(Object& other) void Player::update() { - x_pos += 0.1; - sprite.setPosition((S_WIDTH / 2) + x_pos, S_HEIGHT / 2); + position += max_speed * direction; + + sprite.setPosition(position); return; } -void Player::render(sf::RenderWindow& window) +void Player::render(sf::RenderWindow& window) const { window.draw(sprite); +} + +void Player::handle_input(sf::Event& event) +{ + direction = {0,0}; + + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) + { + direction.y -= 1; + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)) + { + direction.y += 1; + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) + { + direction.x += 1; + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) + { + direction.x -= 1; + } + + // If the vector is diagonal, normalize its length + if (abs(direction.x) + abs(direction.y) == 2) + { + direction.x *= M_SQRT1_2; + direction.y *= M_SQRT1_2; + } } \ No newline at end of file diff --git a/src/Player.h b/src/Player.h index 05bb5ca..c76c8bc 100644 --- a/src/Player.h +++ b/src/Player.h @@ -1,21 +1,28 @@ +#ifndef PLAYER_H +#define PLAYER_H + +#define _USE_MATH_DEFINES #include +#include #include "Moving_object.h" #include "constants.h" - class Player : public Moving_object { public: Player(); ~Player() = default; - bool collides(Object& other); - void collision(Object& other); - void update(); - void render(sf::RenderWindow& window); - + bool collides(Object& other) override; + void collision(Object& other) override; + void update() override; + void render(sf::RenderWindow& window) const override; + void handle_input(sf::Event& event); protected: private: int collected; -}; \ No newline at end of file + float max_speed; +}; + +#endif \ No newline at end of file diff --git a/src/Static_object.cc b/src/Static_object.cc deleted file mode 100644 index e69de29..0000000 -- 2.30.2