diff --git a/cell.tscn b/cell.tscn index 21a829e..721eae0 100644 --- a/cell.tscn +++ b/cell.tscn @@ -25,3 +25,5 @@ debug_color = Color(0, 0.6, 0.701961, 0.419608) [node name="CellState" type="CellState" parent="."] lifespan = 20.0 + +[node name="Mitochondria" type="Mitochondria" parent="CellState"] diff --git a/cell_spawner.tscn b/cell_spawner.tscn index e6f94b4..f4fe8c9 100644 --- a/cell_spawner.tscn +++ b/cell_spawner.tscn @@ -19,8 +19,8 @@ offset_bottom = 23.0 [node name="StatsCounter" type="StatsCounter" parent="."] z_index = 2 -offset_right = 40.0 offset_top = 23.0 +offset_right = 40.0 offset_bottom = 46.0 [node name="ViewportBoundaries" type="StaticBody2D" parent="."] diff --git a/src/cell.cpp b/src/cell.cpp index a9f53d4..d86ae17 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -43,7 +43,9 @@ float Cell::getScale() const { return _cellState->getScale(); } Size2 Cell::getSpriteSize() const { return _spriteSize; } -void Cell::_ready() { _cellState = this->get_node("CellState"); } +void Cell::_ready() { + _cellState = this->get_node("CellState"); +} void Cell::_process(double delta) { // Don't run if in editor @@ -53,10 +55,12 @@ void Cell::_process(double delta) { if (_cellState->getAlive()) { // Living Cell behavior - // Increment the Cell's age + // Increment the Cell's age and decrement nutrients _cellState->incrementAge(delta); + _cellState->getMitochondria()->decrementNutrients(delta); - // Aging and death + // Aging, starvation and death + float nutrients = _cellState->getMitochondria()->getNutrients(); float ageDiff = _cellState->getAge() - _cellState->getLifespan(); if (ageDiff > 0) { // The Cell's age exceeds its lifespan @@ -71,6 +75,11 @@ void Cell::_process(double delta) { this->set_linear_damp(10.0); } } + if (nutrients <= 0) { + _cellState->setAlive(false); + // Stop Cell movement + this->set_linear_damp(10.0); + } } } diff --git a/src/cell.hpp b/src/cell.hpp index f08bb4e..ab5b20e 100644 --- a/src/cell.hpp +++ b/src/cell.hpp @@ -1,5 +1,6 @@ #pragma once #include "cell_state.hpp" +#include "mitochondria.hpp" #include #include diff --git a/src/cell_state.cpp b/src/cell_state.cpp index 8b1f3f6..fb87f02 100644 --- a/src/cell_state.cpp +++ b/src/cell_state.cpp @@ -18,6 +18,9 @@ CellState::CellState() { } CellState::~CellState() {} +void CellState::setMitochondria(Mitochondria *mitochondria) { _mitochondria = mitochondria; } +Mitochondria *CellState::getMitochondria() { return _mitochondria; } + void CellState::setAlive(const bool alive) { _alive = alive; } bool CellState::getAlive() const { return _alive; } @@ -44,4 +47,8 @@ void CellState::applyScale(const float scale) { if (scale <= 0) _scale *= scale; } -float CellState::getScale() const { return _scale; } \ No newline at end of file +float CellState::getScale() const { return _scale; } + +void CellState::_ready() { + _mitochondria = this->get_node("Mitochondria"); +} \ No newline at end of file diff --git a/src/cell_state.hpp b/src/cell_state.hpp index c9e39a2..95367b5 100644 --- a/src/cell_state.hpp +++ b/src/cell_state.hpp @@ -1,5 +1,6 @@ #pragma once +#include "mitochondria.hpp" #include namespace godot { @@ -14,6 +15,9 @@ class CellState : public Node { CellState(); ~CellState(); + void setMitochondria(Mitochondria *); + Mitochondria *getMitochondria(); + void setAlive(const bool); bool getAlive() const; @@ -28,7 +32,10 @@ class CellState : public Node { void applyScale(const float); float getScale() const; + void _ready() override; + private: + Mitochondria *_mitochondria; bool _alive; float _age; float _lifespan; diff --git a/src/mitochondria.cpp b/src/mitochondria.cpp new file mode 100644 index 0000000..ae5d7d9 --- /dev/null +++ b/src/mitochondria.cpp @@ -0,0 +1,40 @@ +#include "mitochondria.hpp" + +using namespace godot; + +void Mitochondria::_bind_methods() { + ClassDB::bind_method(D_METHOD("add_nutrients", "amount"), &Mitochondria::addNutrients); + ClassDB::bind_method(D_METHOD("decrement_nutrients", "amount"), &Mitochondria::decrementNutrients); + ClassDB::bind_method(D_METHOD("get_nutrients"), &Mitochondria::getNutrients); + ClassDB::bind_method(D_METHOD("set_nutrients", "level"), &Mitochondria::setNutrients); + ClassDB::add_property("Mitochondria", PropertyInfo(Variant::FLOAT, "nutrients"), + "set_nutrients", "get_nutrients"); +} + +Mitochondria::Mitochondria() { + _nutrients = 26; + _nutrient_maximum = 26; + _nutrient_efficiency = 1; +} +Mitochondria::~Mitochondria() {} + +void Mitochondria::addNutrients(float nutrients) { + nutrients *= _nutrient_efficiency; + if (_nutrients + nutrients > _nutrient_maximum) + nutrients = _nutrient_maximum; + else + _nutrients += nutrients; +} + +void Mitochondria::decrementNutrients(const float decrement) { + if (_nutrients - decrement > 0) + _nutrients -= decrement; + else if (_nutrients - decrement <= 0) + _nutrients = 0; +} + +float Mitochondria::getNutrients() const { return _nutrients; } + +void Mitochondria::setNutrients(const float level) { + _nutrients = level; +} \ No newline at end of file diff --git a/src/mitochondria.hpp b/src/mitochondria.hpp new file mode 100644 index 0000000..38c3713 --- /dev/null +++ b/src/mitochondria.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include + +namespace godot { + +class Mitochondria : public Node { + GDCLASS(Mitochondria, Node); + +protected: + static void _bind_methods(); + +public: + Mitochondria(); + ~Mitochondria(); + + void addNutrients(float); + void decrementNutrients(const float); + float getNutrients() const; + void setNutrients(const float); + +private: + float _nutrients; + float _nutrient_maximum; + float _nutrient_efficiency; +}; + +}; // namespace godot \ No newline at end of file diff --git a/src/register_types.cpp b/src/register_types.cpp index e4e8f32..e2be0c4 100644 --- a/src/register_types.cpp +++ b/src/register_types.cpp @@ -4,6 +4,7 @@ #include "cell_spawner.hpp" #include "cell_state.hpp" #include "fps_counter.hpp" +#include "mitochondria.hpp" #include "start_button.hpp" #include "stats_counter.hpp" @@ -22,6 +23,7 @@ void initialize_gdextension_module(ModuleInitializationLevel p_level) { ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); + ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); }