Improved directions systems and made basic player movable
This commit is contained in:
parent
0f0629b291
commit
1017fee4d3
17 changed files with 217 additions and 45 deletions
|
@ -44,7 +44,7 @@ set(SOURCES
|
||||||
src/game/input/direction.cpp
|
src/game/input/direction.cpp
|
||||||
src/game/player/player.cpp
|
src/game/player/player.cpp
|
||||||
src/game/player/player.hpp
|
src/game/player/player.hpp
|
||||||
src/game/input/game_inputs.hpp src/game/world/world_view.cpp src/game/world/world_view.h src/utilities/smart_list.cpp src/utilities/smart_list.h src/utilities/vector_utils.hpp)
|
src/game/input/game_inputs.hpp src/game/world/world_view.cpp src/game/world/world_view.h src/utilities/smart_list.cpp src/utilities/smart_list.h src/utilities/vector_utils.hpp src/game/world/ITrackable.h)
|
||||||
|
|
||||||
set(PHYSICS_00_SOURCES
|
set(PHYSICS_00_SOURCES
|
||||||
src/prototypes/physics_00.cpp)
|
src/prototypes/physics_00.cpp)
|
||||||
|
|
|
@ -18,7 +18,8 @@
|
||||||
#define ISOMETRIC_SKEW 0.3f
|
#define ISOMETRIC_SKEW 0.3f
|
||||||
#define WORLD_TO_ISO_SCALE 10.0f
|
#define WORLD_TO_ISO_SCALE 10.0f
|
||||||
|
|
||||||
|
// Directions
|
||||||
|
#define DIRECTION_HARD_ACTIVATION_THRESHOLD 0.45f
|
||||||
|
|
||||||
// DEBUG
|
// DEBUG
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,18 @@
|
||||||
//
|
|
||||||
// Created by max on 27.04.23.
|
|
||||||
//
|
|
||||||
|
|
||||||
#ifndef HOLESOME_COORDINATES_H
|
#ifndef HOLESOME_COORDINATES_H
|
||||||
#define HOLESOME_COORDINATES_H
|
#define HOLESOME_COORDINATES_H
|
||||||
|
|
||||||
|
#include <SFML/System/Vector2.hpp>
|
||||||
|
|
||||||
struct WorldCoordinates
|
struct WorldCoordinates
|
||||||
{
|
{
|
||||||
|
WorldCoordinates() = default;
|
||||||
|
|
||||||
|
WorldCoordinates(float x, float y, float z = 0) : x(x), y(y), z(z)
|
||||||
|
{}
|
||||||
|
|
||||||
|
WorldCoordinates(sf::Vector2f vector) : x(vector.x), y(vector.y), z(0)
|
||||||
|
{}
|
||||||
|
|
||||||
float x;
|
float x;
|
||||||
float y;
|
float y;
|
||||||
float z; // Height
|
float z; // Height
|
||||||
|
@ -55,6 +61,11 @@ struct IsometricCoordinates
|
||||||
float x;
|
float x;
|
||||||
float y;
|
float y;
|
||||||
float depth; // Bigger means further back. Can be used for accurate rendering order.
|
float depth; // Bigger means further back. Can be used for accurate rendering order.
|
||||||
|
|
||||||
|
sf::Vector2f toScreen() const
|
||||||
|
{
|
||||||
|
return {x, y};
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct GridCoordinates
|
struct GridCoordinates
|
||||||
|
|
|
@ -21,6 +21,10 @@ void TranslatedCoordinates::set(WorldCoordinates newWorldCoordinates) {
|
||||||
this->worldCoordinates = newWorldCoordinates;
|
this->worldCoordinates = newWorldCoordinates;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TranslatedCoordinates::set(TranslatedCoordinates newCoordinates) {
|
||||||
|
this->worldCoordinates = newCoordinates.world();
|
||||||
|
}
|
||||||
|
|
||||||
void TranslatedCoordinates::move(WorldCoordinates deltaWorldCoordinates) {
|
void TranslatedCoordinates::move(WorldCoordinates deltaWorldCoordinates) {
|
||||||
this->worldCoordinates = this->worldCoordinates + deltaWorldCoordinates;
|
this->worldCoordinates = this->worldCoordinates + deltaWorldCoordinates;
|
||||||
}
|
}
|
||||||
|
@ -29,4 +33,9 @@ TranslatedCoordinates::TranslatedCoordinates(WorldCoordinates worldCoordinates)
|
||||||
: worldCoordinates(worldCoordinates) {
|
: worldCoordinates(worldCoordinates) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TranslatedCoordinates::move(sf::Vector2f deltaWorldCoordinates)
|
||||||
|
{
|
||||||
|
move({deltaWorldCoordinates.x, deltaWorldCoordinates.y, 0});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,8 @@
|
||||||
|
|
||||||
#define INITIAL_WORLD_TO_GRID_FACTOR 0.25f
|
#define INITIAL_WORLD_TO_GRID_FACTOR 0.25f
|
||||||
|
|
||||||
class TranslatedCoordinates {
|
class TranslatedCoordinates
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
explicit TranslatedCoordinates(WorldCoordinates worldCoordinates);
|
explicit TranslatedCoordinates(WorldCoordinates worldCoordinates);
|
||||||
|
|
||||||
|
@ -23,8 +24,12 @@ public:
|
||||||
|
|
||||||
void set(WorldCoordinates newWorldCoordinates);
|
void set(WorldCoordinates newWorldCoordinates);
|
||||||
|
|
||||||
|
void set(TranslatedCoordinates newCoordinates);
|
||||||
|
|
||||||
void move(WorldCoordinates deltaWorldCoordinates);
|
void move(WorldCoordinates deltaWorldCoordinates);
|
||||||
|
|
||||||
|
void move(sf::Vector2f deltaWorldCoordinates);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
WorldCoordinates worldCoordinates;
|
WorldCoordinates worldCoordinates;
|
||||||
const float worldToGridFactor = INITIAL_WORLD_TO_GRID_FACTOR;
|
const float worldToGridFactor = INITIAL_WORLD_TO_GRID_FACTOR;
|
||||||
|
|
|
@ -1,51 +1,105 @@
|
||||||
#include <map>
|
#include <map>
|
||||||
#include "direction.h"
|
#include "direction.h"
|
||||||
#include "../../utilities/vector_utils.hpp"
|
#include "../../utilities/vector_utils.hpp"
|
||||||
|
#include "../../config.h"
|
||||||
|
|
||||||
InputDirection Direction::getDirection(sf::Keyboard::Key key)
|
HardDirection Direction::getKeyDirection(sf::Keyboard::Key key)
|
||||||
{
|
{
|
||||||
auto map = std::map<sf::Keyboard::Key, InputDirection>();
|
auto map = std::map<sf::Keyboard::Key, HardDirection>();
|
||||||
map[sf::Keyboard::W] = InputDirection::UP;
|
map[sf::Keyboard::W] = HardDirection::UP;
|
||||||
map[sf::Keyboard::S] = InputDirection::DOWN;
|
map[sf::Keyboard::S] = HardDirection::DOWN;
|
||||||
map[sf::Keyboard::A] = InputDirection::LEFT;
|
map[sf::Keyboard::A] = HardDirection::LEFT;
|
||||||
map[sf::Keyboard::D] = InputDirection::RIGHT;
|
map[sf::Keyboard::D] = HardDirection::RIGHT;
|
||||||
map[sf::Keyboard::Up] = InputDirection::UP;
|
map[sf::Keyboard::Up] = HardDirection::UP;
|
||||||
map[sf::Keyboard::Down] = InputDirection::DOWN;
|
map[sf::Keyboard::Down] = HardDirection::DOWN;
|
||||||
map[sf::Keyboard::Left] = InputDirection::LEFT;
|
map[sf::Keyboard::Left] = HardDirection::LEFT;
|
||||||
map[sf::Keyboard::Right] = InputDirection::RIGHT;
|
map[sf::Keyboard::Right] = HardDirection::RIGHT;
|
||||||
|
|
||||||
if (map.find(key) == map.end())
|
if (map.find(key) == map.end())
|
||||||
return InputDirection::NONE;
|
return HardDirection::NONE;
|
||||||
|
|
||||||
return map[key];
|
return map[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
sf::Vector2f Direction::getVector(InputDirection direction)
|
sf::Vector2f Direction::getVector(HardDirection direction)
|
||||||
{
|
{
|
||||||
auto vector = sf::Vector2f(0.0f, 0.0f);
|
auto vector = sf::Vector2f(0.0f, 0.0f);
|
||||||
|
|
||||||
if (direction == InputDirection::NONE)
|
if (direction == HardDirection::NONE)
|
||||||
{
|
{
|
||||||
return vector;
|
return vector;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Combine all relevant directions into one vector
|
// Combine all relevant directions into one vector
|
||||||
if (direction & InputDirection::UP)
|
if (direction & HardDirection::UP)
|
||||||
{
|
{
|
||||||
vector.y -= 1;
|
vector.y -= 1;
|
||||||
}
|
}
|
||||||
if (direction & InputDirection::DOWN)
|
if (direction & HardDirection::DOWN)
|
||||||
{
|
{
|
||||||
vector.y += 1;
|
vector.y += 1;
|
||||||
}
|
}
|
||||||
if (direction & InputDirection::LEFT)
|
if (direction & HardDirection::LEFT)
|
||||||
{
|
{
|
||||||
vector.x -= 1;
|
vector.x -= 1;
|
||||||
}
|
}
|
||||||
if (direction & InputDirection::RIGHT)
|
if (direction & HardDirection::RIGHT)
|
||||||
{
|
{
|
||||||
vector.x += 1;
|
vector.x += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return normalize(vector);
|
return normalize(vector);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
HardDirection Direction::getHardDirection(sf::Vector2f vector)
|
||||||
|
{
|
||||||
|
HardDirection direction = HardDirection::NONE;
|
||||||
|
vector = normalize(vector);
|
||||||
|
auto absVector = abs(vector);
|
||||||
|
|
||||||
|
// X axis
|
||||||
|
if (absVector.x >= DIRECTION_HARD_ACTIVATION_THRESHOLD)
|
||||||
|
{
|
||||||
|
if (vector.x > 0)
|
||||||
|
{
|
||||||
|
direction = static_cast<HardDirection>(direction | HardDirection::RIGHT);
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
direction = static_cast<HardDirection>(direction | HardDirection::LEFT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Y axis
|
||||||
|
if (absVector.y >= DIRECTION_HARD_ACTIVATION_THRESHOLD)
|
||||||
|
{
|
||||||
|
if (vector.y > 0)
|
||||||
|
{
|
||||||
|
direction = static_cast<HardDirection>(direction | HardDirection::DOWN);
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
direction = static_cast<HardDirection>(direction | HardDirection::UP);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return direction;
|
||||||
|
}
|
||||||
|
|
||||||
|
sf::Vector2f Direction::asVector() const
|
||||||
|
{
|
||||||
|
return directionVector;
|
||||||
|
}
|
||||||
|
|
||||||
|
HardDirection Direction::asHardDirection() const
|
||||||
|
{
|
||||||
|
return getHardDirection(directionVector);
|
||||||
|
}
|
||||||
|
|
||||||
|
Direction::Direction(sf::Vector2f directionVector)
|
||||||
|
{
|
||||||
|
this->directionVector = directionVector;
|
||||||
|
}
|
||||||
|
|
||||||
|
Direction::Direction(HardDirection direction)
|
||||||
|
{
|
||||||
|
this->directionVector = getVector(direction);
|
||||||
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
#include <SFML/Window/Keyboard.hpp>
|
#include <SFML/Window/Keyboard.hpp>
|
||||||
#include <SFML/System/Vector2.hpp>
|
#include <SFML/System/Vector2.hpp>
|
||||||
|
|
||||||
enum InputDirection
|
enum HardDirection
|
||||||
{
|
{
|
||||||
NONE = 0,
|
NONE = 0,
|
||||||
UP = 1,
|
UP = 1,
|
||||||
|
@ -13,11 +13,26 @@ enum InputDirection
|
||||||
RIGHT = 8
|
RIGHT = 8
|
||||||
};
|
};
|
||||||
|
|
||||||
class Direction
|
struct Direction
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static InputDirection getDirection(sf::Keyboard::Key key);
|
explicit Direction(sf::Vector2f directionVector);
|
||||||
static sf::Vector2f getVector(InputDirection direction);
|
|
||||||
|
explicit Direction(HardDirection direction);
|
||||||
|
|
||||||
|
static HardDirection getKeyDirection(sf::Keyboard::Key key);
|
||||||
|
|
||||||
|
static sf::Vector2f getVector(HardDirection direction);
|
||||||
|
|
||||||
|
static HardDirection getHardDirection(sf::Vector2f vector);
|
||||||
|
|
||||||
|
public:
|
||||||
|
sf::Vector2f asVector() const;
|
||||||
|
|
||||||
|
HardDirection asHardDirection() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
sf::Vector2f directionVector;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //HOLESOME_DIRECTION_H
|
#endif //HOLESOME_DIRECTION_H
|
||||||
|
|
|
@ -40,33 +40,33 @@ void InputMapper::handleKeyPress(sf::Event::KeyEvent event)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle direction
|
// Handle directionVector
|
||||||
auto direction = Direction::getDirection(event.code);
|
auto direction = Direction::getKeyDirection(event.code);
|
||||||
if (direction != InputDirection::NONE)
|
if (direction != HardDirection::NONE)
|
||||||
{
|
{
|
||||||
inputDirectionBuffer.push_back(direction);
|
inputDirectionBuffer.push_back(direction);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
InputDirection InputMapper::getInputDirection()
|
Direction InputMapper::getInputDirection()
|
||||||
{
|
{
|
||||||
InputDirection direction = InputDirection::NONE;
|
HardDirection direction = HardDirection::NONE;
|
||||||
|
|
||||||
for (InputDirection directionPart: inputDirectionBuffer)
|
for (HardDirection directionPart: inputDirectionBuffer)
|
||||||
{
|
{
|
||||||
direction = static_cast<InputDirection>(direction | directionPart);
|
direction = static_cast<HardDirection>(direction | directionPart);
|
||||||
}
|
}
|
||||||
|
|
||||||
return direction;
|
return Direction(direction);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InputMapper::handleKeyRelease(sf::Event::KeyEvent event)
|
void InputMapper::handleKeyRelease(sf::Event::KeyEvent event)
|
||||||
{
|
{
|
||||||
// Handle direction
|
// Handle directionVector
|
||||||
auto direction = Direction::getDirection(event.code);
|
auto direction = Direction::getKeyDirection(event.code);
|
||||||
if (direction != InputDirection::NONE)
|
if (direction != HardDirection::NONE)
|
||||||
{
|
{
|
||||||
// Remove direction from buffer
|
// Remove directionVector from buffer
|
||||||
inputDirectionBuffer.erase(std::remove(inputDirectionBuffer.begin(), inputDirectionBuffer.end(), direction),
|
inputDirectionBuffer.erase(std::remove(inputDirectionBuffer.begin(), inputDirectionBuffer.end(), direction),
|
||||||
inputDirectionBuffer.end());
|
inputDirectionBuffer.end());
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,12 +18,12 @@ public:
|
||||||
|
|
||||||
static void processEvents();
|
static void processEvents();
|
||||||
|
|
||||||
static InputDirection getInputDirection();
|
static Direction getInputDirection();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static inline Game *game = nullptr;
|
static inline Game *game = nullptr;
|
||||||
|
|
||||||
static inline std::vector<InputDirection> inputDirectionBuffer = std::vector<InputDirection>();
|
static inline std::vector<HardDirection> inputDirectionBuffer = std::vector<HardDirection>();
|
||||||
|
|
||||||
static void handleKeyPress(sf::Event::KeyEvent event);
|
static void handleKeyPress(sf::Event::KeyEvent event);
|
||||||
|
|
||||||
|
|
|
@ -1 +1,38 @@
|
||||||
#include "player.hpp"
|
#include "player.hpp"
|
||||||
|
|
||||||
|
sf::Vector2f Player::getTrackablePosition() const
|
||||||
|
{
|
||||||
|
return coordinates.isometric().toScreen();
|
||||||
|
}
|
||||||
|
|
||||||
|
sf::Vector2f Player::getTrackableSize() const
|
||||||
|
{
|
||||||
|
// TODO: Proper implementation
|
||||||
|
return {static_cast<float>(circle->getRadius() * 2), static_cast<float>(circle->getRadius() * 2)};
|
||||||
|
}
|
||||||
|
|
||||||
|
void Player::update(Game *game)
|
||||||
|
{
|
||||||
|
auto moveDirection = InputMapper::getInputDirection().asVector();
|
||||||
|
auto moveDelta = moveDirection * 10.0f * FRAME_TIME.asSeconds();
|
||||||
|
coordinates.move(moveDelta);
|
||||||
|
circle->coordinates.set(coordinates);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Player::draw(sf::RenderWindow *window) const
|
||||||
|
{
|
||||||
|
circle->draw(window);
|
||||||
|
}
|
||||||
|
|
||||||
|
Player::~Player()
|
||||||
|
{
|
||||||
|
delete circle;
|
||||||
|
}
|
||||||
|
|
||||||
|
Player::Player(const sf::Color color, WorldCoordinates initCoordinates)
|
||||||
|
{
|
||||||
|
coordinates.set(initCoordinates);
|
||||||
|
|
||||||
|
circle = new CircleObject(50, color);
|
||||||
|
circle->coordinates.set(coordinates);
|
||||||
|
}
|
||||||
|
|
|
@ -2,17 +2,28 @@
|
||||||
#define HOLESOME_PLAYER_HPP
|
#define HOLESOME_PLAYER_HPP
|
||||||
|
|
||||||
#include "../game_object.h"
|
#include "../game_object.h"
|
||||||
|
#include "../world/ITrackable.h"
|
||||||
|
#include "../../primitives/circle_object.h"
|
||||||
|
|
||||||
class Player : GameObject
|
class Player : public GameObject, public ITrackable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Player();
|
Player();
|
||||||
|
|
||||||
|
Player(const sf::Color color, WorldCoordinates initCoordinates);
|
||||||
|
|
||||||
~Player();
|
~Player();
|
||||||
|
|
||||||
void draw(sf::RenderWindow *window) const override;
|
void draw(sf::RenderWindow *window) const override;
|
||||||
|
|
||||||
void update(Game *game) override;
|
void update(Game *game) override;
|
||||||
|
|
||||||
|
sf::Vector2f getTrackablePosition() const override;
|
||||||
|
|
||||||
|
sf::Vector2f getTrackableSize() const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
CircleObject *circle;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
14
src/game/world/ITrackable.h
Normal file
14
src/game/world/ITrackable.h
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
#ifndef HOLESOME_ITRACKABLE_H
|
||||||
|
#define HOLESOME_ITRACKABLE_H
|
||||||
|
|
||||||
|
#include <SFML/System/Vector2.hpp>
|
||||||
|
|
||||||
|
class ITrackable
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual sf::Vector2f getTrackablePosition() const = 0;
|
||||||
|
|
||||||
|
virtual sf::Vector2f getTrackableSize() const = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif //HOLESOME_ITRACKABLE_H
|
|
@ -51,8 +51,8 @@ void WorldView::setSize(sf::Vector2u windowSize)
|
||||||
|
|
||||||
void WorldView::moveViewByControls()
|
void WorldView::moveViewByControls()
|
||||||
{
|
{
|
||||||
auto moveDirection = InputMapper::getInputDirection();
|
auto moveDirection = InputMapper::getInputDirection().asHardDirection();
|
||||||
if (moveDirection == InputDirection::NONE)
|
if (moveDirection == HardDirection::NONE)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#include "game/game_factory.hpp"
|
#include "game/game_factory.hpp"
|
||||||
#include "debug/grid_debug_layer.h"
|
#include "debug/grid_debug_layer.h"
|
||||||
#include "game/world/world_view.h"
|
#include "game/world/world_view.h"
|
||||||
|
#include "game/player/player.hpp"
|
||||||
|
|
||||||
INITIALIZE_EASYLOGGINGPP
|
INITIALIZE_EASYLOGGINGPP
|
||||||
|
|
||||||
|
@ -15,6 +16,7 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
game->addGameObject(new GridDebugLayer(0, 50, 0, 50));
|
game->addGameObject(new GridDebugLayer(0, 50, 0, 50));
|
||||||
game->addGameObject(new WorldView());
|
game->addGameObject(new WorldView());
|
||||||
|
game->addGameObject(new Player(sf::Color::Blue, WorldCoordinates(0, 0)));
|
||||||
|
|
||||||
game->run();
|
game->run();
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,4 +20,9 @@ void CircleObject::update(Game *game)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int CircleObject::getRadius() const
|
||||||
|
{
|
||||||
|
return radius;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,8 @@ public:
|
||||||
void draw(sf::RenderWindow *window) const override;
|
void draw(sf::RenderWindow *window) const override;
|
||||||
void update(Game *game) override;
|
void update(Game *game) override;
|
||||||
|
|
||||||
|
int getRadius() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int radius;
|
int radius;
|
||||||
sf::Color color;
|
sf::Color color;
|
||||||
|
|
|
@ -16,4 +16,10 @@ sf::Vector2<T> normalize(sf::Vector2<T> v)
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
sf::Vector2<T> abs(sf::Vector2<T> v)
|
||||||
|
{
|
||||||
|
return sf::Vector2<T>(std::abs(v.x), std::abs(v.y));
|
||||||
|
}
|
||||||
|
|
||||||
#endif //HOLESOME_VECTOR_UTILS_HPP
|
#endif //HOLESOME_VECTOR_UTILS_HPP
|
||||||
|
|
Loading…
Reference in a new issue