p5js/卡牌生存/card.js
2022-12-13 21:26:05 +08:00

63 lines
1.5 KiB
JavaScript

class BaseCard extends p5.Vector {
constructor(x, y, w, h, r) {
super(x, y);
this.w = w;
this.h = h;
this.r = r;
this.cardToInteract = null;
}
get mouseIsOnMe() {
return (
abs(this.x - mouseX) * 2 < this.w &&
abs(this.y - mouseY) * 2 < this.h
);
}
get hasCardOnMe() {
return (
cardDragging &&
cardDragging != this &&
abs(this.x - cardDragging.x) * 2 < this.w &&
abs(this.y - cardDragging.y) * 2 < this.h
);
}
followMouse() {
this.add(movedX, movedY);
}
moveToBegin() {
const index = cards.indexOf(this);
cards.splice(index, 1);
cards.unshift(this);
}
update() {
if (!cardDragging && mouseIsPressed && this.mouseIsOnMe) {
cardDragging = this;
this.moveToBegin();
}
if (this.hasCardOnMe) {
this.cardToInteract = cardDragging;
cardDragging.cardToInteract = this;
} else if (cardDragging) {
this.cardToInteract = null;
cardDragging.cardToInteract = null;
}
}
draw() {
push();
fill(128);
if (this.hasCardOnMe) {
stroke("#FFFF00");
strokeWeight(2);
} else {
stroke(255);
}
rect(this.x, this.y, this.w, this.h, this.r);
pop();
}
}