Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mitochondria #43

Merged
merged 4 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cell.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
2 changes: 1 addition & 1 deletion cell_spawner.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -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="."]
Expand Down
15 changes: 12 additions & 3 deletions src/cell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>("CellState"); }
void Cell::_ready() {
_cellState = this->get_node<CellState>("CellState");
}

void Cell::_process(double delta) {
// Don't run if in editor
Expand All @@ -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
Expand All @@ -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);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/cell.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "cell_state.hpp"
#include "mitochondria.hpp"

#include <godot_cpp/classes/Engine.hpp>
#include <godot_cpp/classes/random_number_generator.hpp>
Expand Down
9 changes: 8 additions & 1 deletion src/cell_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand All @@ -44,4 +47,8 @@ void CellState::applyScale(const float scale) {
if (scale <= 0)
_scale *= scale;
}
float CellState::getScale() const { return _scale; }
float CellState::getScale() const { return _scale; }

void CellState::_ready() {
_mitochondria = this->get_node<Mitochondria>("Mitochondria");
}
7 changes: 7 additions & 0 deletions src/cell_state.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "mitochondria.hpp"
#include <godot_cpp/classes/node.hpp>

namespace godot {
Expand All @@ -14,6 +15,9 @@ class CellState : public Node {
CellState();
~CellState();

void setMitochondria(Mitochondria *);
Mitochondria *getMitochondria();

void setAlive(const bool);
bool getAlive() const;

Expand All @@ -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;
Expand Down
40 changes: 40 additions & 0 deletions src/mitochondria.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
28 changes: 28 additions & 0 deletions src/mitochondria.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <godot_cpp/classes/node.hpp>

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
2 changes: 2 additions & 0 deletions src/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -22,6 +23,7 @@ void initialize_gdextension_module(ModuleInitializationLevel p_level) {
ClassDB::register_class<FpsCounter>();
ClassDB::register_class<Cell>();
ClassDB::register_class<CellState>();
ClassDB::register_class<Mitochondria>();
ClassDB::register_class<CellSpawner>();
ClassDB::register_class<StartButton>();
}
Expand Down
Loading