You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
2.3 KiB
104 lines
2.3 KiB
2 years ago
|
"use strict"
|
||
|
|
||
|
class Brick{
|
||
|
|
||
|
constructor(x, y, w, h, v){
|
||
|
this.pos = createVector(x, y);
|
||
|
this.vel = createVector(v.x, v.y);
|
||
|
this.dim = createVector(w, h);
|
||
|
this.prepareNoise();
|
||
|
}
|
||
|
|
||
|
createTrees(){
|
||
|
this.trees = [];
|
||
|
for (let i = 0; i < int(random(3)); i++) this.trees.push(new Tree(this));
|
||
|
}
|
||
|
|
||
|
createPlants(){
|
||
|
this.grasses = [];
|
||
|
for (let i = 0; i < int(random(4)); i++) this.grasses.push(new Grass(this, random(this.dim.x)));
|
||
|
}
|
||
|
|
||
|
createCoins(){
|
||
|
let rarity = int(values.coinRarity);
|
||
|
this.coin = ranBool(rarity) ? new Coin(this) : null;
|
||
|
}
|
||
|
|
||
|
update(world){
|
||
|
if (!game.paused) {
|
||
|
this.borders(world);
|
||
|
this.move();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
display(){
|
||
|
let thrashHold = 300;
|
||
|
if (this.pos.x < viewPort.x + width + thrashHold && this.pos.x + this.dim.x + thrashHold > viewPort.x &&
|
||
|
this.pos.y < viewPort.y + height + thrashHold && this.pos.y + this.dim.y > viewPort.y - thrashHold){
|
||
|
this.show();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
move(){
|
||
|
this.pos.add(this.vel);
|
||
|
}
|
||
|
|
||
|
borders(world){
|
||
|
if (this.pos.x > world.pos.x + world.dim.x - this.dim.x
|
||
|
|| this.pos.x < world.pos.x){
|
||
|
this.vel.x *= -1;
|
||
|
}
|
||
|
if (this.pos.y > world.pos.y + world.dim.y - this.dim.y
|
||
|
|| this.pos.y < world.pos.y){
|
||
|
this.vel.y *= -1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
show(){
|
||
|
noStroke();
|
||
|
|
||
|
//Brown earth
|
||
|
fill(colors.brick.earth);
|
||
|
rect(this.pos.x, this.pos.y + this.dim.y * 0.3, this.dim.x, this.dim.y * 0.7, this.dim.y / 4);
|
||
|
|
||
|
//Green grass ground
|
||
|
fill(colors.brick.ground);
|
||
|
beginShape();
|
||
|
for (let x = 0; x < this.dim.x; x += 5){
|
||
|
let y = this.pos.y - this.noiseValues[x] * 3;
|
||
|
vertex(this.pos.x + x, y);
|
||
|
if (!(x + 5 < this.dim.x)){
|
||
|
vertex(this.pos.x + this.dim.x, y);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
vertex(this.pos.x + this.dim.x, this.pos.y + this.dim.y / 2);
|
||
|
vertex(this.pos.x, this.pos.y + this.dim.y / 2);
|
||
|
endShape();
|
||
|
|
||
|
//Trees and grasses with flowers
|
||
|
for (let t of this.trees) t.show();
|
||
|
for (let g of this.grasses) g.show();
|
||
|
|
||
|
//Coin
|
||
|
if (this.coin) this.coin.update();
|
||
|
}
|
||
|
|
||
|
getHitbox(){
|
||
|
let hitbox = {
|
||
|
pos: {x: this.pos.x, y: this.pos.y},
|
||
|
dim: {x: this.dim.x, y: 0},
|
||
|
vel: {x: this.vel.x, y: this.vel.y}
|
||
|
};
|
||
|
return hitbox;
|
||
|
}
|
||
|
|
||
|
//For better performance
|
||
|
prepareNoise(){
|
||
|
this.noiseValues = [];
|
||
|
let trashHold = 100;
|
||
|
for (let x = random(trashHold); x <= int(values.maxBrickWidth) + trashHold; x++){
|
||
|
this.noiseValues.push(noise(x));
|
||
|
}
|
||
|
}
|
||
|
}
|