Boucing B(l)ob
The Images
The Idea
The idea for this game came from Doodle Jump. I wanted to add a second player and try and make it kind of competitive. The twist this game has is that you lose mass over time, and if you are too small, you lose. To mitigate your loss of mass, you can jump to the other player and grab a bite from him. The mass lost of the other player is transferred to your mass. The mass of the player also defines how fast he is, meaning more mass -> slower, less mass -> faster. The game is over once a player dies.
The Code
For Bouncing Blob, we implemented a component system. The root was always a game object that held a list of components. This allowed us to split the functionality of things into components that then just were added to the specific game objects.
For the respawning platform, I created an object pool that just reuses them instead of instantiating new ones all the time.
↓ PseudoCode for readability
PlatformPool::PlatformPool(
const size_t size,
const std::string& textureFile,
sf::IntRect textureRect,
const std::string& layerName,
sf::RenderWindow& renderWindow,
GameObjectManager& gameObjectManager) :
m_pool(size)
{
for (auto& gameObject : m_pool) //m_pool is a Vector of GameObjects
{
//Create game Object
gameObject = createGameobject();
auto renderComp = createSpriteRedererCOmponent();
auto colliderComp = createColliderComponent();
}
for (int i = 0; i < m_pool.size(); i++)
{
//Give each Platform a random start position
m_pool[i].position = new Random();
}
}
void PlatformPool::UpdatePlatform(float deltaTime)
{
for (auto platform : m_pool)
{
platform->getComponent<ColliderComponent>()->SetCollider(FloatRect(platform->getPosition(), Vector2f(176, 16)));
}
}
void PlatformPool::RepositionPlatform(std::shared_ptr<GameObject> camera)
{
for (int i = 0; i < m_pool.size(); i++)
{
if (camera->getPosition().y + 540 <= m_pool[i]->getPosition().y)
{
//Reposition platform to the top if out of camera frustum
}
}
}
GameObject::Ptr PlatformPool::get()
{
int nextPlatfrom = m_counter++ % m_pool.size();
return m_pool[nextPlatfrom];
}