1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- /*
- * 2D Game Engine
- * ECS.cpp:
- * Based on pikuma.com 2D game engine in C++ and Lua course
- * Copyright (c) 2021 986-Studio. All rights reserved.
- *
- * Created by Manoël Trapier on 11/02/2021.
- */
- #include <Logger.h>
- #include <ECS.h>
- /* Entity */
- uint32_t IComponent::nextId = 0;
- /* TODO: this should be moved to the header file for optimisation purposes */
- uint32_t Entity::getId() const
- {
- return this->id;
- }
- /* System */
- void System::addEntity(Entity entity)
- {
- this->entities.push_back(entity);
- }
- void System::removeEntity(Entity entity)
- {
- this->entities.erase(std::remove_if(this->entities.begin(), this->entities.end(), [&entity](Entity other)
- {
- return other.getId() == entity.getId();
- }), this->entities.end());
- }
- std::vector<Entity> System::getEntities() const
- {
- return this->entities;
- }
- const Signature& System::getComponentSignature() const
- {
- return this->componentSignature;
- }
- Entity Registry::CreateEntity()
- {
- uint32_t entityId = this->numEntities++;
- Entity entity(entityId);
- this->entitiesToBeAdded.insert(entity);
- Logger::Info("Entity created with id = %s", entityId);
- return entity;
- }
- void Registry::addEntityToSystem(Entity entity)
- {
- const auto entityId = entity.getId();
- const auto entityComponentSignature = this->entityComponentSignatures[entityId];
- for(auto &system: this->systems)
- {
- const auto systemComponentSignature = system.second->getComponentSignature();
- bool isInterested = (entityComponentSignature & systemComponentSignature) == systemComponentSignature;
- if (isInterested)
- {
- system.second->addEntity(entity);
- }
- }
- }
- void Registry::Update()
- {
- }
|