From f0b2f2a6595c97eb0c25a896aa11b5e61961516c Mon Sep 17 00:00:00 2001 From: ViktorLjung Date: Tue, 22 Apr 2014 17:18:54 +0200 Subject: [PATCH] Barebones entity component commit events Conflicts: src/GameWorld.cpp --- src/GameWorld.cpp | 10 ++++++++++ src/System.h | 2 ++ src/World.cpp | 9 +++++++++ src/World.h | 2 ++ 4 files changed, 23 insertions(+) diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index ee282f2..7da4e68 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -16,6 +16,7 @@ void GameWorld::Initialize() cameraComp->FarClip = 2000.f; AddComponent(camera, "Input"); auto freeSteering = AddComponent(camera, "FreeSteering"); + CommitEntity(camera); } @@ -35,6 +36,8 @@ void GameWorld::Initialize() auto physics = AddComponent(ground, "Physics"); physics->Mass = 10; physics->Static = true; + + CommitEntity(ground); } @@ -66,6 +69,7 @@ void GameWorld::Initialize() Wheel->Mass = 10; Wheel->Radius = 0.6f; Wheel->Steering = true; + CommitEntity(ent); } { // Front Left Wheel @@ -78,6 +82,7 @@ void GameWorld::Initialize() Wheel->Mass = 10; Wheel->Radius = 0.6f; Wheel->Steering = true; + CommitEntity(ent); } { // Back Right Wheel @@ -90,6 +95,7 @@ void GameWorld::Initialize() Wheel->Mass = 10; Wheel->Radius = 0.6f; Wheel->Steering = false; + CommitEntity(ent); } { // Back Left Wheel @@ -102,8 +108,10 @@ void GameWorld::Initialize() Wheel->Mass = 10; Wheel->Radius = 0.6f; Wheel->Steering = false; + CommitEntity(ent); } + CommitEntity(car); } @@ -123,6 +131,7 @@ void GameWorld::Initialize() box->Width = 5.f; box->Height = 5.f; box->Depth = 5.f; + CommitEntity(ball); } { @@ -132,6 +141,7 @@ void GameWorld::Initialize() emitter->Path = "Sounds/korvring.wav"; emitter->Loop = true; GetSystem("SoundSystem")->PlaySound(emitter); + CommitEntity(entity); } } diff --git a/src/System.h b/src/System.h index cbe9cb8..d433871 100755 --- a/src/System.h +++ b/src/System.h @@ -24,6 +24,8 @@ public: virtual void OnComponentCreated(std::string type, std::shared_ptr component) { } // Called when a component is removed virtual void OnComponentRemoved(std::string type, Component* component) { } + // Called when components are committed to an entity + virtual void OnEntityCommit(EntityID entity) { } protected: World* m_World; diff --git a/src/World.cpp b/src/World.cpp index d7a8ee4..49a5c3b 100755 --- a/src/World.cpp +++ b/src/World.cpp @@ -147,6 +147,15 @@ std::shared_ptr World::AddComponent(EntityID entity, std::string comp return AddComponent(entity, componentType); } +void World::CommitEntity(EntityID entity) +{ + for (auto pair : m_Systems) + { + auto system = pair.second; + system->OnEntityCommit(entity); + } +} + void World::AddSystem(std::string systemType) { m_Systems[systemType] = std::shared_ptr(m_SystemFactory.Create(systemType)); diff --git a/src/World.h b/src/World.h index bae324e..0d15b89 100755 --- a/src/World.h +++ b/src/World.h @@ -64,6 +64,8 @@ public: std::shared_ptr AddComponent(EntityID entity, std::string componentType); template T* GetComponent(EntityID entity, std::string componentType); + // Triggers commit events in systems + void CommitEntity(EntityID entity); /*std::vector GetEntityChildren(EntityID entity);*/