12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097 |
- const game = {
- audio: {}, lang: {},
- image: {}, sprite: {},
- mediaTypes: ['lang', 'audio', 'image', 'sprite'],
- loadedMedia: [],
- isLoaded: [],
-
- load: {
-
- lang: function (url) {
- game.isLoaded['lang'] = false;
- game.loadedMedia['lang'] = 0;
- game.lang = {};
- const init = { mode: 'same-origin' };
- fetch(url, init)
- .then(function (response) {
- return response.text();
- })
- .then(function (text) {
- let msg = text.split('\n');
- msg.forEach(cur => {
- try {
- let msg = cur.split('=');
- game.lang[msg[0].trim()] = msg[1].trim();
- } catch (Error) { if (debugMode) console.log('Sintax error fixed'); }
- game.load._informMediaIsLoaded(msg.length - 1, 'lang');
- });
- });
- },
-
- audio: function (urls) {
- game.isLoaded['audio'] = false;
- game.loadedMedia['audio'] = 0;
- urls = this._getNotLoadedUrls(urls, game.audio);
- if (urls.length == 0) {
- game.load._informMediaIsLoaded(0, 'audio');
- } else {
- const init = { mode: 'same-origin' };
- urls.forEach(cur => {
- fetch(cur[1][1], init)
- .then(response => response.blob())
- .then(function (myBlob) {
- game.audio[cur[0]] = new Audio(URL.createObjectURL(myBlob));
- game.load._informMediaIsLoaded(urls.length - 1, 'audio');
- });
- });
- }
- },
-
- image: function (urls) {
- game.isLoaded['image'] = false;
- game.loadedMedia['image'] = 0;
- urls = this._getNotLoadedUrls(urls, game.image);
- if (urls.length == 0) {
- game.load._informMediaIsLoaded(0, 'image');
- } else {
- urls.forEach(cur => {
- const img = new Image();
- img.onload = () => {
- game.image[cur[0]] = img;
- game.load._informMediaIsLoaded(urls.length - 1, 'image');
- }
- img.src = cur[1];
- });
- }
- },
-
- sprite: function (urls) {
- game.isLoaded['sprite'] = false;
- game.loadedMedia['sprite'] = 0;
- urls = this._getNotLoadedUrls(urls, game.sprite);
- if (urls.length == 0) {
- game.load._informMediaIsLoaded(0, 'sprite');
- } else {
- urls.forEach(cur => {
- const img = new Image();
- img.onload = () => {
- game.sprite[cur[0]] = img;
- game.load._informMediaIsLoaded(urls.length - 1, 'sprite');
- }
- img.src = cur[1];
- img.frames = cur[2];
- });
- }
- },
-
- _getNotLoadedUrls: function (urls, media) {
- const newUrls = [];
- urls.forEach(cur => { if (media[cur[0]] == undefined) newUrls.push(cur); });
- return newUrls;
- },
-
- _informMediaIsLoaded: function (last, type) {
- if (game.loadedMedia[type] == last) {
- game.isLoaded[type] = true;
- game.load._isPreloadDone(type);
- }
- game.loadedMedia[type]++;
- },
-
- _isPreloadDone: function (type) {
- let flag = true;
- for (let i in game.mediaTypes) {
- if (game.isLoaded[game.mediaTypes[i]] == false) {
- flag = false;
- break;
- }
- }
-
- if (flag) {
- game.isLoaded = [];
- game.loadedMedia[type] = 0;
- game.state._create();
- }
- }
- },
-
- add: {
-
-
-
-
- image: function (x, y, img, scale, alpha) {
- if (x == undefined || y == undefined || img == undefined) console.error('Game error: missing parameters');
- else if (game.image[img] == undefined) console.error('Game error: image not found in cache: ' + img);
- else {
- const med = {
- typeOfMedia: 'image',
- name: img,
- x: x || game.add._default.x,
- y: y || game.add._default.y,
- _xWithAnchor: x || game.add._default._xWithAnchor,
- _yWithAnchor: y || game.add._default._yWithAnchor,
- xAnchor: game.add._default.xAnchor,
- yAnchor: game.add._default.yAnchor,
- shadow: game.add._default.shadow,
- shadowColor: game.add._default.shadowColor,
- shadowBlur: game.add._default.shadowBlur,
- alpha: (alpha != undefined) ? alpha : game.add._default.alpha,
- scale: scale || game.add._default.scale,
- width: game.image[img].width,
- height: game.image[img].height,
- anchor: function (xAnchor, yAnchor) {
- this.xAnchor = xAnchor;
- this.yAnchor = yAnchor;
- },
- get xWithAnchor() { return this.x - (this.width * this.scale * this.xAnchor); },
- get yWithAnchor() { return this.y - (this.height * this.scale * this.yAnchor); }
- };
- med.originalScale = med.scale;
- game.render.queue.push(med);
- return med;
- }
- },
-
-
-
-
-
- sprite: function (x, y, img, curFrame, scale, alpha) {
- if (x == undefined || y == undefined || img == undefined) console.error('Game error: missing parameters');
- else if (game.sprite[img] == undefined) console.error('Game error: sprite not found in cache: ' + img);
- else {
- const med = {
- typeOfMedia: 'sprite',
- name: img,
- x: x || game.add._default.x,
- y: y || game.add._default.y,
- _xWithAnchor: x || game.add._default._xWithAnchor,
- _yWithAnchor: y || game.add._default._yWithAnchor,
- xAnchor: game.add._default.xAnchor,
- yAnchor: game.add._default.yAnchor,
- shadow: game.add._default.shadow,
- shadowColor: game.add._default.shadowColor,
- shadowBlur: game.add._default.shadowBlur,
- alpha: (alpha != undefined) ? alpha : game.add._default.alpha,
- scale: scale || game.add._default.scale,
- width: game.sprite[img].width / game.sprite[img].frames,
- height: game.sprite[img].height,
- curFrame: curFrame || 0,
- anchor: function (xAnchor, yAnchor) {
- this.xAnchor = xAnchor;
- this.yAnchor = yAnchor;
- },
- get xWithAnchor() { return this.x - (this.width * this.scale * this.xAnchor); },
- get yWithAnchor() { return this.y - (this.height * this.scale * this.yAnchor); }
- };
- med.originalScale = med.scale;
- game.render.queue.push(med);
- return med;
- }
- },
-
-
-
- text: function (x, y, text, style, align) {
- if (x == undefined || y == undefined || text == undefined || style == undefined) console.error('Game error: missing parameters');
- else {
- const med = {
- typeOfMedia: 'text',
- name: text,
- x: x || game.add._default.x,
- y: y || game.add._default.y,
- _xWithAnchor: x || game.add._default._xWithAnchor,
- _yWithAnchor: y || game.add._default._yWithAnchor,
- xAnchor: game.add._default.xAnchor,
- yAnchor: game.add._default.yAnchor,
- shadow: game.add._default.shadow,
- shadowColor: game.add._default.shadowColor,
- shadowBlur: game.add._default.shadowBlur,
- alpha: game.add._default.alpha,
- font: style.font || game.add._default.font,
- fill: style.fill || game.add._default.fill,
- align: align || style.align || game.add._default.align,
- anchor: function () { console.error('Game error: there\'s no anchor for text'); },
- set style (style) {
- this.font = style.font;
- this.fill = style.fill;
- this.align = style.align;
- },
- get xWithAnchor() { return this.x; },
- get yWithAnchor() { return this.y; },
- };
- game.render.queue.push(med);
- return med;
- }
- },
-
- graphic: {
-
-
-
-
-
-
- rect: function (x, y, width, height, lineColor, lineWidth, fillColor, alpha) {
- if (x == undefined || y == undefined || width == undefined) console.error('Game error: missing parameters');
- else {
- const med = {
- typeOfMedia: 'rect',
- x: x || game.add._default.x,
- y: y || game.add._default.y,
- _xWithAnchor: x || game.add._default._xWithAnchor,
- _yWithAnchor: y || game.add._default._yWithAnchor,
- xAnchor: game.add._default.xAnchor,
- yAnchor: game.add._default.yAnchor,
- shadow: game.add._default.shadow,
- shadowColor: game.add._default.shadowColor,
- shadowBlur: game.add._default.shadowBlur,
- alpha: (alpha != undefined) ? alpha : game.add._default.alpha,
- scale: game.add._default.scale,
- width: 0,
- height: 0,
- lineColor: lineColor || game.add._default.lineColor,
- lineWidth: 0,
- fillColor: fillColor || game.add._default.fillColor,
- anchor: function (xAnchor, yAnchor) {
- this.xAnchor = xAnchor;
- this.yAnchor = yAnchor;
- },
- get xWithAnchor() { return this.x - (this.width * this.scale * this.xAnchor); },
- get yWithAnchor() { return this.y - (this.height * this.scale * this.yAnchor); }
- };
- med.originalScale = med.scale;
- if (width != 0) { med.width = width || game.add._default.width; }
- if (height != 0) { med.height = height || width || game.add._default.height; }
- if (lineWidth != 0) { med.lineWidth = lineWidth || game.add._default.lineWidth; }
- game.render.queue.push(med);
- return med;
- }
- },
-
-
-
-
-
-
- circle: function (x, y, diameter, lineColor, lineWidth, fillColor, alpha) {
- if (x == undefined || y == undefined || diameter == undefined) console.error('Game error: missing parameters');
- else {
- const med = {
- typeOfMedia: 'arc',
- x: x || game.add._default.x,
- y: y || game.add._default.y,
- _xWithAnchor: x || game.add._default._xWithAnchor,
- _yWithAnchor: y || game.add._default._yWithAnchor,
- xAnchor: game.add._default.xAnchor,
- yAnchor: game.add._default.yAnchor,
- shadow: game.add._default.shadow,
- shadowColor: game.add._default.shadowColor,
- shadowBlur: game.add._default.shadowBlur,
- alpha: (alpha != undefined) ? alpha : game.add._default.alpha,
- scale: game.add._default.scale,
- diameter: 0,
- width: 0,
- height: 0,
- angleStart: 0,
- angleEnd: 2 * Math.PI,
- anticlockwise: game.add._default.anticlockwise,
- lineColor: lineColor || game.add._default.lineColor,
- lineWidth: 0,
- fillColor: fillColor || game.add._default.fillColor,
- anchor: function (xAnchor, yAnchor) {
- this.xAnchor = xAnchor;
- this.yAnchor = yAnchor;
- },
- get xWithAnchor() { return this.x - (this.width * this.scale * this.xAnchor); },
- get yWithAnchor() { return this.y - (this.height * this.scale * this.yAnchor); }
- };
- med.originalScale = med.scale;
- if (diameter != 0) {
- med.diameter = diameter || game.add._default.diameter;
- med.width = med.height = med.diameter;
- }
- if (lineWidth != 0) {
- med.lineWidth = lineWidth || game.add._default.lineWidth;
- }
- game.render.queue.push(med);
- return med;
- }
- },
-
-
-
-
-
-
-
- arc: function (x, y, diameter, angleStart, angleEnd, anticlockwise, lineColor, lineWidth, fillColor, alpha) {
- if (x == undefined || y == undefined || diameter == undefined || angleStart == undefined || angleEnd == undefined) console.error('Game error: missing parameters');
- else {
- const med = {
- typeOfMedia: 'arc',
- x: x || game.add._default.x,
- y: y || game.add._default.y,
- _xWithAnchor: x || game.add._default._xWithAnchor,
- _yWithAnchor: y || game.add._default._yWithAnchor,
- xAnchor: game.add._default.xAnchor,
- yAnchor: game.add._default.yAnchor,
- shadow: game.add._default.shadow,
- shadowColor: game.add._default.shadowColor,
- shadowBlur: game.add._default.shadowBlur,
- alpha: (alpha != undefined) ? alpha : game.add._default.alpha,
- scale: game.add._default.scale,
- diameter: 0,
- width: 0,
- height: 0,
- angleStart: angleStart || 0,
- angleEnd: angleEnd || 2 * Math.PI,
- anticlockwise: anticlockwise || game.add._default.anticlockwise,
- lineColor: lineColor || game.add._default.lineColor,
- lineWidth: 0,
- fillColor: fillColor || game.add._default.fillColor,
- anchor: function (xAnchor, yAnchor) {
- this.xAnchor = xAnchor;
- this.yAnchor = yAnchor;
- },
- get xWithAnchor() { return this.x - (this.width * this.scale * this.xAnchor); },
- get yWithAnchor() { return this.y - (this.height * this.scale * this.yAnchor); }
- };
- med.originalScale = med.scale;
- if (diameter != 0) {
- med.diameter = diameter || game.add._default.diameter;
- med.width = med.height = med.diameter;
- }
- if (lineWidth != 0) { med.lineWidth = lineWidth || game.add._default.lineWidth; }
- game.render.queue.push(med);
- return med;
- }
- }
- },
- _default: {
-
- x: 0,
- y: 0,
- _xWithAnchor: 0,
- _yWithAnchor: 0,
- xAnchor: 0,
- yAnchor: 0,
- shadow: false,
- shadowColor: '#0075c5',
- shadowBlur: 20,
- alpha: 1,
-
- scale: 1,
-
- font: '14px Arial,sans-serif',
- fill: '#000',
- align: 'center',
-
- width: 50,
- height: 50,
- lineColor: '#000',
- lineWidth: 1,
- fillColor: 0,
-
- diameter: 50,
- anticlockwise: false,
- },
- },
-
- render: {
- queue: [],
-
- _image: function (cur) {
- const x = cur.xWithAnchor, y = cur.yWithAnchor;
- if (cur.rotate && cur.rotate != 0) {
- context.save();
- context.translate(cur.x, cur.y);
- context.rotate(cur.rotate * Math.PI / 180);
- context.translate(-cur.x, -cur.y);
- }
- context.globalAlpha = cur.alpha;
- context.shadowBlur = (cur.shadow) ? cur.shadowBlur : 0;
- context.shadowColor = cur.shadowColor;
- context.drawImage(
- game.image[cur.name],
- x,
- y,
- cur.width * cur.scale,
- cur.height * cur.scale
- );
- context.shadowBlur = 0;
- context.globalAlpha = 1;
- if (cur.rotate && cur.rotate != 0) context.restore();
- },
-
- _sprite: function (cur) {
- const x = cur.xWithAnchor, y = cur.yWithAnchor;
- if (cur.rotate && cur.rotate != 0) {
- context.save();
- context.translate(cur.x, cur.y);
- context.rotate(cur.rotate * Math.PI / 180);
- context.translate(-cur.x, -cur.y);
- }
- context.globalAlpha = cur.alpha;
- context.shadowBlur = (cur.shadow) ? cur.shadowBlur : 0;
- context.shadowColor = cur.shadowColor;
- context.drawImage(
- game.sprite[cur.name],
- cur.width * cur.curFrame,
- 0,
- cur.width,
- cur.height,
- x,
- y,
- cur.width * cur.scale,
- cur.height * cur.scale
- );
- context.shadowBlur = 0;
- context.globalAlpha = 1;
- if (cur.rotate && cur.rotate != 0) context.restore();
- },
-
- _text: function (cur) {
- const x = cur.xWithAnchor, y = cur.yWithAnchor;
- if (cur.rotate && cur.rotate != 0) {
- context.save();
- context.translate(cur.x, cur.y);
- context.rotate(cur.rotate * Math.PI / 180);
- context.translate(-cur.x, -cur.y);
- }
- context.globalAlpha = cur.alpha;
- context.shadowBlur = (cur.shadow) ? cur.shadowBlur : 0;
- context.shadowColor = cur.shadowColor;
- context.font = cur.font;
- context.textAlign = cur.align;
- context.fillStyle = cur.fill;
- context.fillText(cur.name, x, y);
- context.shadowBlur = 0;
- context.globalAlpha = 1;
- if (cur.rotate && cur.rotate != 0) context.restore();
- },
-
- _graphic: {
-
- _rect: function (cur) {
- const x = cur.xWithAnchor, y = cur.yWithAnchor;
-
- if (cur.rotate && cur.rotate != 0) {
- context.save();
- context.translate(cur.x, cur.y);
- context.rotate(cur.rotate * Math.PI / 180);
- context.translate(-cur.x, -cur.y);
- }
-
- context.globalAlpha = cur.alpha;
-
- context.shadowBlur = (cur.shadow) ? cur.shadowBlur : 0;
- context.shadowColor = cur.shadowColor;
-
- if (cur.fillColor != 0) {
- context.fillStyle = cur.fillColor;
- context.fillRect(x, y, cur.width * cur.scale, cur.height * cur.scale);
- }
-
- if (cur.lineWidth != 0) {
- context.strokeStyle = cur.lineColor;
- context.lineWidth = cur.lineWidth;
- context.strokeRect(x, y, cur.width * cur.scale, cur.height * cur.scale);
- }
-
- context.shadowBlur = 0;
- context.globalAlpha = 1;
- if (cur.rotate && cur.rotate != 0) context.restore();
- },
-
- _arc: function (cur) {
- const x = cur.xWithAnchor, y = cur.yWithAnchor;
-
- if (cur.rotate && cur.rotate != 0) {
- context.save();
- context.translate(cur.x, cur.y);
- context.rotate(cur.rotate * Math.PI / 180);
- context.translate(-cur.x, -cur.y);
- }
-
- context.globalAlpha = cur.alpha;
-
- context.shadowBlur = (cur.shadow) ? cur.shadowBlur : 0;
- context.shadowColor = cur.shadowColor;
-
- if (cur.fillColor != 0) context.fillStyle = cur.fillColor;
-
- if (cur.lineWidth != 0) {
- context.strokeStyle = cur.lineColor;
- context.lineWidth = cur.lineWidth;
- }
-
- context.beginPath();
- if (cur.angleEnd != 2 * Math.PI) context.lineTo(x, y);
- context.arc(x, y, (cur.diameter / 2) * cur.scale, cur.angleStart, cur.angleEnd, cur.anticlockwise);
- if (cur.angleEnd != 2 * Math.PI) context.lineTo(x, y);
-
- if (cur.fillColor != 0) context.fill();
- if (cur.lineWidth != 0) context.stroke();
- context.shadowBlur = 0;
- context.globalAlpha = 1;
- if (cur.rotate && cur.rotate != 0) context.restore();
- },
- },
-
-
- all: function () {
- game.render.queue.forEach(cur => {
- switch (cur.typeOfMedia) {
- case 'image': this._image(cur); break;
- case 'sprite': this._sprite(cur); break;
- case 'text': this._text(cur); break;
- case 'rect': this._graphic._rect(cur); break;
- case 'arc': this._graphic._arc(cur); break;
- }
- });
- },
-
-
- clear: function () {
- game.render.queue = [];
- }
- },
-
- math: {
-
- randomInRange: function (min, max) {
- min = Math.ceil(min);
- max = Math.floor(max);
- return Math.floor(Math.random() * (max - min + 1) + min);
- },
-
- randomDivisor: function (number) {
- const validDivisors = [];
- for (let i = 2; i < number; i++) {
-
- if (number % i == 0) validDivisors.push(i);
- }
- const randIndex = game.math.randomInRange(0, validDivisors.length - 1);
- return validDivisors[randIndex];
- },
-
- degreeToRad: function (degree) {
- return degree * Math.PI / 180;
- },
-
- distanceToPointer: function (xMouse, xIcon, yMouse, yIcon) {
- const a = Math.max(xMouse, xIcon) - Math.min(xMouse, xIcon);
- const b = Math.max(yMouse, yIcon) - Math.min(yMouse, yIcon);
- return Math.sqrt(a * a + b * b);
- },
-
- isOverIcon: function (xMouse, yMouse, icon) {
- const x = xMouse, y = yMouse, cur = icon;
- return y >= cur.yWithAnchor && y <= (cur.yWithAnchor + cur.height * cur.scale) &&
- (x >= cur.xWithAnchor && x <= (cur.xWithAnchor + cur.width * cur.scale));
- }
- },
-
- timer: {
- _start: 0,
- _end: 0,
- elapsed: 0,
-
-
- start: function () {
- game.timer._start = game.timer._end = game.timer._elapsed = 0;
- game.timer._start = new Date().getTime();
- },
-
-
- stop: function () {
- if (game.timer._start != 0 && game.timer._end == 0) {
- game.timer._end = new Date().getTime();
- game.timer._elapsed = Math.floor((game.timer._end - game.timer._start) / 1000);
- }
- },
- get elapsed() { return game.timer._elapsed; }
- },
-
- event: {
- _list: [],
-
- add: function (name, func) {
- canvas.addEventListener(name, func);
- game.event._list.push([name, func]);
- },
-
-
- clear: function () {
- game.event._list.forEach(cur => {
- canvas.removeEventListener(cur[0], cur[1]);
- });
- game.event._list = [];
- },
- },
-
- loop: {
- id: undefined,
- curState: undefined,
- status: 'off',
- waitingToStart: undefined,
- startTime: 0,
- duration: 1000 / 60,
-
-
- start: function (state) {
- if (game.loop.status == 'off') {
- game.loop.curState = state;
- game.loop.startTime = new Date().getTime();
- game.loop.status = 'on';
- game.loop.id = requestAnimationFrame(game.loop._run);
- } else {
- game.loop.waitingToStart = state;
- if (game.loop.status == 'on') game.loop.stop();
- }
- },
-
-
- stop: function () {
- if (game.loop.status == 'on') game.loop.status = 'ending';
- },
-
- _run: function () {
- if (game.loop.status != 'on') {
- game.loop._clear();
- } else {
- const timestamp = new Date().getTime();
- const runtime = timestamp - game.loop.startTime;
- if (runtime >= game.loop.duration) {
- if (debugMode) {
- let fps = runtime / 1000;
- fps = Math.round(1 / fps);
- displayFps.innerHTML = 'Fps: ' + fps;
- }
-
- game.loop.curState.update();
-
- game.animation._run();
- }
- game.loop.id = requestAnimationFrame(game.loop._run);
- }
- },
-
- _clear: function () {
- if (game.loop.id != undefined) {
- cancelAnimationFrame(game.loop.id);
- game.loop.id = undefined;
- game.loop.curState = undefined;
- game.loop.status = 'off';
- displayFps.innerHTML = '';
- }
- if (game.loop.waitingToStart != undefined) {
- const temp = game.loop.waitingToStart;
- game.loop.waitingToStart = undefined;
- game.loop.start(temp);
- }
- },
- },
-
- animation: {
- queue: [],
- count: 0,
-
-
- play: function (name) {
- let newAnimation;
-
- for (let i in game.render.queue) {
- if (game.render.queue[i].animation != undefined && game.render.queue[i].animation[0] == name) {
- newAnimation = game.render.queue[i];
- break;
- }
- }
-
- if (newAnimation != undefined) game.animation.queue.push(newAnimation);
- },
-
-
- stop: function (name) {
-
- game.animation.queue.forEach(cur => {
- if (cur.animation[0] == name) {
- game.animation.queue.splice(cur, 1);
- }
- });
- },
-
- _run: function () {
- game.animation.queue.forEach(character => {
- if (!character.animation[2] || game.animation.count % character.animation[2] == 0) {
- const i = character.animation[1].indexOf(character.curFrame);
- if (i == -1) {
- if (debugMode) console.error('Game error: animation frame not found');
- } else if (i < character.animation[1].length - 1) {
- character.curFrame = character.animation[1][i + 1];
- } else {
- character.curFrame = character.animation[1][0];
- }
- }
- });
- game.animation.count++;
- },
-
-
- clear: function () {
-
- game.animation.count = 0;
-
- game.render.queue.forEach(cur => {
- if (cur.animation != undefined) {
- delete cur.animation;
- }
- });
-
- game.animation.queue = [];
- },
- },
-
- state: {
- list: [],
- name: undefined,
-
-
- add: function (name, obj) {
- game.state.list[name] = obj;
- },
-
-
- start: function (name) {
- document.body.style.cursor = 'auto';
- game.loop.stop();
- game.event.clear();
- game.animation.clear();
- game.state.name = name;
- self = game.state.list[name];
- if (self.preload) self.preload();
- else game.state._create();
- },
-
- _create: function () {
- game.render.clear();
- self.create();
- game.render.all();
- if (self.restart && self.restart == true) {
- game.state.start(game.state.name);
- } else {
- if (self.update) game.loop.start(self);
- }
- },
- },
- };
|