]> git.localhorst.tv Git - blobs.git/blobdiff - src/creature/need.cpp
eating and drinking
[blobs.git] / src / creature / need.cpp
index 88b44197c8de80af4f6a0afcb297915c75053a33..60e7cb816b3a0b8cd375df295da00e80723dbc3a 100644 (file)
@@ -3,7 +3,9 @@
 #include "IngestNeed.hpp"
 
 #include "Creature.hpp"
+#include "LocateResourceGoal.hpp"
 #include "../world/Planet.hpp"
+#include "../world/TileType.hpp"
 
 
 namespace blobs {
@@ -28,7 +30,9 @@ void Need::Decrease(double delta) noexcept {
 IngestNeed::IngestNeed(int resource, double speed, double damage)
 : resource(resource)
 , speed(speed)
-, damage(damage) {
+, damage(damage)
+, ingesting(false)
+, locating(false) {
 }
 
 IngestNeed::~IngestNeed() {
@@ -36,7 +40,29 @@ IngestNeed::~IngestNeed() {
 
 void IngestNeed::ApplyEffect(Creature &c, double dt) {
        if (!IsSatisfied()) {
-               // TODO: find resource and start ingest task
+               ingesting = true;
+       }
+       if (ingesting) {
+               if (c.GetSituation().OnSurface()) {
+                       const world::TileType &t = c.GetSituation().GetTileType();
+                       bool found = false;
+                       for (auto &yield : t.resources) {
+                               if (yield.resource == resource) {
+                                       found = true;
+                                       // TODO: check if not busy with something else
+                                       Decrease(std::min(yield.ubiquity, speed) * dt);
+                                       if (value == 0.0) {
+                                               ingesting = false;
+                                               locating = false;
+                                       }
+                                       break;
+                               }
+                       }
+                       if (!found && !locating) {
+                               c.AddGoal(std::unique_ptr<Goal>(new LocateResourceGoal(resource)));
+                               locating = true;
+                       }
+               }
        }
        if (IsCritical()) {
                c.Hurt(damage * dt);
@@ -55,7 +81,7 @@ InhaleNeed::~InhaleNeed() {
 }
 
 void InhaleNeed::ApplyEffect(Creature &c, double dt) {
-       if (!IsSatisfied() && !inhaling) {
+       if (!IsSatisfied()) {
                inhaling = true;
        }
        if (inhaling) {