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.

84 lines
1.9 KiB

2 years ago
class Container{
index: number
pos: p5.Vector = p.createVector()
dim: p5.Vector = p.createVector()
content: Tree | Decoration
constructor(index: number, content: Tree | Decoration){
this.index = index;
this.content = content;
}
get mouseIsOver(): boolean{
return p.mouseX > this.pos.x && p.mouseX < this.pos.x + this.dim.x &&
p.mouseY > this.pos.y && p.mouseY < this.pos.y + this.dim.y;
}
get center(): p5.Vector{
return p5.Vector.add(this.pos, p5.Vector.div(this.dim, 2));
}
display(){
let w = this.dim.x,
h = this.dim.y;
let x = this.pos.x + w / 2,
y = this.pos.y + h / 2;
p.image(images['box'], x, y, w, h);
if (this.content !== game.tree){
this.content.display();
}
if (this.mouseIsOver){
p.fill(0, 50);
p.noStroke();
p.rect(x - w / 2, y - h / 2, w , h);
}
}
update(){
let currentCount = game.currentContainers.length;
let w = p.width / currentCount,
h = w;
let x = w * this.index,
y = p.height - h;
if (this.content instanceof Decoration){
let otherCount = game.containers.filter(c => game.currentContainers.find(ic => ic == c) == null).length;
w = h = p.width / otherCount;
x = w * this.index;
y = p.height - h;
x += p.width / 2 - w * currentCount / 2;
}
this.pos.set(x, y);
this.dim.set(w, h);
if (this.content !== game.tree)
this.content.update();
}
mousePressed(){
if (this.content instanceof Tree){
game.tree.animationDirection = -1;
if (this.content === game.tree){
game.tree = new Tree(null);
} else {
game.tree = this.content;
game.tree.animationDirection = 1;
}
}
if (this.content instanceof Decoration && !game.tree.isPlaceholder){
let decoration = Decoration.Create(this.content.properties);
decoration.isTaken = true;
decoration.isBoundToMouse = true;
game.tree.decorations.push(decoration);
if (decoration instanceof Chain){
decoration.p1IsBoundToMouse = true;
}
}
}
}