Tried to implement lives. Failed. Everything crashes if I try to RemoveEntity in Update.

This commit is contained in:
PlatinumSkink
2015-09-15 12:40:23 +02:00
parent 157fa36f68
commit 88bd96861d
9 changed files with 226 additions and 17 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ namespace Components
struct Ball : Component
{
int number = 0;
};
}
+26
View File
@@ -0,0 +1,26 @@
//
// Created by Administrator on 2015-09-15.
//
#ifndef DAYDREAM_CLIFE_H
#define DAYDREAM_CLIFE_H
#include "Core/Component.h"
namespace dd
{
namespace Components
{
struct Life : Component {
int number = 0;
};
}
}
#endif //DAYDREAM_CLIFE_H
+27
View File
@@ -0,0 +1,27 @@
//
// Created by Adniklastrator on 2015-09-14.
//
#ifndef DAYDREAM_EBALLFELLOFFSTAGE_H
#define DAYDREAM_EBALLFELLOFFSTAGE_H
#include "Core/EventBroker.h"
#include "Core/Entity.h"
namespace dd
{
namespace Events
{
struct BallFellOffStage : Event
{
EntityID entity;
};
}
}
#endif //DAYDREAM_EBALLFELLOFFSTAGE_H
+14 -2
View File
@@ -9,13 +9,17 @@
#include "Core/CTransform.h"
#include "Rendering/CSprite.h"
#include "Game/CBrick.h"
#include "Physics/EContact.h"
#include "Core/EventBroker.h"
#include "Core/World.h"
#include "Game/CBall.h"
#include "Game/CLife.h"
#include "Game/EStageCleared.h"
#include "Game/EBallFellOffStage.h"
#include "Physics/CBoxShape.h"
#include "Physics/CPhysics.h"
#include "Physics/CCircleShape.h"
#include "Physics/ESetImpulse.h"
#include "Physics/EContact.h"
#include <fstream>
#include <iostream>
#include <intrin.h>
@@ -47,6 +51,7 @@ public:
void Initialize() override;
void CreateBasicLevel(int, int, glm::vec2, int);
void CreateLife(int);
void SaveLevel(int, int, glm::vec2, int); // Shouldn't be here, but I'm experimenting.
void LoadLevel(char[20]);
void CreateBrick(int, int, glm::vec2, int, int);
@@ -56,12 +61,17 @@ public:
void EndLevel();
void Update(double dt);
void Update(double dt) override;
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
int lives = 3;
int pastLives = 3;
private:
dd::EventRelay<LevelSystem, dd::Events::Contact> m_EContact;
dd::EventRelay<LevelSystem, dd::Events::BallFellOffStage> m_EBall;
bool OnContact(const dd::Events::Contact &event);
bool BallFellOffStage(const dd::Events::BallFellOffStage &event);
bool m_Initialized = false;
@@ -70,6 +80,8 @@ private:
int tLines = 8;
glm::vec2 tSpaceBetweenBricks = glm::vec2(2, 0.5);
int tSpaceToEdge = 0;
EntityID entityToRemove = NULL;
};
}
+13 -5
View File
@@ -7,19 +7,22 @@
#include "Core/InputController.h"
#include "Core/System.h"
#include "Core/Component.h"
#include "Core/CTransform.h"
#include "Core/EventBroker.h"
#include "Core/EKeyDown.h"
#include "Core/EKeyUp.h"
#include "Input/EBindKey.h"
#include "Physics/EContact.h"
#include "Core/CTransform.h"
#include "Physics/CBoxShape.h"
#include "Physics/CPhysics.h"
#include "Physics/CCircleShape.h"
#include "Physics/ESetImpulse.h"
#include "Rendering/CSprite.h"
#include "Game/CBall.h"
#include "Core/Component.h"
#include "Physics/ESetImpulse.h"
#include "Physics/CCircleShape.h"
#include "Game/EBallFellOffStage.h"
namespace dd
@@ -37,17 +40,22 @@ public:
void Initialize() override;
void Update(double dt) override;
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
private:
dd::EventRelay<PadSystem, dd::Events::KeyDown> m_EKeyDown;
dd::EventRelay<PadSystem, dd::Events::KeyUp> m_EKeyUp;
dd::EventRelay<PadSystem, dd::Events::Contact> m_EContact;
dd::EventRelay<PadSystem, dd::Events::BallFellOffStage> m_EBall;
bool OnKeyDown(const dd::Events::KeyDown &event);
bool OnKeyUp(const dd::Events::KeyUp &event);
bool OnContact(const dd::Events::Contact &event);
bool BallFellOffStage(const dd::Events::BallFellOffStage &event);
EntityID removeThisObject = NULL;
float slowdownModifier = 5.f;
float accelerationSpeed = 80.f;
@@ -61,7 +69,7 @@ private:
EntityID ent = 0;
Components::Transform* transform;
glm::vec3 acceleration = glm::vec3(0.f, 0.f, 0.f);
bool left = false, right = false;
bool left = false, right = false, replaceBall = false;
};
class PadSystem::PadSteeringInputController : InputController<PadSystem>
+1
View File
@@ -80,6 +80,7 @@ private:
e.Entity1 = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureA()->GetBody()];
e.Entity2 = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureB()->GetBody()];
m_PhysicsSystem->EventBroker->Publish(e);
}
void EndContact(b2Contact* contact)
+1 -2
View File
@@ -75,8 +75,7 @@ set(SOURCE_FILES
${SOURCE_FILES_Rendering}
${SOURCE_FILES_Transform}
${SOURCE_FILES_Physics}
${SOURCE_FILES_Game}
)
${SOURCE_FILES_Game} ../../include/Game/CLife.h)
if(CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
+97 -2
View File
@@ -10,15 +10,73 @@
void dd::Systems::LevelSystem::Initialize()
{
EVENT_SUBSCRIBE_MEMBER(m_EContact, LevelSystem::OnContact);
EVENT_SUBSCRIBE_MEMBER(m_EBall, LevelSystem::BallFellOffStage);
for (int i = 0; i < lives; i++)
{
CreateLife(i);
}
return;
}
void dd::Systems::LevelSystem::Update(double dt) {
void dd::Systems::LevelSystem::CreateLife(int number)
{
/*auto life = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(life);
transform->Position = glm::vec3(-11.f + number, -8.f, -10.f);
transform->Scale = glm::vec3(0.5f, 0.5f, 0.5f);
std::shared_ptr<Components::Life> lifeNr = m_World->AddComponent<Components::Life>(life);
lifeNr->number = number;
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(life);
sprite->SpriteFile = "Textures/Ball.png";
m_World->CommitEntity(life);*/
}
void dd::Systems::LevelSystem::Update(double dt)
{
if (!m_Initialized) {
CreateBasicLevel(tRows, tLines, tSpaceBetweenBricks, tSpaceToEdge);
m_Initialized = true;
}
if (entityToRemove != NULL)
{
//m_World->RemoveEntity(entityToRemove);
entityToRemove = NULL;
}
}
void dd::Systems::LevelSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
{
auto ball = m_World->GetComponent<Components::Ball>(entity);
if (ball != NULL)
{
auto transformBall = m_World->GetComponent<Components::Transform>(entity);
if (transformBall->Position.y < -10)
{
Events::BallFellOffStage e;
e.entity = entity;
EventBroker->Publish(e);
return;
}
}
if (lives != pastLives)
{
auto life = m_World->GetComponent<Components::Life>(entity);
if (life != NULL)
{
if (life->number + 1 == pastLives) {
pastLives--;
//m_World->RemoveEntity(entity);
}
}
}
}
void dd::Systems::LevelSystem::CreateBasicLevel(int rows, int lines, glm::vec2 spacesBetweenBricks, int spaceToEdge)
@@ -127,7 +185,6 @@ bool dd::Systems::LevelSystem::OnContact(const dd::Events::Contact &event)
if (entityBrick != NULL && entityBall != NULL)
{*/
//std::cout << "We're destroying!" << std::endl;
m_World->RemoveEntity(entityBrick);
return true;
@@ -136,6 +193,44 @@ bool dd::Systems::LevelSystem::OnContact(const dd::Events::Contact &event)
return false;
}
bool dd::Systems::LevelSystem::BallFellOffStage(const dd::Events::BallFellOffStage &event)
{
/*std::cout << "Did you get here?" << std::endl;
EntityID entity = event.entity;
//Temporary. Create new ball.
auto ent = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(ent);
transform->Position = glm::vec3(0.5f, 0.f, -10.f);;
transform->Scale = glm::vec3(1.f, 1.f, 1.f);
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(ent);
sprite->SpriteFile = "Textures/Ball.png";
std::shared_ptr<Components::CircleShape> circleShape = m_World->AddComponent<Components::CircleShape>(ent);
std::shared_ptr<Components::Ball> cball = m_World->AddComponent<Components::Ball>(ent);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(ent);
physics->Static = false;
std::cout << "How about here?" << std::endl;
*/
//m_World->RemoveEntity(event.entity);
//entityToRemove = event.entity;
/* m_World->CommitEntity(ent);
std::cout << "Or here?" << std::endl;
Events::SetImpulse e;
e.Entity = ent;
e.Impulse = glm::vec2(0.f, -7.f);
e.Point = glm::vec2(transform->Position.x, transform->Position.y);
EventBroker->Publish(e);
std::cout << "Maybe even here?" << std::endl;
*/
lives--;
return true;
}
void dd::Systems::LevelSystem::EndLevel()
{
Events::StageCleared e;
+46 -5
View File
@@ -24,10 +24,41 @@ void dd::Systems::PadSystem::Initialize()
EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, PadSystem::OnKeyDown);
EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, PadSystem::OnKeyUp);
EVENT_SUBSCRIBE_MEMBER(m_EContact, PadSystem::OnContact);
EVENT_SUBSCRIBE_MEMBER(m_EBall, PadSystem::BallFellOffStage);
return;
}
void dd::Systems::PadSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
{
auto ball = m_World->GetComponent<Components::Ball>(entity);
if (replaceBall == true)
{
replaceBall = false;
//Temporary. Create new ball.
auto ent = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(ent);
transform->Position = glm::vec3(0.5f, 0.f, -10.f);;
transform->Scale = glm::vec3(1.f, 1.f, 1.f);
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(ent);
sprite->SpriteFile = "Textures/Ball.png";
std::shared_ptr<Components::CircleShape> circleShape = m_World->AddComponent<Components::CircleShape>(ent);
std::shared_ptr<Components::Ball> cball = m_World->AddComponent<Components::Ball>(ent);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(ent);
physics->Static = false;
m_World->RemoveEntity(entity);
m_World->CommitEntity(ent);
Events::SetImpulse e;
e.Entity = ent;
e.Impulse = glm::vec2(0.f, -7.f);
e.Point = glm::vec2(transform->Position.x, transform->Position.y);
EventBroker->Publish(e);
}
}
void dd::Systems::PadSystem::Update(double dt)
{
if (ent == 0) {
@@ -85,12 +116,16 @@ bool dd::Systems::PadSystem::OnKeyDown(const dd::Events::KeyDown &event)
//acceleration.x = -0.01f;
left = true;
}
else if (val = 262)
else if (val == 262)
{
//std::cout << "Right!" << std::endl;
//acceleration.x = 0.01f;
right = true;
}
else if (val == 82)
{
replaceBall = true;
}
return true;
}
@@ -109,7 +144,7 @@ bool dd::Systems::PadSystem::OnKeyUp(const dd::Events::KeyUp &event)
{
left = false;
}
else if (val = 262)
else if (val == 262)
{
right = false;
}
@@ -140,12 +175,12 @@ bool dd::Systems::PadSystem::OnContact(const dd::Events::Contact &event)
if (whatX > 0)
{
movementX = movementMultiplier * whatX;
std::cout << "Right!" << std::endl;
//std::cout << "Right!" << std::endl;
}
else
{
movementX = movementMultiplier * whatX;
std::cout << "Left!" << std::endl;
//std::cout << "Left!" << std::endl;
}
//float movementY = 1;
@@ -153,7 +188,7 @@ bool dd::Systems::PadSystem::OnContact(const dd::Events::Contact &event)
//float movementX = (event.ContactPoint.x - transformPad->Position.x) * movementMultiplier;
float movementY = glm::cos((abs(movementX) / ((1.6f) * movementMultiplier)) * 3.14159265359f / 2);
std::cout << movementX << " " << movementY << std::endl;
//std::cout << movementX << " " << movementY << std::endl;
//Temporary solution. Add a new ball and delete the old one.
auto ent = m_World->CreateEntity();
@@ -177,6 +212,12 @@ bool dd::Systems::PadSystem::OnContact(const dd::Events::Contact &event)
EventBroker->Publish(e);
}
bool dd::Systems::PadSystem::BallFellOffStage(const dd::Events::BallFellOffStage &event)
{
replaceBall = true;
return true;
}
bool dd::Systems::PadSystem::PadSteeringInputController::OnCommand(const Events::InputCommand &event)
{
std::string command = event.Command;