Implemented parent/child relationships for coordinates

This commit is contained in:
Maximilian Giller 2023-05-29 21:39:18 +02:00
parent ce270864b2
commit 36f6a5e22d
10 changed files with 34 additions and 22 deletions

View file

@ -5,7 +5,7 @@
#include <map>
#include "game/input/input_device_group.h"
#define DEVELOPER_MODE true
#define DEVELOPER_MODE false
// FPS

View file

@ -1,16 +1,26 @@
#include "translated_coordinates.h"
#include <utility>
WorldCoordinates TranslatedCoordinates::world() const {
return worldCoordinates;
auto coordinates = worldCoordinates;
if (parent != nullptr)
{
coordinates = coordinates + parent->world();
}
return coordinates;
}
IsometricCoordinates TranslatedCoordinates::isometric() const {
return coordTransformer->worldToIsometric(worldCoordinates);
return coordTransformer->worldToIsometric(world());
}
GridCoordinates TranslatedCoordinates::grid() const {
auto referenceWordCoordinates = world();
// Grid coords are just camera coords without height, and scaled differently
return {worldCoordinates.x * worldToGridFactor, worldCoordinates.y * worldToGridFactor};
return {referenceWordCoordinates.x * worldToGridFactor, referenceWordCoordinates.y * worldToGridFactor};
}
void TranslatedCoordinates::set(WorldCoordinates newWorldCoordinates) {
@ -38,4 +48,8 @@ void TranslatedCoordinates::move(sf::Vector2f deltaWorldCoordinates)
move({deltaWorldCoordinates.x, deltaWorldCoordinates.y, 0});
}
void TranslatedCoordinates::setParent(std::shared_ptr<TranslatedCoordinates> parent, WorldCoordinates offset)
{
this->parent = std::move(parent);
this->worldCoordinates = offset;
}

View file

@ -32,10 +32,14 @@ public:
void move(sf::Vector2f deltaWorldCoordinates);
void setParent(std::shared_ptr<TranslatedCoordinates> parent, WorldCoordinates offset = {0, 0});
private:
WorldCoordinates worldCoordinates;
const float worldToGridFactor = INITIAL_WORLD_TO_GRID_FACTOR;
const std::shared_ptr<CoordinateTransformer> coordTransformer = std::make_shared<CoordinateTransformer>();
std::shared_ptr<TranslatedCoordinates> parent = nullptr;
};

View file

@ -23,7 +23,8 @@ GridDebugLayer::GridDebugLayer(int minX, int maxX, int minY, int maxY)
}
auto *gameObject = new CircleObject(DB_ISOPLANE_CORNER_RADIUS, color);
gameObject->coordinates.set(WorldCoordinates(x, y, 0));
gameObject->coordinates->setParent(coordinates);
gameObject->coordinates->set(WorldCoordinates(x, y, 0));
marker.push_back(gameObject);
}
}
@ -44,8 +45,3 @@ void GridDebugLayer::draw(sf::RenderWindow *window) const
gameObject->draw(window);
}
}
void GridDebugLayer::update()
{
}

View file

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

View file

@ -95,7 +95,7 @@ void TrackingView::followTrackables()
auto trackingPoint = getTrackingArea().getCenter();
if (DEVELOPER_MODE)
{
marker->coordinates.set(IsometricCoordinates(trackingPoint));
marker->coordinates->set(IsometricCoordinates(trackingPoint));
}
// Calculate distance to target to check how to handle it

View file

@ -1,6 +1,6 @@
#include "game_object.h"
GameObject::GameObject() : coordinates(WorldCoordinates(0, 0, 0))
GameObject::GameObject()
{
coordinates = std::make_shared<TranslatedCoordinates>(WorldCoordinates(0, 0));
}

View file

@ -21,7 +21,7 @@ public:
virtual void lateUpdate()
{}
TranslatedCoordinates coordinates;
std::shared_ptr<TranslatedCoordinates> coordinates;
bool isActive = true;
};

View file

@ -4,7 +4,7 @@
sf::Vector2f Player::getTrackablePosition() const
{
return coordinates.isometric().toScreen();
return coordinates->isometric().toScreen();
}
sf::Vector2f Player::getTrackableSize() const
@ -23,8 +23,7 @@ void Player::update()
auto moveDirection = input->direction.asIsometricVector();
auto moveDelta = moveDirection * speed * FRAME_TIME.asSeconds();
coordinates.move(moveDelta);
circle->coordinates.set(coordinates);
coordinates->move(moveDelta);
}
void Player::draw(sf::RenderWindow *window) const
@ -39,12 +38,12 @@ Player::~Player()
Player::Player(std::shared_ptr<InputIdentity> assignedInput, const sf::Color color, WorldCoordinates initCoordinates)
{
coordinates.set(initCoordinates);
coordinates->set(initCoordinates);
input = std::move(assignedInput);
circle = new CircleObject(10, color);
circle->coordinates.set(coordinates);
circle->coordinates->setParent(coordinates);
}
TrackableState Player::getTrackableState() const

View file

@ -10,7 +10,7 @@ void CircleObject::draw(sf::RenderWindow *window) const
{
sf::CircleShape circle(radius);
circle.setFillColor(color);
circle.setPosition(coordinates.isometric().x - radius, coordinates.isometric().y - radius);
circle.setPosition(coordinates->isometric().x - radius, coordinates->isometric().y - radius);
window->draw(circle);
}