Properly implemented grid coordinates

This commit is contained in:
Maximilian Giller 2023-06-11 15:54:05 +02:00
parent 4b96f4f9be
commit 12b73c00ba
19 changed files with 45 additions and 45 deletions

View file

@ -82,13 +82,14 @@ set(SOURCES
src/game/input/button_config_factory.hpp
src/game/input/game_action_config.hpp
src/game/input/gamepad_buttons.hpp
src/game/physics/map_simulation.cpp
src/game/physics/map_simulation.hpp
src/game/physics/map_player.hpp
src/game/physics/map/map_simulation.cpp
src/game/physics/map/map_simulation.hpp
src/game/physics/map/map_player.hpp
src/game/physics/map/map_player.cpp
src/game/level/level_config.hpp
src/game/level/level_loader.cpp
src/game/level/level_loader.hpp
src/levels.hpp src/game/physics/map_player.cpp)
src/levels.hpp)
set(PHYSICS_00_SOURCES
src/prototypes/physics_00.cpp)

View file

@ -80,6 +80,11 @@ struct GridCoordinates
{
float x;
float y;
GridCoordinates() = default;
GridCoordinates(float x, float y) : x(x), y(y)
{}
};

View file

@ -20,7 +20,7 @@ GridCoordinates TranslatedCoordinates::grid() const {
auto referenceWordCoordinates = world();
// Grid coords are just camera coords without height, and scaled differently
return {referenceWordCoordinates.x * worldToGridFactor, referenceWordCoordinates.y * worldToGridFactor};
return {referenceWordCoordinates.x - 0.5f, referenceWordCoordinates.y - 0.5f};
}
void TranslatedCoordinates::set(WorldCoordinates newWorldCoordinates) {
@ -56,7 +56,7 @@ void TranslatedCoordinates::setParent(std::shared_ptr<TranslatedCoordinates> par
void TranslatedCoordinates::set(GridCoordinates newGridCoordinates)
{
this->worldCoordinates = {newGridCoordinates.x / worldToGridFactor, newGridCoordinates.y / worldToGridFactor, 0};
this->worldCoordinates = {newGridCoordinates.x + 0.5f, newGridCoordinates.y + 0.5f, 0};
}
void TranslatedCoordinates::setWorldOffset(WorldCoordinates offset)

View file

@ -1,7 +1,3 @@
//
// Created by max on 27.04.23.
//
#ifndef HOLESOME_TRANSLATED_COORDINATES_H
#define HOLESOME_TRANSLATED_COORDINATES_H
@ -9,8 +5,6 @@
#include "coordinates.h"
#include "coordinate_transformer.h"
#define INITIAL_WORLD_TO_GRID_FACTOR 0.25f
class TranslatedCoordinates
{
public:
@ -42,7 +36,6 @@ public:
private:
WorldCoordinates worldCoordinates;
const float worldToGridFactor = INITIAL_WORLD_TO_GRID_FACTOR;
std::shared_ptr<TranslatedCoordinates> parent = nullptr;
};

View file

@ -0,0 +1,11 @@
#include "collectable.hpp"
Collectable::Collectable(GridCoordinates position)
{
coordinates->set(position);
}
void Collectable::draw(sf::RenderWindow *window)
{
}

View file

@ -4,10 +4,10 @@
#include "../game_object.h"
class EnvironmentCollectable : public GameObject
class Collectable : public GameObject
{
public:
EnvironmentCollectable(GridCoordinates position);
Collectable(GridCoordinates position);
void draw(sf::RenderWindow *window) override;
};

View file

@ -1,11 +0,0 @@
#include "environment_collectable.hpp"
EnvironmentCollectable::EnvironmentCollectable(GridCoordinates position)
{
coordinates->set(position);
}
void EnvironmentCollectable::draw(sf::RenderWindow *window)
{
}

View file

@ -4,7 +4,7 @@
#include "game.h"
#include "level/level_loader.hpp"
#include "physics/map_simulation.hpp"
#include "physics/map/map_simulation.hpp"
#include "../debug/grid_debug_layer.h"
#include "player/player_spawner.hpp"

View file

@ -3,6 +3,7 @@
#include <SFML/System/Vector2.hpp>
#include "../../coordinates/coordinates.h"
#include <utility>
#include <vector>
@ -10,10 +11,10 @@ struct LevelConfig
{
std::string name;
sf::Vector2f worldMapSize = {};
std::vector<sf::Vector2f> playerSpawnPoints = {};
std::vector<GridCoordinates> playerSpawnPoints = {};
LevelConfig(std::string name, const sf::Vector2f &worldMapSize,
const std::vector<sf::Vector2f> &playerSpawnPoints)
const std::vector<GridCoordinates> &playerSpawnPoints)
: name(std::move(name)), worldMapSize(worldMapSize), playerSpawnPoints(playerSpawnPoints)
{ }

View file

@ -1,6 +1,6 @@
#include "level_loader.hpp"
#include "../game.h"
#include "../physics/map_simulation.hpp"
#include "../physics/map/map_simulation.hpp"
#include "../../debug/grid_debug_layer.h"
#include "../player/player_spawner.hpp"
#include "../../levels.hpp"

View file

@ -3,8 +3,8 @@
#include <box2d/box2d.h>
#include "../player/player.hpp"
#include "../../config.h"
#include "../../player/player.hpp"
#include "../../../config.h"
class MapPlayer
{

View file

@ -1,5 +1,5 @@
#include "map_simulation.hpp"
#include "../../config.h"
#include "../../../config.h"
MapSimulation::MapSimulation()
{

View file

@ -4,7 +4,7 @@
#include <memory>
#include <box2d/box2d.h>
#include <SFML/System/Vector2.hpp>
#include "../player/player.hpp"
#include "../../player/player.hpp"
#include "map_player.hpp"
class MapSimulation

View file

@ -4,11 +4,11 @@
#include <utility>
Player::Player(std::shared_ptr<InputIdentity> assignedInput, const std::string &skinRessourceName,
sf::Vector2f spawnPosition)
GridCoordinates initCoordinates)
{
playerId = playerCreationCounter++;
this->spawnPosition = spawnPosition;
coordinates->set(spawnPosition);
this->spawnPosition = initCoordinates;
coordinates->set(initCoordinates);
input = std::move(assignedInput);

View file

@ -11,7 +11,7 @@ class Player : public GameObject, public ITrackable
{
public:
Player(std::shared_ptr<InputIdentity> assignedInput, const std::string &skinRessourceName,
sf::Vector2f initCoordinates);
GridCoordinates initCoordinates);
~Player();
@ -36,7 +36,7 @@ private:
float radiusInWorld = DEFAULT_PLAYER_RADIUS;
std::shared_ptr<VersatileSprite> skinSprite;
sf::Vector2f spawnPosition;
GridCoordinates spawnPosition;
int playerId;
static inline int playerCreationCounter = 0;

View file

@ -1,9 +1,9 @@
#include "player_spawner.hpp"
#include "player.hpp"
#include "../../texture_config.h"
#include "../physics/map_simulation.hpp"
#include "../physics/map/map_simulation.hpp"
PlayerSpawner::PlayerSpawner(const std::vector<sf::Vector2f> &spawnPoints)
PlayerSpawner::PlayerSpawner(std::vector<GridCoordinates> spawnPoints)
{
this->spawnPoints = spawnPoints;
@ -26,7 +26,7 @@ void PlayerSpawner::update()
void PlayerSpawner::spawnPlayer(const std::shared_ptr<InputIdentity> &inputIdentity)
{
// Get proper Spawn point, if available
sf::Vector2f spawn = spawnPoints[nextSpawnPointIndex];
auto spawn = spawnPoints[nextSpawnPointIndex];
nextSpawnPointIndex = static_cast<int>((nextSpawnPointIndex + 1) % spawnPoints.size());
auto player = new Player(inputIdentity, PLAYER_SKIN, spawn);

View file

@ -8,14 +8,14 @@
class PlayerSpawner : public GameObject
{
public:
PlayerSpawner(const std::vector<sf::Vector2f>& spawnPoints);
PlayerSpawner(std::vector<GridCoordinates> spawnPoints);
void update() override;
void spawnPlayer(const std::shared_ptr<InputIdentity> &inputIdentity);
private:
std::vector<sf::Vector2f> spawnPoints;
std::vector<GridCoordinates> spawnPoints;
int nextSpawnPointIndex = 0;
};

View file

@ -8,7 +8,7 @@
#define INITIAL_LEVEL "default"
std::map<std::string, LevelConfig> const LEVELS = {
{"default", LevelConfig("Default", {25, 25}, {{1, 1}})}
{"default", LevelConfig("Default", {25, 25}, {{0, 0}})}
};
#endif //HOLESOME_LEVELS_HPP