From 24963b2d0ac333905dabd21b96c29d84270ec11b Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Fri, 30 Jun 2023 20:57:28 +0200 Subject: [PATCH] Fixed bug in calculating sprite offset for collectables --- TODO.md | 2 +- src/coordinates/translated_coordinates.cpp | 7 ++++++- src/coordinates/translated_coordinates.h | 3 +++ src/game/collectables/collectable.cpp | 2 +- .../collectables/collection/collectables_collection.cpp | 9 ++++++++- src/game/level/level_loader.cpp | 8 -------- src/game/physics/holes/collectable_simulation.cpp | 2 +- src/game/physics/holes/collectable_simulation.hpp | 2 +- src/game/physics/holes/holes_simulation.cpp | 2 +- src/levels.hpp | 2 +- 10 files changed, 23 insertions(+), 16 deletions(-) diff --git a/TODO.md b/TODO.md index a59aa5b..377d0aa 100644 --- a/TODO.md +++ b/TODO.md @@ -2,6 +2,7 @@ ## Next +- Physics - Damage other players on contact ## Bugs @@ -10,7 +11,6 @@ ## High priority -- Physics - Players-join-screen before the game starts - Short input delay when a new identity connects, to avoid accidental inputs at beginning diff --git a/src/coordinates/translated_coordinates.cpp b/src/coordinates/translated_coordinates.cpp index 07f4d03..56d1af2 100644 --- a/src/coordinates/translated_coordinates.cpp +++ b/src/coordinates/translated_coordinates.cpp @@ -46,6 +46,11 @@ void TranslatedCoordinates::move(WorldCoordinates deltaWorldCoordinates) this->worldCoordinates = this->worldCoordinates + deltaWorldCoordinates; } +void TranslatedCoordinates::move(IsometricCoordinates deltaIsometricCoordinates) +{ + move(CoordinateTransformer::isometricToWorld(deltaIsometricCoordinates)); +} + TranslatedCoordinates::TranslatedCoordinates(WorldCoordinates worldCoordinates) : worldCoordinates(worldCoordinates) { @@ -53,7 +58,7 @@ TranslatedCoordinates::TranslatedCoordinates(WorldCoordinates worldCoordinates) void TranslatedCoordinates::move(sf::Vector2f deltaWorldCoordinates) { - move({deltaWorldCoordinates.x, deltaWorldCoordinates.y, 0}); + move(WorldCoordinates {deltaWorldCoordinates.x, deltaWorldCoordinates.y, 0}); } void TranslatedCoordinates::setParent(std::shared_ptr parent, WorldCoordinates offset) diff --git a/src/coordinates/translated_coordinates.h b/src/coordinates/translated_coordinates.h index e0482e5..11e9e49 100644 --- a/src/coordinates/translated_coordinates.h +++ b/src/coordinates/translated_coordinates.h @@ -40,10 +40,13 @@ public: void setScreenOffset(IsometricCoordinates offset); + void move(IsometricCoordinates deltaIsometricCoordinates); + private: WorldCoordinates worldCoordinates{}; std::shared_ptr parent = nullptr; + }; diff --git a/src/game/collectables/collectable.cpp b/src/game/collectables/collectable.cpp index db56a81..57e8c27 100644 --- a/src/game/collectables/collectable.cpp +++ b/src/game/collectables/collectable.cpp @@ -27,5 +27,5 @@ void Collectable::setSprite(const std::string &spriteName) addChildScreenOffset(sprite, IsometricCoordinates(-size / 2.f)); // Set half size offset of coordinates - coordinates->move(WorldCoordinates(0, 0, sizeWidth / 2.f)); + coordinates->move(IsometricCoordinates(0, -sizeWidth / 2.f, 0)); } diff --git a/src/game/collectables/collection/collectables_collection.cpp b/src/game/collectables/collection/collectables_collection.cpp index 093d68a..9bfe36c 100644 --- a/src/game/collectables/collection/collectables_collection.cpp +++ b/src/game/collectables/collection/collectables_collection.cpp @@ -57,8 +57,15 @@ void CollectablesCollection::update() for (auto &collectable: collectables) { int newDepth = std::floor(collectable->getDepth()); - if (newDepth != depth && newDepth < depthCollections.size()) + if (newDepth != depth) { + if (newDepth < 0 || newDepth >= (int) depthCollections.size()) + { + LOG(ERROR) << "Collectable " << collectable->getId() << " has invalid depth " << newDepth + << "! Keeping it at previous depth " << depth << " ..."; + continue; + } + depthCollection->remove(collectable); depthCollections[newDepth]->add(collectable); } diff --git a/src/game/level/level_loader.cpp b/src/game/level/level_loader.cpp index 859ffcc..8d5e00e 100644 --- a/src/game/level/level_loader.cpp +++ b/src/game/level/level_loader.cpp @@ -61,14 +61,6 @@ void LevelLoader::loadLevel(const LevelConfig &levelConfig) } - for (int i = 0; i <= 25; i++) - { - auto debugImage = SpriteFactory::createSingleSprite("edge"); - debugImage->coordinates->setWorld({0, 0, (float) i}); - levelRenderer->addDetachedChild(debugImage); - } - - LOG(INFO) << "Finished loading level '" << levelConfig.name << "'."; } diff --git a/src/game/physics/holes/collectable_simulation.cpp b/src/game/physics/holes/collectable_simulation.cpp index 46ed060..9584f97 100644 --- a/src/game/physics/holes/collectable_simulation.cpp +++ b/src/game/physics/holes/collectable_simulation.cpp @@ -20,7 +20,7 @@ void CollectableSimulation::physicsUpdate() { updateGroundHole(); - world->Step(FRAME_TIME.asSeconds(), COLLECTABLES_SIM_VELOCITY_ITERATIONS, COLLECTABLES_SIM_POSITION_ITERATIONS); +// world->Step(FRAME_TIME.asSeconds(), COLLECTABLES_SIM_VELOCITY_ITERATIONS, COLLECTABLES_SIM_POSITION_ITERATIONS); updateCollectable(); } diff --git a/src/game/physics/holes/collectable_simulation.hpp b/src/game/physics/holes/collectable_simulation.hpp index f04122d..a24e5d2 100644 --- a/src/game/physics/holes/collectable_simulation.hpp +++ b/src/game/physics/holes/collectable_simulation.hpp @@ -13,7 +13,7 @@ public: void physicsUpdate() override; - std::shared_ptr getCollectable() const; + [[nodiscard]] std::shared_ptr getCollectable() const; private: std::shared_ptr world; diff --git a/src/game/physics/holes/holes_simulation.cpp b/src/game/physics/holes/holes_simulation.cpp index 705fcec..b1499db 100644 --- a/src/game/physics/holes/holes_simulation.cpp +++ b/src/game/physics/holes/holes_simulation.cpp @@ -29,7 +29,7 @@ void HolesSimulation::addCollectable(const std::shared_ptr &collect { // Create new collectable simulation auto collectableSim = std::make_shared(collectable); - addChild(collectableSim); + addDetachedChild(collectableSim); } void HolesSimulation::lateUpdate() diff --git a/src/levels.hpp b/src/levels.hpp index b53c0bf..b155794 100644 --- a/src/levels.hpp +++ b/src/levels.hpp @@ -12,7 +12,7 @@ std::map const all_levels = { {18, 18}, {0, 18}, {18, 0}}, { - CollectableInLevel("box", {5, 5}) + CollectableInLevel("box", {0, 0}) }, { // Blues