123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674 |
- const game = {
- image: {},
- sprite: {},
- audio: {},
- lang: {},
-
- state: {
-
- list: [],
-
- name: undefined,
-
- progressBar: undefined,
- progressBarLabel: 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.loadedCur = 0;
- game.loadedMax = 0;
- game.state.name = name;
- navigation.list = [];
- self = game.state.list[name];
- if (self.preload) {
- game.render.clear();
-
- game.add.geom.rect(
- 0,
- 0,
- context.canvas.width,
- context.canvas.height,
- colors.blueBg,
- 1
- );
- game.add.geom.rect(
- context.canvas.width / 2 - 500 / 2,
- context.canvas.height / 2 - 40 / 2,
- 500,
- 40,
- colors.white,
- 1
- );
- game.state.progressBar = game.add.geom.rect(
- context.canvas.width / 2 - 500 / 2,
- context.canvas.height / 2,
- 40,
- 40,
- colors.blue,
- 0.4
- );
- game.state.progressBar.anchor(0, 0.5);
- game.state.progressBarLabel = game.add.text(
- context.canvas.width / 2,
- context.canvas.height / 2 + 80,
- 'Loading...',
- textStyles.h2_
- );
-
- self.preload();
- } else {
- game.state.create();
- }
- },
-
- create: function () {
- game.render.clear();
- if (!self.create) {
- console.error(
- "Game error: The state called does not have a 'create' function. Unable to continue."
- );
- } else {
- self.create();
- game.render.all();
- if (self && self.restart) {
-
- game.state.start(game.state.name);
- } else {
- if (self.update) game.loop.start(self);
- }
- }
- },
- },
- loadHandler: {
- cur: 0,
- max: 0,
-
- type: {
- lang: { isLoading: false, cached: 0, length: 0 },
- audio: { isLoading: false, cached: 0, length: 0 },
- image: { isLoading: false, cached: 0, length: 0 },
- sprite: { isLoading: false, cached: 0, length: 0 },
- },
-
- getUncachedUrlsOnly: function (unfilteredUrls, mediaCategory) {
- const uncachedUrls = [];
- unfilteredUrls.forEach((url) => {
- if (mediaCategory[url[0]] === undefined) uncachedUrls.push(url);
- });
- return uncachedUrls;
- },
-
- cachedOneFile: function (mediaType) {
- const length = game.loadHandler.type[mediaType].length;
-
- if (length - 1 !== 0) {
-
-
-
-
-
- game.state.progressBar.width =
- (500 / game.loadHandler.max) * game.loadHandler.cur;
- game.render.all();
- game.loadHandler.cur++;
-
- }
-
- if (game.loadHandler.type[mediaType].cached >= length - 1) {
-
- game.loadHandler.type[mediaType].isLoading = false;
- game.loadHandler.type[mediaType].cached = 0;
- game.loadHandler.type[mediaType].length = 0;
-
- game.loadHandler.cachedAllInOneMediaType();
- } else {
-
- game.loadHandler.type[mediaType].cached++;
- }
- },
-
- cachedAllInOneMediaType: function () {
-
- let endPreload = true;
- for (let key in game.loadHandler.type) {
- if (game.loadHandler.type[key].isLoading) {
- endPreload = false;
- break;
- }
- }
-
- if (endPreload) {
- game.loadHandler.cur = 0;
- game.loadHandler.max = 0;
- game.state.create();
- }
- },
- },
-
- load: {
-
- lang: function (url) {
- game.loadHandler.type.lang.isLoading = true;
- game.loadHandler.type.lang.cached = 0;
- game.lang = {};
- fetch(url, { mode: 'same-origin' })
- .then((response) => {
- if (!response.ok)
- throw new Error(
- 'Game error: translation not found "' + url[0] + '".'
- );
- return response.text();
- })
- .then((text) => {
- const lines = text.split('\n');
- game.loadHandler.type.lang.length = lines.length;
- game.loadHandler.max += lines.length;
- lines.forEach((line) => {
- try {
- const msg = line.split('=');
- if (msg.length !== 2)
- throw Error('Game error: sintax error in i18y file.');
- game.lang[msg[0].trim()] = msg[1].trim();
- } catch (error) {
- console.error(error.message);
- }
- game.loadHandler.cachedOneFile('lang');
- });
- })
- .catch((error) => {
- game.loadHandler.cachedOneFile('lang');
- console.error(error);
- });
- },
-
- audio: function (urls) {
- game.loadHandler.type.audio.isLoading = true;
- game.loadHandler.type.audio.cached = 0;
- urls = game.loadHandler.getUncachedUrlsOnly(urls, game.audio);
- game.loadHandler.type.audio.length = urls.length;
- if (urls.length == 0) {
- game.loadHandler.cachedOneFile('audio');
- } else {
- game.loadHandler.max += urls.length;
- urls.forEach((url) => {
- fetch(url[1][1], { mode: 'same-origin' })
- .then((response) => {
-
- if (!response.ok)
- throw new Error(
- 'Game error: audio not found "' + url[0] + '".'
- );
- return response.blob();
- })
- .then((data) => {
- game.audio[url[0]] = new Audio(URL.createObjectURL(data));
- game.loadHandler.cachedOneFile('audio');
- })
-
- .catch((error) => {
- game.loadHandler.cachedOneFile('audio');
- console.error(error);
- });
- });
- }
- },
-
- image: function (urls) {
- game.loadHandler.type.image.isLoading = true;
- game.loadHandler.type.image.cached = 0;
- urls = game.loadHandler.getUncachedUrlsOnly(urls, game.image);
- game.loadHandler.type.image.length = urls.length;
- if (urls.length == 0) {
- game.loadHandler.cachedOneFile('image');
- } else {
- game.loadHandler.max += urls.length;
- urls.forEach((url) => {
- try {
- const img = new Image();
- img.onload = () => {
- game.image[url[0]] = img;
- game.loadHandler.cachedOneFile('image');
- };
- img.onerror = () => {
- console.error(
- 'Game error: image not found "' +
- url[0] +
- '". Loading fallback image.'
- );
-
- game.image[url[0]] = img;
- img.src = fallbackImgUrl;
- game.loadHandler.cachedOneFile('image');
- };
- img.src = url[1];
- } catch (error) {
-
- console.error(
- 'Game error: unkown image error "' + url[0] + '" - ' + error
- );
- }
- });
- }
- },
-
- sprite: function (urls) {
- game.loadHandler.type.sprite.isLoading = true;
- game.loadHandler.type.sprite.cached = 0;
- urls = game.loadHandler.getUncachedUrlsOnly(urls, game.sprite);
- game.loadHandler.type.sprite.length = urls.length;
- if (urls.length == 0) {
- game.loadHandler.cachedOneFile('sprite');
- } else {
- game.loadHandler.max += urls.length;
- urls.forEach((url) => {
- try {
- const img = new Image();
- img.onload = () => {
- game.sprite[url[0]] = img;
- game.loadHandler.cachedOneFile('sprite');
- };
- img.onerror = () => {
- console.error(
- 'Game error: sprite not found "' +
- url[0] +
- '". Loading fallback image.'
- );
-
- game.sprite[url[0]] = img;
- img.src = fallbackImgUrl;
- img.frames = 1;
- game.loadHandler.cachedOneFile('sprite');
- };
- img.src = url[1];
- img.frames = url[2];
- } catch (error) {
-
- console.error(
- 'Game error: unkown sprite error "' + url[0] + '" - ' + error
- );
- }
- });
- }
- },
- },
-
- add: {
-
- image: function (x, y, img, scale, alpha) {
- if (x == undefined || y == undefined || img == undefined)
- console.error(
- 'Game error: Could not render image "' +
- img +
- '". Missing required 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.initialScale = 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: Could not render sprite "' +
- img +
- '". Missing required 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.initialScale = med.scale;
- game.render.queue.push(med);
- return med;
- }
- },
-
- text: function (x, y, text, style, lineHeight) {
- if (
- x == undefined ||
- y == undefined ||
- text == undefined ||
- style == undefined
- ) {
- console.error(
- 'Game error: Could not render text. Missing required parameters.'
- );
- } else {
- const med = {
- typeOfMedia: 'text',
- name:
- style && style.increaseLetterSpacing
- ? text.split('').join(String.fromCharCode(8202))
- : 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: style.align || game.add.default.align,
- lineHeight: lineHeight || game.add.default.lineHeight,
- 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;
- }
- },
-
- geom: {
-
- rect: function (
- x,
- y,
- width,
- height,
- fillColor,
- alpha,
- lineColor,
- lineWidth
- ) {
- if (x == undefined || y == undefined || width == undefined)
- console.error(
- 'Game error: Could not render rectangle. Missing required 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.initialScale = 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;
- }
- },
-
- line: function (x0, y0, x1, y1, lineWidth, lineColor, alpha) {
- if (x0 == undefined || y0 == undefined)
- console.error(
- 'Game error: Could not render line. Missing required parameters.'
- );
- else {
- const med = {
- typeOfMedia: 'line',
- x0: x0 || game.add.default.x,
- y0: y0 || game.add.default.y,
- x1: x1 || x0 + 50,
- y1: y1 || y0,
- x0Anchor: 0,
- y0Anchor: 0,
- x1Anchor: 0,
- y1Anchor: 0,
- shadow: game.add.default.shadow,
- shadowColor: game.add.default.shadowColor,
- shadowBlur: game.add.default.shadowBlur,
- alpha: alpha != undefined ? alpha : game.add.default.alpha,
- width: 0,
- lineColor: lineColor || game.add.default.lineColor,
- lineWidth: lineWidth || 1,
- anchor: function (xAnchor, yAnchor) {
- this.x0Anchor = xAnchor;
- this.y0Anchor = yAnchor;
- this.x1Anchor = xAnchor;
- this.y1Anchor = yAnchor;
- },
- get x0WithAnchor() {
- return this.x0 - this.width * this.x0Anchor;
- },
- get y0WithAnchor() {
- return this.y0 - this.width * this.y0Anchor;
- },
- get x1WithAnchor() {
- return this.x1 - this.width * this.x1Anchor;
- },
- get y1WithAnchor() {
- return this.y1 - this.width * this.y1Anchor;
- },
- get x() {
- return this.x0;
- },
- get y() {
- return this.y0;
- },
- set x(x0) {
- const distance = this.x1 - this.x0;
- this.x0 = x0;
- this.x1 = x0 + distance;
- },
- set y(y0) {
- const distance = this.y1 - this.y0;
- this.y0 = y0;
- this.y1 = y0 + distance;
- },
- };
- const width =
- (med.x1 - x0) * (med.x1 - x0) + (med.y1 - y0) * (med.y1 - y0);
- med.width = Math.sqrt(width);
- 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: Could not render circle. Missing required 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,
- counterclockwise: game.add.default.counterclockwise,
- 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.initialScale = 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,
- counterclockwise,
- lineColor,
- lineWidth,
- fillColor,
- alpha
- ) {
- if (
- x == undefined ||
- y == undefined ||
- diameter == undefined ||
- angleStart == undefined ||
- angleEnd == undefined
- )
- console.error(
- 'Game error: Could not render arc. Missing required 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,
- counterclockwise:
- counterclockwise || game.add.default.counterclockwise,
- 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.initialScale = 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',
- lineHeight: 50,
-
- width: 50,
- height: 50,
- lineColor: '#000',
- lineWidth: 0,
- fillColor: '#fff',
-
- diameter: 50,
- counterclockwise: 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;
-
- const paragraphs = String(cur.name).split('\n');
- let newY = y;
- for (let line in paragraphs) {
- context.fillText(paragraphs[line], x, newY);
- newY += cur.lineHeight;
- }
-
- context.shadowBlur = 0;
- context.globalAlpha = 1;
- if (cur.rotate && cur.rotate != 0) context.restore();
- },
-
- geom: {
-
- 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 !== 'transparent') {
- 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();
- },
-
- line: function (cur) {
- const x0 = cur.x0WithAnchor,
- y0 = cur.y0WithAnchor,
- x1 = cur.x1WithAnchor,
- y1 = cur.y1WithAnchor;
-
- if (cur.rotate && cur.rotate != 0) {
- context.save();
- context.translate(cur.x0, cur.y0);
- context.rotate((cur.rotate * Math.PI) / 180);
- context.translate(-cur.x0, -cur.y0);
- }
-
- context.globalAlpha = cur.alpha;
-
- context.shadowBlur = cur.shadow ? cur.shadowBlur : 0;
- context.shadowColor = cur.shadowColor;
-
- if (cur.lineWidth != 0) {
- context.strokeStyle = cur.lineColor;
- context.lineWidth = cur.lineWidth;
- context.beginPath();
- context.moveTo(x0, y0);
- context.lineTo(x1, y1);
- context.stroke();
- }
-
- 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.counterclockwise
- );
- 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.geom.rect(cur);
- break;
- case 'line':
- this.geom.line(cur);
- break;
- case 'arc':
- this.geom.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;
- },
- getRadiusFromCircunference: function (circunference) {
- return circunference / (2 * Math.PI);
- },
-
- 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);
- },
- mdc: function (num1, num2) {
- if (num2 === 0) return num1;
- return game.math.mdc(num2, num1 % num2);
- },
- mmcArray: (input) => {
- if (toString.call(input) !== '[object Array]') return false;
- var len, a, b;
- len = input.length;
- if (!len) {
- return null;
- }
- a = input[0];
- for (var i = 1; i < len; i++) {
- b = input[i];
- a = game.math.mmcTwoNumbers(a, b);
- }
- return a;
- },
- mmcTwoNumbers: (num1, num2) => {
- var resto, x, y;
- x = num1;
- y = num2;
- while (resto != 0) {
- resto = x % y;
- x = y;
- y = resto;
- }
- return (num1 * num2) / x;
- },
- getFractionFromDecimal: function (fraction) {
- const len = fraction.toString().length - 2;
- let denominator = Math.pow(10, len);
- let numerator = fraction * denominator;
- const divisor = game.math.greatestCommonDivisor(numerator, denominator);
- numerator /= divisor;
- denominator /= divisor;
- return {
- string: Math.floor(numerator) + '/' + Math.floor(denominator),
- numerator: Math.floor(numerator),
- denominator: Math.floor(denominator),
- };
- },
-
- 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
- );
- },
-
- isOverlap: function (imageA, imageB) {
- const xA = imageA.x;
- const xB = imageB.x;
-
- return !(Math.abs(xA - xB) > 14);
- },
-
- getMouse: function (mouseEvent) {
- const c = context.canvas.getBoundingClientRect();
- const canvas_scale = context.canvas.width / parseFloat(c.width);
- return {
- x: (mouseEvent.clientX - c.left) * canvas_scale,
- y: (mouseEvent.clientY - c.top) * canvas_scale,
- };
- },
-
- getOffset: function (width, numberOfIcons) {
- return width / (numberOfIcons + 1);
- },
-
- convertTime: function (s) {
- let h = 0,
- m = 0;
- if (s > 1200) {
- h = s / 1200;
- s = s % 1200;
- }
- if (s > 60) {
- m = s / 60;
- s = s % 60;
- }
- h = '' + h;
- m = '' + m;
- s = '' + s;
- if (h.length < 2) h = '0' + h;
- if (m.length < 2) m = '0' + m;
- if (s.length < 2) s = '0' + s;
- return h + ':' + m + ':' + s;
- },
- },
-
- 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
- );
- }
- },
- },
-
- event: {
-
- list: [],
-
- add: function (name, func) {
- context.canvas.addEventListener(name, func);
- game.event.list.push([name, func]);
- },
-
- clear: function () {
- game.event.list.forEach((cur) => {
- context.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) {
-
- 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';
- }
- 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 (isDebugMode)
- 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 = [];
- },
- },
- };
|