xxxxxxxxxx
356
//The "gamestate" is the director, it dictates where the game is at the moment.
//Therefore, the following numbers represent where the player is:
//0 = Start Menu
//1 = Instructions
//2 = Game
//3 = Confirmation to return to start menu
let gamestate = 2;
//General scenic variables;
let menu;
let ui;
//Individual items for the scenario, and their variables...
let scenario_table;
//Interactable variables;
let glasses = [];
let cubes = [];
//Audio variables;
let music;
let sfx;
//Image variables;
let tableimg;
let glassimg;
let cubeimg;
//Font variables;
let font1;
let font2;
let font3;
//Value storing.
let mouse;
function preload() {
//Nothing to preload at the moment.
}
function setup() {
createCanvas(800, 600);
//Starting variables.//
//a. Menu
startmenu = new startMenu();
//b. UI
ui = new UI();
//c. Scenario items
scenario_table = new Table(90, height / 1.1, width - 180, height); //Table covers almost everything.
//d. Mouse value storing.
mouse = new Mouse();
}
function draw() {
background(220);
//Check where the game is right now.
//Show start menu.
if (gamestate == 0) {
menuandinstructions(); //Display main menu.
if (mouseIsPressed) {
if (frameCount % 10 == 0) {
gamestate = 1;
}
}
} else if (gamestate == 1) {
startmenu.instructions(); //Show a made image with p5js text.
if (mouseIsPressed) {
//Avoid multiple presses in one click with frameCount.
if (frameCount % 10 == 0) {
gamestate = 2;
}
}
} else if (gamestate == 2) {
game();
}
}
function menuandinstructions() {
if (gamestate == 0) {
startmenu.display();
}
if (gamestate == 1) {
startmenu.instructions();
}
}
function game() {
//Just in case, check if cubes and glasses do not exist.
if (glasses.length == 0) {
newglass = new Glass(100, 150, 50, 70);
glasses.push(newglass);
}
//Display scenic items.
scenario_table.display();
// DISPLAY INTERACTABLE ITEMS. //
//Display cubes and apply collision and gravity.
for (let i = 0; i < cubes.length; i++) {
cubes[i].display();
//We check collition with the cubes.
if (i >= 0) {
for (let c = cubes.length - 1; c >= 0; c--) {
//Just in case we find the same object in the array.
if (cubes[i] == cubes[c]) {
//This is useful for both the first object, and to also determine if the object should fall from the table.
if (
cubes[c].x + cubes[c].w < scenario_table.x ||
cubes[c].x > scenario_table.x + scenario_table.w
) {
cubes[c].tempy = 1000; //Send to deletion.
}
continue;
} else {
// All of this was unnecesary..
/*if (
cubes[i].x <= cubes[c].x + cubes[c].w &&
cubes[i].x + cubes[i].w >= cubes[c].x
) {
if (cubes[c].type == "glass") {
if (cubes[i].tempy > cubes[c].y && cubes[c].stop == 1) {
//print("it works.");
//cubes[i].tempy = cubes[c].y + cubes[c].h;
}
}
if (cubes[c].type == "ice") {
if (cubes[i].tempy > cubes[c].y && cubes[c].stop == 1) {
//print("it works for cubes.");
//cubes[i].tempy = cubes[c].y;
}
}
} */
//Falling again due to collisions. Another for is necessary in order to keep the cubes falling in order and not traspassing with each other.
// Check collitions //
// Check collitions for ice cubes//
//Check collition in X axis.
if (
cubes[i].y + cubes[i].h > cubes[c].y &&
cubes[i].y < cubes[c].y + cubes[c].h
) {
if (
cubes[i].x + cubes[i].w > cubes[c].x &&
cubes[i].x + cubes[i].w < cubes[c].x + 15
) {
cubes[c].hit_x_r();
} else if (
cubes[i].x < cubes[c].x + cubes[c].w &&
cubes[i].x > cubes[c].x + cubes[c].w - 15
) {
cubes[c].hit_x_l();
}
}
//Check collition in Y axis.
if (
cubes[i].x < cubes[c].x + cubes[c].w &&
cubes[i].x + cubes[i].w > cubes[c].x
) {
if (
cubes[i].y + cubes[i].h > cubes[c].y &&
cubes[i].y + cubes[i].h < cubes[c].y + 15
) {
cubes[c].hit_y_d();
} else if (
cubes[i].y > cubes[c].y &&
cubes[i].y < cubes[c].y + cubes[c].h + 1
) {
cubes[c].hit_y_u();
}
}
//Check collition with table.
if (
cubes[c].y + cubes[c].h > scenario_table.y &&
cubes[c].y + cubes[c].h < scenario_table.y + 60 &&
cubes[c].x + cubes[c].w > scenario_table.x &&
cubes[c].x < scenario_table.x + scenario_table.w
) {
cubes[c].hit_y_u();
} else {
cubes[c].tempy = 1000; //Send to deletion.
}
}
}
//If grabbed, revaluate where to put the cube.
if (cubes[i].isgrabbed == 1) {
//Collisions with the top of the cubes.
for (let c = cubes.length - 1; c >= 0; c--) {
//Just in case we find the same object in the array.
if (cubes[i] == cubes[c]) {
continue;
} else {
// All of this was unnecesary..
/* if (
cubes[i].x <= cubes[c].x + cubes[c].w &&
cubes[i].x + cubes[i].w >= cubes[c].x
) {
if (cubes[c].type == "glass") {
if (cubes[i].tempy > cubes[c].y && cubes[c].stop == 1) {
//print("it works for glass.");
//cubes[i].tempy = cubes[c].y + cubes[c].h;
}
}
if (cubes[c].type == "ice") {
if (cubes[i].tempy > cubes[c].y && cubes[c].stop == 1) {
//print("it works for cubes.");
//cubes[i].tempy = cubes[c].y;
}
}
} */
//Falling again due to collisions. Another for is necessary in order to keep the cubes falling in order and not traspassing with each other.
// Check collitions //
// Check for ice cubes //
if (
cubes[i].y + cubes[i].h >= cubes[c].y &&
cubes[i].y <= cubes[c].y + cubes[c].h
) {
if (
cubes[i].x + cubes[i].w > cubes[c].x &&
cubes[i].x + cubes[i].w < cubes[c].x + 15
) {
cubes[c].hit_x_r();
} else if (
cubes[i].x < cubes[c].x + cubes[c].w &&
cubes[i].x > cubes[c].x + cubes[c].w - 15
) {
cubes[c].hit_x_l();
}
}
//Check collitions via Y axis.
if (
cubes[i].x < cubes[c].x + cubes[c].w &&
cubes[i].x + cubes[i].w > cubes[c].x
) {
if (
cubes[i].y + cubes[i].h > cubes[c].y &&
cubes[i].y + cubes[i].h < cubes[c].y + 15
) {
cubes[c].hit_y_d();
} else if (
cubes[c].y + cubes[c].h > scenario_table.y &&
cubes[c].y + cubes[c].h < scenario_table.y + 60 &&
cubes[c].x + cubes[c].w > scenario_table.x &&
cubes[c].x < scenario_table.x + scenario_table.w
) {
cubes[c].hit_y_u();
} else {
cubes[c].tempy = 1000; //Send to deletion.
}
}
}
}
}
//Aply gravity according to determined height.
if (cubes[i].y + cubes[i].h < cubes[i].tempy) {
cubes[i].gravity();
} else if (cubes[i].y + cubes[i].h > cubes[i].tempy) {
additional_force = 0; //According to how many elements are in how of the cube, apply additional force.
for (let c = cubes.length - 1; c >= 0; c--) {
if (cubes[i] == cubes[c]) {
continue;
} else {
if (
cubes[i].x <= cubes[c].x + cubes[c].w &&
cubes[i].x + cubes[i].w >= cubes[c].x &&
cubes[i].y + cubes[i].h >= cubes[c].y
) {
additional_force += 1;
}
}
}
cubes[i].hit_y_u_applyforce(additional_force);
}
//If object fell of map, then remove it.
}
//Check if the user is trying to move the cube with the mouse. (A for loop is not necessary)
if (mouseIsPressed) {
if (
mouseX >= cubes[i].x - 10 &&
mouseX <= cubes[i].x + cubes[i].w + 10 &&
mouseY >= cubes[i].y - 15 &&
mouseY <= cubes[i].y + cubes[i].h + 10
) {
cubes[i].ifgrabbed(i); //Check if others are grabbed and call grab function inside it.
}
}
if (cubes[i].y > height) {
index = cubes.indexOf(cubes[i]);
//Only splice if index is found.
if (index > -1) {
cubes.splice(index, 1);
}
}
}
}
function mouseReleased() {
if (mouse.called == 1) {
mouse.called = 0;
if (cubes[mouse.grabbing] == null) {
print("Some value is null...");
} else {
cubes[mouse.grabbing].isgrabbed = 0;
}
}
}
//For testing purposes.
function keyPressed() {
if (keyCode === SHIFT) {
newcube = new Cube(
mouseX,
mouseY,
int(random(30, 40)),
int(random(30, 40)),
"ice"
);
cubes.push(newcube);
}
if (keyCode === CONTROL) {
cubes = [];
}
if (key === "c" || key === "C") {
newcube = new Cube(mouseX, mouseY, 120, 160, "glass");
cubes.push(newcube);
}
}