xxxxxxxxxx
151
const snds = [],
NUM_SNDS = 2,
imgs_env = [],
NUM_ENV_IMGS = 10,
imgs_bio = [],
NUM_BIO_IMGS = 4,
imgs_dhalia_seq = [],
NUM_DHALIA_IMGS = 110,
imgs_tyler_seq = [],
NUM_TYLER_IMGS = 126,
possibleLivingRooms = ['living-room-1', 'living-room-2', 'living-room-3'],
possibleBedrooms = ['tyler', 'dhalia'],
isBedrooms = [false, false]; //possible room to generate
let playing = false,
isGen_LR = false, //switches to true when room is generated,
btn,
randRoom = 0,
bedroom_0_index = 0,
bedroom_1_index = 0;
let title;
//preload images and audio
function preload() {
//using template literals rather than concatenation (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals)
title = loadImage('assets/images/environment/env7.png');
for (let i = 0; i < NUM_ENV_IMGS; i++) {
imgs_env[i] = loadImage(`assets/images/environment/env${i}.png`);
}
for (let i = 0; i < NUM_BIO_IMGS; i++) {
imgs_bio[i] = loadImage(`assets/images/bio/bio${i}.png`);
}
for (let i = 0; i < NUM_DHALIA_IMGS; i++) {
imgs_dhalia_seq[i] = loadImage(`assets/images/sequences/dhalia/dhalia${i}.png`);
}
for (let i = 0; i < NUM_TYLER_IMGS; i++) {
imgs_tyler_seq[i] = loadImage(`assets/images/sequences/tyler/tyler${i}.png`);
}
for (let i = 0; i < NUM_SNDS; i++) {
snds[i] = loadSound(`assets/audio/snd${i}.mp3`);
}
}
function setup() {
createCanvas(960, 540);
background(220);
image(title, 0, 0, title.width / 4, title.height / 4);
textSize(50);
fill(200, 190, 0);
textFont('Avenir');
textStyle(BOLD);
textAlign(CENTER,CENTER);
text('HOME SWEET HOME', width*0.525, height*0.4);
btn = createButton('Generate Room');
btn.position(width / 2, height * 1.15);
btn.mousePressed(_ => {
generateRoom.call(this, randRoom);
console.log(`generated room ${randRoom}`);
livingRooms(randRoom)
});
snds[0].play();
snds[0].setVolume(0.15);
snds[0].loop();
}
function draw() {
if (isBedrooms[0]) {
bedrooms(possibleBedrooms[0]);
} else if (isBedrooms[1]) {
bedrooms(possibleBedrooms[1]);
}
}
//living room generator
function generateRoom(rand) {
livingRooms(possibleLivingRooms[rand]);
isGen_LR = true;
randRoom = floor(random(3));
}
function livingRooms(room) {
switch (room) {
//everyone
case 'living-room-1':
image(imgs_env[4], 0, 0, imgs_env[4].width / 4, imgs_env[4].height / 4); //living-room-1
image(imgs_env[8], 855, 170, imgs_env[8].width / 4, imgs_env[8].height / 4); //photo-frame
break;
//no one
case 'living-room-2':
image(imgs_env[5], 0, 0, imgs_env[5].width / 4, imgs_env[5].height / 4); //living-room-2
image(imgs_env[2], 131, 235, imgs_env[2].width / 4, imgs_env[2].height / 4); //stairs
break;
//half
case 'living-room-3':
image(imgs_env[6], 0, 0, imgs_env[6].width / 4, imgs_env[6].height / 4); //living-room-3
image(imgs_bio[0], 326, 220, imgs_bio[0].width / 7, imgs_bio[0].height / 7); //bio-adhara
image(imgs_bio[2], 730, 90, imgs_bio[2].width / 7, imgs_bio[2].height / 7); //bio-emi
break;
}
}
//allows bedrooms to be created
function bedrooms(room) {
switch (room) {
case 'tyler':
image(imgs_tyler_seq[bedroom_0_index], 0, 0, imgs_tyler_seq[bedroom_0_index].width, imgs_tyler_seq[tyler_index].height);
bedroom_0_index = (bedroom_0_index + 1) % imgs_tyler_seq.length; //sets index to 0 to loop sequence
image(imgs_bio[3], 670, 150, imgs_bio[3].width / 7, imgs_bio[3].height / 7); //bio-tyler
break;
case 'dhalia':
image(imgs_dhalia_seq[bedroom_1_index], 0, 0, imgs_dhalia_seq[bedroom_1_index].width, imgs_dhalia_seq[dhalia_index].height);
bedroom_1_index = (bedroom_1_index + 1) % imgs_dhalia_seq.length; //sets index to 0 to loop sequence
image(imgs_bio[1], 540, 300, imgs_bio[1].width / 7, imgs_bio[1].height / 7); //bio-dhalia
break;
}
}
function mousePressed() {
let left = dist(mouseX, mouseY, 190, 300);
let right = dist(mouseX, mouseY, 880, 195);
if (isGen_LR) {
if (left < 100 && !isBedrooms[1]) {
tyler_index = 0;
isBedrooms[1] = false;
isBedrooms[0] = true;
} else if (right < 100 && !isBedrooms[0]) {
dhalia_index = 0;
isBedrooms[0] = false;
isBedrooms[1] = true;
} else {
livingRooms(possibleLivingRooms[randRoom]);
isBedrooms[0] = false;
isBedrooms[1] = false;
}
}
}