starting on state management

This commit is contained in:
Rudis Muiznieks 2022-06-18 00:05:30 -05:00
parent d3bc64781b
commit 2f2ae4012e
Signed by: rudism
GPG Key ID: CABF2F86EF7884F9
4 changed files with 191 additions and 15 deletions

37
lookups.ino Normal file
View File

@ -0,0 +1,37 @@
String lookupLocation(GameLocation loc) {
switch (loc) {
case LOC_BRONX:
return F("Bronx");
case LOC_GHETTO:
return F("Ghetto");
case LOC_CENTRAL_PARK:
return F("Central Park");
case LOC_MANHATTEN:
return F("Manhatten");
case LOC_CONEY_ISLAND:
return F("Coney Island");
case LOC_BROOKLYN:
return F("Brooklyn");
default:
return F("");
}
}
String lookupDrug(Drug drug) {
switch (drug) {
case DRUG_COCAINE:
return F("Cocaine");
case DRUG_HEROINE:
return F("Heroine");
case DRUG_ACID:
return F("Acid");
case DRUG_WEED:
return F("Weed");
case DRUG_SPEED:
return F("Speed");
case DRUG_LUDES:
return F("Ludes");
default:
return F("");
}
}

View File

@ -29,9 +29,9 @@ void computeMenuSizes(const bool small, const bool cols,
const int newLen = items[i + col1Count].length();
if (newLen > col2Width) col2Width = newLen;
}
x1 = 66 - round((col1Width + col2Width + 2.5) * (charWidth / 2.0));
x2 = x1 + round((col1Width + 2.5) * charWidth);
y = 40 - round(col1Count * (charHeight / 2.0));
x1 = 66 - round((col1Width + col2Width + 1.75) * (charWidth / 2.0));
x2 = x1 + round((col1Width + 1.75) * charWidth);
y = 42 - round(col1Count * (charHeight / 2.0));
} else { // one column
int colWidth = 0;
for (int i = 0; i < itemCount; i++) {
@ -40,7 +40,7 @@ void computeMenuSizes(const bool small, const bool cols,
}
x1 = 66 - round(colWidth * (charWidth / 2.0));
x2 = -1;
y = 40 - round(itemCount * (charHeight / 2.0));
y = 42 - round(itemCount * (charHeight / 2.0));
}
}

View File

@ -52,12 +52,20 @@ enum DialogType {
DIALOG_AMOUNT
};
const uint8_t PROGMEM spriteDollar[] = {
5, 5,
0x12, 0x15, 0x1f, 0x15, 0x09,
};
/**************/
/* game state */
/**************/
bool screenInitialized;
GameState sGameState;
GameState sPreviousGameState;
int sCurrentDay;
int sDrugPrices[6];
int sRandomEvent;
/****************/
/* player state */
@ -69,7 +77,7 @@ GameLocation pLocation;
int pGuns;
int pCapacity;
int pHealth;
int pInventory[6] = {0};
int pInventory[6];
String menu[8];
int menuLength = 0;
@ -93,43 +101,72 @@ void loop() {
// draw screen if needed
if (!screenInitialized) {
menuSelected = 0;
arduboy.clear();
menuSelected = 0;
switch (sGameState) {
case STATE_TITLE:
arduboy.setCursor(25, 10);
arduboy.print(F("Slangin' v0.9"));
menu[0] = F("Cocaine 00000");
menu[1] = F("Heroine 00000");
menu[2] = F("Acid 0000");
menu[3] = F("Weed 000");
menu[4] = F("Speed 000");
menu[5] = F("Ludes 00");
menuLength = 6;
menu[0] = F("New Game");
menuLength = 1;
menuSmall = false;
menuCols = true;
drawMenu(menuSmall, menuCols, menuLength, menu);
drawMenuIndicator(menuSelected, menuSmall, menuCols, menuLength, menu, false);
break;
case STATE_TURN_MENU:
drawStatusBar();
drawTitle(lookupLocation(pLocation));
menu[0] = F("Trenchcoat");
menu[1] = F("Buy Drugs");
menu[2] = F("Sell Drugs");
menu[3] = F("Jet");
if (pLocation == LOC_BRONX) {
menu[4] = F("Loan Shark");
menu[5] = F("Bank");
menuLength = 6;
} else {
menuLength = 4;
}
menuSmall = false;
menuCols = true;
drawMenu(menuSmall, menuCols, menuLength, menu);
drawMenuIndicator(menuSelected, menuSmall, menuCols, menuLength, menu, false);
break;
case STATE_INVENTORY:
drawStatusBar();
drawTitle(F("Trenchcoat"));
break;
}
screenInitialized = true;
}
// handle user input
arduboy.pollButtons();
switch (sGameState) {
// menu screens
case STATE_TITLE:
case STATE_TURN_MENU:
case STATE_JET_MENU:
case STATE_BUY_MENU:
case STATE_SELL_MENU:
case STATE_FIGHT_MENU:
case STATE_SHARK_MENU:
case STATE_BANK_MENU:
drawMenuIndicator(menuSelected, menuSmall, menuCols, menuLength, menu, true);
int col1Count = round(menuLength / 2.0);
int inCol = menuSelected < col1Count ? 1 : 2;
if (arduboy.justPressed(DOWN_BUTTON)) {
menuSelected++;
if (menuSelected >= menuLength) menuSelected = menuLength - 1;
} else if (arduboy.justPressed(UP_BUTTON)) {
menuSelected--;
if (menuSelected < 0) menuSelected = 0;
} else if (arduboy.justPressed(RIGHT_BUTTON)) {
if (menuCols && inCol == 1) {
menuSelected += col1Count;
@ -137,6 +174,7 @@ void loop() {
menuSelected++;
}
if (menuSelected >= menuLength) menuSelected = menuLength - 1;
} else if (arduboy.justPressed(LEFT_BUTTON)) {
if (menuCols && inCol == 2) {
menuSelected -= col1Count;
@ -144,11 +182,31 @@ void loop() {
menuSelected--;
}
if (menuSelected < 0) menuSelected = 0;
} else if (arduboy.justPressed(A_BUTTON)) {
// advance to next state
handleMenuAction();
} else if (arduboy.justPressed(B_BUTTON)) {
// return to turn menu from the other menus
if (sGameState == STATE_JET_MENU ||
sGameState == STATE_BUY_MENU ||
sGameState == STATE_SELL_MENU ||
sGameState == STATE_SHARK_MENU ||
sGameState == STATE_BANK_MENU) {
sGameState = STATE_TURN_MENU;
}
}
drawMenuIndicator(menuSelected, menuSmall, menuCols, menuLength, menu, false);
break;
case STATE_INVENTORY:
// TODO: not working
if (arduboy.justPressed(B_BUTTON)) {
screenInitialized = false;
sGameState = STATE_TURN_MENU;
}
break;
}
arduboy.display();
@ -157,7 +215,20 @@ void loop() {
/***********************/
/* screen draw heplers */
/***********************/
void drawStatusBar() {
const int chars = log10(pMoney);
const int x = 128 - (chars * 4);
Sprites::drawOverwrite(x - 6, 1, spriteDollar, 0);
arduboy.fillRect(0, 8, 128, 1, WHITE);
font3x5.setCursor(1, 0);
font3x5.print("Day");
font3x5.setCursor(14, 0);
font3x5.print(sCurrentDay);
font3x5.setCursor(x, 0);
font3x5.print(pMoney);
}
void drawTitle(const String title) {
font4x6.setCursor(6, 8);
font4x6.setCursor(6, 11);
font4x6.print(title);
}

68
states.ino Normal file
View File

@ -0,0 +1,68 @@
void initializeNewGame() {
sGameState = STATE_TURN_MENU;
pMoney = 2000;
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
// generate new drug prices
sDrugPrices[DRUG_COCAINE] = rand() % 12001 + 16000;
sDrugPrices[DRUG_HEROINE] = rand() % 7001 + 5000;
sDrugPrices[DRUG_ACID] = (rand() % 35 + 10) * 100;
sDrugPrices[DRUG_WEED] = (rand() % 43 + 33) * 10;
sDrugPrices[DRUG_SPEED] = (rand() % 16 + 7) * 10;
sDrugPrices[DRUG_LUDES] = (rand() % 5 + 1) * 10;
}
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:
switch (menuSelected) {
case 0: // trenchcoat
sGameState = STATE_INVENTORY;
break;
case 1: // buy drugs
case 2: // sell drugs
case 3: // jet
case 4: // loan shark
case 5: // bank
break;
}
break;
case STATE_JET_MENU:
case STATE_BUY_MENU:
case STATE_SELL_MENU:
case STATE_SHARK_MENU:
case STATE_BANK_MENU:
break;
}
}