From 0f55c89b6ab6d23a3dfb252cbbd886e88d467b31 Mon Sep 17 00:00:00 2001
From: Joseph Walton-Rivers <joseph@walton-rivers.uk>
Date: Fri, 17 Dec 2021 16:19:23 +0000
Subject: [PATCH] add object placement

---
 demo/main.cpp | 57 +++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 42 insertions(+), 15 deletions(-)

diff --git a/demo/main.cpp b/demo/main.cpp
index aef9157..977bdac 100644
--- a/demo/main.cpp
+++ b/demo/main.cpp
@@ -78,6 +78,20 @@ void process_camera(fggl::ecs3::World& ecs, const InputManager& input) {
 	fggl::input::process_edgescroll( ecs, *input, cam );
 }
 
+void placeObject(fggl::ecs3::World& world, fggl::ecs::entity_t parent, fggl::ecs::entity_t prototype, glm::vec3 targetPos) {
+    auto obj = world.copy(prototype);
+    auto result = world.get<fggl::math::Transform>(obj);
+
+    int xPos = (int)targetPos.x;
+    int zPos = (int)targetPos.z * -1;
+
+    // figure out the floor height
+    auto heightMap = world.get<fggl::data::HeightMap>(parent);
+    targetPos.y = heightMap->getValue(xPos, zPos); // TODO should really be the gradient at the required point
+
+    result->origin( targetPos );
+}
+
 class MenuScene : public fggl::scenes::Scene {
 
 public:
@@ -103,8 +117,8 @@ public:
 
     }
 
-    private:
-        InputManager m_inputs;
+private:
+    InputManager m_inputs;
 
 };
 
@@ -159,6 +173,23 @@ public:
             m_world.set<fggl::data::HeightMap>(terrain, &terrainData);
         }
 
+        // create foundation object
+        fggl::ecs3::entity_t foundation;
+        {
+            foundation = m_world.create(true);
+            m_world.add(foundation, types.find(fggl::math::Transform::name));
+
+            // plot rendering
+            fggl::data::Mesh mesh;
+            fggl::data::make_cube(mesh);
+            mesh.removeDups();
+
+            // add mesh as a component
+            constexpr char shader[] = "phong";
+            fggl::gfx::StaticMesh staticMesh{mesh, shader};
+            m_world.set<fggl::gfx::StaticMesh>(foundation, &staticMesh);
+        }
+
         // create building prototype
         fggl::ecs3::entity_t bunker;
         {
@@ -192,21 +223,17 @@ public:
             m_world.set<fggl::gfx::StaticMesh>(bunker, &staticMesh);
         }
 
+        for (int i=0; i<3; ++i) {
+            glm::vec3 location(i * 6.5f + 1.0f, 0.0f, -7.0f);
+            placeObject(m_world, terrain, foundation, location);
+        }
+
         int nCubes = 3;
         for ( int i=0; i<nCubes; i++ ) {
-            auto bunkerClone = m_world.copy(bunker);
-            auto result = m_world.get<fggl::math::Transform>(bunkerClone);
-
-            float x = (float)i * 6.f + 1.0f;
-            int xPos = (int)x;
-            float z = (float)-5.0f + 1.0f;
-            int zPos = (int)z * -1;
-
-            // figure out the floor height
-            auto heightMap = m_world.get<fggl::data::HeightMap>(terrain);
-            float y = heightMap->getValue(xPos, zPos); // TODO should really be the gradient at the required point
-
-            result->origin( glm::vec3( x, y, z) );
+            glm::vec3 location;
+            location.x = i * 6.f + 1.0f;
+            location.z = -5.0f + 1.0f;
+            placeObject(m_world, terrain, bunker, location);
         }
     }
 
-- 
GitLab