diff options
Diffstat (limited to 'mwe/ecs-homemade/src')
-rw-r--r-- | mwe/ecs-homemade/src/ComponentManager.cpp | 8 | ||||
-rw-r--r-- | mwe/ecs-homemade/src/Components.cpp | 16 | ||||
-rw-r--r-- | mwe/ecs-homemade/src/GameObjectMax.cpp | 9 | ||||
-rw-r--r-- | mwe/ecs-homemade/src/main.cpp | 23 |
4 files changed, 43 insertions, 13 deletions
diff --git a/mwe/ecs-homemade/src/ComponentManager.cpp b/mwe/ecs-homemade/src/ComponentManager.cpp index 536c152..835bdda 100644 --- a/mwe/ecs-homemade/src/ComponentManager.cpp +++ b/mwe/ecs-homemade/src/ComponentManager.cpp @@ -9,11 +9,9 @@ ComponentManager::ComponentManager() {} void ComponentManager::DeleteAllComponentsOfId(std::uint32_t id) { for (auto & [type, componentArray] : mComponents) { //Loop through all the types (in the unordered_map<>) - if (id - < componentArray - .size()) { //Make sure that the id (that we are looking for) is within the boundaries of the vector<> - componentArray[id] - .clear(); //Clear the components at this specific id + if (id < componentArray.size( + )) { //Make sure that the id (that we are looking for) is within the boundaries of the vector<> + componentArray[id].clear(); //Clear the components at this specific id } } } diff --git a/mwe/ecs-homemade/src/Components.cpp b/mwe/ecs-homemade/src/Components.cpp index c8347b3..0d62bd5 100644 --- a/mwe/ecs-homemade/src/Components.cpp +++ b/mwe/ecs-homemade/src/Components.cpp @@ -6,6 +6,20 @@ Component::Component() : mActive(true) {} Sprite::Sprite(std::string path) : mPath(path) {} Rigidbody::Rigidbody(int mass, int gravityScale, int bodyType) - : mMass(mass), mGravityScale(gravityScale), mBodyType(bodyType) {} + : mMass(mass), + mGravityScale(gravityScale), + mBodyType(bodyType) {} Colider::Colider(int size) : mSize(size) {} + +void BehaviourScript::onStart() { + if (behaviour) { + behaviour->onStart(); + } +} + +void BehaviourScript::onUpdate() { + if (behaviour) { + behaviour->onUpdate(); + } +} diff --git a/mwe/ecs-homemade/src/GameObjectMax.cpp b/mwe/ecs-homemade/src/GameObjectMax.cpp index b0c5af7..0516f68 100644 --- a/mwe/ecs-homemade/src/GameObjectMax.cpp +++ b/mwe/ecs-homemade/src/GameObjectMax.cpp @@ -2,6 +2,9 @@ #include "ComponentManager.h" -GameObject::GameObject(std::uint32_t id, std::string name, std::string tag, - int layer) - : mId(id), mName(name), mTag(tag), mActive(true), mLayer(layer) {} +GameObject::GameObject(std::uint32_t id, std::string name, std::string tag, int layer) + : mId(id), + mName(name), + mTag(tag), + mActive(true), + mLayer(layer) {} diff --git a/mwe/ecs-homemade/src/main.cpp b/mwe/ecs-homemade/src/main.cpp index 330e154..85ab1f1 100644 --- a/mwe/ecs-homemade/src/main.cpp +++ b/mwe/ecs-homemade/src/main.cpp @@ -7,6 +7,13 @@ #include "Components.h" #include "GameObjectMax.h" +class myScript { +public: + void onStart() { std::cout << "In onStart" << std::endl; } + + void onUpdate() { std::cout << "In onUpdate" << std::endl; } +}; + int main() { auto startAdding = std::chrono::high_resolution_clock::now(); @@ -18,6 +25,7 @@ int main() { gameObject[i]->AddComponent<Sprite>("C:/Test"); gameObject[i]->AddComponent<Rigidbody>(0, 0, i); gameObject[i]->AddComponent<Colider>(i); + gameObject[i]->AddComponent<BehaviourScript>().addScript<myScript>(); } auto stopAdding = std::chrono::high_resolution_clock::now(); @@ -44,16 +52,23 @@ int main() { //std::cout << colider.get().mSize << std::endl; } + std::vector<std::reference_wrapper<BehaviourScript>> scripts + = ComponentManager::GetInstance().GetComponentsByType<BehaviourScript>(); + for (BehaviourScript & script : scripts) { + //script.onStart(); + //script.onUpdate(); + } + auto stopLooping = std::chrono::high_resolution_clock::now(); for (int i = 0; i < 100000; ++i) { delete gameObject[i]; } - auto Addtime = std::chrono::duration_cast<std::chrono::microseconds>( - stopAdding - startAdding); - auto LoopTime = std::chrono::duration_cast<std::chrono::microseconds>( - stopLooping - stopAdding); + auto Addtime + = std::chrono::duration_cast<std::chrono::microseconds>(stopAdding - startAdding); + auto LoopTime + = std::chrono::duration_cast<std::chrono::microseconds>(stopLooping - stopAdding); std::cout << "AddTime: " << Addtime.count() << " us" << std::endl; std::cout << "LoopTime: " << LoopTime.count() << " us" << std::endl; } |