Some small perfomance improvements in regards to collectables
This commit is contained in:
parent
16ddfaa942
commit
d009ef328c
8 changed files with 93 additions and 27 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
|||
/build/
|
||||
/build-release/
|
||||
/.idea/
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include <cmath>
|
||||
#include "collectables_depth_collection.hpp"
|
||||
#include "../../../logging/easylogging++.h"
|
||||
#include "../../../config.h"
|
||||
|
||||
std::shared_ptr<CollectablesCollection> 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
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
#include "layouts/hole_layout.hpp"
|
||||
|
||||
CollectableSimulation::CollectableSimulation(const std::shared_ptr<Collectable> &collectable)
|
||||
: collectable(collectable)
|
||||
: collectable(collectable), simulationDepth(collectable->getDepth())
|
||||
{
|
||||
// Create simulation
|
||||
world = std::make_shared<b2World>(WORLD_GRAVITY);
|
||||
|
@ -40,14 +40,14 @@ std::shared_ptr<Collectable> 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;
|
||||
|
|
|
@ -22,6 +22,7 @@ public:
|
|||
private:
|
||||
std::shared_ptr<b2World> world;
|
||||
std::shared_ptr<BodyAdapter> collectableBody;
|
||||
float simulationDepth;
|
||||
|
||||
std::shared_ptr<Collectable> collectable;
|
||||
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
#include <utility>
|
||||
#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<b2World> 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<CollectableSimGroundSegment> CollectableSimGround::createSegment(float leftCorner, float rightCorner,
|
||||
std::shared_ptr<CollectableSimGroundSegment> 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;
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ public:
|
|||
private:
|
||||
std::vector<std::shared_ptr<CollectableSimGroundSegment>> segments;
|
||||
std::shared_ptr<b2World> world;
|
||||
std::vector<DepthHoleDescription> currentLayoutHoles;
|
||||
float groundWidth;
|
||||
|
||||
struct SideSegments {
|
||||
|
@ -27,13 +28,15 @@ private:
|
|||
std::shared_ptr<CollectableSimGroundSegment> right;
|
||||
};
|
||||
|
||||
SideSegments getOuterSegments(int holeId = -1);
|
||||
CollectableSimGround::SideSegments getOuterSegments();
|
||||
|
||||
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();
|
||||
|
||||
bool hasLayoutChanged(DepthHoleLayout &layout);
|
||||
};
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue