From 54ba8d3d453723f8676b57ece5c16382729f3eda Mon Sep 17 00:00:00 2001
From: Joseph Walton-Rivers <joseph@walton-rivers.uk>
Date: Sat, 4 Jun 2022 20:28:17 +0100
Subject: [PATCH] minor interface cleanup

---
 demo/demo/rollball.cpp              |  4 +--
 include/fggl/ecs3/prototype/world.h | 56 ++++++++++++++---------------
 2 files changed, 28 insertions(+), 32 deletions(-)

diff --git a/demo/demo/rollball.cpp b/demo/demo/rollball.cpp
index 1f30bb5..5d8851c 100644
--- a/demo/demo/rollball.cpp
+++ b/demo/demo/rollball.cpp
@@ -36,13 +36,13 @@ static void setupPrefabs(fggl::ecs3::World& world, Prefabs& prefabs) {
 
 	{
 		// player (cube because my sphere function doesn't exist yet
-		prefabs.player = world.findProtoype("player");
+		prefabs.player = world.findPrototype("player");
 		world.add<fggl::phys::Dynamics>(prefabs.player);
 	}
 
 	{
 		// collectable
-		prefabs.collectable = world.findProtoype("collectable");
+		prefabs.collectable = world.findPrototype("collectable");
 
 		// we need both of these for callbacks to trigger.
 		world.add<fggl::phys::CollisionCallbacks>(prefabs.collectable);
diff --git a/include/fggl/ecs3/prototype/world.h b/include/fggl/ecs3/prototype/world.h
index 1add95c..c22ee52 100644
--- a/include/fggl/ecs3/prototype/world.h
+++ b/include/fggl/ecs3/prototype/world.h
@@ -19,7 +19,7 @@
  */
 namespace fggl::ecs3::prototype {
 
-	using entity_cb = std::function<void(const entity_t)>;
+	using EntityCallback = std::function<void(const entity_t)>;
 
 	class Entity {
 		public:
@@ -124,7 +124,7 @@ namespace fggl::ecs3::prototype {
 			}
 
 			inline entity_t createFromPrototype(const std::string& name) {
-				return copy(findProtoype(name) );
+				return copy(findPrototype(name) );
 			}
 
 			entity_t copy(entity_t prototype) {
@@ -150,9 +150,9 @@ namespace fggl::ecs3::prototype {
 
 			void createFromSpec(entity_t entity, const YAML::Node& compConfig) {
 				if ( compConfig ) {
-					for (const auto& it : compConfig ) {
-						const auto name = it.first.as<std::string>();
-						auto& config = it.second;
+					for (const auto& itr : compConfig ) {
+						const auto name = itr.first.as<std::string>();
+						const auto& config = itr.second;
 
 						auto compType = m_types.find( name.c_str() );
 						addFromConfig(entity, compType, config);
@@ -160,23 +160,19 @@ namespace fggl::ecs3::prototype {
 				}
 			}
 
-			bool alive(entity_t entity) const {
-				if (entity == NULL_ENTITY) {
-					// tis a silly question, but can be asked by accident.
-					return false;
-				}
-
-				if ( m_killList.find( entity ) != m_killList.end() ) {
-					// getting reaped
-					return false;
-				}
+			inline auto alive(entity_t entity) const -> bool {
+				return entity != NULL_ENTITY
+					&& m_killList.find( entity ) == m_killList.end()
+					&& m_entities.find( entity ) != m_entities.end();
+			}
 
-				// check liveness
-				return m_entities.find( entity ) != m_entities.end();
+			inline auto exists(entity_t entity) const -> bool {
+				return entity != NULL_ENTITY
+					&& m_entities.find( entity ) != m_entities.end();
 			}
 
 			void destroy(entity_t entity) {
-				assert( entity != NULL_ENTITY && "attempted to kill null entity" );
+				assert( alive(entity) && "attempted to kill null entity" );
 				// TOOD resolve and clean components
 				//m_entities.erase(entity);
 				m_killList.insert(entity);
@@ -238,7 +234,7 @@ namespace fggl::ecs3::prototype {
 
 			template<typename C>
 			C *add(entity_t entity_id) {
-				assert( entity_id != NULL_ENTITY && "attempted to add component on null entity" );
+				assert( alive(entity_id) && "attempted to add component on null entity" );
 
 				//spdlog::info("component '{}' added to '{}'", C::name, entity_id);
 				auto &entity = m_entities.at(entity_id);
@@ -249,7 +245,7 @@ namespace fggl::ecs3::prototype {
 			}
 
 			void *add(entity_t entity_id, component_type_t component_id) {
-				assert( entity_id != NULL_ENTITY && "attempted to add component on null entity" );
+				assert( alive(entity_id) && "attempted to add component on null entity" );
 
 				auto meta = m_types.meta(component_id);
 				auto &entity = m_entities.at(entity_id);
@@ -261,7 +257,7 @@ namespace fggl::ecs3::prototype {
 
 			template<typename C>
 			C *set(entity_t entity_id, const C *ptr) {
-				assert( entity_id != NULL_ENTITY && "attempted to set component on null entity" );
+				assert( alive( entity_id ) && "attempted to set component on null entity" );
 
 				//spdlog::info("component '{}' set on '{}'", C::name, entity_id);
 				auto &entity = m_entities.at(entity_id);
@@ -272,7 +268,7 @@ namespace fggl::ecs3::prototype {
 			}
 
 			void *set(entity_t entity_id, component_type_t cid, const void *ptr) {
-				assert( entity_id != NULL_ENTITY && "attempted to set component on null entity" );
+				assert( alive( entity_id ) && "attempted to set component on null entity" );
 
 				auto &entity = m_entities.at(entity_id);
 				auto cMeta = m_types.meta(cid);
@@ -301,8 +297,7 @@ namespace fggl::ecs3::prototype {
 
 			template<typename C>
 			C *get(entity_t entity_id) const {
-				assert( entity_id != NULL_ENTITY && "attempted to get component on null entity" );
-
+				assert( exists(entity_id) && "attempted to get component on null entity" );
 				C* ptr = tryGet<C>(entity_id);
 				if (ptr == nullptr) {
 					std::cerr << "entity " << entity_id << " does not have component "<< C::name << std::endl;
@@ -312,7 +307,7 @@ namespace fggl::ecs3::prototype {
 
 			template<typename C>
 			void remove(entity_t entity_id) {
-				assert( entity_id != NULL_ENTITY && "attempted to remove component on null entity" );
+				assert( alive(entity_id) && "attempted to remove component on null entity" );
 
 				try {
 					auto &entity = m_entities.at(entity_id);
@@ -326,16 +321,17 @@ namespace fggl::ecs3::prototype {
 				}
 			}
 
-			void *get(entity_t entity_id, component_type_t t) {
+			void *get(entity_t entity_id, component_type_t componentType) {
+				assert( exists(entity_id) && "attempted to get component on null entity" );
 				auto &entity = m_entities.at(entity_id);
-				return entity.get(t);
+				return entity.get(componentType);
 			}
 
-			void addDeathListener(const entity_cb& callback) {
+			void addDeathListener(const EntityCallback& callback) {
 				m_deathListeners.emplace_back(callback);
 			}
 
-			entity_t findProtoype(const std::string& name) const {
+			entity_t findPrototype(const std::string& name) const {
 				for ( const auto& [entity, obj] : m_entities ) {
 					if ( !obj.m_abstract ){
 						continue;
@@ -351,7 +347,7 @@ namespace fggl::ecs3::prototype {
 			}
 
 		private:
-			std::vector< entity_cb > m_deathListeners;
+			std::vector<EntityCallback > m_deathListeners;
 			TypeRegistry &m_types;
 			entity_t m_next;
 			std::map<entity_t, Entity> m_entities;
-- 
GitLab