improved debounce logic, selecting previous menu when backing out

closes #1
closes #2
This commit is contained in:
Rudis Muiznieks 2022-06-21 10:13:30 -05:00
parent b9bba1447f
commit a48e0f71e9
Signed by: rudism
GPG Key ID: CABF2F86EF7884F9
2 changed files with 52 additions and 8 deletions

View File

@ -87,7 +87,7 @@ int sCurrentDrug;
long sCurrentQty; long sCurrentQty;
long sQtyMax; long sQtyMax;
long sLastDebounce = 0; long sLastDebounce = 0;
short sDebounceCount = 0; int sDebounceCount = 0;
bool sAlreadyBorrowed; bool sAlreadyBorrowed;
int sPigs = 0; int sPigs = 0;
@ -103,11 +103,12 @@ int pCapacity;
int pHealth; int pHealth;
long pInventory[6]; long pInventory[6];
String menu[8]; String menu[6];
int menuLength = 0; int menuLength = 0;
bool menuCols = false; bool menuCols = false;
bool menuSmall = false; bool menuSmall = false;
int menuSelected = 0; int menuSelected = 0;
int menuBackOut = 0;
String dialog[5]; String dialog[5];
int dialogLength; int dialogLength;
@ -137,7 +138,8 @@ void loop() {
if (!screenInitialized) { if (!screenInitialized) {
arduboy.clear(); arduboy.clear();
menuSelected = 0; menuSelected = menuBackOut;
menuBackOut = 0;
// if we're entering the turn menu on day 31 then game is over instead // if we're entering the turn menu on day 31 then game is over instead
if (sGameState == STATE_TURN_MENU if (sGameState == STATE_TURN_MENU
@ -430,6 +432,7 @@ void loop() {
sGameState == STATE_SHARK_MENU || sGameState == STATE_SHARK_MENU ||
sGameState == STATE_BANK_MENU) { sGameState == STATE_BANK_MENU) {
screenInitialized = false; screenInitialized = false;
menuBackOut = getMenuBackOut();
sGameState = STATE_TURN_MENU; sGameState = STATE_TURN_MENU;
} }
} }
@ -524,10 +527,16 @@ void loop() {
sGameState = sPreviousGameState; sGameState = sPreviousGameState;
} else { } else {
const unsigned long currentMillis = millis(); const unsigned long currentMillis = millis();
const int incAmount = sGameState == STATE_BUY_QTY_INPUT || const long incAmount = sGameState == STATE_BUY_QTY_INPUT ||
sGameState == STATE_SELL_QTY_INPUT ? 1 : 10; sGameState == STATE_SELL_QTY_INPUT ? 1 : 10;
if (currentMillis - sLastDebounce > (sDebounceCount < 2 ? 500 : 100)) { if (currentMillis - sLastDebounce > (sDebounceCount < 2 ? 500 : sDebounceCount < 21 ? 100 : 0)) {
if (sDebounceCount < 2) sDebounceCount++; const long incMult = sDebounceCount < 100
? 1 : sDebounceCount < 125
? 10 : sDebounceCount < 150
? 100 : sDebounceCount < 175
? 1000
: 10000;
if (sDebounceCount < 1000) sDebounceCount++;
if (arduboy.pressed(UP_BUTTON)) { if (arduboy.pressed(UP_BUTTON)) {
sLastDebounce = currentMillis; sLastDebounce = currentMillis;
if (sCurrentQty <= sQtyMax - incAmount) sCurrentQty += incAmount; if (sCurrentQty <= sQtyMax - incAmount) sCurrentQty += incAmount;
@ -535,7 +544,8 @@ void loop() {
drawQtyInputTotal(); drawQtyInputTotal();
} else if (arduboy.pressed(RIGHT_BUTTON)) { } else if (arduboy.pressed(RIGHT_BUTTON)) {
sLastDebounce = currentMillis; sLastDebounce = currentMillis;
if (sCurrentQty <= sQtyMax - incAmount * 10) sCurrentQty += incAmount * 10; if (sCurrentQty + incAmount * 10 * incMult <= sQtyMax)
sCurrentQty += incAmount * 10 * incMult;
else sCurrentQty = sQtyMax; else sCurrentQty = sQtyMax;
drawQtyInputTotal(); drawQtyInputTotal();
} else if (arduboy.pressed(DOWN_BUTTON)) { } else if (arduboy.pressed(DOWN_BUTTON)) {
@ -545,7 +555,8 @@ void loop() {
drawQtyInputTotal(); drawQtyInputTotal();
} else if (arduboy.pressed(LEFT_BUTTON)) { } else if (arduboy.pressed(LEFT_BUTTON)) {
sLastDebounce = currentMillis; sLastDebounce = currentMillis;
if (sCurrentQty >= incAmount * 10) sCurrentQty -= incAmount * 10; if (sCurrentQty >= incAmount * 10 * incMult)
sCurrentQty -= incAmount * 10 * incMult;
else sCurrentQty = 0; else sCurrentQty = 0;
drawQtyInputTotal(); drawQtyInputTotal();
} else { } else {

View File

@ -192,6 +192,38 @@ void newDayRandomEvent() {
} }
} }
int getMenuBackOut() {
// this whole thing could have been done so much better :(
// i suck at C++
switch (sGameState) {
case STATE_SELL_MENU:
case STATE_BORROW_INPUT:
case STATE_WITHDRAW_INPUT:
return 1;
case STATE_INVENTORY:
return 2;
case STATE_JET_MENU:
return 3;
case STATE_SHARK_MENU:
return 4;
case STATE_BANK_MENU:
return 5;
case STATE_BUY_QTY_INPUT:
case STATE_SELL_QTY_INPUT:
return sCurrentDrug;
case STATE_INFO_DIALOG:
switch (sPreviousGameState) {
case STATE_BUY_MENU:
case STATE_SELL_MENU:
return sCurrentDrug;
default:
return 0;
}
default:
return 0;
}
}
void handleMenuAction() { void handleMenuAction() {
screenInitialized = false; screenInitialized = false;
switch (sGameState) { switch (sGameState) {
@ -418,6 +450,7 @@ bool checkBackedOut(const bool eitherButton) {
if (arduboy.justPressed(B_BUTTON) || if (arduboy.justPressed(B_BUTTON) ||
(eitherButton && arduboy.justPressed(A_BUTTON))) { (eitherButton && arduboy.justPressed(A_BUTTON))) {
screenInitialized = false; screenInitialized = false;
menuBackOut = getMenuBackOut();
sGameState = sPreviousGameState; sGameState = sPreviousGameState;
return true; return true;
} }