123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671 |
- const squareOne = {
-
- create: function () {
-
- this.checkAnswer = false;
- this.animate = false;
- this.animateEnding = false;
- this.hasClicked = false;
- this.result = false;
- this.count = 0;
- this.divisorsList = '';
- this.DIREC_LEVEL = (gameOperationType == 'Minus') ? -1 : 1;
- this.animationSpeed = 2 * this.DIREC_LEVEL;
-
- this.defaultBlockWidth = 80;
- this.defaultBlockHeight = 40;
- this.startX = (gameOperationType == 'Minus') ? 730 : 170;
-
-
- game.add.image(0, 0, 'bgimage');
-
- game.add.image(300, 100, 'cloud');
- game.add.image(660, 80, 'cloud');
- game.add.image(110, 85, 'cloud', 0.8);
-
- for (let i = 0; i < 9; i++) { game.add.image(i * 100, 501, 'floor'); }
-
- navigationIcons.func_addIcons(
- true, true, true,
- true, false,
- 'customMenu', this.func_viewHelp
- );
-
- this.tractor = game.add.sprite(this.startX, 445, 'tractor', 0, 0.8);
- if (gameOperationType == 'Plus') {
- this.tractor.anchor(1, 0.5);
- this.tractor.animation = ['move', [0, 1, 2, 3, 4], 4];
- } else {
- this.tractor.anchor(0, 0.5);
- this.tractor.animation = ['move', [5, 6, 7, 8, 9], 4];
- this.tractor.curFrame = 5;
- }
-
- this.stck = {
- blocks: [],
- labels: [],
- index: undefined,
-
- curIndex: 0,
- curBlockEnd: undefined,
-
- correctIndex: undefined,
- };
-
- this.floor = {
- blocks: [],
- index: undefined,
-
- curIndex: -1,
-
- correctIndex: undefined,
- correctX: undefined,
- correctXA: undefined,
- correctXB: undefined,
- };
-
- this.restart = this.func_createStckBlocks();
-
- this.func_createFloorBlocks();
-
- if (gameModeType == 'A') {
- this.arrow = game.add.image(this.startX + this.defaultBlockWidth * this.DIREC_LEVEL, 480, 'arrow_down');
- this.arrow.anchor(0.5, 0.5);
- this.arrow.alpha = 0.5;
- }
-
- this.help = game.add.image(0, 0, 'help_pointer', 0.5);
- this.help.anchor(0.5, 0);
- this.help.alpha = 0;
- if (!this.restart) {
- game.timer.start();
- game.event.add('click', this.func_onInputDown);
- game.event.add('mousemove', this.func_onInputOver);
- }
- },
-
- update: function () {
-
-
- if (self.animate) {
- const stck = self.stck;
- const floor = self.floor;
-
-
- self.tractor.x += self.animationSpeed;
-
- for (let i in stck.blocks) {
- stck.blocks[i].x += self.animationSpeed;
- }
-
-
- const restOfCurBlock = (self.defaultBlockWidth - stck.blocks[stck.curIndex].width) * self.DIREC_LEVEL;
-
- if ((gameOperationType == 'Plus' && stck.blocks[0].x >= (stck.curBlockEnd + restOfCurBlock)) ||
- (gameOperationType == 'Minus' && stck.blocks[0].x <= (stck.curBlockEnd + restOfCurBlock))) {
- let lowerBlock = true;
- const curEnd = stck.blocks[0].x + stck.blocks[stck.curIndex].width * self.DIREC_LEVEL;
-
-
- if (stck.curIndex == stck.index) {
-
-
- const selectedEnd = floor.blocks[floor.index].x + floor.blocks[0].width * self.DIREC_LEVEL;
-
-
- if ((gameOperationType == 'Plus' && curEnd > selectedEnd) || (gameOperationType == 'Minus' && curEnd < selectedEnd)) {
- lowerBlock = false;
- }
- } else {
-
- stck.curBlockEnd += stck.blocks[stck.curIndex + 1].width * self.DIREC_LEVEL;
- }
-
- if (lowerBlock) {
-
-
-
- for (let i = 0; i <= floor.index; i++) {
- if ((gameOperationType == 'Plus' && floor.blocks[i].x < curEnd) || (gameOperationType == 'Minus' && floor.blocks[i].x > curEnd)) {
- floor.blocks[i].alpha = 0.2;
- floor.curIndex = i;
- }
- }
-
- stck.blocks[stck.curIndex].alpha = 0;
- stck.blocks.forEach(cur => { cur.y += self.defaultBlockHeight - 2; });
- }
- stck.curIndex++;
- }
-
- if (stck.curIndex > stck.index || floor.curIndex == floor.index) {
- self.animate = false;
- self.checkAnswer = true;
- }
- }
-
- if (self.checkAnswer) {
- game.timer.stop();
- game.animation.stop(self.tractor.animation[0]);
- if (gameModeType == 'A') {
- self.result = self.floor.index == self.floor.correctIndex;
- } else {
- self.result = self.stck.index == self.stck.correctIndex;
- }
-
- if (self.result) {
- game.animation.play(self.tractor.animation[0]);
-
- game.add.image(defaultWidth / 2, defaultHeight / 2, 'ok').anchor(0.5, 0.5);
- if (audioStatus) game.audio.okSound.play();
- completedLevels++;
- if (debugMode) console.log('completedLevels = ' + completedLevels);
- } else {
-
- game.add.image(defaultWidth / 2, defaultHeight / 2, 'error').anchor(0.5, 0.5);
- if (audioStatus) game.audio.errorSound.play();
- }
- self.postScore();
-
- self.checkAnswer = false;
- self.animateEnding = true;
- }
-
- if (self.animateEnding) {
-
- self.count++;
-
- if (self.result) self.tractor.x += self.animationSpeed;
-
- if (self.count >= 140) {
-
- if (self.result) mapMove = true;
- else mapMove = false;
- game.state.start('map');
- }
- }
- game.render.all();
- },
-
-
- func_onInputDown: function (mouseEvent) {
- const x = mouseEvent.offsetX;
- const y = mouseEvent.offsetY;
- if (gameModeType == 'A') {
- self.floor.blocks.forEach(cur => {
- if (game.math.isOverIcon(x, y, cur)) self.func_clickSquare(cur);
- });
- } else {
- self.stck.blocks.forEach(cur => {
- if (game.math.isOverIcon(x, y, cur)) self.func_clickSquare(cur);
- });
- }
- navigationIcons.func_onInputDown(x, y);
- game.render.all();
- },
-
- func_onInputOver: function (mouseEvent) {
- const x = mouseEvent.offsetX;
- const y = mouseEvent.offsetY;
- let flagA = false;
- let flagB = false;
- if (gameModeType == 'A') {
-
- if (!self.hasClicked && !self.animateEnding) {
- if (game.math.distanceToPointer(self.arrow.x, x, self.arrow.y, y) > 8) {
- self.arrow.x = (x < 250) ? 250 : x;
- }
- }
- self.floor.blocks.forEach(cur => {
- if (game.math.isOverIcon(x, y, cur)) {
- flagA = true;
- self.func_overSquare(cur);
- }
- });
- if (!flagA) self.func_outSquare('A');
- }
- if (gameModeType == 'B') {
- self.stck.blocks.forEach(cur => {
- if (game.math.isOverIcon(x, y, cur)) {
- flagB = true;
- self.func_overSquare(cur);
- }
- });
- if (!flagB) self.func_outSquare('B');
- }
- navigationIcons.func_onInputOver(x, y);
- game.render.all();
- },
-
-
- func_overSquare: function (cur) {
- if (!self.hasClicked) {
- document.body.style.cursor = 'pointer';
-
- if (gameModeType == 'A') {
- for (let i in self.floor.blocks) {
- self.floor.blocks[i].alpha = (i <= cur.index) ? 1 : 0.5;
- }
-
- self.floor.index = cur.index;
-
- } else {
- for (let i in self.stck.blocks) {
- self.stck.blocks[i].alpha = (i <= cur.index) ? 0.5 : 0.2;
- }
-
- self.stck.index = cur.index;
- }
- }
- },
-
- func_outSquare: function () {
- if (!self.hasClicked) {
- document.body.style.cursor = 'auto';
-
- if (gameModeType == 'A') {
- for (let i in self.floor.blocks) {
- self.floor.blocks[i].alpha = 0.5;
- }
- self.floor.index = -1;
-
- } else {
- for (let i in self.stck.blocks) {
- self.stck.blocks[i].alpha = 0.5;
- }
- self.stck.index = -1;
- }
- }
- },
-
- func_clickSquare: function () {
- if (!self.hasClicked && !self.animateEnding) {
- document.body.style.cursor = 'auto';
-
- if (gameModeType == 'A') {
-
- self.arrow.alpha = 1;
-
- for (let i in self.floor.blocks) {
-
- if (i > self.floor.index) self.floor.blocks[i].alpha = 0;
- }
-
- self.stck.index = self.stck.blocks.length - 1;
-
- } else {
- for (let i in self.stck.blocks) {
-
- if (i > self.stck.index) self.stck.blocks[i].alpha = 0;
- }
-
- self.floor.index = self.floor.blocks.length - 1;
-
- self.stck.blocks.length = self.stck.index + 1;
- }
-
- if (audioStatus) game.audio.beepSound.play();
-
- if (fractionLabel) {
- self.stck.labels.forEach(cur => {
- cur.forEach(cur => { cur.alpha = 0; });
- });
- }
-
- if (self.help != undefined) self.help.alpha = 0;
-
- game.animation.play(self.tractor.animation[0]);
- self.hasClicked = true;
- self.animate = true;
- }
- },
-
-
- func_createStckBlocks: function () {
- let hasBaseDifficulty = false;
- const max = (gameModeType == 'B') ? 10 : mapPosition + 4;
- const total = game.math.randomInRange(mapPosition + 2, max);
- self.floor.correctXA = self.startX + self.defaultBlockWidth * self.DIREC_LEVEL;
- for (let i = 0; i < total; i++) {
- let divisor = game.math.randomInRange(1, gameDifficulty);
- if (divisor == gameDifficulty) hasBaseDifficulty = true;
- if (divisor == 3) divisor = 4;
- self.divisorsList += divisor + ',';
- const curBlockWidth = self.defaultBlockWidth / divisor;
- self.floor.correctXA += curBlockWidth * self.DIREC_LEVEL;
-
- const lineColor = (gameOperationType == 'Minus') ? colors.red : colors.darkBlue;
- const lineSize = 2;
- const block = game.add.graphic.rect(
- self.startX,
- 462 - i * (self.defaultBlockHeight - lineSize),
- curBlockWidth - lineSize,
- self.defaultBlockHeight - lineSize,
- lineColor,
- lineSize,
- colors.white,
- 1);
- const anchor = (gameOperationType == 'Minus') ? 1 : 0;
- block.anchor(anchor, 0);
-
- if (gameModeType == 'B') {
- block.alpha = 0.5;
- block.index = i;
- }
- self.stck.blocks.push(block);
-
- if (fractionLabel) {
- const x = self.startX + (curBlockWidth + 15) * self.DIREC_LEVEL;
- const y = self.defaultBlockHeight - lineSize;
- const label = [];
- if (divisor == 1) {
- label[0] = game.add.text(x, 488 - i * y, divisor, textStyles.h2_blue);
- } else {
- label[0] = game.add.text(x, 479 - i * y + 16, divisor, textStyles.p_blue);
- label[1] = game.add.text(x, 479 - i * y, '1', textStyles.p_blue);
- label[2] = game.add.text(x, 479 - i * y, '_', textStyles.p_blue);
- }
-
- self.stck.labels.push(label);
- }
- }
-
- self.stck.curBlockEnd = self.startX + self.stck.blocks[0].width * self.DIREC_LEVEL;
- let restart = false;
-
- if (!hasBaseDifficulty ||
- (gameOperationType == 'Plus' && (self.floor.correctXA < (self.startX + self.defaultBlockWidth) ||
- self.floor.correctXA > (self.startX + 8 * self.defaultBlockWidth))) ||
- (gameOperationType == 'Minus' && (self.floor.correctXA < (self.startX - (8 * self.defaultBlockWidth)) ||
- self.floor.correctXA > (self.startX - self.defaultBlockWidth)))
- ) {
- restart = true;
- }
- if (debugMode) console.log('Stacked blocks: ' + total + ' (min: ' + (mapPosition + 2) + ', max: ' + max + ')');
- return restart;
- },
-
- func_createFloorBlocks: function () {
- const divisor = (gameDifficulty == 3) ? 4 : gameDifficulty;
- let total = 8 * divisor;
- const blockWidth = self.defaultBlockWidth / divisor;
-
- if (gameModeType == 'B') {
- self.stck.correctIndex = game.math.randomInRange(0, (self.stck.blocks.length - 1));
- self.floor.correctXB = self.startX + self.defaultBlockWidth * self.DIREC_LEVEL;
- for (let i = 0; i <= self.stck.correctIndex; i++) {
- self.floor.correctXB += self.stck.blocks[i].width * self.DIREC_LEVEL;
- }
- }
- let flag = true;
- for (let i = 0; i < total; i++) {
-
- const x = self.startX + (self.defaultBlockWidth + i * blockWidth) * self.DIREC_LEVEL;
- if (flag && gameModeType == 'A') {
- if ((gameOperationType == 'Plus' && x >= self.floor.correctXA) || (gameOperationType == 'Minus' && x <= self.floor.correctXA)) {
- self.floor.correctIndex = i - 1;
- flag = false;
- }
- }
- if (gameModeType == 'B') {
- if ((gameOperationType == 'Plus' && x >= self.floor.correctXB) || (gameOperationType == 'Minus' && x <= self.floor.correctXB)) {
- total = i;
- break;
- }
- }
-
- const lineSize = 0.9;
- const block = game.add.graphic.rect(
- x,
- 462 + self.defaultBlockHeight - lineSize,
- blockWidth - lineSize,
- self.defaultBlockHeight - lineSize,
- colors.blueBckg,
- lineSize,
- colors.blueBckgInsideLevel,
- 1);
- const anchor = (gameOperationType == 'Minus') ? 1 : 0;
- block.anchor(anchor, 0);
-
- if (gameModeType == 'A') {
- block.alpha = 0.5;
- block.index = i;
- }
-
- self.floor.blocks.push(block);
- }
- if (gameModeType == 'A') self.floor.correctX = self.floor.correctXA;
- else if (gameModeType == 'B') self.floor.correctX = self.floor.correctXB;
-
- for (let i = 1; i < 10; i++) {
- const x = self.startX + (i * self.defaultBlockWidth * self.DIREC_LEVEL);
- game.add.text(x, 462 + self.defaultBlockHeight + 58, i - 1, textStyles.h2_blue);
- }
- },
-
- func_viewHelp: function () {
- if (!self.hasClicked) {
-
- if (gameModeType == 'A') {
- const aux = self.floor.blocks[0];
- self.help.x = self.floor.correctX - aux.width / 2 * self.DIREC_LEVEL;
- self.help.y = 501;
-
- } else {
- const aux = self.stck.blocks[self.stck.correctIndex];
- self.help.x = aux.x + aux.width / 2 * self.DIREC_LEVEL;
- self.help.y = aux.y;
- }
- self.help.alpha = 0.7;
- }
- },
-
-
- postScore: function () {
-
- const data = '&line_game=' + gameShape
- + '&line_mode=' + gameModeType
- + '&line_oper=' + gameOperationType
- + '&line_leve=' + gameDifficulty
- + '&line_posi=' + mapPosition
- + '&line_resu=' + self.result
- + '&line_time=' + game.timer.elapsed
- + '&line_deta='
- + 'numBlocks:' + self.stck.blocks.length
- + ', valBlocks: ' + self.divisorsList
- + ' blockIndex: ' + self.stck.index
- + ', floorIndex: ' + self.floor.index;
- sendToDB(data);
- },
- };
|