feat(pet): extract pet class

This commit is contained in:
2026-04-29 00:41:31 +02:00
parent 6c349c2ada
commit 5b763a33bc
2 changed files with 89 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
#pragma once
#include <Arduino.h>
#define MAXIMUM_STAT 100
class Pet {
public:
Pet();
bool isAlive;
void updateHunger(int8_t delta);
void updateJoy(int8_t delta);
void updateEnergy(int8_t delta);
void updateCleanliness(int8_t delta);
String getReasonForDeath() const;
private:
int8_t hunger;
int8_t joy;
int8_t energy;
int8_t cleanliness;
String reasonForDeath;
};
+68
View File
@@ -0,0 +1,68 @@
#include "pet.hpp"
Pet::Pet()
: isAlive(true), hunger(0), joy(100), energy(100), cleanliness(100), reasonForDeath("") {}
void Pet::updateHunger(int8_t delta) {
hunger += delta;
if (hunger < 0) {
isAlive = false;
reasonForDeath = "starvation";
return;
}
if (hunger > MAXIMUM_STAT) {
isAlive = false;
reasonForDeath = "overfeeding";
}
}
void Pet::updateJoy(int8_t delta) {
joy += delta;
if (joy < 0) {
isAlive = false;
reasonForDeath = "depression";
return;
}
if (joy > MAXIMUM_STAT) {
isAlive = false;
reasonForDeath = "overstimulation";
}
}
void Pet::updateEnergy(int8_t delta) {
energy += delta;
if (energy < 0) {
isAlive = false;
reasonForDeath = "exhaustion";
return;
}
if (energy > MAXIMUM_STAT) {
isAlive = false;
reasonForDeath = "overresting";
}
}
void Pet::updateCleanliness(int8_t delta) {
cleanliness += delta;
if (cleanliness < 0) {
isAlive = false;
reasonForDeath = "disease";
return;
}
if (cleanliness > MAXIMUM_STAT) {
isAlive = false;
reasonForDeath = "overcleaning";
}
}
String Pet::getReasonForDeath() const {
return reasonForDeath;
}