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.
 
 
 

90 lines
2.1 KiB

"use strict"
class Coin{
constructor(brick){
this.pos = brick.pos;
this.dispoX = random(brick.dim.x);
this.brick = brick;
this.dir = ranBool() ? 1 : -1;
this.colorSetup();
}
update(){
this.blink();
this.show();
this.collision();
}
blink(){
this.hue += this.dir * sin(frameCount / 10) * 5;
this.hue = this.hue < 0 ? this.hue + 360 : this.hue;
this.hue %= 360;
colorMode(HSB);
let s = this.color.stroke;
s = color(this.hue, saturation(s), brightness(s));
this.color.stroke = s;
colorMode(RGB);
}
show(){
fill(this.color.fill);
stroke(this.color.stroke);
strokeWeight(4);
strokeJoin(ROUND);
let size = int(values.coinSize),
dispoY = int(values.coinDispoY);
let p1 = {x: this.pos.x + this.dispoX, y: this.pos.y - dispoY},
p2 = {x: this.pos.x + this.dispoX + size / 3, y: this.pos.y - (dispoY + size / 2)},
p3 = {x: this.pos.x + this.dispoX, y: this.pos.y - (dispoY + size)},
p4 = {x: this.pos.x + this.dispoX - size / 3, y: this.pos.y - (dispoY + size / 2)};
quad(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y);
if (debug){
let hitbox = this.getHitbox();
noFill();
stroke(255, 0, 0);
rect(hitbox.pos.x, hitbox.pos.y, hitbox.dim.x, hitbox.dim.y);
}
}
collision(){
if (game.player.touches(this) && hue(game.player.color) != hue(this.color.fill)){
game.player.setColor(this.color.fill);
new Sound(Sound.Coin()).play();
this.brick.coin = null;
}
}
colorSetup(){
let s = color(colors.brick.coin.stroke),
f = color(colors.brick.coin.fill);
this.hue = int(random(360));
this.hue = floor(this.hue / 60) * 60;
colorMode(HSB);
let stroke = color(this.hue, saturation(s), brightness(s)),
fill = color(this.hue, saturation(f), brightness(f));
this.color = {
stroke: stroke,
fill: fill
};
colorMode(RGB);
}
getHitbox(){
let size = int(values.coinSize),
dispoY = int(values.coinDispoY);
let hitbox = {
pos: {x: this.pos.x + this.dispoX - size / 3, y: this.pos.y - (dispoY + size)},
dim: {x: size * 2 / 3, y: size}
};
return hitbox;
}
}