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.
143 lines
5.2 KiB
143 lines
5.2 KiB
2 years ago
|
'use strict';
|
||
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||
|
return new (P || (P = Promise))(function (resolve, reject) {
|
||
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||
|
});
|
||
|
};
|
||
|
let debug = false, font, settings, loader;
|
||
|
//Only for online games
|
||
|
let socket;
|
||
|
let antiCacheQuery = '?_=' + new Date().getTime();
|
||
|
let game;
|
||
|
let released = false;
|
||
|
let images = {};
|
||
|
const p = new p5((p) => {
|
||
|
p.preload = () => {
|
||
|
settings = p.loadJSON('data/settings/settings.json' + antiCacheQuery, {}, 'json', (json) => {
|
||
|
console.log('Local settings loaded: ', json);
|
||
|
}, (error) => {
|
||
|
console.log('Local settings failed: ', error);
|
||
|
});
|
||
|
font = p.loadFont('data/styles/fonts/Tajawal/Tajawal-Regular.ttf' + antiCacheQuery, (font) => {
|
||
|
console.log('Local font loaded: ', font);
|
||
|
}, (error) => {
|
||
|
console.log('Local font failed: ', error);
|
||
|
});
|
||
|
images['ball'] = p.loadImage('data/images/ball.png' + antiCacheQuery, img => {
|
||
|
console.log('Ball image loaded: ', img);
|
||
|
}, error => {
|
||
|
console.log('Ball image failed: ', error);
|
||
|
});
|
||
|
images['box'] = p.loadImage('data/images/box.png' + antiCacheQuery, img => {
|
||
|
console.log('Box image loaded: ', img);
|
||
|
}, error => {
|
||
|
console.log('Box image failed: ', error);
|
||
|
});
|
||
|
},
|
||
|
p.setup = () => {
|
||
|
interfaceSetup();
|
||
|
canvasSetup();
|
||
|
//loader = new Loader(p.createVector(p.width / 2, p.height / 2), Math.min(p.width, p.height) / 2);
|
||
|
loadDynamicScripts().then(() => {
|
||
|
//Load other stuff, then =>
|
||
|
//loader = null;
|
||
|
game = new Game();
|
||
|
game.trees[0].container.mousePressed();
|
||
|
});
|
||
|
},
|
||
|
p.draw = () => {
|
||
|
p.clear();
|
||
|
if (game) {
|
||
|
game.update();
|
||
|
game.display();
|
||
|
}
|
||
|
if (loader) {
|
||
|
loader.update();
|
||
|
loader.display();
|
||
|
}
|
||
|
if (debug)
|
||
|
debugInformation();
|
||
|
};
|
||
|
});
|
||
|
function debugInformation() {
|
||
|
}
|
||
|
function interfaceSetup() {
|
||
|
let files_elem = $('#file_browser');
|
||
|
files_elem.on('change', () => {
|
||
|
let file = files_elem.get(0).files[0];
|
||
|
file.text().then(text => {
|
||
|
game.restoreTrees(JSON.parse(text)[0]);
|
||
|
});
|
||
|
});
|
||
|
let colorSelect = $('#color');
|
||
|
let possibleColors = $('#possible_colors');
|
||
|
for (let colorName in settings.game.colors) {
|
||
|
if (!settings.game.colors.hasOwnProperty(colorName))
|
||
|
continue;
|
||
|
let publicName = colorName.substr(0, 1).toUpperCase() + colorName.substr(1);
|
||
|
let option = $('<option></option>');
|
||
|
option.html(publicName);
|
||
|
option.val("[" + settings.game.colors[colorName] + "]");
|
||
|
colorSelect.append(option);
|
||
|
let checkbox = $('<input/>');
|
||
|
checkbox.attr({
|
||
|
type: 'checkbox',
|
||
|
id: colorName
|
||
|
});
|
||
|
checkbox.on('click', event => {
|
||
|
if (game) {
|
||
|
if (!game.updateChainSettings($(event.target))) {
|
||
|
console.info('failed');
|
||
|
event.preventDefault();
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
let label = $('<label></label>');
|
||
|
label.html(publicName);
|
||
|
label.attr('for', colorName);
|
||
|
possibleColors.append(checkbox, label);
|
||
|
}
|
||
|
}
|
||
|
function downloadTrees() {
|
||
|
let str = JSON.stringify(game.trees, (key, value) => {
|
||
|
if (value instanceof p5.Vector) {
|
||
|
return {
|
||
|
x: value.x,
|
||
|
y: value.y
|
||
|
};
|
||
|
}
|
||
|
return value;
|
||
|
});
|
||
|
let data = 'data:application/json;charset=utf-8,[' + encodeURIComponent(str) + "]";
|
||
|
$("#save_link").attr("href", data);
|
||
|
$("#save_link")[0].click();
|
||
|
}
|
||
|
function canvasSetup() {
|
||
|
p.frameRate(60);
|
||
|
let w = $('#canvas_holder').width(), h = $('#canvas_holder').height();
|
||
|
let canvas = p.createCanvas(w, h);
|
||
|
canvas.parent('canvas_holder');
|
||
|
p.textFont(font);
|
||
|
p.imageMode(p.CENTER);
|
||
|
}
|
||
|
function loadDynamicScripts() {
|
||
|
return __awaiter(this, void 0, void 0, function* () {
|
||
|
const json = yield p.httpGet('data/settings/libraries.json' + antiCacheQuery, 'json');
|
||
|
let requests = [];
|
||
|
for (let script in json) {
|
||
|
if (json[script]) {
|
||
|
let url = '/lib/benjocraeft/' + script + '.js';
|
||
|
requests.push($.getScript(url, () => {
|
||
|
console.log('Successfully loaded script: ', url);
|
||
|
}));
|
||
|
}
|
||
|
}
|
||
|
yield $.when(...requests);
|
||
|
console.log('All dynamic scripts have been loaded!');
|
||
|
});
|
||
|
}
|
||
|
//# sourceMappingURL=init.js.map
|