Basic trackable view

This commit is contained in:
Maximilian Giller 2023-05-24 14:00:51 +02:00
parent 29c5eb7bdd
commit b3c129e356
14 changed files with 44 additions and 43 deletions

View file

@ -54,7 +54,8 @@ set(SOURCES
src/game/input/input_identity.h src/game/input/input_identity.h
src/utilities/magic_enum.hpp src/utilities/magic_enum.hpp
src/game/player/player_spawner.cpp src/game/player/player_spawner.cpp
src/game/player/player_spawner.hpp) src/game/player/player_spawner.hpp
)
set(PHYSICS_00_SOURCES set(PHYSICS_00_SOURCES
src/prototypes/physics_00.cpp) src/prototypes/physics_00.cpp)

View file

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

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(Game *game) override; void update() override;
private: private:
std::vector<GameObject*> marker; std::vector<GameObject*> marker;

View file

@ -8,6 +8,7 @@ TrackingView::TrackingView(float freeMoveRadius, float dynamicFollowRadius) : fr
{ {
trackables = std::vector<ITrackable *>(); trackables = std::vector<ITrackable *>();
marker = new CircleObject(2, sf::Color::Yellow); marker = new CircleObject(2, sf::Color::Yellow);
Game::getInstance()->registerView(this);
} }
TrackingView::~TrackingView() TrackingView::~TrackingView()
@ -16,19 +17,19 @@ TrackingView::~TrackingView()
delete marker; delete marker;
} }
void TrackingView::lateUpdate(Game *game) void TrackingView::lateUpdate()
{ {
// Initialize if necessary // Initialize if necessary
if (view == nullptr) if (view == nullptr)
{ {
initializeView(game); initializeView();
} }
processTrackableStates(); processTrackableStates();
// Update size // Update size
// TODO: Update size based on distance of tracked objects // TODO: Update size based on distance of tracked objects
setSize(game->window->getSize()); setSize(Game::getInstance()->window->getSize());
if (!trackables.empty()) if (!trackables.empty())
{ {
@ -38,14 +39,14 @@ void TrackingView::lateUpdate(Game *game)
// Update window if necessary // Update window if necessary
if (hasViewChanged) if (hasViewChanged)
{ {
game->window->setView(*this->view); Game::getInstance()->window->setView(*this->view);
hasViewChanged = false; hasViewChanged = false;
} }
} }
void TrackingView::initializeView(Game *game) void TrackingView::initializeView()
{ {
auto size = game->window->getView().getSize(); auto size = Game::getInstance()->window->getView().getSize();
view = new sf::View({0, 0}, size); view = new sf::View({0, 0}, size);
} }
@ -78,6 +79,7 @@ void TrackingView::followTarget()
{ {
auto trackingPoint = getCollectiveTrackingPoint(); auto trackingPoint = getCollectiveTrackingPoint();
marker->coordinates.set(IsometricCoordinates(trackingPoint)); marker->coordinates.set(IsometricCoordinates(trackingPoint));
moveCenter(trackingPoint - getCenter());
return; return;
// if (isTargetInArea(freeMoveArea)) // if (isTargetInArea(freeMoveArea))

View file

@ -3,10 +3,11 @@
#include "../game_object.h" #include "../game_object.h"
#include "../game.h"
#include "ITrackable.h" #include "ITrackable.h"
#include "../../primitives/circle_object.h" #include "../../primitives/circle_object.h"
class CircleObject;
class TrackingView : public GameObject class TrackingView : public GameObject
{ {
public: public:
@ -17,11 +18,13 @@ public:
void draw(sf::RenderWindow *window) const override; void draw(sf::RenderWindow *window) const override;
void lateUpdate(Game *game) override;
void lateUpdate() override;
void addTrackable(ITrackable *trackable); void addTrackable(ITrackable *trackable);
sf::Vector2f getSize() const; sf::Vector2f getSize() const;
sf::Vector2f getCenter() const; sf::Vector2f getCenter() const;
private: private:
@ -33,26 +36,14 @@ private:
CircleObject *marker; CircleObject *marker;
void initializeView(Game *game); void initializeView();
void setSize(sf::Vector2u windowSize); void setSize(sf::Vector2u windowSize);
void followTarget(); void followTarget();
bool isTargetInArea(sf::Vector2f areaSize);
sf::Vector2f getClosestPositionInArea(sf::Vector2f areaSize) const;
/// Calculates the hardDirection the target should be pulled back in.
/// \return Normalized vector, pointing from center to target.
sf::Vector2f getRubber() const;
void performHardFollow();
void moveCenter(sf::Vector2<float> delta); void moveCenter(sf::Vector2<float> delta);
void performDynamicFollow();
sf::Vector2f getCollectiveTrackingPoint() const; sf::Vector2f getCollectiveTrackingPoint() const;
void processTrackableStates(); void processTrackableStates();

View file

@ -5,7 +5,8 @@
#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(),
views()
{ {
} }
@ -70,7 +71,7 @@ void Game::update()
{ {
if (gameObject->isActive) if (gameObject->isActive)
{ {
gameObject->update(this); gameObject->update();
} }
} }
@ -79,7 +80,7 @@ void Game::update()
{ {
if (gameObject->isActive) if (gameObject->isActive)
{ {
gameObject->lateUpdate(this); gameObject->lateUpdate();
} }
} }
@ -107,3 +108,8 @@ std::shared_ptr<Game> Game::constructInstance(const std::shared_ptr<sf::RenderWi
return singletonInstance; return singletonInstance;
} }
void Game::registerView(TrackingView *view)
{
views.push_back(view);
}

View file

@ -6,10 +6,9 @@
#include "../logging/easylogging++.h" #include "../logging/easylogging++.h"
#include "game_object.h" #include "game_object.h"
#include "input/input_mapper.h" #include "input/input_mapper.h"
#include "camera/tracking_view.h"
class TrackingView;
class GameObject;
class ActionController;
class Game class Game
@ -27,7 +26,10 @@ public:
void addGameObject(GameObject *gameObject); void addGameObject(GameObject *gameObject);
void registerView(TrackingView *view);
std::shared_ptr<sf::RenderWindow> window; std::shared_ptr<sf::RenderWindow> window;
std::vector<TrackingView*> views;
private: private:
static inline std::shared_ptr<Game> singletonInstance = nullptr; static inline std::shared_ptr<Game> singletonInstance = nullptr;
std::vector<GameObject *> gameObjects; std::vector<GameObject *> gameObjects;

View file

@ -6,9 +6,6 @@
#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
{ {
@ -18,10 +15,10 @@ public:
virtual void draw(sf::RenderWindow *window) const virtual void draw(sf::RenderWindow *window) const
{} {}
virtual void update(Game *game) virtual void update()
{} {}
virtual void lateUpdate(Game *game) virtual void lateUpdate()
{} {}
TranslatedCoordinates coordinates; TranslatedCoordinates coordinates;

View file

@ -13,7 +13,7 @@ sf::Vector2f Player::getTrackableSize() const
return {static_cast<float>(circle->getRadius() * 2), static_cast<float>(circle->getRadius() * 2)}; return {static_cast<float>(circle->getRadius() * 2), static_cast<float>(circle->getRadius() * 2)};
} }
void Player::update(Game *game) void Player::update()
{ {
if (!input->isActive) { if (!input->isActive) {
isActive = false; isActive = false;

View file

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

View file

@ -1,12 +1,13 @@
#include "player_spawner.hpp" #include "player_spawner.hpp"
#include "player.hpp" #include "player.hpp"
void PlayerSpawner::update(Game *game) void PlayerSpawner::update()
{ {
// Create player for new input identities // Create player for new input identities
for (auto &inputIdentity: InputMapper::getInstance()->newInputIdentities) for (auto &inputIdentity: InputMapper::getInstance()->newInputIdentities)
{ {
auto player = new Player(inputIdentity, sf::Color::Red, {0, 0}); auto player = new Player(inputIdentity, sf::Color::Red, {0, 0});
game->addGameObject(player); Game::getInstance()->addGameObject(player);
Game::getInstance()->views[0]->addTrackable(player);
} }
} }

View file

@ -7,7 +7,7 @@
class PlayerSpawner : public GameObject class PlayerSpawner : public GameObject
{ {
public: public:
void update(Game *game) override; void update() override;
}; };

View file

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

View file

@ -2,15 +2,16 @@
#define HOLESOME_CIRCLE_OBJECT_H #define HOLESOME_CIRCLE_OBJECT_H
#include "../game/game_object.h"
#include <SFML/Graphics/RenderTarget.hpp> #include <SFML/Graphics/RenderTarget.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include "../game/game.h"
class CircleObject : public GameObject { class CircleObject : public GameObject {
public: 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(Game *game) override; void update() override;
int getRadius() const; int getRadius() const;