From ae7fed0466786a925883e9e5c716ea9dcb7a36a7 Mon Sep 17 00:00:00 2001
From: Joseph Walton-Rivers <joseph@walton-rivers.uk>
Date: Sat, 4 Jun 2022 19:50:06 +0100
Subject: [PATCH] cleanup scene generation

---
 demo/demo/rollball.cpp                 | 27 +++++++++-----------------
 include/fggl/ecs3/prototype/loader.hpp |  7 +++++--
 include/fggl/ecs3/prototype/world.h    |  4 ++++
 include/fggl/ecs3/types.hpp            |  3 +--
 4 files changed, 19 insertions(+), 22 deletions(-)

diff --git a/demo/demo/rollball.cpp b/demo/demo/rollball.cpp
index bbf1ae4..1f30bb5 100644
--- a/demo/demo/rollball.cpp
+++ b/demo/demo/rollball.cpp
@@ -19,17 +19,12 @@
  */
 
 #include "rollball.hpp"
-#include "fggl/data/model.hpp"
-#include "fggl/data/procedural.hpp"
 #include "fggl/gfx/camera.hpp"
 #include "fggl/input/camera_input.h"
 #include "fggl/util/service.h"
 #include "fggl/ecs3/prototype/loader.hpp"
 
 struct Prefabs {
-	fggl::ecs3::entity_t wallX;
-	fggl::ecs3::entity_t wallZ;
-	fggl::ecs3::entity_t floor;
 	fggl::ecs3::entity_t collectable;
 	fggl::ecs3::entity_t player;
 };
@@ -39,10 +34,6 @@ static void setupPrefabs(fggl::ecs3::World& world, Prefabs& prefabs) {
 	auto storage = fggl::util::ServiceLocator::instance().get<fggl::data::Storage>();
 	fggl::ecs3::load_prototype_file(world, *storage, "rollball.yml");
 
-	prefabs.wallX = world.findProtoype("wallX");
-	prefabs.wallZ = world.findProtoype("wallZ");
-	prefabs.floor = world.findProtoype("floor");
-
 	{
 		// player (cube because my sphere function doesn't exist yet
 		prefabs.player = world.findProtoype("player");
@@ -73,33 +64,33 @@ static void setupCamera(fggl::ecs3::World& world) {
 	world.add<fggl::gfx::Camera>(prototype);
 }
 
-static fggl::ecs3::entity_t setupEnvironment(fggl::ecs3::World& world, const Prefabs prefabs, fggl::math::vec2& size) {
+static fggl::ecs3::entity_t setupEnvironment(fggl::ecs3::World& world, fggl::math::vec2& size) {
 	{
-		auto northWall = world.copy(prefabs.wallX);
+		auto northWall = world.createFromPrototype("wallX");
 		auto* transform = world.get<fggl::math::Transform>(northWall);
 		transform->origin({size.x/2, 0.0f, 0.0f});
 	}
 
 	{
-		auto southWall = world.copy(prefabs.wallX);
+		auto southWall = world.createFromPrototype("wallX");
 		auto* transform = world.get<fggl::math::Transform>(southWall);
 		transform->origin({-size.x/2, 0.0f, 0.0f});
 	}
 
 	{
-		auto westWall = world.copy(prefabs.wallZ);
+		auto westWall = world.createFromPrototype("wallZ");
 		auto* transform = world.get<fggl::math::Transform>(westWall);
 		transform->origin({0.0f, 0.0f, -size.y/2});
 	}
 
 	{
-		auto eastWall = world.copy(prefabs.wallZ);
+		auto eastWall = world.createFromPrototype("wallZ");
 		auto* transform = world.get<fggl::math::Transform>(eastWall);
 		transform->origin({0.0f, 0.0f, size.y/2});
 	}
 
 	{
-		auto floor = world.copy(prefabs.floor);
+		auto floor = world.createFromPrototype("floor");
 		auto *transform = world.get<fggl::math::Transform>(floor);
 		transform->origin({0.0f, -2.5f, 0.0f});
 	}
@@ -107,7 +98,7 @@ static fggl::ecs3::entity_t setupEnvironment(fggl::ecs3::World& world, const Pre
 	auto player = fggl::ecs3::NULL_ENTITY;
 	{
 		// player just starts off as the prefab dictates
-		player = world.copy(prefabs.player);
+		player = world.createFromPrototype("player");
 	}
 
 	{
@@ -120,7 +111,7 @@ static fggl::ecs3::entity_t setupEnvironment(fggl::ecs3::World& world, const Pre
 
 		// build the collectables
 		for (auto& pos : collectPos) {
-			auto collectable = world.copy(prefabs.collectable);
+			auto collectable = world.createFromPrototype("collectable");
 			auto* transform = world.get<fggl::math::Transform>(collectable);
 			transform->origin(pos);
 		}
@@ -154,7 +145,7 @@ namespace demo {
 
 		// create a 20x20 grid
 		fggl::math::vec2 size{40.0f, 40.0f};
-		player = setupEnvironment(world(), prefabs, size);
+		player = setupEnvironment(world(), size);
 
 	}
 
diff --git a/include/fggl/ecs3/prototype/loader.hpp b/include/fggl/ecs3/prototype/loader.hpp
index 5086ab6..10f3346 100644
--- a/include/fggl/ecs3/prototype/loader.hpp
+++ b/include/fggl/ecs3/prototype/loader.hpp
@@ -26,10 +26,13 @@
 #define FGGL_ECS3_PROTOTYPE_LOADER_HPP
 
 #include "yaml-cpp/yaml.h"
-#include "fggl/data/storage.hpp"
-#include "fggl/ecs3/ecs.hpp"
 
 #include "fggl/math/types.hpp"
+#include "fggl/data/storage.hpp"
+#include "fggl/data/model.hpp"
+#include "fggl/data/procedural.hpp"
+
+#include "fggl/ecs3/ecs.hpp"
 
 namespace fggl::ecs3 {
 
diff --git a/include/fggl/ecs3/prototype/world.h b/include/fggl/ecs3/prototype/world.h
index 697f003..1add95c 100644
--- a/include/fggl/ecs3/prototype/world.h
+++ b/include/fggl/ecs3/prototype/world.h
@@ -123,6 +123,10 @@ namespace fggl::ecs3::prototype {
 				return nextID;
 			}
 
+			inline entity_t createFromPrototype(const std::string& name) {
+				return copy(findProtoype(name) );
+			}
+
 			entity_t copy(entity_t prototype) {
 				auto clone = create(false);
 
diff --git a/include/fggl/ecs3/types.hpp b/include/fggl/ecs3/types.hpp
index 906cbe7..3090daf 100644
--- a/include/fggl/ecs3/types.hpp
+++ b/include/fggl/ecs3/types.hpp
@@ -186,8 +186,7 @@ namespace fggl::ecs3 {
 			}
 
 			inline component_type_t find(const char *name) const {
-				for (auto &[type, meta] : m_types) {
-					std::cerr << meta->name() << std::endl;
+				for (const auto &[type, meta] : m_types) {
 					if (std::strcmp(name, meta->name()) == 0) {
 						return type;
 					}
-- 
GitLab