slangin/slangin.ino

286 lines
6.8 KiB
Arduino
Raw Normal View History

2022-06-15 12:47:42 -05:00
/*
2022-06-15 12:51:24 -05:00
Slangin'
Author: Rudis Muiznieks
License: WTFPL
2022-06-15 12:47:42 -05:00
*/
#include <Arduboy2.h>
2022-06-17 18:55:36 -05:00
#include "src/fonts/Font3x5.h"
#include "src/fonts/Font4x6.h"
2022-06-15 12:47:42 -05:00
Arduboy2 arduboy;
2022-06-16 23:25:49 -05:00
Font4x6 font4x6 = Font4x6();
Font3x5 font3x5 = Font3x5();
2022-06-15 12:47:42 -05:00
2022-06-15 18:36:12 -05:00
enum GameState {
STATE_TITLE = 0,
STATE_TURN_MENU,
STATE_JET_MENU,
STATE_BUY_MENU,
STATE_SELL_MENU,
STATE_FIGHT_MENU,
STATE_SHARK_MENU,
STATE_BANK_MENU,
2022-06-18 12:24:23 -05:00
STATE_INVENTORY,
2022-06-15 18:36:12 -05:00
STATE_INFO_DIALOG,
STATE_QUESTION_DIALOG,
STATE_AMOUNT_DIALOG,
STATE_SUBWAY
};
enum GameLocation {
LOC_BRONX = 0,
LOC_GHETTO,
LOC_CENTRAL_PARK,
LOC_MANHATTEN,
LOC_CONEY_ISLAND,
LOC_BROOKLYN
};
enum Drug {
DRUG_COCAINE = 0,
DRUG_HEROINE,
DRUG_ACID,
DRUG_WEED,
DRUG_SPEED,
DRUG_LUDES
};
enum DialogType {
DIALOG_INFO = 0,
DIALOG_YESNO,
DIALOG_AMOUNT
};
2022-06-18 00:05:30 -05:00
const uint8_t PROGMEM spriteDollar[] = {
5, 5,
0x12, 0x15, 0x1f, 0x15, 0x09,
};
2022-06-16 23:25:49 -05:00
/**************/
/* game state */
/**************/
2022-06-15 18:36:12 -05:00
bool screenInitialized;
2022-06-16 23:25:49 -05:00
GameState sGameState;
2022-06-18 00:05:30 -05:00
GameState sPreviousGameState;
2022-06-15 18:36:12 -05:00
int sCurrentDay;
2022-06-18 00:05:30 -05:00
int sDrugPrices[6];
int sRandomEvent;
2022-06-16 23:25:49 -05:00
/****************/
/* player state */
/****************/
2022-06-15 18:36:12 -05:00
int pMoney;
int pLoanAmount;
int pSavingsAmount;
GameLocation pLocation;
int pGuns;
int pCapacity;
int pHealth;
2022-06-18 00:05:30 -05:00
int pInventory[6];
2022-06-15 18:36:12 -05:00
2022-06-17 18:55:36 -05:00
String menu[8];
int menuLength = 0;
bool menuCols = false;
bool menuSmall = false;
int menuSelected = 0;
2022-06-16 23:25:49 -05:00
/***********************/
/* game setup and loop */
/***********************/
2022-06-15 12:47:42 -05:00
void setup() {
2022-06-16 23:25:49 -05:00
sGameState = STATE_TITLE;
2022-06-15 18:36:12 -05:00
screenInitialized = false;
2022-06-15 12:47:42 -05:00
arduboy.begin();
arduboy.setFrameRate(15);
}
void loop() {
if (!(arduboy.nextFrame()))
return;
2022-06-18 12:24:23 -05:00
arduboy.pollButtons();
2022-06-17 18:55:36 -05:00
// draw screen if needed
2022-06-15 18:36:12 -05:00
if (!screenInitialized) {
2022-06-16 23:25:49 -05:00
arduboy.clear();
2022-06-18 00:05:30 -05:00
menuSelected = 0;
2022-06-16 23:25:49 -05:00
switch (sGameState) {
2022-06-15 18:36:12 -05:00
case STATE_TITLE:
arduboy.setCursor(25, 10);
arduboy.print(F("Slangin' v0.9"));
2022-06-18 00:05:30 -05:00
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;
2022-06-17 18:55:36 -05:00
menuCols = true;
drawMenu(menuSmall, menuCols, menuLength, menu);
2022-06-17 18:55:36 -05:00
drawMenuIndicator(menuSelected, menuSmall, menuCols, menuLength, menu, false);
2022-06-15 18:36:12 -05:00
break;
2022-06-18 00:05:30 -05:00
case STATE_INVENTORY:
drawStatusBar();
drawTitle(F("Trenchcoat"));
2022-06-18 12:24:23 -05:00
buildDrugMenu(pInventory);
drawMenu(menuSmall, menuCols, menuLength, menu);
2022-06-18 00:05:30 -05:00
break;
2022-06-15 18:36:12 -05:00
}
2022-06-17 18:55:36 -05:00
screenInitialized = true;
}
// handle user input
2022-06-18 12:24:23 -05:00
// menu screens
2022-06-17 18:55:36 -05:00
switch (sGameState) {
case STATE_TITLE:
2022-06-18 00:05:30 -05:00
case STATE_TURN_MENU:
case STATE_JET_MENU:
case STATE_BUY_MENU:
case STATE_SELL_MENU:
case STATE_FIGHT_MENU:
case STATE_SHARK_MENU:
2022-06-18 12:24:23 -05:00
case STATE_BANK_MENU: {
if (arduboy.justPressed(UP_BUTTON) ||
arduboy.justPressed(DOWN_BUTTON) ||
arduboy.justPressed(LEFT_BUTTON) ||
arduboy.justPressed(RIGHT_BUTTON) ||
arduboy.justPressed(A_BUTTON) ||
arduboy.justPressed(B_BUTTON)) {
drawMenuIndicator(menuSelected, menuSmall, menuCols, menuLength, menu, true);
int col1Count = round(menuLength / 2.0);
int inCol = menuSelected < col1Count ? 1 : 2;
if (arduboy.justPressed(DOWN_BUTTON)) {
2022-06-17 18:55:36 -05:00
menuSelected++;
2022-06-18 12:24:23 -05:00
if (menuSelected >= menuLength) menuSelected = menuLength - 1;
2022-06-18 00:05:30 -05:00
2022-06-18 12:24:23 -05:00
} else if (arduboy.justPressed(UP_BUTTON)) {
2022-06-17 18:55:36 -05:00
menuSelected--;
2022-06-18 12:24:23 -05:00
if (menuSelected < 0) menuSelected = 0;
} else if (arduboy.justPressed(RIGHT_BUTTON)) {
if (menuCols && inCol == 1) {
menuSelected += col1Count;
} else {
menuSelected++;
}
if (menuSelected >= menuLength) menuSelected = menuLength - 1;
2022-06-18 00:05:30 -05:00
2022-06-18 12:24:23 -05:00
} else if (arduboy.justPressed(LEFT_BUTTON)) {
if (menuCols && inCol == 2) {
menuSelected -= col1Count;
} else {
menuSelected--;
}
if (menuSelected < 0) menuSelected = 0;
2022-06-18 00:05:30 -05:00
2022-06-18 12:24:23 -05:00
} else if (arduboy.justPressed(A_BUTTON)) {
2022-06-18 00:05:30 -05:00
2022-06-18 12:24:23 -05:00
// 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;
}
2022-06-18 00:05:30 -05:00
}
2022-06-18 12:24:23 -05:00
drawMenuIndicator(menuSelected, menuSmall, menuCols, menuLength, menu, false);
2022-06-17 18:55:36 -05:00
}
break;
2022-06-18 12:24:23 -05:00
}
case STATE_INVENTORY: {
2022-06-18 00:05:30 -05:00
if (arduboy.justPressed(B_BUTTON)) {
screenInitialized = false;
sGameState = STATE_TURN_MENU;
}
break;
2022-06-18 12:24:23 -05:00
}
2022-06-16 23:25:49 -05:00
}
2022-06-17 18:55:36 -05:00
arduboy.display();
2022-06-16 23:25:49 -05:00
}
/***********************/
/* screen draw heplers */
/***********************/
2022-06-18 00:05:30 -05:00
void drawStatusBar() {
2022-06-18 12:24:23 -05:00
if (sGameState == STATE_INVENTORY) {
const int chars = numberChars(pCapacity);
const int x = 128 - ((chars + 9) * 4);
font3x5.setCursor(1, 0);
font3x5.print("Guns ");
font3x5.print(pGuns);
font3x5.setCursor(x, 0);
font3x5.print("Capacity ");
font3x5.print(pCapacity);
} else {
const int chars = numberChars(pMoney);
const int x = 128 - (chars * 4);
Sprites::drawOverwrite(x - 6, 1, spriteDollar, 0);
font3x5.setCursor(1, 0);
font3x5.print("Day");
font3x5.setCursor(14, 0);
font3x5.print(sCurrentDay);
font3x5.setCursor(x, 0);
font3x5.print(pMoney);
}
2022-06-18 00:05:30 -05:00
arduboy.fillRect(0, 8, 128, 1, WHITE);
}
2022-06-17 08:58:56 -05:00
void drawTitle(const String title) {
2022-06-18 12:24:23 -05:00
font4x6.setCursor(6, 12);
font4x6.print(title);
2022-06-15 12:47:42 -05:00
}
2022-06-18 12:24:23 -05:00
void buildDrugMenu(int extra[6]) {
int lengths[6];
int col1Max = 0;
int col2Max = 0;
for (int i = 0; i < 6; i++) {
int extraLen = numberChars(extra[i]);
lengths[i] = lookupDrug(i).length() + 1 + extraLen;
if (i < 3 && lengths[i] > col1Max) col1Max = lengths[i];
if (i >= 3 && lengths[i] > col2Max) col2Max = lengths[i];
}
for (int i = 0; i < 6; i++) {
int colMax = i < 3 ? col1Max : col2Max;
menu[i] = lookupDrug(i) + F(" ");
for (int s = 0; s < colMax - lengths[i]; s++) {
menu[i] = menu[i] + F(" ");
}
menu[i] = menu[i] + extra[i];
}
menuLength = 6;
menuCols = true;
menuSmall = col1Max + col2Max > 22;
}
int numberChars(int num) {
int length = 1;
while (num /= 10) length++;
return length;
}