Basic user controlled world view

This commit is contained in:
Maximilian Giller 2023-05-10 11:03:14 +02:00
parent 9d738f5707
commit 1b5bc82c1c
16 changed files with 47 additions and 72 deletions

View file

@ -40,8 +40,6 @@ set(SOURCES
src/debug/grid_debug_layer.h src/debug/grid_debug_layer.h
src/game/input/input_mapper.cpp src/game/input/input_mapper.cpp
src/game/input/input_mapper.h 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.h
src/game/input/direction.cpp src/game/input/direction.cpp
src/game/player/player.cpp src/game/player/player.cpp

View file

@ -8,7 +8,7 @@
// FPS // FPS
#define FRAME_RATE 60 #define FRAME_RATE 60
#define FRAME_TIME 1.0f / FRAME_RATE #define FRAME_TIME sf::Time(sf::seconds(1.0f / FRAME_RATE))
// Window settings // Window settings
#define ANTIALIASINGLEVEL 8 #define ANTIALIASINGLEVEL 8

View file

@ -32,7 +32,7 @@ void GridDebugLayer::draw(sf::RenderWindow *window) const
} }
} }
void GridDebugLayer::update() void GridDebugLayer::update(Game *game)
{ {
} }

View file

@ -11,7 +11,7 @@ public:
~GridDebugLayer(); ~GridDebugLayer();
void draw(sf::RenderWindow *window) const override; void draw(sf::RenderWindow *window) const override;
void update() override; void update(Game *game) override;
private: private:
std::vector<GameObject*> marker; std::vector<GameObject*> marker;

View file

@ -5,16 +5,13 @@
#include "game.h" #include "game.h"
Game::Game(std::shared_ptr<sf::RenderWindow> window) : window(std::move(window)), Game::Game(std::shared_ptr<sf::RenderWindow> window) : window(std::move(window)),
gameObjects(), gameObjects()
actionController(new ActionController(this))
{ {
InputMapper::setGame(this); InputMapper::setGame(this);
} }
Game::~Game() Game::~Game()
{ {
delete actionController;
for (auto &gameObject: gameObjects) for (auto &gameObject: gameObjects)
{ {
delete gameObject; delete gameObject;
@ -68,6 +65,6 @@ void Game::update()
{ {
for (auto &gameObject: gameObjects) for (auto &gameObject: gameObjects)
{ {
gameObject->update(); gameObject->update(this);
} }
} }

View file

@ -5,10 +5,10 @@
#include <memory> #include <memory>
#include "../logging/easylogging++.h" #include "../logging/easylogging++.h"
#include "game_object.h" #include "game_object.h"
#include "input/action_controller.h"
#include "input/input_mapper.h" #include "input/input_mapper.h"
class GameObject;
class ActionController; class ActionController;
@ -26,7 +26,6 @@ public:
void addGameObject(GameObject *gameObject); void addGameObject(GameObject *gameObject);
std::shared_ptr<sf::RenderWindow> window; std::shared_ptr<sf::RenderWindow> window;
ActionController *actionController;
private: private:
std::vector<GameObject *> gameObjects; std::vector<GameObject *> gameObjects;

View file

@ -6,13 +6,16 @@
#include <SFML/Graphics/Drawable.hpp> #include <SFML/Graphics/Drawable.hpp>
#include <SFML/Graphics/RenderWindow.hpp> #include <SFML/Graphics/RenderWindow.hpp>
#include "../coordinates/translated_coordinates.h" #include "../coordinates/translated_coordinates.h"
#include "game.h"
class Game;
class GameObject { class GameObject {
public: public:
GameObject(); GameObject();
virtual void draw(sf::RenderWindow *window) const = 0; virtual void draw(sf::RenderWindow *window) const = 0;
virtual void update() = 0; virtual void update(Game *game) = 0;
TranslatedCoordinates coordinates; TranslatedCoordinates coordinates;
}; };

View file

@ -1,15 +0,0 @@
#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);
}

View file

@ -1,26 +0,0 @@
#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

View file

@ -24,7 +24,6 @@ void InputMapper::processEvents()
game->exit(); game->exit();
break; break;
case sf::Event::Resized: case sf::Event::Resized:
game->window->setView(sf::View(sf::FloatRect(0, 0, event.size.width, event.size.height)));
break; break;
default: default:
break; break;

View file

@ -12,7 +12,7 @@ public:
void draw(sf::RenderWindow *window) const override; void draw(sf::RenderWindow *window) const override;
void update() override; void update(Game *game) override;
}; };

View file

@ -1,29 +1,46 @@
#include "world_view.h" #include "world_view.h"
WorldView::WorldView(Game *game) WorldView::WorldView() : view(nullptr)
{ {
this->game = game;
view = game->window->getView();
} }
WorldView::~WorldView() WorldView::~WorldView()
{ {
delete view;
} }
void WorldView::draw(sf::RenderWindow *window) const void WorldView::draw(sf::RenderWindow *window) const
{ {
} }
void WorldView::update() void WorldView::update(Game *game)
{ {
auto moveDirection = InputMapper::getInputDirection(); if (view == nullptr)
{
initializeView(game);
}
// Update size
auto windowSize = game->window->getSize();
sf::Vector2f viewSize = sf::Vector2f(windowSize.x, windowSize.y);
view->setSize(viewSize);
// Update position
auto moveDirection = InputMapper::getInputDirection();
if (moveDirection != InputDirection::NONE) if (moveDirection != InputDirection::NONE)
{ {
float stepSize = 100.0f * FRAME_TIME; float stepSize = 100.0f * FRAME_TIME.asSeconds();
auto delta = stepSize * Direction::getVector(moveDirection); auto delta = stepSize * Direction::getVector(moveDirection);
this->view.move(delta); this->view->move(delta);
game->window->setView(this->view);
} }
// TODO: Only update if necessary
game->window->setView(*this->view);
}
void WorldView::initializeView(Game *game)
{
auto center = game->window->getView().getCenter();
auto size = game->window->getView().getSize();
view = new sf::View(center, size);
} }

View file

@ -5,20 +5,21 @@
#include "../game_object.h" #include "../game_object.h"
#include "../game.h" #include "../game.h"
class WorldView : GameObject class WorldView : public GameObject
{ {
public: public:
explicit WorldView(Game *game); explicit WorldView();
~WorldView(); ~WorldView();
void draw(sf::RenderWindow *window) const override; void draw(sf::RenderWindow *window) const override;
void update() override; void update(Game *game) override;
private: private:
sf::View view; sf::View *view;
Game *game;
void initializeView(Game *game);
}; };

View file

@ -3,6 +3,7 @@
#include "logging/easylogging++.h" #include "logging/easylogging++.h"
#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"
INITIALIZE_EASYLOGGINGPP INITIALIZE_EASYLOGGINGPP
@ -13,6 +14,7 @@ int main(int argc, char *argv[])
auto game = GameFactory::createWindowed("Holesome"); auto game = GameFactory::createWindowed("Holesome");
game->addGameObject(new GridDebugLayer(0, 50, 0, 50)); game->addGameObject(new GridDebugLayer(0, 50, 0, 50));
game->addGameObject(new WorldView());
game->run(); game->run();
} }

View file

@ -15,7 +15,7 @@ void CircleObject::draw(sf::RenderWindow *window) const
window->draw(circle); window->draw(circle);
} }
void CircleObject::update() void CircleObject::update(Game *game)
{ {
} }

View file

@ -10,7 +10,7 @@ public:
CircleObject(int radius, sf::Color color); CircleObject(int radius, sf::Color color);
void draw(sf::RenderWindow *window) const override; void draw(sf::RenderWindow *window) const override;
void update() override; void update(Game *game) override;
private: private:
int radius; int radius;