xxxxxxxxxx
314
//INTRODUCTION TO IM FALL 2023//
//WITH PROFESSOR MICHAEL SHILOH
///////////////////////DECLARATIONS OF VARIABLES//////////////
//declaring scenes array
let scenes = [];
let currentScene;
let gifImg;
//scenes
let loading,
instructions,
alarm,
breakfast,
d2,
end_party,
end_skip,
end_sleep,
end_snooze,
go_party,
goclass,
goorskip,
partybreak,
riddle,
sleeporparty;
//sound
let water, lights, gamesong, anthem;
//serialcomm
let serial;
//buttons:
let playButtonState, button1State, button2State;
///////////////PRELOADING/////////////////////
function preload() {
//Images
loading = loadImage("loading.png");
instructions = loadImage("instructions.png");
alarm = loadImage("alarm.png");
breakfast = loadImage("breakfast.png");
d2 = loadImage("d2.png");
end_party = loadImage("end_party.png");
end_skip = loadImage("end_skip.png");
end_sleep = loadImage("end_sleep.png");
end_snooze = loadImage("end_snooze.png");
go_party = loadImage("go_party.png");
goclass = loadImage("goclass.png");
goorskip = loadImage("goorskip.png");
partybreak = loadImage("partybreak.png");
riddle = loadImage("riddle.png");
sleeporparty = loadImage("sleeporparty.png");
//Sounds
soundFormats("mp3");
water = loadSound("water.mp3");
lights = loadSound("lights.mp3");
gamesong = loadSound("gamesong.mp3");
anthem = loadSound("anthem.mp3");
}
//////////////////////////SCENE CLASS///////////////////////
class Scene {
constructor(image) {
this.image = image;
}
display() {
image(this.image, 0, 0);
}
}
//////////////////////////SETUP FUNCTION//////////////////////
function setup() {
createCanvas(2000, 1000);
//gamesong.play();
//gamesong.loop();
//Defining scenes
//Push scene 0
scenes.push(new Scene(loading));
//Push scene 1
scenes.push(new Scene(instructions));
//Push scene 2
scenes.push(new Scene(alarm));
//Push scene 3
scenes.push(new Scene(d2));
//Push scene 4
scenes.push(new Scene(breakfast));
//Push scene 5
scenes.push(new Scene(end_snooze));
//Push scene 6
scenes.push(new Scene(goorskip));
//Push scene 7
scenes.push(new Scene(end_skip));
//Push scene 8
scenes.push(new Scene(goclass));
//Push scene 9
scenes.push(new Scene(sleeporparty));
//Push scene 10
scenes.push(new Scene(end_sleep));
//Push scene 11
scenes.push(new Scene(go_party));
//Push scene 12
scenes.push(new Scene(partybreak));
//Push scene 13
scenes.push(new Scene(end_party));
//start with the first scene
currentScene = scenes[0];
}
///////////////////DRAW FUNCTION/////////////////////////
function draw() {
clear(); // Clear the canvas
if (currentScene) {
currentScene.display();
}
// Connect to the serial port
if (!serialActive) {
text("Press 'p' key to select Serial Port", 20, 30);
console.log("key pressed")
} else {
text("Connected", 20, 30);
}
}
/////////////////KEYPRESSED FUNCTION/////////////////////
function keyPressed()
{
if (key === 'p') { // If 'p' is pressed, set up serial communication
setUpSerial(); // Function to set up serial communication
}
// CONTROLLED BY BUTTON 1: PLAY
if (playButtonState === 1) {
if (currentScene === 0) {
sceneChange(1);
}
if (currentScene === 1) {
sceneChange(2);
}
}
//CONTROLLED BY 2 CHOICES BUTTONS
switch (currentScene) {
//LOADING
//ALARM
case scenes[2]:
//Alarm GIF
gifImg = createImg("_.gif");
gifImg.size(200, 200);
// Center the GIF image
let x = (width - gifImg.width) / 2;
let y = (height - gifImg.height) / 2;
gifImg.position(x, y);
// CHOICE 1: WAKE UP
if (button1State === 1) sceneChange(3); // THE USER WILL GO TO D2
// Choice 2: SNOOZE
if (button2State === 1) sceneChange(5); // END OF STORY 1
break;
//D2
case scenes[3]:
if (button1State === 1) sceneChange(4); //END OF STORY 1
break;
//BREAKFAST:
//BOTH CHOICES LEAD TO THE SAME SCENE: D2. HERE THE USER MAKES A BREAKFAST CHOICE => BOTH CHOICES HAVE SAME OUTCOME
case scenes[4]:
//CHOICE 1: ACAI
if (button1State === 1) sceneChange(6); //SAME OUTCOME=DILEMNA
//CHOICE 2: SMOOTHIE
if (button2State === 1) sceneChange(6); //SAME OUTCOME=DILEMNA
break;
//DILEMNA SCENE: SHOULD NYUAD KID GO/ SKIP CLASS?
case scenes[6]:
gif2Img = createImg("gif2.gif");
gif2Img.size(200, 200);
// Center the GIF image
x = (width - gif2Img.width) / 2;
y = (height - gif2Img.height) / 2;
gif2Img.position(x, y);
//GO TO CLASS
if (button1State === 1) sceneChange(8); //THE OUTCOME IS GOING TO CLASS
//SKIP CLASS
if (button2State === 1) sceneChange(7); //END OF STORY 2
break;
//GO TO CLASS SCENE
case scenes[8]:
//HERE WE DON'T HAVE CHOICES. A QUICK ANIMATION WITH AN ANECDONE. THE USER PRESSES THE BUTTON TO CONTINUE TO NEXT SCENE = ANOTHER DILEMNA
if (button1State === 1) sceneChange(9);
break;
//DILEMNA 2: NYUAD KID IS EXHAUSTED! SLEEP OR PARTY?
case scenes[9]:
//GO TO PARTY
if (button1State === 1) sceneChange(11); //THE STORY CONTINUES IN THE PARTY
//SLEEP
if (button2State === 1) sceneChange(10); //END OF STORY 3
break;
case scenes[11]:
//IN THE PARTY
//HERE, CLICKING ON ANY OF THE TWO MUSIC CHOICES LEADS TO THE SAME SCENE = PARTY BREAK
if (button1State === 1) sceneChange(12);
if (button2State === 1) sceneChange(12);
break;
//PARTY BREAK
case scenes[12]:
if (button1State === 1) sceneChange(13); //END OF STORY 4
break;
}
// CONTROLLED BY PLAY BUTTON
// Restarting
if (playButtonState === 1) {
if (
currentScene === 4 ||
currentScene === 7 ||
currentScene === 10 ||
currentScene === 13
) {
// Go back to main menu
sceneChange(0);
gamesong.stop();
}
}
// CONTROLLED BY CHOICES BUTTONS AS WELL
// Playing music choices
if (currentScene === 11 && button1State === 1) {
gamesong.stop();
water.play();
}
if (currentScene === 11 && button2State === 1) {
gamesong.stop();
lights.play();
}
}
/////////////////////////SCENE CHANGE FUNCTION////////////////
function sceneChange(nextSceneIndex) {
console.log("Changing scene to:", nextSceneIndex);
currentSceneIndex = nextSceneIndex - 1;
currentScene = scenes[nextSceneIndex];
}
/////////////////////SERIAL DATA FUNCTION//////////////////////
//This story is controlled by 3 physical switches: play, 1 & 2.
function readSerial(data) {
if (data != null) {
let fromArduino = split(trim(data), ",");
print(fromArduino);
if (fromArduino.length === 3) {
let playButtonState = int(fromArduino[0]);
let button1State = int(fromArduino[1]);
let button2State = int(fromArduino[2]);
// Handle the playButtonState
if (playButtonState == 1) {
console.log("Play button pressed!");
}
// Handle button1State
if (button1State == 1) {
console.log("Button 1 pressed!");
}
// Handle button2State
if (button2State == 1) {
console.log("Button 2 pressed!");
}
}
}
}
/////////////////////////ARDUINO CODE HERE//////////////////////
// const int playbuttonPin = 2; // Pin for the re/play button
// const int button1Pin = 3; // Pin for choice button 1
// const int button2Pin = 4; // Pin for choice button 2
// int playbuttonState = 0; // State of the play button
// int button1State = 0; // State of choice button 1
// int button2State = 0; // State of choice button 2
// void setup() {
// pinMode(playbuttonPin, INPUT); // Set play button pin as input
// pinMode(button1Pin, INPUT); // Set button 1 pin as input
// pinMode(button2Pin, INPUT); // Set button 2 pin as input
// Serial.begin(9600); // Initialize serial communication
// START THE HANDSHAKE
// while (Serial.available() <= 0) {
// digitalWrite(LED_BUILTIN, HIGH); // on/blink while waiting for serial data
// Serial.println("0,0,0,0,0,0"); // send a starting message
// delay(300); // wait 1/3 second
// digitalWrite(LED_BUILTIN, LOW);
// delay(50);
// }
// }
// void loop() {
// playbuttonState = digitalRead(playbuttonPin); // Read state of play button
// button1State = digitalRead(button1Pin); // Read state of button 1
// button2State = digitalRead(button2Pin); // Read state of button 2
// Send the button states to p5.js
// Serial.print(playbuttonState);
// Serial.print(",");
// Serial.print(button1State);
// Serial.print(",");
// Serial.print(button2State);
// Serial.println(); // End the line
// delay(100); // Delay to avoid issues
// }