diff --git a/include/constants.hpp b/include/constants.hpp index 53c6ffc..e02afc6 100644 --- a/include/constants.hpp +++ b/include/constants.hpp @@ -8,6 +8,8 @@ #define LCD_ROWS 4 #define ACTION_INTERVAL 60000 // 1 minute + #define MAXIMUM_STAT 100 +#define ANIMATION_FRAME_INTERVAL 1500 // Time in milliseconds between animation frames #define DEBOUNCE_DELAY 50 // Debounce delay in milliseconds for the joystick button diff --git a/include/display.hpp b/include/display.hpp index fc9ada9..09427ca 100644 --- a/include/display.hpp +++ b/include/display.hpp @@ -14,6 +14,7 @@ class Display { void begin(); void clear(); + void drawPetStats(Pet& pet); void drawPet(Pet& pet); void drawBuffer(String buffer[]); void drawMenu(Menu& menu); diff --git a/include/pet.hpp b/include/pet.hpp index 30f0ca8..254ed2c 100644 --- a/include/pet.hpp +++ b/include/pet.hpp @@ -1,6 +1,6 @@ #pragma once #include -#define MAXIMUM_STAT 100 +#include "constants.hpp" class Pet { public: @@ -27,4 +27,5 @@ class Pet { String reasonForDeath; byte lastAnimationFrame; + uint64_t lastAnimationFrameTime; }; diff --git a/src/display.cpp b/src/display.cpp index ed16b45..cf533a8 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -45,7 +45,7 @@ void Display::drawBuffer(String buffer[]) { } } -void Display::drawPet(Pet &pet) { +void Display::drawPetStats(Pet &pet) { clear(); lcd.setCursor(0, 0); @@ -56,7 +56,9 @@ void Display::drawPet(Pet &pet) { lcd.print("Energy: " + String(pet.getEnergy())); lcd.setCursor(0, 3); lcd.print("Cleanliness: " + String(pet.getCleanliness())); +} +void Display::drawPet(Pet &pet) { lcd.setCursor(LCD_COLS - 2, LCD_ROWS / 2 - 1); // Position the pet lcd.write(pet.getAnimationFrame()); // Draw the custom pet character } diff --git a/src/game.cpp b/src/game.cpp index 9f5256a..d62faf6 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -86,7 +86,14 @@ void Game::update() { } void Game::render() { + // If the display doesn't need to be cleared, we can skip rendering if (!state.shouldClearDisplay) { + // However, we still need to draw the pet if it's alive and + // we're not in the menu + if (state.pet.isAlive && !state.isMenuOpen) { + display.drawPet(state.pet); + } + return; } @@ -112,7 +119,7 @@ void Game::render() { } // Render the pet's stats on the display - display.drawPet(state.pet); + display.drawPetStats(state.pet); } void Game::forceUpdate(String reason) { diff --git a/src/joystick.cpp b/src/joystick.cpp index eb71e05..b593a79 100644 --- a/src/joystick.cpp +++ b/src/joystick.cpp @@ -29,9 +29,9 @@ Joystick::Joystick() : vrx(A0), vry(A1), sw(Switch(4)) { pinMode(vrx, INPUT); pinMode(vry, INPUT); - // Calibrate the joystick by reading the center position and storing it as an offset - xOffset = map(analogRead(vrx), 0, 1023, -100, 100); - yOffset = map(analogRead(vry), 0, 1023, -100, 100); + // TODO: Calibrate the joystick by reading the center position and storing it as an offset + xOffset = 10; + yOffset = 13; } // Map the analog readings from the joystick to a range of -100 to 100 for both X and Y axes diff --git a/src/pet.cpp b/src/pet.cpp index 27e1233..f20b720 100644 --- a/src/pet.cpp +++ b/src/pet.cpp @@ -1,7 +1,15 @@ #include "pet.hpp" -Pet::Pet() - : isAlive(true), hunger(0), joy(100), energy(100), cleanliness(100), reasonForDeath(""), lastAnimationFrame(0) {} +Pet::Pet() : + isAlive(true), + hunger(0), + joy(100), + energy(100), + cleanliness(100), + reasonForDeath(""), + lastAnimationFrame(0), + lastAnimationFrameTime(0) +{} void Pet::updateHunger(int8_t delta) { hunger += delta; @@ -84,7 +92,12 @@ String Pet::getReasonForDeath() const { } byte Pet::getAnimationFrame() { - // Alternate between two frames for a simple animation effect - lastAnimationFrame = (lastAnimationFrame + 1) % 2; + uint64_t currentTime = millis(); + + if (currentTime - lastAnimationFrameTime >= ANIMATION_FRAME_INTERVAL) { + lastAnimationFrameTime = currentTime; + lastAnimationFrame = (lastAnimationFrame + 1) % 2; // Alternate between frame 0 and 1 + } + return lastAnimationFrame; }