From c6923c209c3ad07d509f403edbb28e23fc164e92 Mon Sep 17 00:00:00 2001 From: Zvonimir Rudinski Date: Wed, 29 Apr 2026 01:00:02 +0200 Subject: [PATCH] feat(game): add custom pet icon --- include/constants.hpp | 1 + include/display.hpp | 2 ++ include/pet.hpp | 5 +++++ src/display.cpp | 28 ++++++++++++++++++++++++++++ src/game.cpp | 28 +++++++++++++--------------- src/pet.cpp | 20 ++++++++++++++++++-- 6 files changed, 67 insertions(+), 17 deletions(-) diff --git a/include/constants.hpp b/include/constants.hpp index 06c098d..88aa324 100644 --- a/include/constants.hpp +++ b/include/constants.hpp @@ -2,6 +2,7 @@ /* * A header file to store all the constants used in the project. */ +#include #define LCD_I2C_ADDRESS 0x27 #define LCD_COLS 20 #define LCD_ROWS 4 diff --git a/include/display.hpp b/include/display.hpp index 32ed387..fc9ada9 100644 --- a/include/display.hpp +++ b/include/display.hpp @@ -3,6 +3,7 @@ #include #include "constants.hpp" #include "menu.hpp" +#include "pet.hpp" /* * A helper class to facilitate drawing on a HD44780 LCD display. @@ -13,6 +14,7 @@ class Display { void begin(); void clear(); + void drawPet(Pet& pet); void drawBuffer(String buffer[]); void drawMenu(Menu& menu); diff --git a/include/pet.hpp b/include/pet.hpp index a753f41..eeed5a4 100644 --- a/include/pet.hpp +++ b/include/pet.hpp @@ -11,6 +11,11 @@ class Pet { void updateJoy(int8_t delta); void updateEnergy(int8_t delta); void updateCleanliness(int8_t delta); + + int8_t getHunger() const; + int8_t getJoy() const; + int8_t getEnergy() const; + int8_t getCleanliness() const; String getReasonForDeath() const; private: int8_t hunger; diff --git a/src/display.cpp b/src/display.cpp index 65a0d4e..5738b57 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -4,8 +4,20 @@ Display::Display() : lcd(LCD_I2C_ADDRESS, LCD_COLS, LCD_ROWS) {} void Display::begin() { + byte CUSTOM_CHAR_PET[] = { + B00100, + B01110, + B11111, + B10101, + B11111, + B11111, + B10001, + B11111 + }; + lcd.init(); lcd.backlight(); + lcd.createChar(0, CUSTOM_CHAR_PET); // Create a custom character for the pet } void Display::clear() { @@ -20,6 +32,22 @@ void Display::drawBuffer(String buffer[]) { } } +void Display::drawPet(Pet &pet) { + clear(); + + lcd.setCursor(0, 0); + lcd.print("Hunger: " + String(pet.getHunger())); + lcd.setCursor(0, 1); + lcd.print("Joy: " + String(pet.getJoy())); + lcd.setCursor(0, 2); + lcd.print("Energy: " + String(pet.getEnergy())); + lcd.setCursor(0, 3); + lcd.print("Cleanliness: " + String(pet.getCleanliness())); + + lcd.setCursor(LCD_COLS - 2, LCD_ROWS / 2 - 1); // Position the pet + lcd.write(byte(0)); // Draw the custom pet character +} + void Display::drawMenu(Menu &menu) { clear(); size_t currentItemIndex = menu.getCurrentItemIndex(); diff --git a/src/game.cpp b/src/game.cpp index c8543c8..9f5256a 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -5,7 +5,7 @@ Game::Game() : joystick(), display(), menu() { .pet = Pet(), .lastActionTime = 0, .isMenuOpen = false, - .shouldClearDisplay = false, + .shouldClearDisplay = true, }; String items[] = { @@ -86,35 +86,33 @@ void Game::update() { } void Game::render() { - // Clear display if needed, and if the menu is open, redraw it - if (state.shouldClearDisplay) { - display.clear(); - state.shouldClearDisplay = false; - - if (state.isMenuOpen) { - display.drawMenu(menu); - Serial.println("Rendering menu"); - return; - } + if (!state.shouldClearDisplay) { + return; } - // If the menu is open, we don't need to render the pet's stats + state.shouldClearDisplay = false; + if (state.isMenuOpen) { + display.drawMenu(menu); + Serial.println("Rendering menu"); return; } // If the pet is dead, display a message and return if (!state.pet.isAlive) { String buffer[LCD_ROWS] = { - "Your pet has died", - state.pet.getReasonForDeath(), + "Your pet has died of", + state.pet.getReasonForDeath() + ".", "Reset the device", - "to start over" + "to start over." }; display.drawBuffer(buffer); return; } + + // Render the pet's stats on the display + display.drawPet(state.pet); } void Game::forceUpdate(String reason) { diff --git a/src/pet.cpp b/src/pet.cpp index c370523..8b0d795 100644 --- a/src/pet.cpp +++ b/src/pet.cpp @@ -8,13 +8,13 @@ void Pet::updateHunger(int8_t delta) { if (hunger < 0) { isAlive = false; - reasonForDeath = "starvation"; + reasonForDeath = "overfeeding"; return; } if (hunger > MAXIMUM_STAT) { isAlive = false; - reasonForDeath = "overfeeding"; + reasonForDeath = "starvation"; } } @@ -63,6 +63,22 @@ void Pet::updateCleanliness(int8_t delta) { } } +int8_t Pet::getHunger() const { + return hunger; +} + +int8_t Pet::getJoy() const { + return joy; +} + +int8_t Pet::getEnergy() const { + return energy; +} + +int8_t Pet::getCleanliness() const { + return cleanliness; +} + String Pet::getReasonForDeath() const { return reasonForDeath; }