xxxxxxxxxx
160
//background music source: https://freesound.org/people/Seth_Makes_Sounds/sounds/701610/
let numParticles = 200;
let nightParticles;
let portConnected = false;
let showStartScreen = true;
let font, font2;
let bgMusic;
let musicPlaying = false;
//variables to store data from Arduino board
let redAction = 0;
let blueAction = 0;
let yellowAction = 0;
let greenAction = 0;
//flags to check if the action was performed
let redActionPerformed = false;
let yellowActionPerformed = false;
let greenActionPerformed = false;
let blueActionPerformed = false;
let lastInteractionTime = 0;
const idleTimeThreshold = 60000;
let amp;
//start screen for user
function startScreen() {
textFont(font);
textSize(80);
fill("white");
let message = "StarScape";
// console.log(width, height);
let textW = textWidth(message);
let textH = textAscent() + textDescent();
let centerX = width / 2;
let centerY = height / 2;
// Set text alignment to center and display the text
textAlign(CENTER, CENTER);
text(message, centerX, centerY);
textFont(font2);
textSize(20);
let captionX = centerX;
let captionY = centerY + 80;
text("Click anywhere to begin. Click again to restart.", captionX, captionY);
}
function preload() {
soundFormats("mp3", "ogg");
bgMusic = loadSound("/sounds/bgMusic.mp3");
font = loadFont("fonts/NewYork.otf");
font2 = loadFont("fonts/AppleGaramond-LightItalic.ttf");
}
function setup() {
//responsive canvas set to the dimensions of the window
createCanvas(windowWidth, windowHeight);
//initialize particles
nightParticles = new Galaxy();
}
function draw() {
background("#0f0f0f");
if (showStartScreen) {
startScreen();
} else {
//if red button pressed, particles jitter + scatter
if (redAction && !redActionPerformed) {
nightParticles.jitterParticles();
redActionPerformed = true;
//if yellow button is pressed, particles cluster together
} else if (yellowAction && !yellowActionPerformed) {
nightParticles.jitterParticlesTowardsCenter();
yellowActionPerformed = true;
//if the green button is pressed, particles form a heart
} else if (greenAction && !greenActionPerformed) {
nightParticles.moveParticlesInHeart();
greenActionPerformed = true;
// if blue button, the particles form a spiral
} else if (blueAction && !blueActionPerformed) {
nightParticles.moveParticlesInSpiral();
blueActionPerformed = true;
}
//the particles are continuously rotating, with a speed proportional to the amplitude of the music
let vol = amp.getLevel();
let pSpeed = map(vol, 0, 1, 0, 0.005);
nightParticles.rotateGalaxy(pSpeed);
nightParticles.drawGalaxy();
}
}
function readSerial(data) {
////////////////////////////////////
//READ FROM ARDUINO HERE
////////////////////////////////////
if (data != null) {
// make sure there is actually a message
// split the message
let fromArduino = split(trim(data), ",");
// console.log(fromArduino);
// if the right length, then proceed
if (fromArduino.length == 4) {
// only store values here
// do everything with those values in the main draw loop
// We take the string we get from Arduino and explicitly
// convert it to a number by using int()
redAction = int(fromArduino[0]);
yellowAction = int(fromArduino[1]);
greenAction = int(fromArduino[2]);
blueAction = int(fromArduino[3]);
//reset the actionsPerformed to false everytime data is read from the arduino
redActionPerformed = false;
yellowActionPerformed = false;
greenActionPerformed = false;
blueActionPerformed = false;
}
//////////////////////////////////
//SEND TO ARDUINO HERE (handshake)
//////////////////////////////////
let sendToArduino = "\n";
writeSerial(sendToArduino);
}
}
function mousePressed() {
//first, connnect the port
if (!musicPlaying) {
//loop the music
console.log("here");
bgMusic.loop();
amp = new p5.Amplitude();
musicPlaying = true;
}
if (!portConnected) {
setUpSerial();
portConnected = true;
} else {
//after port is connected, use mouse press to start (and restart)
showStartScreen = !showStartScreen;
//if the game is restarting, create a new instance of Galaxy class
if (showStartScreen) {
nightParticles = new Galaxy();
}
}
}