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/player/player.cpp
|
||||
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
|
||||
src/prototypes/physics_00.cpp)
|
||||
|
|
|
@ -18,7 +18,8 @@
|
|||
#define ISOMETRIC_SKEW 0.3f
|
||||
#define WORLD_TO_ISO_SCALE 10.0f
|
||||
|
||||
|
||||
// Directions
|
||||
#define DIRECTION_HARD_ACTIVATION_THRESHOLD 0.45f
|
||||
|
||||
// DEBUG
|
||||
|
||||
|
|
|
@ -1,12 +1,18 @@
|
|||
//
|
||||
// Created by max on 27.04.23.
|
||||
//
|
||||
|
||||
#ifndef HOLESOME_COORDINATES_H
|
||||
#define HOLESOME_COORDINATES_H
|
||||
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
|
||||
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 y;
|
||||
float z; // Height
|
||||
|
@ -55,6 +61,11 @@ struct IsometricCoordinates
|
|||
float x;
|
||||
float y;
|
||||
float depth; // Bigger means further back. Can be used for accurate rendering order.
|
||||
|
||||
sf::Vector2f toScreen() const
|
||||
{
|
||||
return {x, y};
|
||||
}
|
||||
};
|
||||
|
||||
struct GridCoordinates
|
||||
|
|
|
@ -21,6 +21,10 @@ void TranslatedCoordinates::set(WorldCoordinates newWorldCoordinates) {
|
|||
this->worldCoordinates = newWorldCoordinates;
|
||||
}
|
||||
|
||||
void TranslatedCoordinates::set(TranslatedCoordinates newCoordinates) {
|
||||
this->worldCoordinates = newCoordinates.world();
|
||||
}
|
||||
|
||||
void TranslatedCoordinates::move(WorldCoordinates deltaWorldCoordinates) {
|
||||
this->worldCoordinates = this->worldCoordinates + deltaWorldCoordinates;
|
||||
}
|
||||
|
@ -29,4 +33,9 @@ TranslatedCoordinates::TranslatedCoordinates(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
|
||||
|
||||
class TranslatedCoordinates {
|
||||
class TranslatedCoordinates
|
||||
{
|
||||
public:
|
||||
explicit TranslatedCoordinates(WorldCoordinates worldCoordinates);
|
||||
|
||||
|
@ -23,8 +24,12 @@ public:
|
|||
|
||||
void set(WorldCoordinates newWorldCoordinates);
|
||||
|
||||
void set(TranslatedCoordinates newCoordinates);
|
||||
|
||||
void move(WorldCoordinates deltaWorldCoordinates);
|
||||
|
||||
void move(sf::Vector2f deltaWorldCoordinates);
|
||||
|
||||
private:
|
||||
WorldCoordinates worldCoordinates;
|
||||
const float worldToGridFactor = INITIAL_WORLD_TO_GRID_FACTOR;
|
||||
|
|
|
@ -1,51 +1,105 @@
|
|||
#include <map>
|
||||
#include "direction.h"
|
||||
#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>();
|
||||
map[sf::Keyboard::W] = InputDirection::UP;
|
||||
map[sf::Keyboard::S] = InputDirection::DOWN;
|
||||
map[sf::Keyboard::A] = InputDirection::LEFT;
|
||||
map[sf::Keyboard::D] = InputDirection::RIGHT;
|
||||
map[sf::Keyboard::Up] = InputDirection::UP;
|
||||
map[sf::Keyboard::Down] = InputDirection::DOWN;
|
||||
map[sf::Keyboard::Left] = InputDirection::LEFT;
|
||||
map[sf::Keyboard::Right] = InputDirection::RIGHT;
|
||||
auto map = std::map<sf::Keyboard::Key, HardDirection>();
|
||||
map[sf::Keyboard::W] = HardDirection::UP;
|
||||
map[sf::Keyboard::S] = HardDirection::DOWN;
|
||||
map[sf::Keyboard::A] = HardDirection::LEFT;
|
||||
map[sf::Keyboard::D] = HardDirection::RIGHT;
|
||||
map[sf::Keyboard::Up] = HardDirection::UP;
|
||||
map[sf::Keyboard::Down] = HardDirection::DOWN;
|
||||
map[sf::Keyboard::Left] = HardDirection::LEFT;
|
||||
map[sf::Keyboard::Right] = HardDirection::RIGHT;
|
||||
|
||||
if (map.find(key) == map.end())
|
||||
return InputDirection::NONE;
|
||||
return HardDirection::NONE;
|
||||
|
||||
return map[key];
|
||||
}
|
||||
|
||||
sf::Vector2f Direction::getVector(InputDirection direction)
|
||||
sf::Vector2f Direction::getVector(HardDirection direction)
|
||||
{
|
||||
auto vector = sf::Vector2f(0.0f, 0.0f);
|
||||
|
||||
if (direction == InputDirection::NONE)
|
||||
if (direction == HardDirection::NONE)
|
||||
{
|
||||
return vector;
|
||||
}
|
||||
|
||||
// Combine all relevant directions into one vector
|
||||
if (direction & InputDirection::UP)
|
||||
if (direction & HardDirection::UP)
|
||||
{
|
||||
vector.y -= 1;
|
||||
}
|
||||
if (direction & InputDirection::DOWN)
|
||||
if (direction & HardDirection::DOWN)
|
||||
{
|
||||
vector.y += 1;
|
||||
}
|
||||
if (direction & InputDirection::LEFT)
|
||||
if (direction & HardDirection::LEFT)
|
||||
{
|
||||
vector.x -= 1;
|
||||
}
|
||||
if (direction & InputDirection::RIGHT)
|
||||
if (direction & HardDirection::RIGHT)
|
||||
{
|
||||
vector.x += 1;
|
||||
}
|
||||
|
||||
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/System/Vector2.hpp>
|
||||
|
||||
enum InputDirection
|
||||
enum HardDirection
|
||||
{
|
||||
NONE = 0,
|
||||
UP = 1,
|
||||
|
@ -13,11 +13,26 @@ enum InputDirection
|
|||
RIGHT = 8
|
||||
};
|
||||
|
||||
class Direction
|
||||
struct Direction
|
||||
{
|
||||
public:
|
||||
static InputDirection getDirection(sf::Keyboard::Key key);
|
||||
static sf::Vector2f getVector(InputDirection direction);
|
||||
explicit Direction(sf::Vector2f directionVector);
|
||||
|
||||
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
|
||||
|
|
|
@ -40,33 +40,33 @@ void InputMapper::handleKeyPress(sf::Event::KeyEvent event)
|
|||
return;
|
||||
}
|
||||
|
||||
// Handle direction
|
||||
auto direction = Direction::getDirection(event.code);
|
||||
if (direction != InputDirection::NONE)
|
||||
// Handle directionVector
|
||||
auto direction = Direction::getKeyDirection(event.code);
|
||||
if (direction != HardDirection::NONE)
|
||||
{
|
||||
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)
|
||||
{
|
||||
// Handle direction
|
||||
auto direction = Direction::getDirection(event.code);
|
||||
if (direction != InputDirection::NONE)
|
||||
// Handle directionVector
|
||||
auto direction = Direction::getKeyDirection(event.code);
|
||||
if (direction != HardDirection::NONE)
|
||||
{
|
||||
// Remove direction from buffer
|
||||
// Remove directionVector from buffer
|
||||
inputDirectionBuffer.erase(std::remove(inputDirectionBuffer.begin(), inputDirectionBuffer.end(), direction),
|
||||
inputDirectionBuffer.end());
|
||||
}
|
||||
|
|
|
@ -18,12 +18,12 @@ public:
|
|||
|
||||
static void processEvents();
|
||||
|
||||
static InputDirection getInputDirection();
|
||||
static Direction getInputDirection();
|
||||
|
||||
private:
|
||||
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);
|
||||
|
||||
|
|
|
@ -1 +1,38 @@
|
|||
#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
|
||||
|
||||
#include "../game_object.h"
|
||||
#include "../world/ITrackable.h"
|
||||
#include "../../primitives/circle_object.h"
|
||||
|
||||
class Player : GameObject
|
||||
class Player : public GameObject, public ITrackable
|
||||
{
|
||||
public:
|
||||
Player();
|
||||
|
||||
Player(const sf::Color color, WorldCoordinates initCoordinates);
|
||||
|
||||
~Player();
|
||||
|
||||
void draw(sf::RenderWindow *window) const 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()
|
||||
{
|
||||
auto moveDirection = InputMapper::getInputDirection();
|
||||
if (moveDirection == InputDirection::NONE)
|
||||
auto moveDirection = InputMapper::getInputDirection().asHardDirection();
|
||||
if (moveDirection == HardDirection::NONE)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include "game/game_factory.hpp"
|
||||
#include "debug/grid_debug_layer.h"
|
||||
#include "game/world/world_view.h"
|
||||
#include "game/player/player.hpp"
|
||||
|
||||
INITIALIZE_EASYLOGGINGPP
|
||||
|
||||
|
@ -15,6 +16,7 @@ int main(int argc, char *argv[])
|
|||
|
||||
game->addGameObject(new GridDebugLayer(0, 50, 0, 50));
|
||||
game->addGameObject(new WorldView());
|
||||
game->addGameObject(new Player(sf::Color::Blue, WorldCoordinates(0, 0)));
|
||||
|
||||
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 update(Game *game) override;
|
||||
|
||||
int getRadius() const;
|
||||
|
||||
private:
|
||||
int radius;
|
||||
sf::Color color;
|
||||
|
|
|
@ -16,4 +16,10 @@ sf::Vector2<T> normalize(sf::Vector2<T> 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
|
||||
|
|
Loading…
Reference in a new issue