]> git.localhorst.tv Git - blobs.git/commitdiff
extract situation from creature
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 21 Nov 2017 20:55:53 +0000 (21:55 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 21 Nov 2017 20:55:53 +0000 (21:55 +0100)
src/app/states.cpp
src/creature/Creature.hpp
src/creature/Situation.hpp [new file with mode: 0644]
src/creature/creature.cpp
src/world/world.cpp

index 75d9fcf638cd4d7ae8258ad8757cc8f83d9975b1..9cf6a9c92f878f1ca5e5702a69aa78812ca466ca 100644 (file)
@@ -124,7 +124,7 @@ void MasterState::OnRender(graphics::Viewport &viewport) {
        assets.shaders.creature_skin.SetTexture(assets.textures.skins);
        // TODO: extend to nearby bodies as well
        for (auto c : cam.Reference().Creatures()) {
-               assets.shaders.creature_skin.SetM(cam.Model(c->GetBody()) * glm::mat4(c->LocalTransform()));
+               assets.shaders.creature_skin.SetM(cam.Model(cam.Reference()) * glm::mat4(c->LocalTransform()));
                c->Draw(assets, viewport);
        }
 
index 135cf9eb1d98d80d70a327f38a66e106d42af679..f4c000408a63fa9f5bee40af3927f34b92403230 100644 (file)
@@ -2,6 +2,7 @@
 #define BLOBS_CREATURE_CREATURE_HPP_
 
 #include "Need.hpp"
+#include "Situation.hpp"
 #include "../graphics/glm.hpp"
 #include "../graphics/SimpleVAO.hpp"
 
@@ -35,13 +36,6 @@ public:
        Creature &operator =(Creature &&) = delete;
 
 public:
-       void SetBody(world::Body &b) noexcept { body = &b; }
-       world::Body &GetBody() noexcept { return *body; }
-       const world::Body &GetBody() const noexcept { return *body; }
-
-       void Surface(int s) noexcept { surface = s; }
-       void Position(const glm::dvec3 &p) noexcept { position = p; }
-
        void Name(const std::string &n) noexcept { name = n; }
        const std::string &Name() const noexcept { return name; }
 
@@ -53,20 +47,21 @@ public:
 
        void Tick(double dt);
 
+       Situation &GetSituation() noexcept { return situation; }
+       const Situation &GetSituation() const noexcept { return situation; }
+
        glm::dmat4 LocalTransform() noexcept;
 
        void BuildVAO();
        void Draw(app::Assets &, graphics::Viewport &);
 
 private:
-       world::Body *body;
-       int surface;
-       glm::dvec3 position;
-
        std::string name;
        double health;
        std::vector<Need> needs;
 
+       Situation situation;
+
        struct Attributes {
                glm::vec3 position;
                glm::vec3 normal;
diff --git a/src/creature/Situation.hpp b/src/creature/Situation.hpp
new file mode 100644 (file)
index 0000000..4dbb1c6
--- /dev/null
@@ -0,0 +1,41 @@
+#ifndef BLOBS_CREATURE_SITUATION_HPP_
+#define BLOBS_CREATURE_SITUATION_HPP_
+
+#include "../graphics/glm.hpp"
+
+
+namespace blobs {
+namespace world {
+       class Planet;
+}
+namespace creature {
+
+class Situation {
+
+public:
+       Situation();
+       ~Situation();
+
+public:
+       bool OnPlanet() const noexcept;
+       world::Planet &GetPlanet() const noexcept { return *planet; }
+       int Surface() const noexcept { return surface; }
+       const glm::dvec3 &Position() const noexcept { return position; }
+
+       void SetPlanetSurface(world::Planet &, int srf, const glm::dvec3 &pos) noexcept;
+
+public:
+       world::Planet *planet;
+       glm::dvec3 position;
+       int surface;
+       enum {
+               LOST,
+               PLANET_SURFACE,
+       } type;
+
+};
+
+}
+}
+
+#endif
index ed8551ca49bd1e8330d0336cf0eae885ab97812f..f279641ec445d4201f6670b326f0abd1221ae101 100644 (file)
@@ -1,5 +1,6 @@
 #include "Creature.hpp"
 #include "Need.hpp"
+#include "Situation.hpp"
 
 #include "../app/Assets.hpp"
 #include "../world/Body.hpp"
@@ -15,12 +16,10 @@ namespace blobs {
 namespace creature {
 
 Creature::Creature()
-: body(nullptr)
-, surface(0)
-, position()
-, name()
+: name()
 , health(1.0)
 , needs()
+, situation()
 , vao() {
 }
 
@@ -40,7 +39,8 @@ void Creature::Tick(double dt) {
 glm::dmat4 Creature::LocalTransform() noexcept {
        // TODO: surface transform
        constexpr double half_height = 0.25;
-       return glm::translate(glm::dvec3(position.x, position.y, position.z + body->Radius() + half_height))
+       const glm::dvec3 &pos = situation.Position();
+       return glm::translate(glm::dvec3(pos.x, pos.y, pos.z + situation.GetPlanet().Radius() + half_height))
                * glm::scale(glm::dvec3(half_height, half_height, half_height));
 }
 
@@ -134,8 +134,7 @@ void Creature::Draw(app::Assets &assets, graphics::Viewport &viewport) {
 
 void Spawn(Creature &c, world::Planet &p, app::Assets &assets) {
        p.AddCreature(&c);
-       c.Surface(0);
-       c.Position(glm::dvec3(0.0, 0.0, 0.0));
+       c.GetSituation().SetPlanetSurface(p, 0, glm::dvec3(0.0, 0.0, 0.0));
 
        // probe surrounding area for common resources
        int start = p.SideLength() / 2 - 2;
@@ -197,5 +196,27 @@ void Need::Tick(double dt) noexcept {
        value = std::min(1.0, value + gain * dt);
 }
 
+
+Situation::Situation()
+: planet(nullptr)
+, position(0.0)
+, surface(0)
+, type(LOST) {
+}
+
+Situation::~Situation() {
+}
+
+bool Situation::OnPlanet() const noexcept {
+       return type == PLANET_SURFACE;
+}
+
+void Situation::SetPlanetSurface(world::Planet &p, int srf, const glm::dvec3 &pos) noexcept {
+       type = PLANET_SURFACE;
+       planet = &p;
+       surface = srf;
+       position = pos;
+}
+
 }
 }
index 18cb2414b53aa5cce993b549e2919054cdcba673..56923cf6500fb6995bfa7a9a3edac59c57a8c59a 100644 (file)
@@ -167,7 +167,6 @@ void Body::Cache() noexcept {
 }
 
 void Body::AddCreature(creature::Creature *c) {
-       c->SetBody(*this);
        creatures.push_back(c);
 }