#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// PIN KONFIGURASI UNTUK ESP32
#define BTN_LEFT 12
#define BTN_RIGHT 14
#define BTN_DOOR 27
#define BUZZER_PIN 26
// Game Variables
int cameraIndex = 0;
bool doorClosed = false;
int power = 100;
int hour = 0;
int animatronicPos = 0; // 0=ShowStage, 1=Dining, 2=Hallway, 3=Office
bool gameOver = false;
unsigned long lastAnimMove = 0;
unsigned long lastTick = 0;
const char* rooms[] = {"Show", "Dining", "Hallway", "Office"};
void setup() {
pinMode(BTN_LEFT, INPUT_PULLUP);
pinMode(BTN_RIGHT, INPUT_PULLUP);
pinMode(BTN_DOOR, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
Serial.begin(115200);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("OLED init failed");
while (true);
}
display.clearDisplay();
display.display();
randomSeed(analogRead(0));
}
void drawUI() {
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.print("Cam: ");
display.print(rooms[cameraIndex]);
display.setCursor(0, 10);
display.print("Anima: ");
display.print(rooms[animatronicPos]);
display.setCursor(0, 20);
display.print("Door: ");
display.print(doorClosed ? "CLOSED" : "OPEN");
display.setCursor(0, 30);
display.print("Power: ");
display.print(power);
display.print("%");
display.setCursor(0, 40);
display.print("Time: ");
display.print(hour == 0 ? 12 : hour);
display.print("AM");
if (gameOver) {
display.setTextSize(2);
display.setCursor(10, 52);
display.print("GAME OVER");
}
display.display();
}
void checkInput() {
static bool lastLeft = HIGH, lastRight = HIGH, lastDoor = HIGH;
bool left = digitalRead(BTN_LEFT);
bool right = digitalRead(BTN_RIGHT);
bool door = digitalRead(BTN_DOOR);
if (left == LOW && lastLeft == HIGH) {
cameraIndex = (cameraIndex - 1 + 3) % 3;
power -= 1;
}
if (right == LOW && lastRight == HIGH) {
cameraIndex = (cameraIndex + 1) % 3;
power -= 1;
}
if (door == LOW && lastDoor == HIGH) {
doorClosed = !doorClosed;
power -= 2;
}
lastLeft = left;
lastRight = right;
lastDoor = door;
}
void moveAnimatronic() {
if (millis() - lastAnimMove > 4000 && !gameOver) {
lastAnimMove = millis();
int moveChance = random(100);
if (moveChance < 60 && animatronicPos < 3) {
animatronicPos++;
} else if (moveChance > 80 && animatronicPos > 0) {
animatronicPos--;
}
if (animatronicPos == 3 && !doorClosed) {
gameOver = true;
tone(BUZZER_PIN, 1000, 1500);
}
}
}
void updateClock() {
if (millis() - lastTick > 1000 && !gameOver) {
lastTick = millis();
static int seconds = 0;
seconds++;
if (seconds % 5 == 0 && hour < 6) hour++;
if (power > 0) {
power--;
} else {
gameOver = true;
tone(BUZZER_PIN, 600, 1000);
}
}
}
void loop() {
checkInput();
updateClock();
moveAnimatronic();
drawUI();
if (hour >= 6 && !gameOver) {
gameOver = true;
tone(BUZZER_PIN, 1200, 1000);
display.setCursor(30, 52);
display.print("YOU WIN");
display.display();
}
delay(100);
}