Fixed bug in calculating sprite offset for collectables

This commit is contained in:
Maximilian Giller 2023-06-30 20:57:28 +02:00
parent 23fb9d4fd5
commit 24963b2d0a
10 changed files with 23 additions and 16 deletions

View file

@ -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

View file

@ -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<TranslatedCoordinates> parent, WorldCoordinates offset)

View file

@ -40,10 +40,13 @@ public:
void setScreenOffset(IsometricCoordinates offset);
void move(IsometricCoordinates deltaIsometricCoordinates);
private:
WorldCoordinates worldCoordinates{};
std::shared_ptr<TranslatedCoordinates> parent = nullptr;
};

View file

@ -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));
}

View file

@ -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);
}

View file

@ -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 << "'.";
}

View file

@ -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();
}

View file

@ -13,7 +13,7 @@ public:
void physicsUpdate() override;
std::shared_ptr<Collectable> getCollectable() const;
[[nodiscard]] std::shared_ptr<Collectable> getCollectable() const;
private:
std::shared_ptr<b2World> world;

View file

@ -29,7 +29,7 @@ void HolesSimulation::addCollectable(const std::shared_ptr<Collectable> &collect
{
// Create new collectable simulation
auto collectableSim = std::make_shared<CollectableSimulation>(collectable);
addChild(collectableSim);
addDetachedChild(collectableSim);
}
void HolesSimulation::lateUpdate()

View file

@ -12,7 +12,7 @@ std::map<std::string, LevelConfig> const all_levels = {
{18, 18},
{0, 18},
{18, 0}}, {
CollectableInLevel("box", {5, 5})
CollectableInLevel("box", {0, 0})
},
{
// Blues