Input handling structure and tested isometric plane rendering
This commit is contained in:
parent
0402ed2a67
commit
adc2246bb7
11 changed files with 227 additions and 55 deletions
|
@ -38,7 +38,10 @@ set(SOURCES
|
|||
src/config.h
|
||||
src/debug/grid_debug_layer.cpp
|
||||
src/debug/grid_debug_layer.h
|
||||
src/game/input/input_mapper.cpp src/game/input/input_mapper.h src/game/action_controller.cpp src/game/action_controller.h)
|
||||
src/game/input/input_mapper.cpp
|
||||
src/game/input/input_mapper.h
|
||||
src/game/input/action_controller.cpp
|
||||
src/game/input/action_controller.h src/game/input/direction.h src/game/input/direction.cpp)
|
||||
|
||||
set(PHYSICS_00_SOURCES
|
||||
src/prototypes/physics_00.cpp)
|
||||
|
|
|
@ -18,4 +18,10 @@
|
|||
#define ISOMETRIC_SKEW 0.3f
|
||||
#define WORLD_TO_ISO_SCALE 10.0f
|
||||
|
||||
|
||||
|
||||
// DEBUG
|
||||
|
||||
#define DB_ISOPLANE_CORNER_RADIUS 2
|
||||
|
||||
#endif //HOLESOME_CONFIG_H
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include "grid_debug_layer.h"
|
||||
#include "../primitives/circle_object.h"
|
||||
#include "../config.h"
|
||||
|
||||
GridDebugLayer::GridDebugLayer(int minX, int maxX, int minY, int maxY)
|
||||
{
|
||||
|
@ -8,7 +9,7 @@ GridDebugLayer::GridDebugLayer(int minX, int maxX, int minY, int maxY)
|
|||
{
|
||||
for (int y = minY; y <= maxY; y++)
|
||||
{
|
||||
auto *gameObject = new CircleObject(5, sf::Color::Red);
|
||||
auto *gameObject = new CircleObject(DB_ISOPLANE_CORNER_RADIUS, sf::Color::Red);
|
||||
gameObject->coordinates.set(WorldCoordinates(x, y, 0));
|
||||
marker.push_back(gameObject);
|
||||
}
|
||||
|
|
|
@ -5,10 +5,21 @@
|
|||
#include "game.h"
|
||||
#include "../config.h"
|
||||
|
||||
Game::Game(std::shared_ptr<sf::RenderWindow> window) : window(std::move(window)), gameObjects()
|
||||
Game::Game(std::shared_ptr<sf::RenderWindow> window) : window(std::move(window)),
|
||||
gameObjects(),
|
||||
actionController(new ActionController(this)),
|
||||
inputMapper(new InputMapper(this))
|
||||
{
|
||||
}
|
||||
|
||||
Game::~Game()
|
||||
{
|
||||
for (auto &gameObject: gameObjects)
|
||||
{
|
||||
delete gameObject;
|
||||
}
|
||||
}
|
||||
|
||||
void Game::run()
|
||||
{
|
||||
sf::Clock clock;
|
||||
|
@ -16,14 +27,15 @@ void Game::run()
|
|||
|
||||
while (window->isOpen())
|
||||
{
|
||||
processEvents();
|
||||
|
||||
inputMapper->processEvents();
|
||||
TimeSinceLastUpdate += clock.restart();
|
||||
while (TimeSinceLastUpdate >= FRAME_TIME)
|
||||
{
|
||||
TimeSinceLastUpdate -= FRAME_TIME;
|
||||
|
||||
update();
|
||||
processEvents();
|
||||
inputMapper->processEvents();
|
||||
}
|
||||
drawFrame();
|
||||
}
|
||||
|
@ -46,14 +58,6 @@ void Game::drawFrame()
|
|||
window->display();
|
||||
}
|
||||
|
||||
Game::~Game()
|
||||
{
|
||||
for (auto &gameObject: gameObjects)
|
||||
{
|
||||
delete gameObject;
|
||||
}
|
||||
}
|
||||
|
||||
void Game::addGameObject(GameObject *gameObject)
|
||||
{
|
||||
gameObjects.push_back(gameObject);
|
||||
|
@ -66,36 +70,3 @@ void Game::update()
|
|||
gameObject->update();
|
||||
}
|
||||
}
|
||||
|
||||
void Game::processEvents()
|
||||
{
|
||||
sf::Event event;
|
||||
while (window->pollEvent(event))
|
||||
{
|
||||
switch (event.type)
|
||||
{
|
||||
case sf::Event::KeyPressed:
|
||||
// Close game on Escape or Q in DEV Mode
|
||||
if (DEVELOPER_MODE && (event.key.code == sf::Keyboard::Escape || event.key.code == sf::Keyboard::Q))
|
||||
{
|
||||
exit();
|
||||
}
|
||||
break;
|
||||
case sf::Event::KeyReleased:
|
||||
break;
|
||||
case sf::Event::Closed:
|
||||
exit();
|
||||
break;
|
||||
case sf::Event::Resized:
|
||||
handleWindowResize(event);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Game::handleWindowResize(const sf::Event &event)
|
||||
{
|
||||
window->setView(sf::View(sf::FloatRect(0, 0, event.size.width, event.size.height)));
|
||||
}
|
||||
|
|
|
@ -1,7 +1,3 @@
|
|||
//
|
||||
// Created by max on 26.04.23.
|
||||
//
|
||||
|
||||
#ifndef HOLESOME_GAME_H
|
||||
#define HOLESOME_GAME_H
|
||||
|
||||
|
@ -9,11 +5,19 @@
|
|||
#include <memory>
|
||||
#include "../logging/easylogging++.h"
|
||||
#include "game_object.h"
|
||||
#include "input/action_controller.h"
|
||||
#include "input/input_mapper.h"
|
||||
|
||||
|
||||
class ActionController;
|
||||
class InputMapper;
|
||||
|
||||
|
||||
class Game
|
||||
{
|
||||
public:
|
||||
explicit Game(std::shared_ptr<sf::RenderWindow> window);
|
||||
|
||||
~Game();
|
||||
|
||||
void run();
|
||||
|
@ -22,17 +26,15 @@ public:
|
|||
|
||||
void addGameObject(GameObject *gameObject);
|
||||
|
||||
private:
|
||||
std::shared_ptr<sf::RenderWindow> window;
|
||||
ActionController *actionController;
|
||||
InputMapper *inputMapper;
|
||||
private:
|
||||
std::vector<GameObject *> gameObjects;
|
||||
|
||||
void drawFrame();
|
||||
|
||||
void update();
|
||||
|
||||
void processEvents();
|
||||
|
||||
void handleWindowResize(const sf::Event &event);
|
||||
};
|
||||
|
||||
|
||||
|
|
15
src/game/input/action_controller.cpp
Normal file
15
src/game/input/action_controller.cpp
Normal file
|
@ -0,0 +1,15 @@
|
|||
#include "action_controller.h"
|
||||
|
||||
ActionController::ActionController(Game *game) : game(game)
|
||||
{
|
||||
}
|
||||
|
||||
void ActionController::moveView(InputDirection direction)
|
||||
{
|
||||
float stepSize = 100.0f;
|
||||
auto delta = stepSize * Direction::getVector(direction);
|
||||
|
||||
auto view = game->window->getView();
|
||||
view.move(delta);
|
||||
game->window->setView(view);
|
||||
}
|
26
src/game/input/action_controller.h
Normal file
26
src/game/input/action_controller.h
Normal file
|
@ -0,0 +1,26 @@
|
|||
#ifndef HOLESOME_ACTION_CONTROLLER_H
|
||||
#define HOLESOME_ACTION_CONTROLLER_H
|
||||
|
||||
|
||||
#include "../game.h"
|
||||
#include "direction.h"
|
||||
|
||||
class Game;
|
||||
|
||||
|
||||
/**
|
||||
* Holds all the actions that can be performed and executes them when requested.
|
||||
*/
|
||||
class ActionController
|
||||
{
|
||||
public:
|
||||
explicit ActionController(Game *game);
|
||||
|
||||
void moveView(InputDirection direction);
|
||||
|
||||
private:
|
||||
Game *game;
|
||||
};
|
||||
|
||||
|
||||
#endif //HOLESOME_ACTION_CONTROLLER_H
|
50
src/game/input/direction.cpp
Normal file
50
src/game/input/direction.cpp
Normal file
|
@ -0,0 +1,50 @@
|
|||
#include <map>
|
||||
#include "direction.h"
|
||||
|
||||
InputDirection Direction::getDirection(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;
|
||||
|
||||
if (map.find(key) == map.end())
|
||||
return InputDirection::NONE;
|
||||
|
||||
return map[key];
|
||||
}
|
||||
|
||||
sf::Vector2f Direction::getVector(InputDirection direction)
|
||||
{
|
||||
auto vector = sf::Vector2f(0.0f, 0.0f);
|
||||
|
||||
if (direction == InputDirection::NONE)
|
||||
{
|
||||
return vector;
|
||||
}
|
||||
|
||||
// Combine all relevant directions into one vector
|
||||
if (direction & InputDirection::UP)
|
||||
{
|
||||
vector.y -= 1;
|
||||
}
|
||||
if (direction & InputDirection::DOWN)
|
||||
{
|
||||
vector.y += 1;
|
||||
}
|
||||
if (direction & InputDirection::LEFT)
|
||||
{
|
||||
vector.x -= 1;
|
||||
}
|
||||
if (direction & InputDirection::RIGHT)
|
||||
{
|
||||
vector.x += 1;
|
||||
}
|
||||
|
||||
return vector;
|
||||
}
|
23
src/game/input/direction.h
Normal file
23
src/game/input/direction.h
Normal file
|
@ -0,0 +1,23 @@
|
|||
#ifndef HOLESOME_DIRECTION_H
|
||||
#define HOLESOME_DIRECTION_H
|
||||
|
||||
#include <SFML/Window/Keyboard.hpp>
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
|
||||
enum InputDirection
|
||||
{
|
||||
NONE = 0,
|
||||
UP = 1,
|
||||
DOWN = 2,
|
||||
LEFT = 4,
|
||||
RIGHT = 8
|
||||
};
|
||||
|
||||
class Direction
|
||||
{
|
||||
public:
|
||||
static InputDirection getDirection(sf::Keyboard::Key key);
|
||||
static sf::Vector2f getVector(InputDirection direction);
|
||||
};
|
||||
|
||||
#endif //HOLESOME_DIRECTION_H
|
48
src/game/input/input_mapper.cpp
Normal file
48
src/game/input/input_mapper.cpp
Normal file
|
@ -0,0 +1,48 @@
|
|||
#include "input_mapper.h"
|
||||
|
||||
|
||||
InputMapper::InputMapper(Game *game) : game(game)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void InputMapper::processEvents()
|
||||
{
|
||||
sf::Event event{};
|
||||
while (game->window->pollEvent(event))
|
||||
{
|
||||
switch (event.type)
|
||||
{
|
||||
case sf::Event::KeyPressed:
|
||||
handleKeyPress(event.key);
|
||||
break;
|
||||
case sf::Event::KeyReleased:
|
||||
break;
|
||||
case sf::Event::Closed:
|
||||
game->exit();
|
||||
break;
|
||||
case sf::Event::Resized:
|
||||
game->window->setView(sf::View(sf::FloatRect(0, 0, event.size.width, event.size.height)));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void InputMapper::handleKeyPress(sf::Event::KeyEvent event)
|
||||
{
|
||||
// Close game on Escape or Q in DEV Mode
|
||||
if (DEVELOPER_MODE && (event.code == sf::Keyboard::Escape || event.code == sf::Keyboard::Q))
|
||||
{
|
||||
game->exit();
|
||||
return;
|
||||
}
|
||||
|
||||
// Move view
|
||||
auto direction = Direction::getDirection(event.code);
|
||||
if (direction != InputDirection::NONE)
|
||||
{
|
||||
game->actionController->moveView(direction);
|
||||
}
|
||||
}
|
27
src/game/input/input_mapper.h
Normal file
27
src/game/input/input_mapper.h
Normal file
|
@ -0,0 +1,27 @@
|
|||
#ifndef HOLESOME_INPUT_MAPPER_H
|
||||
#define HOLESOME_INPUT_MAPPER_H
|
||||
|
||||
#include "../game.h"
|
||||
#include <SFML/Window/Event.hpp>
|
||||
#include "../../config.h"
|
||||
|
||||
class Game;
|
||||
|
||||
/**
|
||||
* Maps the inputs to actions.
|
||||
*/
|
||||
class InputMapper
|
||||
{
|
||||
public:
|
||||
explicit InputMapper(Game *game);
|
||||
|
||||
void processEvents();
|
||||
|
||||
private:
|
||||
Game *game;
|
||||
|
||||
void handleKeyPress(sf::Event::KeyEvent event);
|
||||
};
|
||||
|
||||
|
||||
#endif //HOLESOME_INPUT_MAPPER_H
|
Loading…
Reference in a new issue