Some small perfomance improvements in regards to collectables

This commit is contained in:
Maximilian Giller 2023-07-06 01:16:47 +02:00
parent 16ddfaa942
commit d009ef328c
8 changed files with 93 additions and 27 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
/build/ /build/
/build-release/
/.idea/ /.idea/

View file

@ -8,7 +8,7 @@ Maximilian Giller,
## What is this Game about? ## 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: Gameplay:
- Navigate the environment as a hole - 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? ## 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 - **Controller Support**: Use a controller to control the hole
- **Local Multiplayer**: Play with up to 4 players on one device using split screen - **Local Multiplayer**: Play with up to 4 players on one device using split screen
- **Level Files**: Levels are stored using a simple file format - **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: Potential expansions:
- **Multithreading**: Improving performance by running the phsics in a separat thread and similar concepts - **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 - **Online Multiplayer**: Play with friends online
## Project Setup ## Project Setup

View file

@ -16,6 +16,7 @@
// World // World
#define WORLD_GRAVITY b2Vec2(0.f, -9.8f) #define WORLD_GRAVITY b2Vec2(0.f, -9.8f)
#define SKY_HEIGHT_SCALE 2.f #define SKY_HEIGHT_SCALE 2.f
#define CONSIDER_COLLECTABLE_DEPTH_MOVEMENT false // Might cost perfomance and is currently not in use
// FPS // FPS
#define FRAME_RATE 60 #define FRAME_RATE 60

View file

@ -3,6 +3,7 @@
#include <cmath> #include <cmath>
#include "collectables_depth_collection.hpp" #include "collectables_depth_collection.hpp"
#include "../../../logging/easylogging++.h" #include "../../../logging/easylogging++.h"
#include "../../../config.h"
std::shared_ptr<CollectablesCollection> CollectablesCollection::getInstance() std::shared_ptr<CollectablesCollection> CollectablesCollection::getInstance()
{ {
@ -43,6 +44,10 @@ void CollectablesCollection::update()
{ {
GameObject::update(); GameObject::update();
if (!CONSIDER_COLLECTABLE_DEPTH_MOVEMENT) {
return;
}
// Move collectables to new depth collections if necessary // Move collectables to new depth collections if necessary
// First, clear history of all depth collections // First, clear history of all depth collections

View file

@ -5,7 +5,7 @@
#include "layouts/hole_layout.hpp" #include "layouts/hole_layout.hpp"
CollectableSimulation::CollectableSimulation(const std::shared_ptr<Collectable> &collectable) CollectableSimulation::CollectableSimulation(const std::shared_ptr<Collectable> &collectable)
: collectable(collectable) : collectable(collectable), simulationDepth(collectable->getDepth())
{ {
// Create simulation // Create simulation
world = std::make_shared<b2World>(WORLD_GRAVITY); world = std::make_shared<b2World>(WORLD_GRAVITY);
@ -40,14 +40,14 @@ std::shared_ptr<Collectable> CollectableSimulation::getCollectable() const
void CollectableSimulation::updateGroundHole() void CollectableSimulation::updateGroundHole()
{ {
auto holeLayout = HoleLayout::getInstance()->atDepth(collectable->getDepth()); auto holeLayout = HoleLayout::getInstance()->atDepth(simulationDepth);
simGround->createLayout(holeLayout); simGround->createLayout(holeLayout);
} }
void CollectableSimulation::updateCollectable() void CollectableSimulation::updateCollectable()
{ {
auto bodyPosition = collectableBody->body()->GetPosition(); 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 // Convert radians to degrees
float angle = -collectableBody->body()->GetAngle() * 180 / M_PI; float angle = -collectableBody->body()->GetAngle() * 180 / M_PI;

View file

@ -22,6 +22,7 @@ public:
private: private:
std::shared_ptr<b2World> world; std::shared_ptr<b2World> world;
std::shared_ptr<BodyAdapter> collectableBody; std::shared_ptr<BodyAdapter> collectableBody;
float simulationDepth;
std::shared_ptr<Collectable> collectable; std::shared_ptr<Collectable> collectable;

View file

@ -2,7 +2,6 @@
#include <utility> #include <utility>
#include "../../../../config.h" #include "../../../../config.h"
#include "../layouts/depth_hole_layout.hpp"
void CollectableSimGround::closeAllHoles() void CollectableSimGround::closeAllHoles()
{ {
@ -12,17 +11,21 @@ void CollectableSimGround::closeAllHoles()
void CollectableSimGround::createLayout(DepthHoleLayout &layout) void CollectableSimGround::createLayout(DepthHoleLayout &layout)
{ {
// TODO: Check if layout changed at all before clearing segments if (!hasLayoutChanged(layout))
{
return;
}
currentLayoutHoles = layout.holes;
segments.clear(); segments.clear();
if (layout.holes.empty()) if (currentLayoutHoles.empty())
{ {
closeAllHoles(); closeAllHoles();
return; return;
} }
// Sort holes from left to right // Sort holes from left to right
std::sort(layout.holes.begin(), layout.holes.end(), std::sort(currentLayoutHoles.begin(), currentLayoutHoles.end(),
[](DepthHoleDescription a, DepthHoleDescription b) [](DepthHoleDescription a, DepthHoleDescription b)
{ {
return a.x < b.x; return a.x < b.x;
@ -30,21 +33,31 @@ void CollectableSimGround::createLayout(DepthHoleLayout &layout)
// Create segments for holes // Create segments for holes
float leftCorner = -groundWidth / 2.f; 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; 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; leftCorner = hole.x + hole.width / 2.f;
} }
// Create segment for the right side // 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<b2World> world, float groundWidth) CollectableSimGround::CollectableSimGround(std::shared_ptr<b2World> world, float groundWidth)
: world(std::move(world)), groundWidth(0) : world(std::move(world)), groundWidth(groundWidth)
{ {
setGroundWidth(groundWidth);
closeAllHoles(); closeAllHoles();
} }
@ -92,13 +105,13 @@ void CollectableSimGround::updateOuterSegmentsToWidth()
} }
} }
void CollectableSimGround::createSegment(float leftCorner, float rightCorner, std::shared_ptr<CollectableSimGroundSegment> CollectableSimGround::createSegment(float leftCorner, float rightCorner,
std::shared_ptr<CollectableSimGroundSegment> segment) std::shared_ptr<CollectableSimGroundSegment> segment)
{ {
if (leftCorner > rightCorner) if (leftCorner > rightCorner)
{ {
// Segment would have negative width, don't create it and leave empty instead // Segment would have negative width, don't create it and leave empty instead
return; return segment;
} }
if (segment == nullptr) if (segment == nullptr)
@ -111,28 +124,70 @@ void CollectableSimGround::createSegment(float leftCorner, float rightCorner,
auto size = sf::Vector2f(rightCorner - leftCorner, COLLECTABLES_SIM_GROUND_THICKNESS); auto size = sf::Vector2f(rightCorner - leftCorner, COLLECTABLES_SIM_GROUND_THICKNESS);
segment->body->createSquare(b2_kinematicBody, center, size); segment->body->createSquare(b2_kinematicBody, center, size);
return segment;
} }
CollectableSimGround::SideSegments CollectableSimGround::getOuterSegments(int holeId) CollectableSimGround::SideSegments CollectableSimGround::getOuterSegments()
{ {
SideSegments sideSegments; SideSegments sideSegments;
for (const auto &segment: segments) for (const auto &segment: segments)
{ {
// If valid holeId, match left hole id if (segment->rightHoleId < 0)
// If invalid holeId, get outer segment
if ((segment->leftHoleId == holeId && holeId >= 0)
|| (segment->rightHoleId < 0 && holeId < 0))
{ {
sideSegments.right = segment; sideSegments.right = segment;
} }
if ((segment->rightHoleId == holeId && holeId >= 0) if (segment->leftHoleId < 0)
|| (segment->leftHoleId < 0 && holeId < 0))
{ {
sideSegments.left = segment; sideSegments.left = segment;
} }
if (sideSegments.left != nullptr && sideSegments.right != nullptr)
{
break;
}
} }
return sideSegments; 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;
}

View file

@ -20,6 +20,7 @@ public:
private: private:
std::vector<std::shared_ptr<CollectableSimGroundSegment>> segments; std::vector<std::shared_ptr<CollectableSimGroundSegment>> segments;
std::shared_ptr<b2World> world; std::shared_ptr<b2World> world;
std::vector<DepthHoleDescription> currentLayoutHoles;
float groundWidth; float groundWidth;
struct SideSegments { struct SideSegments {
@ -27,13 +28,15 @@ private:
std::shared_ptr<CollectableSimGroundSegment> right; std::shared_ptr<CollectableSimGroundSegment> right;
}; };
SideSegments getOuterSegments(int holeId = -1); CollectableSimGround::SideSegments getOuterSegments();
void updateOuterSegmentsToWidth(); void updateOuterSegmentsToWidth();
void createSegment(float leftCorner, float rightCorner, std::shared_ptr<CollectableSimGroundSegment> segment = nullptr); std::shared_ptr<CollectableSimGroundSegment> createSegment(float leftCorner, float rightCorner, std::shared_ptr<CollectableSimGroundSegment> segment = nullptr);
void closeAllHoles(); void closeAllHoles();
bool hasLayoutChanged(DepthHoleLayout &layout);
}; };