added high score saving and reading

closes #5
This commit is contained in:
Rudis Muiznieks 2022-06-21 21:36:50 -05:00
parent cd55de5ce0
commit 27419291c7
Signed by: rudism
GPG Key ID: CABF2F86EF7884F9
3 changed files with 60 additions and 10 deletions

View File

@ -170,6 +170,7 @@ int getMenuBackOut() {
case STATE_WITHDRAW_INPUT: case STATE_WITHDRAW_INPUT:
return 1; return 1;
case STATE_INVENTORY: case STATE_INVENTORY:
case STATE_HIGH_SCORES:
return 2; return 2;
case STATE_JET_MENU: case STATE_JET_MENU:
return 3; return 3;

View File

@ -5,6 +5,7 @@ License: WTFPL
*/ */
#include <Arduboy2.h> #include <Arduboy2.h>
#include <EEPROM.h>
#include "src/Font3x5.h" #include "src/Font3x5.h"
#include "src/Font4x6.h" #include "src/Font4x6.h"
@ -14,6 +15,7 @@ Font3x5 font3x5 = Font3x5();
enum GameState { enum GameState {
STATE_TITLE = 0, STATE_TITLE = 0,
STATE_HIGH_SCORES,
STATE_TURN_MENU, STATE_TURN_MENU,
STATE_JET_MENU, STATE_JET_MENU,
STATE_BUY_MENU, STATE_BUY_MENU,
@ -75,22 +77,27 @@ const uint8_t PROGMEM spriteWeed[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x22, 0x32, 0x2a, 0x24, 0x22, 0x10, 0x0a, 0x08, 0x0c, 0x08, 0x12, 0x21, 0x24, 0x2a, 0x32, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x22, 0x32, 0x2a, 0x24, 0x22, 0x10, 0x0a, 0x08, 0x0c, 0x08, 0x12, 0x21, 0x24, 0x2a, 0x32, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}; };
const uint8_t eepStart = EEPROM_STORAGE_SPACE_START + 1;
/**************/ /**************/
/* game state */ /* game state */
/**************/ /**************/
bool screenInitialized; bool screenInitialized;
GameState sGameState; GameState sGameState;
GameState sPreviousGameState; GameState sPreviousGameState;
int sMaxDays; short sMaxDays;
int sCurrentDay; short sCurrentDay;
long sDrugPrices[6]; long sDrugPrices[6];
int sCurrentDrug; short sCurrentDrug;
long sCurrentQty; long sCurrentQty;
long sQtyMax; long sQtyMax;
long sLastDebounce = 0; long sLastDebounce = 0;
int sDebounceCount = 0; int sDebounceCount = 0;
bool sAlreadyBorrowed; bool sAlreadyBorrowed;
int sPigs = 0; short sPigs = 0;
// default high scores
long s30High = 100000;
long s60High = 200000;
/****************/ /****************/
/* player state */ /* player state */
@ -120,11 +127,23 @@ bool dialogSmall = false;
/***********************/ /***********************/
void setup() { void setup() {
arduboy.boot(); arduboy.boot();
arduboy.bootLogoSpritesOverwrite();
arduboy.setFrameRate(15); arduboy.setFrameRate(15);
arduboy.initRandomSeed(); arduboy.initRandomSeed();
sGameState = STATE_TITLE; sGameState = STATE_TITLE;
screenInitialized = false; screenInitialized = false;
arduboy.begin();
// read high scores from eeprom
char check[5];
EEPROM.get(eepStart, check);
if (strcmp(check, "slng") == 0) {
EEPROM.get(eepStart + 5, s30High);
EEPROM.get(eepStart + 9, s60High);
} else {
EEPROM.put(eepStart, "slng");
EEPROM.put(eepStart + 5, s30High);
EEPROM.put(eepStart + 9, s60High);
}
} }
void loop() { void loop() {
@ -158,14 +177,21 @@ void loop() {
font3x5.print(F("v1.1.1")); font3x5.print(F("v1.1.1"));
menu[0] = F("Hit the Streets"); menu[0] = F("Hit the Streets");
menu[1] = F("Extended Game"); menu[1] = F("Extended Game");
// menu[2] = F("High Scores"); menu[2] = F("High Scores");
menuLength = 2; menuLength = 3;
menuSmall = true; menuSmall = true;
menuCols = false; menuCols = false;
drawMenu(menuSmall, menuCols, menuLength, menu); drawMenu(menuSmall, menuCols, menuLength, menu);
drawMenuIndicator(menuSelected, menuSmall, menuCols, menuLength, menu, false); drawMenuIndicator(menuSelected, menuSmall, menuCols, menuLength, menu, false);
break; break;
case STATE_HIGH_SCORES:
drawDialog();
drawMoney(10, 19, s30High);
drawMoney(10, 39, s60High);
sPreviousGameState = STATE_TITLE;
break;
case STATE_TURN_MENU: case STATE_TURN_MENU:
drawStatusBar(); drawStatusBar();
drawTitle(lookupLocation(pLocation)); drawTitle(lookupLocation(pLocation));
@ -353,9 +379,8 @@ void loop() {
break; break;
case STATE_GAME_OVER: { case STATE_GAME_OVER: {
dialog[0] = "Game over! Score:";
dialogSmall = false; dialogSmall = false;
dialogLength = 1; dialogLength = 3;
sPreviousGameState = STATE_TITLE; sPreviousGameState = STATE_TITLE;
pMoney += pSavingsAmount - pLoanAmount; pMoney += pSavingsAmount - pLoanAmount;
for (int i = 0; i < 6; i++) { for (int i = 0; i < 6; i++) {
@ -364,8 +389,23 @@ void loop() {
if (pMoney < 0) { if (pMoney < 0) {
pMoney = 0; pMoney = 0;
} }
bool newHigh = sMaxDays == 31 ? pMoney > s30High : pMoney > s60High;
dialog[0] = F("Game over! Score:");
dialog[1] = F("");
dialog[2] = newHigh ? F("Old ") : F("");
dialog[2] += F("High Score:");
drawDialog(); drawDialog();
drawMoney(10, 20, pMoney); drawMoney(10, 19, pMoney);
drawMoney(10, 39, sMaxDays == 31 ? s30High : s60High);
if (newHigh) {
if (sMaxDays == 31) {
s30High = pMoney;
EEPROM.put(eepStart + 5, s30High);
} else {
s60High = pMoney;
EEPROM.put(eepStart + 9, s60High);
}
}
break; break;
} }
@ -450,6 +490,7 @@ void loop() {
break; break;
} }
case STATE_INVENTORY: case STATE_INVENTORY:
case STATE_HIGH_SCORES:
case STATE_INFO_DIALOG: case STATE_INFO_DIALOG:
case STATE_DID_WEED_DIALOG_1: case STATE_DID_WEED_DIALOG_1:
case STATE_DID_WEED_DIALOG_2: case STATE_DID_WEED_DIALOG_2:

View File

@ -204,6 +204,14 @@ void handleMenuAction() {
case 1: // extended game case 1: // extended game
initializeNewGame(60); initializeNewGame(60);
break; break;
case 2: // high scores
dialog[0] = F("Normal Game:");
dialog[1] = F("");
dialog[2] = F("Extended Game:");
dialogSmall = false;
dialogLength = 3;
sGameState = STATE_HIGH_SCORES;
break;
} }
break; break;