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.
261 lines
5.9 KiB
261 lines
5.9 KiB
2 years ago
|
class Game{
|
||
|
|
||
|
trees: Tree[] = []
|
||
|
tree: Tree = new Tree(null)
|
||
|
|
||
|
decorations: Decoration[] = []
|
||
|
|
||
|
containers: Container[] = []
|
||
|
|
||
|
isTreeMode: boolean = true
|
||
|
isDecorationMode: boolean = false
|
||
|
|
||
|
barHeight: number
|
||
|
|
||
|
switchButton: Button = new Button('switch')
|
||
|
downloadButton: Button = new Button('download')
|
||
|
restoreButton: Button = new Button('restore')
|
||
|
|
||
|
background: Background = new Background()
|
||
|
|
||
|
constructor(){
|
||
|
settings.game.trees.forEach((s: object) => this.trees.push(new Tree(s)));
|
||
|
let red = settings.game.colors.red;
|
||
|
this.decorations = [
|
||
|
Decoration.Create({
|
||
|
type: "ball", radius: 0.45, colors: [red]
|
||
|
}),
|
||
|
Decoration.Create({
|
||
|
type: "star", radius: 0.45, colors: [red, darker(red)]
|
||
|
}),
|
||
|
Decoration.Create({
|
||
|
type: "chain", radius: 0.45, colors: [
|
||
|
settings.game.colors.gold,
|
||
|
settings.game.colors.silver
|
||
|
]
|
||
|
}),
|
||
|
];
|
||
|
$('#gold, #silver').attr('checked', 'checked');
|
||
|
this.trees.forEach((t, i) => {
|
||
|
let c = new Container(i, t);
|
||
|
this.containers.push(c);
|
||
|
t.container = c;
|
||
|
});
|
||
|
this.decorations.forEach((d, i) => {
|
||
|
let c = new Container(i, d);
|
||
|
this.containers.push(c);
|
||
|
d.container = c;
|
||
|
});
|
||
|
this.updateDecorationSettings();
|
||
|
this.updateStarSettings();
|
||
|
}
|
||
|
|
||
|
updateDecorationSettings(){
|
||
|
this.decorations.filter(d => d instanceof Ball || d instanceof Star).forEach(d => {
|
||
|
d.radius = $('#radius').val() as number / 100 * 0.7;
|
||
|
let colors = [];
|
||
|
colors.push(JSON.parse($('#color').val() as string));
|
||
|
if (d instanceof Ball){
|
||
|
d.colors = colors;
|
||
|
d.updateColor();
|
||
|
}
|
||
|
if (d instanceof Star){
|
||
|
d.innerRadius = $('#inner_radius').val() as number * d.radius;
|
||
|
colors.push(darker(colors[0]));
|
||
|
d.colors = colors;
|
||
|
d.createTriangles();
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
updateStarSettings(){
|
||
|
this.decorations.filter(d => d instanceof Star).forEach((s: Star) => {
|
||
|
s.innerRadius = $('#inner_radius').val() as number * s.radius;
|
||
|
s.raysCount = parseInt($('#rays_count').val() as string);
|
||
|
s.createTriangles();
|
||
|
});
|
||
|
}
|
||
|
|
||
|
updateChainSettings(element: JQuery): boolean{
|
||
|
let chain = this.decorations.find(d => d instanceof Chain);
|
||
|
let colorName = element.attr('id');
|
||
|
let colorValue = settings.game.colors[colorName];
|
||
|
|
||
|
let cvIndex = chain.colors.findIndex(c => equals(c, colorValue));
|
||
|
if (cvIndex >= 0){
|
||
|
chain.colors.splice(cvIndex, 1);
|
||
|
} else {
|
||
|
chain.colors.push(colorValue);
|
||
|
}
|
||
|
|
||
|
if (chain.colors.length == 0){
|
||
|
chain.colors.push(colorValue);
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
|
||
|
updateBackgroundSettings(){
|
||
|
this.background.updateSettings();
|
||
|
}
|
||
|
|
||
|
get center(): p5.Vector{
|
||
|
return p.createVector(p.width / 2, this.height / 2);
|
||
|
}
|
||
|
|
||
|
get height(): number{
|
||
|
return p.height - this.containers[0].dim.y;
|
||
|
}
|
||
|
|
||
|
get currentContainers(): Container[]{
|
||
|
return this.containers.filter(c => {
|
||
|
if (this.isTreeMode)
|
||
|
return c.content instanceof Tree
|
||
|
if (this.isDecorationMode)
|
||
|
return c.content instanceof Decoration
|
||
|
});
|
||
|
}
|
||
|
|
||
|
update(){
|
||
|
this.background.update();
|
||
|
this.switchButton.update();
|
||
|
this.downloadButton.update();
|
||
|
this.restoreButton.update();
|
||
|
this.currentContainers.forEach(c => c.update());
|
||
|
this.tree.update();
|
||
|
}
|
||
|
|
||
|
display(){
|
||
|
this.background.display();
|
||
|
this.switchButton.display();
|
||
|
this.downloadButton.display();
|
||
|
this.restoreButton.display();
|
||
|
this.currentContainers.forEach(c => c.display());
|
||
|
this.tree.display();
|
||
|
this.background.display(true);
|
||
|
}
|
||
|
|
||
|
//Always called
|
||
|
mousePressed(){
|
||
|
this.tree.mousePressed();
|
||
|
this.currentContainers.forEach(c => c.mouseIsOver ? c.mousePressed() : null);
|
||
|
this.switchButton.mouseIsOver ? this.switchButton.mousePressed() : null;
|
||
|
this.downloadButton.mouseIsOver ? this.downloadButton.mousePressed() : null;
|
||
|
this.restoreButton.mouseIsOver ? this.restoreButton.mousePressed() : null;
|
||
|
}
|
||
|
|
||
|
//Always called
|
||
|
mouseReleased(){
|
||
|
this.tree.mouseReleased();
|
||
|
}
|
||
|
|
||
|
restoreTrees(rawTrees: object[]){
|
||
|
this.trees.forEach((t, i) => {
|
||
|
t.restoreFrom(rawTrees[i]);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
class Button{
|
||
|
|
||
|
pos: p5.Vector = p.createVector()
|
||
|
dim: p5.Vector = p.createVector()
|
||
|
|
||
|
type: string
|
||
|
|
||
|
|
||
|
|
||
|
constructor(type: string){
|
||
|
this.type = type;
|
||
|
}
|
||
|
|
||
|
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.type === 'switch'){
|
||
|
if (game.isTreeMode){
|
||
|
game.decorations[0].display(this.center, this.dim);
|
||
|
}
|
||
|
if (game.isDecorationMode){
|
||
|
let tree = game.tree;
|
||
|
if (tree.isPlaceholder)
|
||
|
tree = game.trees[0];
|
||
|
tree.display(this.center, this.dim);
|
||
|
}
|
||
|
}
|
||
|
if (this.type === 'download'){
|
||
|
p.textAlign(p.CENTER, p.CENTER);
|
||
|
p.textSize(25);
|
||
|
p.fill(0);
|
||
|
p.text("Save", this.pos.x, this.pos.y, this.dim.x, this.dim.y);
|
||
|
}
|
||
|
if (this.type === 'restore'){
|
||
|
p.textAlign(p.CENTER, p.CENTER);
|
||
|
p.textSize(20);
|
||
|
p.fill(0);
|
||
|
p.text("Restore", this.pos.x, this.pos.y, this.dim.x, this.dim.y);
|
||
|
}
|
||
|
|
||
|
if (this.mouseIsOver){
|
||
|
p.fill(0, 50);
|
||
|
p.noStroke();
|
||
|
p.rect(x - w / 2, y - h / 2, w , h);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
update(){
|
||
|
let w, h, x, y;
|
||
|
if (this.type === 'switch'){
|
||
|
w = game.containers[0].dim.x / 2;
|
||
|
h = w;
|
||
|
x = 0;
|
||
|
y = game.containers[0].pos.y - h;
|
||
|
}
|
||
|
if (this.type === 'download'){
|
||
|
w = game.containers[0].dim.x / 2;
|
||
|
h = w;
|
||
|
x = p.width - w;
|
||
|
y = game.containers[0].pos.y - h;
|
||
|
}
|
||
|
if (this.type === 'restore'){
|
||
|
w = game.containers[0].dim.x / 2;
|
||
|
h = w;
|
||
|
x = p.width - 2 * w;
|
||
|
y = game.containers[0].pos.y - h;
|
||
|
}
|
||
|
|
||
|
this.pos.set(x, y);
|
||
|
this.dim.set(w, h);
|
||
|
}
|
||
|
|
||
|
mousePressed(){
|
||
|
if (this.type === 'switch'){
|
||
|
game.isDecorationMode = !game.isDecorationMode;
|
||
|
game.isTreeMode = !game.isTreeMode;
|
||
|
}
|
||
|
if (this.type === 'download'){
|
||
|
downloadTrees();
|
||
|
}
|
||
|
if (this.type === 'restore'){
|
||
|
$('#file_browser')[0].click();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|