xxxxxxxxxx
119
class Cube {
constructor(x, y, w, h, type) {
this.x = x;
this.y = y;
this.tempx = 0;
this.tempy = scenario_table.y; //Default value.
this.w = w;
this.h = h;
this.icecubeimg = cubeimg; //Placeholder.
this.glassimg = glassimg;
this.isgrabbed = 0;
this.acceleration = 0;
this.type = type;
this.insidecup = 0;
this.stop = 0;
this.mass = 3; //The force for gravity. Default is 3.
this.todelete = 0; //Determines that the object will fall and be deleted.
}
gravity() {
//14 seems more than enough to simulate gravity and acceleration.
if (this.acceleration < 14) {
this.acceleration++;
}
this.y += this.acceleration;
}
display() {
//Push() and pop() isolates the figure properties.
if (this.type == "ice") {
push();
fill(255, 0, 0);
rect(this.x, this.y, this.w, this.h);
pop();
} else if (this.type == "glass") {
push();
noStroke();
fill(0, 0, 255, 30);
rect(this.x, this.y, this.w, this.h);
pop();
}
}
//Grabbing with the mouse to move.
grabbed() {
if (this.isgrabbed == 1) {
if (this.type == "ice") {
this.acceleration = 0;
this.stop = 0;
this.x = mouseX - 10;
this.y = mouseY - 10;
}
if (this.type == "glass") {
this.acceleration = 0;
this.stop = 0;
if (this.x > mouseX - 55) {
this.x -= 3;
}
if (this.x < mouseX - 55) {
this.x += 3;
}
if (this.y > mouseY - 75) {
this.y -= 3;
}
if (this.y < mouseY - 75) {
this.y += 3;
}
}
}
}
ifgrabbed(i) {
if (mouse.called == 0) {
mouse.called = 1;
this.isgrabbed = 1;
mouse.isgrabbing(i);
}
cubes[i].grabbed();
}
//Check for collisions in X, (left and right) and Y axis (up and down). Separate functions are needed as physics are being implemented.
//Check for collisions and apply force.
hit_x_r() {
if (this.type == "ice") {
this.x += this.mass;
} else if (this.type == "glass") {
this.x += this.mass;
}
}
hit_x_l() {
if (this.type == "ice") {
this.x -= this.mass;
} else if (this.type == "glass") {
this.x -= this.mass;
}
}
hit_y_u() {
if (this.type == "ice") {
this.y -= this.mass;
} else if (this.type == "glass") {
this.y -= this.mass;
}
this.acceleration = 0;
}
hit_y_d() {
if (this.type == "ice") {
this.y += this.mass;
} else if (this.type == "glass") {
this.y += this.mass;
}
}
}