XGravity/Public/ecma/Application/XGravity.ecma.js

145 lines
5.0 KiB
JavaScript

"use strict";
const XGravity = (function(){
const XGravity = function(){
const self = this,
screen = {x : 0, y : 0};
let thread_interval = null,
fps = 12;
this.cells = 40;
this.canvas_quality = 100;
this.item_self = document;
this.values = new Values(self);
this.settings = new SettingsManager(self);
this.i18n = new I18NManager(self);
this.audio = new AudioManager(this);
this.explosions = new ExplosionsManager(this);
this.views = {
main_menu : new MainMenuView(self),
difficulty_select : new DifficultySelectView(this),
options : new OptionsView(self),
ranking : new RankingView(self),
credits : new CreditsView(self),
fps : new FpsOverlayView(this),
game : new GameView(self),
game_over : new GameOverView(this)
};
const constructor = () => {
self.values.set();
let target, preload = setInterval(() => {
if(target = document.querySelector(self.values.position)){
clearInterval(preload);
self.item_self = target.appendChild(document.createElement("div"));
self.item_self.className = self.item_self.id = self.values.class_name;
self.item_self.setAttribute("data-cells", self.values.cells);
thread_interval = setInterval(thread, 1000 / fps);
build();
};
}, self.values.fps);
Common.SCSS(SCSS);
};
const thread = () => {
if(screen.x != self.item_self.offsetWidth || screen.y != self.item_self.offsetHeight){
screen.x = self.item_self.offsetWidth;
screen.y = self.item_self.offsetHeight;
self.item_self.style.fontSize = ((screen.x < screen.y ? screen.x : screen.y) / self.cells) + "px";
};
};
const build = () => {
self.views.main_menu.go();
};
/**
* @param {...(HTMLElement|string|Array.<any|null>)} inputs
* @returns {Array.<HTMLElement>}
* @access public
* @static
*/
this.html = (...inputs) => {
const fragment = new DocumentFragment(),
master = (
inputs[0] && (inputs[0].tagName || inputs[0].nodeName) ? inputs[0] :
typeof inputs[0] == "string" ? document.querySelector(inputs[0]) :
null),
items = [];
(master ? inputs.slice(1) : inputs).forEach(subinputs => {
if(subinputs && (subinputs.tagName || subinputs.nodeName))
fragment.appendChild(subinputs);
else if(typeof subinputs == "string")
fragment.appendChild(document.createTextNode(subinputs));
else if(subinputs instanceof Array){
const [tag, attributes, children] = subinputs.concat([null, null]).slice(0, 3),
item = document.createElement(tag);
attributes && attributes.constructor == Object && Object.entries(attributes).forEach(([key, value]) => {
if(/^on[_\-]?/i.test(key) && typeof value == "function")
item.addEventListener(key.replace(/^on[_\-]?/i, "").replace(/[^a-z0-9]+/gi, "").toLowerCase(), event => {
value(item, event);
});
else if(value !== null)
item.setAttribute(key.replace(/[^a-z0-9]+/gi, "-"), value);
});
if(children instanceof Array)
self.html(...children).forEach(child => item.appendChild(child));
else if(typeof children == "string")
item.innerHTML += children;
fragment.appendChild(item);
};
});
items.push(...fragment.childNodes);
master && master.appendChild(fragment);
return items;
};
constructor();
};
// XGravity.drone_velocity = (px, py, dx, dy, ax, ay, fx, fy, max_a = 40) => {
// // 1. Sumar o restar según la posición del cursor (atracción)
// ax += (px - dx > 0 ? 1 : -1);
// ay += (py - dy > 0 ? 1 : -1);
// // 2. Limitar el acumulador para que la órbita sea elástica pero controlable
// if(ax > max_a) ax = max_a;
// else if(ax < -max_a) ax = -max_a;
// if(ay > max_a) ay = max_a;
// else if(ay < -max_a) ay = -max_a;
// return {
// ax : ax,
// ay : ay,
// x : ax * fx,
// y : ay * fy
// };
// };
XGravity.drone_velocity = (px, py, dx, dy, vx, vy, f, delta = 0) => [
vx += (px - dx) * f,
vy += (py - dy) * f
];
return XGravity;
})();