Skip to content

Instantly share code, notes, and snippets.

@jaimefps
Created May 15, 2019 04:54
Show Gist options
  • Save jaimefps/d744ec1cedcbd61a63fed7537be0e43a to your computer and use it in GitHub Desktop.
Save jaimefps/d744ec1cedcbd61a63fed7537be0e43a to your computer and use it in GitHub Desktop.
#include <Adafruit_RGBLCDShield.h>
// globals:
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
int counter = 0;
int currentButtons;
int currentPlayer = 1;
int currentStat = 0;
int cursor = 0;
int lastButton;
unsigned long lastRender = 0;
void setup() {
lcd.begin(16, 2);
lcd.cursor();
// debugger setup:
Serial.begin(9600);
}
class PlayerState {
public:
int aember = 0;
int chains = 0;
int keys = 0;
int getValueChange(int currentValue, int dir, int maximum, int minimum) {
if (currentValue == minimum && dir == -1) return 0;
if (currentValue == maximum && dir == 1) return 0;
return dir;
}
void changeStat (int stat, int dir) {
if (stat == 0) chains += getValueChange(chains, dir, 24, 0);
if (stat == 1) aember += getValueChange(aember, dir, 99, 0);
if (stat == 2) keys += getValueChange(keys, dir, 3, 0);
}
String getCurrentPenalty() {
if (0 < chains && chains <= 6) return ":-1";
if (6 < chains && chains <= 12) return ":-2";
if (12 < chains && chains <= 18) return ":-3";
if (18 < chains) return ":-4";
return ":0 ";
}
void render (int column) {
// print chains:penalty
lcd.setCursor(column, 0);
if (chains < 10) lcd.print(" ");
lcd.print(chains);
lcd.print(getCurrentPenalty());
// print aember:keys
lcd.setCursor(column, 1);
if (aember < 10) lcd.print(" ");
lcd.print(aember);
lcd.print(":");
lcd.print(keys);
}
};
PlayerState player1;
PlayerState player2;
void renderCursor(int column) {
if (currentStat == 0) lcd.setCursor(1 + column, 0);
if (currentStat == 1) lcd.setCursor(1 + column, 1);
if (currentStat == 2) lcd.setCursor(3 + column, 1);
}
void renderLabels() {
lcd.setCursor(6,0);
lcd.print("ch:p");
lcd.setCursor(6,1);
lcd.print("ae:k");
}
void renderGame() {
player1.render(0);
player2.render(11);
renderLabels();
int offset = currentPlayer == 1 ? 0 : 11;
renderCursor(offset);
}
void changeStat(int dir) {
if (currentPlayer == 1) player1.changeStat(currentStat, dir);
else player2.changeStat(currentStat, dir);
}
void handleKeyForge() {
if (currentPlayer == 1 && player1.aember >= 6) {
player1.keys++;
player1.aember -= 6;
}
if (currentPlayer == 2 && player2.aember >= 6) {
player2.keys++;
player2.aember -= 6;
}
}
void loop() {
int currentButtons = lcd.readButtons();
if (currentButtons != lastButton) {
if (currentButtons & BUTTON_SELECT) {
currentStat = 0;
currentPlayer = currentPlayer == 1 ? 2 : 1;
handleKeyForge();
}
if (currentButtons & BUTTON_LEFT) {
currentStat--;
if (currentStat < 0) currentStat = 2;
}
if (currentButtons & BUTTON_RIGHT) {
currentStat++;
currentStat %= 3;
}
if (currentButtons & BUTTON_DOWN) changeStat(-1);
if (currentButtons & BUTTON_UP) changeStat(1);
}
lastButton = currentButtons;
unsigned long currentTime = millis();
if (currentTime - lastRender > 300) {
lastRender = currentTime;
renderGame();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment