From: Daniel Karbach Date: Tue, 21 Nov 2017 20:55:53 +0000 (+0100) Subject: extract situation from creature X-Git-Url: http://git.localhorst.tv/?p=blobs.git;a=commitdiff_plain;h=bcf776b6d51aeb9147bde32da8dd0768b10db993 extract situation from creature --- diff --git a/src/app/states.cpp b/src/app/states.cpp index 75d9fcf..9cf6a9c 100644 --- a/src/app/states.cpp +++ b/src/app/states.cpp @@ -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); } diff --git a/src/creature/Creature.hpp b/src/creature/Creature.hpp index 135cf9e..f4c0004 100644 --- a/src/creature/Creature.hpp +++ b/src/creature/Creature.hpp @@ -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 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 index 0000000..4dbb1c6 --- /dev/null +++ b/src/creature/Situation.hpp @@ -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 diff --git a/src/creature/creature.cpp b/src/creature/creature.cpp index ed8551c..f279641 100644 --- a/src/creature/creature.cpp +++ b/src/creature/creature.cpp @@ -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; +} + } } diff --git a/src/world/world.cpp b/src/world/world.cpp index 18cb241..56923cf 100644 --- a/src/world/world.cpp +++ b/src/world/world.cpp @@ -167,7 +167,6 @@ void Body::Cache() noexcept { } void Body::AddCreature(creature::Creature *c) { - c->SetBody(*this); creatures.push_back(c); }