feat(game+display): render animation

This commit is contained in:
2026-04-29 01:48:55 +02:00
parent 296b48eb9f
commit 708a7d53bb
7 changed files with 36 additions and 10 deletions
+2
View File
@@ -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
+1
View File
@@ -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);
+2 -1
View File
@@ -1,6 +1,6 @@
#pragma once
#include <Arduino.h>
#define MAXIMUM_STAT 100
#include "constants.hpp"
class Pet {
public:
@@ -27,4 +27,5 @@ class Pet {
String reasonForDeath;
byte lastAnimationFrame;
uint64_t lastAnimationFrameTime;
};
+3 -1
View File
@@ -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
}
+8 -1
View File
@@ -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) {
+3 -3
View File
@@ -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
+17 -4
View File
@@ -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;
}