Skip to content

Instantly share code, notes, and snippets.

@mimetaur
Created November 18, 2019 19:08
Show Gist options
  • Save mimetaur/07a832f79b8bad534a305d6dbcbf42b1 to your computer and use it in GitHub Desktop.
Save mimetaur/07a832f79b8bad534a305d6dbcbf42b1 to your computer and use it in GitHub Desktop.
Terrain Types
let lava;
let goal;
function setup() {
createCanvas(400, 400);
lava = new Terrain(50, 50, 75, 50, 0); // 0 means lava
goal = new Terrain(250, 250, 75, 50, 1); // 1 means goal
}
function draw() {
background(20);
if(lava.checkInside(mouseX,mouseY)) {
print("You lose");
}
if(goal.checkInside(mouseX,mouseY)) {
print("You win");
}
lava.draw();
goal.draw();
}
class Terrain {
constructor(x, y, w, h, property) {
this.x = x; // x
this.y = y; // y
this.w = w; // width
this.h = h; // height
// property is what property the terrain has
// 0 means lava (you die)
// 1 means goal (you win)
// 2 means a simple block (nothing happens)
this.property = property;
if (this.property == 0) {
this.color = "#e30000"; // lava color
} else if (this.property == 1) {
this.color = "#00e300"; // goal color
} else {
this.color = "#7fbfe8"; // block color
}
}
draw() {
noStroke();
fill(this.color);
rect(this.x, this.y, this.w, this.h);
}
checkInside(otherX, otherY) {
if (otherX > this.x && otherX < this.x + this.w && otherY > this.y && otherY < this.y + this.h) {
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment