From 7c0cb00fac415d393fc3c3397caeb97e31235e69 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Wed, 22 Nov 2017 22:43:14 +0100 Subject: [PATCH] smooth breathing a bit --- src/creature/InhaleNeed.hpp | 1 + src/creature/Need.hpp | 2 ++ src/creature/creature.cpp | 4 ++-- src/creature/need.cpp | 24 +++++++++++++++++++----- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/creature/InhaleNeed.hpp b/src/creature/InhaleNeed.hpp index 365bb04..3907a8b 100644 --- a/src/creature/InhaleNeed.hpp +++ b/src/creature/InhaleNeed.hpp @@ -23,6 +23,7 @@ private: int resource; double speed; double damage; + bool inhaling; }; diff --git a/src/creature/Need.hpp b/src/creature/Need.hpp index 2fd4898..67abe3c 100644 --- a/src/creature/Need.hpp +++ b/src/creature/Need.hpp @@ -26,6 +26,8 @@ struct Need { virtual ~Need() noexcept; void Tick(double dt) noexcept; + void Increase(double) noexcept; + void Decrease(double) noexcept; bool IsSatisfied() const noexcept { return value < inconvenient; } bool IsInconvenient() const noexcept { return value >= inconvenient && value < critical; } diff --git a/src/creature/creature.cpp b/src/creature/creature.cpp index 3c4f544..66931e9 100644 --- a/src/creature/creature.cpp +++ b/src/creature/creature.cpp @@ -171,9 +171,9 @@ void Spawn(Creature &c, world::Planet &p, app::Assets &assets) { if (p.HasAtmosphere()) { std::cout << "require breathing " << assets.data.resources[p.Atmosphere()].label << std::endl; - std::unique_ptr need(new InhaleNeed(p.Atmosphere(), 0.25, 0.1)); + std::unique_ptr need(new InhaleNeed(p.Atmosphere(), 0.5, 0.1)); need->name = assets.data.resources[p.Atmosphere()].label; - need->gain = 0.25; + need->gain = 0.2; need->inconvenient = 0.4; need->critical = 0.95; c.AddNeed(std::move(need)); diff --git a/src/creature/need.cpp b/src/creature/need.cpp index b569500..88b4419 100644 --- a/src/creature/need.cpp +++ b/src/creature/need.cpp @@ -13,7 +13,15 @@ Need::~Need() { } void Need::Tick(double dt) noexcept { - value = std::min(1.0, value + gain * dt); + Increase(gain * dt); +} + +void Need::Increase(double delta) noexcept { + value = std::min(1.0, value + delta); +} + +void Need::Decrease(double delta) noexcept { + value = std::max(0.0, value - delta); } @@ -39,17 +47,23 @@ void IngestNeed::ApplyEffect(Creature &c, double dt) { InhaleNeed::InhaleNeed(int resource, double speed, double damage) : resource(resource) , speed(speed) -, damage(damage) { +, damage(damage) +, inhaling(false) { } InhaleNeed::~InhaleNeed() { } void InhaleNeed::ApplyEffect(Creature &c, double dt) { - if (!IsSatisfied()) { - // TODO: make condition more natural with thresholds and stuff + if (!IsSatisfied() && !inhaling) { + inhaling = true; + } + if (inhaling) { if (c.GetSituation().OnPlanet() && c.GetSituation().GetPlanet().Atmosphere() == resource) { - value = std::max(0.0, value - (speed * dt)); + Decrease(speed * dt); + if (value == 0.0) { + inhaling = false; + } } else { // TODO: panic } -- 2.39.2