slangin/states.ino

190 lines
4.9 KiB
Arduino
Raw Normal View History

2022-06-18 16:54:40 -05:00
/*
Slangin'
State Management
Author: Rudis Muiznieks
License: WTFPL
*/
2022-06-18 00:05:30 -05:00
void initializeNewGame() {
sGameState = STATE_TURN_MENU;
2022-06-18 21:34:58 -05:00
pMoney = 2000;
2022-06-18 00:05:30 -05:00
pLoanAmount = 5000;
pSavingsAmount = 0;
pLocation = LOC_BRONX;
pGuns = 0;
pCapacity = 100;
pHealth = 50;
for (int i = 0; i < 6; i++) {
pInventory[i] = 0;
}
sCurrentDay = 0;
incrementDay(); // start on day 1
sRandomEvent = 0; // no event on first day
}
void incrementDay() {
sCurrentDay++;
// TODO: check for game over
2022-06-18 21:34:58 -05:00
sAlreadyBorrowed = false;
2022-06-18 00:05:30 -05:00
// generate new drug prices
2022-06-18 13:20:04 -05:00
sDrugPrices[DRUG_COCAINE] = random(12001) + 16000;
sDrugPrices[DRUG_HEROINE] = random(7001) + 5000;
sDrugPrices[DRUG_ACID] = (random(35) + 10) * 100;
sDrugPrices[DRUG_WEED] = (random(43) + 33) * 10;
sDrugPrices[DRUG_SPEED] = (random(16) + 7) * 10;
sDrugPrices[DRUG_LUDES] = (random(5) + 1) * 10;
2022-06-18 00:05:30 -05:00
}
void handleMenuAction() {
screenInitialized = false;
switch (sGameState) {
case STATE_TITLE:
switch (menuSelected) {
case 0: // new game
initializeNewGame();
break;
// TODO: continue/load saved
}
break;
case STATE_TURN_MENU:
2022-06-18 16:54:40 -05:00
sPreviousGameState = STATE_TURN_MENU;
2022-06-18 00:05:30 -05:00
switch (menuSelected) {
case 0: // trenchcoat
sGameState = STATE_INVENTORY;
break;
case 1: // buy drugs
2022-06-18 13:20:04 -05:00
sGameState = STATE_BUY_MENU;
break;
2022-06-18 00:05:30 -05:00
case 2: // sell drugs
2022-06-18 13:20:04 -05:00
sGameState = STATE_SELL_MENU;
break;
2022-06-18 00:05:30 -05:00
case 3: // jet
2022-06-18 13:20:04 -05:00
sGameState = STATE_JET_MENU;
break;
2022-06-18 00:05:30 -05:00
case 4: // loan shark
2022-06-18 13:20:04 -05:00
sGameState = STATE_SHARK_MENU;
break;
2022-06-18 00:05:30 -05:00
case 5: // bank
2022-06-18 13:20:04 -05:00
sGameState = STATE_BANK_MENU;
2022-06-18 00:05:30 -05:00
break;
}
break;
case STATE_JET_MENU:
2022-06-18 16:54:40 -05:00
break;
2022-06-18 21:34:58 -05:00
2022-06-18 18:17:18 -05:00
case STATE_BUY_MENU: {
int remaining = playerCapacityRemaining();
2022-06-18 16:54:40 -05:00
sPreviousGameState = STATE_BUY_MENU;
sCurrentDrug = menuSelected;
2022-06-18 18:17:18 -05:00
if (remaining <= 0) {
2022-06-18 16:54:40 -05:00
dialog[0] = F("Oops!");
dialog[1] = F("");
2022-06-18 18:17:18 -05:00
dialog[2] = F("You can't carry");
2022-06-18 16:54:40 -05:00
dialog[3] = F("more ");
dialog[3] += lookupDrug(sCurrentDrug);
dialog[3] += F("!");
dialogLength = 4;
dialogSmall = false;
sPreviousGameState = sGameState;
sGameState = STATE_INFO_DIALOG;
} else if (pMoney < sDrugPrices[sCurrentDrug]) {
dialog[0] = F("Oops!");
dialog[1] = F("");
dialog[2] = F("You can't afford");
dialog[3] = lookupDrug(sCurrentDrug) + F("!");
dialogLength = 4;
dialogSmall = false;
sPreviousGameState = sGameState;
sGameState = STATE_INFO_DIALOG;
} else {
sGameState = STATE_BUY_QTY_INPUT;
sCurrentQty = 0;
sQtyMax = floor(pMoney / (float)sDrugPrices[sCurrentDrug]);
2022-06-18 18:17:18 -05:00
if (remaining < sQtyMax) sQtyMax = remaining;
2022-06-18 16:54:40 -05:00
}
break;
2022-06-18 18:17:18 -05:00
}
2022-06-18 21:34:58 -05:00
2022-06-18 00:05:30 -05:00
case STATE_SELL_MENU:
2022-06-18 16:54:40 -05:00
sPreviousGameState = STATE_SELL_MENU;
sCurrentDrug = menuSelected;
if (pInventory[sCurrentDrug] > 0) {
sGameState = STATE_SELL_QTY_INPUT;
sCurrentQty = 0;
sQtyMax = pInventory[sCurrentDrug];
} else {
dialog[0] = F("Oops!");
dialog[1] = F("");
dialog[2] = F("You don't have any");
dialog[3] = lookupDrug(sCurrentDrug) + F("!");
dialogLength = 4;
dialogSmall = false;
sPreviousGameState = sGameState;
sGameState = STATE_INFO_DIALOG;
}
break;
2022-06-18 21:34:58 -05:00
2022-06-18 00:05:30 -05:00
case STATE_SHARK_MENU:
2022-06-18 21:34:58 -05:00
sPreviousGameState = STATE_SHARK_MENU;
sCurrentQty = 0;
switch (menuSelected) {
case 0: // repay loan
sGameState = STATE_PAY_LOAN_INPUT;
sQtyMax = pLoanAmount > pMoney ? pMoney : pLoanAmount;
break;
case 1: // borrow money
if (sAlreadyBorrowed) {
dialog[0] = F("You already");
dialog[1] = F("borrowed money");
dialog[2] = F("today!");
dialogLength = 3;
dialogSmall = false;
sGameState = STATE_INFO_DIALOG;
} else {
sGameState = STATE_BORROW_INPUT;
sQtyMax = 5000;
}
break;
}
break;
2022-06-18 00:05:30 -05:00
case STATE_BANK_MENU:
2022-06-18 21:34:58 -05:00
sPreviousGameState = STATE_BANK_MENU;
switch (menuSelected) {
case 0: // deposit money
sGameState = STATE_DEPOSIT_INPUT;
sQtyMax = pMoney;
break;
case 1: // withdraw money
sGameState = STATE_WITHDRAW_INPUT;
sQtyMax = pSavingsAmount;
break;
}
2022-06-18 00:05:30 -05:00
break;
}
}
2022-06-18 16:54:40 -05:00
2022-06-18 18:17:18 -05:00
bool checkBackedOut(const bool eitherButton) {
if (arduboy.justPressed(B_BUTTON) ||
(eitherButton && arduboy.justPressed(A_BUTTON))) {
2022-06-18 16:54:40 -05:00
screenInitialized = false;
sGameState = sPreviousGameState;
return true;
}
return false;
}
2022-06-18 18:17:18 -05:00
int playerCapacityRemaining() {
int qty = 0;
for (int i = 0; i < 6; i++) {
qty += pInventory[i];
}
return pCapacity - qty;
}