diff --git a/.gitignore b/.gitignore index add2a72..63e29a0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /build/ +/build-release/ /.idea/ \ No newline at end of file diff --git a/README.md b/README.md index 5ef33a3..45f23c8 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Maximilian Giller, ## What is this Game about? -Holesome is about holes! But not the kind you have seen before ... +Holesome is about currentLayoutHoles! But not the kind you have seen before ... Gameplay: - Navigate the environment as a hole @@ -33,7 +33,7 @@ The player controls a hole and has to consume as many objects as possible to gro ## Which components are included? -- **Physics**: Core gameplay element to make the objects fall into the holes in a fun way +- **Physics**: Core gameplay element to make the objects fall into the currentLayoutHoles in a fun way - **Controller Support**: Use a controller to control the hole - **Local Multiplayer**: Play with up to 4 players on one device using split screen - **Level Files**: Levels are stored using a simple file format @@ -43,7 +43,7 @@ The player controls a hole and has to consume as many objects as possible to gro Potential expansions: - **Multithreading**: Improving performance by running the phsics in a separat thread and similar concepts -- **AI**: Some holes could be controlled by AI, which makes singleplayer games more exciting +- **AI**: Some currentLayoutHoles could be controlled by AI, which makes singleplayer games more exciting - **Online Multiplayer**: Play with friends online ## Project Setup diff --git a/src/config.h b/src/config.h index d793e0a..fde27a4 100644 --- a/src/config.h +++ b/src/config.h @@ -16,6 +16,7 @@ // World #define WORLD_GRAVITY b2Vec2(0.f, -9.8f) #define SKY_HEIGHT_SCALE 2.f +#define CONSIDER_COLLECTABLE_DEPTH_MOVEMENT false // Might cost perfomance and is currently not in use // FPS #define FRAME_RATE 60 diff --git a/src/game/collectables/collection/collectables_collection.cpp b/src/game/collectables/collection/collectables_collection.cpp index 1d076c8..fd52503 100644 --- a/src/game/collectables/collection/collectables_collection.cpp +++ b/src/game/collectables/collection/collectables_collection.cpp @@ -3,6 +3,7 @@ #include #include "collectables_depth_collection.hpp" #include "../../../logging/easylogging++.h" +#include "../../../config.h" std::shared_ptr CollectablesCollection::getInstance() { @@ -43,6 +44,10 @@ void CollectablesCollection::update() { GameObject::update(); + if (!CONSIDER_COLLECTABLE_DEPTH_MOVEMENT) { + return; + } + // Move collectables to new depth collections if necessary // First, clear history of all depth collections diff --git a/src/game/physics/holes/collectable_simulation.cpp b/src/game/physics/holes/collectable_simulation.cpp index cc65d49..f412320 100644 --- a/src/game/physics/holes/collectable_simulation.cpp +++ b/src/game/physics/holes/collectable_simulation.cpp @@ -5,7 +5,7 @@ #include "layouts/hole_layout.hpp" CollectableSimulation::CollectableSimulation(const std::shared_ptr &collectable) - : collectable(collectable) + : collectable(collectable), simulationDepth(collectable->getDepth()) { // Create simulation world = std::make_shared(WORLD_GRAVITY); @@ -40,14 +40,14 @@ std::shared_ptr CollectableSimulation::getCollectable() const void CollectableSimulation::updateGroundHole() { - auto holeLayout = HoleLayout::getInstance()->atDepth(collectable->getDepth()); + auto holeLayout = HoleLayout::getInstance()->atDepth(simulationDepth); simGround->createLayout(holeLayout); } void CollectableSimulation::updateCollectable() { auto bodyPosition = collectableBody->body()->GetPosition(); - collectable->coordinates->setDiagonal({bodyPosition.x, bodyPosition.y, collectable->getDepth()}); + collectable->coordinates->setDiagonal({bodyPosition.x, bodyPosition.y, simulationDepth}); // Convert radians to degrees float angle = -collectableBody->body()->GetAngle() * 180 / M_PI; diff --git a/src/game/physics/holes/collectable_simulation.hpp b/src/game/physics/holes/collectable_simulation.hpp index 8ce1cdf..bec4859 100644 --- a/src/game/physics/holes/collectable_simulation.hpp +++ b/src/game/physics/holes/collectable_simulation.hpp @@ -22,6 +22,7 @@ public: private: std::shared_ptr world; std::shared_ptr collectableBody; + float simulationDepth; std::shared_ptr collectable; diff --git a/src/game/physics/holes/ground/collectable_sim_ground.cpp b/src/game/physics/holes/ground/collectable_sim_ground.cpp index f7ae7f7..5f91aa2 100644 --- a/src/game/physics/holes/ground/collectable_sim_ground.cpp +++ b/src/game/physics/holes/ground/collectable_sim_ground.cpp @@ -2,7 +2,6 @@ #include #include "../../../../config.h" -#include "../layouts/depth_hole_layout.hpp" void CollectableSimGround::closeAllHoles() { @@ -12,17 +11,21 @@ void CollectableSimGround::closeAllHoles() void CollectableSimGround::createLayout(DepthHoleLayout &layout) { - // TODO: Check if layout changed at all before clearing segments + if (!hasLayoutChanged(layout)) + { + return; + } + currentLayoutHoles = layout.holes; segments.clear(); - if (layout.holes.empty()) + if (currentLayoutHoles.empty()) { closeAllHoles(); return; } // Sort holes from left to right - std::sort(layout.holes.begin(), layout.holes.end(), + std::sort(currentLayoutHoles.begin(), currentLayoutHoles.end(), [](DepthHoleDescription a, DepthHoleDescription b) { return a.x < b.x; @@ -30,21 +33,31 @@ void CollectableSimGround::createLayout(DepthHoleLayout &layout) // Create segments for holes float leftCorner = -groundWidth / 2.f; - for (auto &hole: layout.holes) + float leftHoleId = -1; + for (auto &hole: currentLayoutHoles) { auto rightCorner = hole.x - hole.width / 2.f; - createSegment(leftCorner, rightCorner); + auto segment = createSegment(leftCorner, rightCorner); + if (segment != nullptr) + { + segment->rightHoleId = hole.playerId; + segment->leftHoleId = leftHoleId; + } + leftHoleId = hole.playerId; leftCorner = hole.x + hole.width / 2.f; } // Create segment for the right side - createSegment(leftCorner, groundWidth / 2.f); + auto segment = createSegment(leftCorner, groundWidth / 2.f); + if (segment != nullptr) + { + segment->leftHoleId = leftHoleId; + } } CollectableSimGround::CollectableSimGround(std::shared_ptr world, float groundWidth) - : world(std::move(world)), groundWidth(0) + : world(std::move(world)), groundWidth(groundWidth) { - setGroundWidth(groundWidth); closeAllHoles(); } @@ -92,13 +105,13 @@ void CollectableSimGround::updateOuterSegmentsToWidth() } } -void CollectableSimGround::createSegment(float leftCorner, float rightCorner, - std::shared_ptr segment) +std::shared_ptr CollectableSimGround::createSegment(float leftCorner, float rightCorner, + std::shared_ptr segment) { if (leftCorner > rightCorner) { // Segment would have negative width, don't create it and leave empty instead - return; + return segment; } if (segment == nullptr) @@ -111,28 +124,70 @@ void CollectableSimGround::createSegment(float leftCorner, float rightCorner, auto size = sf::Vector2f(rightCorner - leftCorner, COLLECTABLES_SIM_GROUND_THICKNESS); segment->body->createSquare(b2_kinematicBody, center, size); + + return segment; } -CollectableSimGround::SideSegments CollectableSimGround::getOuterSegments(int holeId) +CollectableSimGround::SideSegments CollectableSimGround::getOuterSegments() { SideSegments sideSegments; for (const auto &segment: segments) { - // If valid holeId, match left hole id - // If invalid holeId, get outer segment - if ((segment->leftHoleId == holeId && holeId >= 0) - || (segment->rightHoleId < 0 && holeId < 0)) + if (segment->rightHoleId < 0) { sideSegments.right = segment; } - if ((segment->rightHoleId == holeId && holeId >= 0) - || (segment->leftHoleId < 0 && holeId < 0)) + if (segment->leftHoleId < 0) { sideSegments.left = segment; } + + if (sideSegments.left != nullptr && sideSegments.right != nullptr) + { + break; + } } return sideSegments; } + +bool CollectableSimGround::hasLayoutChanged(DepthHoleLayout &layout) +{ + if (layout.holes.size() != currentLayoutHoles.size()) + { + // Number of holes changed + return true; + } + + // Below here: Number of holes is the same + if (currentLayoutHoles.empty()) + { + // Both are empty, so no change + return false; + } + + // Sort holes from left to right + std::sort(layout.holes.begin(), layout.holes.end(), + [](DepthHoleDescription a, DepthHoleDescription b) + { + return a.x < b.x; + }); + + // Compare holes + for (int i = 0; i < layout.holes.size(); i++) + { + auto currentHole = currentLayoutHoles[i]; + auto newHole = layout.holes[i]; + + if (newHole.x != currentHole.x || + newHole.width != currentHole.width) + { + // Hole changed + return true; + } + } + + return false; +} diff --git a/src/game/physics/holes/ground/collectable_sim_ground.hpp b/src/game/physics/holes/ground/collectable_sim_ground.hpp index 5d6757b..e71f998 100644 --- a/src/game/physics/holes/ground/collectable_sim_ground.hpp +++ b/src/game/physics/holes/ground/collectable_sim_ground.hpp @@ -20,6 +20,7 @@ public: private: std::vector> segments; std::shared_ptr world; + std::vector currentLayoutHoles; float groundWidth; struct SideSegments { @@ -27,13 +28,15 @@ private: std::shared_ptr right; }; - SideSegments getOuterSegments(int holeId = -1); + CollectableSimGround::SideSegments getOuterSegments(); void updateOuterSegmentsToWidth(); - void createSegment(float leftCorner, float rightCorner, std::shared_ptr segment = nullptr); + std::shared_ptr createSegment(float leftCorner, float rightCorner, std::shared_ptr segment = nullptr); void closeAllHoles(); + + bool hasLayoutChanged(DepthHoleLayout &layout); };