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.
 
 
 

219 lines
7.1 KiB

"use strict";
class Game {
constructor() {
this.trees = [];
this.tree = new Tree(null);
this.decorations = [];
this.containers = [];
this.isTreeMode = true;
this.isDecorationMode = false;
this.switchButton = new Button('switch');
this.downloadButton = new Button('download');
this.restoreButton = new Button('restore');
this.background = new Background();
settings.game.trees.forEach((s) => 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() / 100 * 0.7;
let colors = [];
colors.push(JSON.parse($('#color').val()));
if (d instanceof Ball) {
d.colors = colors;
d.updateColor();
}
if (d instanceof Star) {
d.innerRadius = $('#inner_radius').val() * d.radius;
colors.push(darker(colors[0]));
d.colors = colors;
d.createTriangles();
}
});
}
updateStarSettings() {
this.decorations.filter(d => d instanceof Star).forEach((s) => {
s.innerRadius = $('#inner_radius').val() * s.radius;
s.raysCount = parseInt($('#rays_count').val());
s.createTriangles();
});
}
updateChainSettings(element) {
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() {
return p.createVector(p.width / 2, this.height / 2);
}
get height() {
return p.height - this.containers[0].dim.y;
}
get currentContainers() {
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) {
this.trees.forEach((t, i) => {
t.restoreFrom(rawTrees[i]);
});
}
}
class Button {
constructor(type) {
this.pos = p.createVector();
this.dim = p.createVector();
this.type = type;
}
get mouseIsOver() {
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() {
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();
}
}
}
//# sourceMappingURL=game.js.map