12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124 |
- const squareOne = {
- default: undefined,
- ui: undefined,
- control: undefined,
- animation: undefined,
- tractor: undefined,
- stack: undefined,
- floor: undefined,
-
- create: function () {
- const truckWidth = 201;
- const divisor = gameDifficulty == 3 ? 4 : gameDifficulty;
- let lineColor = undefined;
- let fillColor = undefined;
- if (gameOperation === 'minus') {
- lineColor = colors.red;
- fillColor = colors.redLight;
- } else {
- lineColor = colors.green;
- fillColor = colors.greenLight;
- }
- this.ui = {
- arrow: undefined,
- help: undefined,
- message: undefined,
- continue: {
-
- button: undefined,
- text: undefined,
- },
- };
- this.control = {
- direc: gameOperation == 'minus' ? -1 : 1,
- divisorsList: '',
- lineWidth: 3,
- hasClicked: false,
- checkAnswer: false,
- isCorrect: false,
- count: 0,
- };
- this.animation = {
- animateTractor: false,
- animateEnding: false,
- speed: 2 * this.control.direc,
- };
- this.default = {
- width: 172,
- height: 70,
- x0:
- gameOperation == 'minus'
- ? context.canvas.width - truckWidth
- : truckWidth,
- y0: context.canvas.height - game.image['floor_grass'].width * 1.5,
- };
- this.default.arrowX0 =
- self.default.x0 + self.default.width * self.control.direc;
- this.default.arrowXEnd =
- self.default.arrowX0 + self.default.width * self.control.direc * 8;
- renderBackground();
-
- if (moodle) {
- navigation.add.right(['audio']);
- } else {
- navigation.add.left(['back', 'menu', 'show_answer'], 'customMenu');
- navigation.add.right(['audio']);
- }
- this.stack = {
- list: [],
- selectedIndex: undefined,
- correctIndex: undefined,
- curIndex: 0,
- curBlockEndX: undefined,
- };
- this.floor = {
- list: [],
- selectedIndex: undefined,
- correctIndex: undefined,
- curIndex: -1,
- correctX: undefined,
- };
- let [restart, correctXA] = this.utils.renderStackedBlocks(
- this.control.direc,
- lineColor,
- fillColor,
- this.control.lineWidth,
- divisor
- );
- this.utils.renderFloorBlocks(
- this.control.direc,
- lineColor,
- this.control.lineWidth,
- divisor,
- correctXA
- );
- this.utils.renderCharacters();
- this.utils.renderMainUI();
- this.restart = restart;
- if (!this.restart) {
- game.timer.start();
- game.event.add('click', this.events.onInputDown);
- game.event.add('mousemove', this.events.onInputOver);
- }
- },
-
- update: function () {
-
- if (self.animation.animateTractor) {
- self.utils.animateTractorHandler();
- }
-
- if (self.control.checkAnswer) {
- self.utils.checkAnswerHandler();
- }
-
- if (self.animation.animateEnding) {
- self.utils.animateEndingHandler();
- }
- game.render.all();
- },
- utils: {
-
-
- renderStackedBlocks: (direc, lineColor, fillColor, lineWidth, divisor) => {
- let restart = false;
- let hasBaseDifficulty = false;
- const max = gameMode == 'b' ? 10 : curMapPosition + 4;
- const total = game.math.randomInRange(curMapPosition + 2, max);
- let correctXA = self.default.x0 + self.default.width * direc;
- for (let i = 0; i < total; i++) {
- let curFractionItems = undefined;
- let font = undefined;
- let curDivisor = game.math.randomInRange(1, gameDifficulty);
- if (curDivisor === gameDifficulty) hasBaseDifficulty = true;
- if (curDivisor === 3) curDivisor = 4;
- const curBlockWidth = self.default.width / curDivisor;
- self.control.divisorsList += curDivisor + ',';
- correctXA += curBlockWidth * direc;
- const curBlock = game.add.geom.rect(
- self.default.x0,
- self.default.y0 - i * self.default.height - lineWidth,
- curBlockWidth,
- self.default.height,
- fillColor,
- 1,
- lineColor,
- lineWidth
- );
- curBlock.anchor(gameOperation === 'minus' ? 1 : 0, 1);
- curBlock.blockValue = divisor / curDivisor;
-
- if (gameMode == 'b') {
- curBlock.blockIndex = i;
- }
- self.stack.list.push(curBlock);
-
- const x = self.default.x0 + (curBlockWidth + 40) * direc;
- const y = self.default.height + 1;
- if (curDivisor === 1) {
- font = textStyles.h2_;
- curFractionItems = [
- {
- x: x,
- y: self.default.y0 - i * y - 20,
- text: '1',
- },
- {
- x: x - 25,
- y: self.default.y0 - i * y - 27,
- text: gameOperation === 'minus' ? '-' : '',
- },
- null,
- ];
- } else {
- font = textStyles.p_;
- curFractionItems = [
- {
- x: x,
- y: self.default.y0 - i * y - 40,
- text: '1\n' + curDivisor,
- },
- {
- x: x - 25,
- y: self.default.y0 - i * y - 27,
- text: gameOperation === 'minus' ? '-' : '',
- },
- {
- x0: x,
- y: self.default.y0 - i * y - 35,
- x1: x + 25,
- lineWidth: 2,
- color: lineColor,
- },
- ];
- }
- font = { ...font, font: 'bold ' + font.font, fill: lineColor };
- const fractionPartsList = [];
- for (let i = 0; i < 2; i++) {
- const item = game.add.text(
- curFractionItems[i].x,
- curFractionItems[i].y,
- curFractionItems[i].text,
- font
- );
- item.lineHeight = 30;
- fractionPartsList.push(item);
- }
- if (curFractionItems[2]) {
- const line = game.add.geom.line(
- curFractionItems[2].x0,
- curFractionItems[2].y,
- curFractionItems[2].x1,
- curFractionItems[2].y,
- curFractionItems[2].lineWidth,
- curFractionItems[2].color
- );
- line.anchor(0.5, 0);
- fractionPartsList.push(line);
- } else {
- fractionPartsList.push(null);
- }
- curBlock.fraction = {
- labels: fractionPartsList,
- nominator: direc,
- denominator: curDivisor,
- };
- if (!showFractions) {
- curBlock.fraction.labels.forEach((label) => {
- if (label) label.alpha = 0;
- });
- }
- }
-
- if (gameMode === 'a') self.stack.selectedIndex = total - 1;
-
-
- self.stack.curBlockEndX =
- self.default.x0 + self.stack.list[0].width * direc;
-
- if (
- !hasBaseDifficulty ||
- (gameOperation == 'plus' &&
- (correctXA < self.default.x0 + self.default.width ||
- correctXA > self.default.x0 + 8 * self.default.width)) ||
- (gameOperation == 'minus' &&
- (correctXA < self.default.x0 - 8 * self.default.width ||
- correctXA > self.default.x0 - self.default.width))
- ) {
- restart = true;
- }
- if (isDebugMode)
- console.log(
- 'Stacked blocks: ' +
- total +
- ' (min: ' +
- (curMapPosition + 2) +
- ', max: ' +
- max +
- ')'
- );
- return [restart, correctXA];
- },
-
- renderFloorBlocks: (direc, lineColor, lineWidth, divisor, correctXA) => {
- let correctXB = 0;
- let total = 8 * divisor;
- const blockWidth = self.default.width / divisor;
-
- if (gameMode == 'b') {
- self.stack.correctIndex = game.math.randomInRange(
- 0,
- self.stack.list.length - 1
- );
- correctXB = self.default.x0 + self.default.width * direc;
- for (let i = 0; i <= self.stack.correctIndex; i++) {
- correctXB += self.stack.list[i].width * direc;
- }
- }
- let flag = true;
- for (let i = 0; i < total; i++) {
- const curX =
- self.default.x0 + (self.default.width + i * blockWidth) * direc;
- if (flag && gameMode == 'a') {
- if (
- (gameOperation == 'plus' && curX >= correctXA) ||
- (gameOperation == 'minus' && curX <= correctXA)
- ) {
- self.floor.correctIndex = i - 1;
- flag = false;
- }
- }
- if (gameMode == 'b') {
- if (
- (gameOperation == 'plus' && curX >= correctXB) ||
- (gameOperation == 'minus' && curX <= correctXB)
- ) {
- total = i;
- break;
- }
- }
-
- const curBlock = game.add.geom.rect(
- curX,
- self.default.y0,
- blockWidth,
- self.default.height + 10,
- colors.blueBgInsideLevel,
- 1,
- colors.gray,
- lineWidth
- );
- const anchor = gameOperation == 'minus' ? 1 : 0;
- curBlock.anchor(anchor, 0);
- curBlock.blockValue = 1;
-
- if (gameMode == 'a') {
- curBlock.fillColor = 'transparent';
- curBlock.blockIndex = i;
- }
-
- self.floor.list.push(curBlock);
- }
-
- if (gameMode === 'b') self.floor.selectedIndex = total - 1;
- if (gameMode == 'a') self.floor.correctX = correctXA;
- else if (gameMode == 'b') self.floor.correctX = correctXB;
-
- for (let i = 0; i <= 8; i++) {
- const x = self.default.x0 + (i + 1) * self.default.width * direc;
- const y = self.default.y0 + self.default.height + 45 - 65;
- let numberX = x;
- let numberText = i;
- let numberCircleSize = 45;
- let numberFont = {
- ...textStyles.h3_,
- fill: lineColor,
- font: 'bold ' + textStyles.h3_.font,
- };
- if (gameOperation === 'minus') {
- numberX = i !== 0 ? x - 2 : x;
- numberText = -i;
- numberCircleSize = 45;
- numberFont.font = 'bold ' + textStyles.h4_.font;
- }
- game.add.geom
- .circle(x, y, numberCircleSize, undefined, 0, colors.white, 1)
- .anchor(0, 0.25);
- game.add.text(numberX, y + 2, numberText, numberFont);
- }
- },
- renderCharacters: () => {
- self.tractor = game.add.sprite(
- self.default.x0,
- self.default.y0,
- 'tractor',
- 0,
- 1
- );
- if (gameOperation == 'plus') {
- self.tractor.anchor(1, 1);
- self.tractor.animation = ['move', [0, 1, 2, 3, 4], 4];
- } else {
- self.tractor.anchor(0, 1);
- self.tractor.animation = ['move', [5, 6, 7, 8, 9], 4];
- self.tractor.curFrame = 5;
- }
- },
- renderMainUI: () => {
-
- self.ui.help = game.add.image(0, 0, 'pointer', 1.7, 0);
- if (gameMode === 'b') self.ui.help.anchor(0.25, 0.7);
- else self.ui.help.anchor(0.2, 0);
-
- if (gameMode == 'a') {
- self.ui.arrow = game.add.image(
- self.default.arrowX0,
- self.default.y0,
- 'arrow_down',
- 1.5
- );
- self.ui.arrow.anchor(0.5, 1);
- self.ui.arrow.alpha = 0.5;
- }
-
- const correctMessage =
- gameMode === 'a'
- ? game.lang.squareOne_intro_a
- : game.lang.squareOne_intro_b;
- const treatedMessage = correctMessage.split('\\n');
- const font = textStyles.h1_;
- self.ui.message = [];
- self.ui.message.push(
- game.add.text(
- context.canvas.width / 2,
- 170,
- treatedMessage[0] + '\n' + treatedMessage[1],
- font
- )
- );
- },
- renderOperationUI: () => {
- let validBlocks = [];
- const lastValidIndex =
- gameMode === 'a' ? self.stack.curIndex : self.stack.selectedIndex;
- for (let i = 0; i <= lastValidIndex; i++) {
- validBlocks.push(self.stack.list[i]);
- }
- const font = textStyles.fraction;
- font.fill = colors.green;
- const nominators = [];
- const denominators = [];
- const renderList = [];
- const padding = 100;
- const offsetX = 100;
- const widthOfChar = 35;
- const x0 = padding;
- const y0 = context.canvas.height / 3;
- let nextX = x0;
- const cardHeight = 400;
- const cardX = x0 - padding;
- const cardY = y0;
-
- const card = game.add.geom.rect(
- cardX,
- cardY,
- 0,
- cardHeight,
- colors.blueLight,
- 0.5,
- colors.blueDark,
- 8
- );
- card.id = 'card';
- card.anchor(0, 0.5);
- renderList.push(card);
-
- for (let i in validBlocks) {
- const curFraction = validBlocks[i].fraction;
- const curFractionString = curFraction.labels[0].name;
- let curFractionSign = i !== '0' ? '+' : '';
- if (curFraction.labels[1].name === '-') {
- curFractionSign = '-';
- font.fill = colors.red;
- }
- const fraction = game.add.text(
- x0 + i * offsetX + offsetX / 2,
- curFractionString === '1' ? y0 + 40 : y0,
- curFractionString,
- font,
- 60
- );
- fraction.lineHeight = 70;
- renderList.push(
- game.add.text(x0 + i * offsetX, y0 + 35, curFractionSign, font)
- );
- renderList.push(fraction);
- renderList.push(
- game.add.text(
- x0 + offsetX / 2 + i * offsetX,
- y0,
- curFractionString === '1' ? '' : '_',
- font
- )
- );
- nominators.push(curFraction.nominator);
- denominators.push(curFraction.denominator);
- }
-
- font.fill = colors.black;
- const updatedNominators = [];
- const mmc = game.math.mmcArray(denominators);
- let resultNominator = 0;
- for (let i in nominators) {
- const temp = nominators[i] * (mmc / denominators[i]);
- updatedNominators.push(temp);
- resultNominator += temp;
- }
- const resultNominatorUnsigned =
- resultNominator < 0 ? -resultNominator : resultNominator;
- const resultAux = resultNominator / mmc;
- const result =
- ('' + resultAux).length > 4 ? resultAux.toFixed(2) : resultAux;
-
- nextX = x0 + validBlocks.length * offsetX + 20;
-
- renderList.push(game.add.text(nextX, y0 + 35, '=', font));
- font.align = 'center';
- nextX += offsetX;
- if (result < 0) {
- nextX += 60;
- renderList.push(game.add.text(nextX - 80, y0 + 35, '-', font));
- nextX -= 30;
- }
- const fractionResult = game.add.text(
- nextX,
- mmc === 1 || resultNominatorUnsigned === 0 ? y0 + 40 : y0,
- mmc === 1 || resultNominatorUnsigned === 0
- ? resultNominatorUnsigned
- : resultNominatorUnsigned + '\n' + mmc,
- font
- );
- fractionResult.lineHeight = 70;
- renderList.push(fractionResult);
- const fractionLine = game.add.geom.line(
- nextX,
- y0 + 15,
- nextX + 60,
- y0 + 15,
- 4,
- colors.black,
- mmc === 1 || resultNominatorUnsigned === 0 ? 0 : 1
- );
- fractionLine.anchor(0.5, 0);
- renderList.push(fractionLine);
-
- const mdcAux = game.math.mdc(resultNominator, mmc);
- const mdc = mdcAux < 0 ? -mdcAux : mdcAux;
- if (mdc !== 1 && resultNominatorUnsigned !== 0) {
- nextX += offsetX;
- renderList.push(game.add.text(nextX, y0 + 35, '=', font));
- nextX += offsetX;
- renderList.push(
- game.add.text(nextX - 50, y0 + 35, result > 0 ? '' : '-', font)
- );
- renderList.push(
- game.add.text(nextX, y0, resultNominatorUnsigned / mdc, font)
- );
- renderList.push(game.add.text(nextX, y0 + 70, mmc / mdc, font));
- const fractionLine = game.add.geom.line(
- nextX,
- y0 + 15,
- nextX + 60,
- y0 + 15,
- 4,
- colors.black
- );
- fractionLine.anchor(0.5, 0);
- renderList.push(fractionLine);
- }
-
- let resultWidth = '_'.length * widthOfChar;
- const cardWidth = nextX - x0 + resultWidth + padding * 2;
- card.width = cardWidth;
-
- moveList(renderList, (context.canvas.width - cardWidth) / 2, 0);
- self.fractionOperationUI = renderList;
-
- if (gameMode === 'a') {
- if (self.control.isCorrect) {
- const floorFlag = game.add.image(
- self.floor.list[self.floor.correctIndex].x,
- self.tractor.y + 10,
- 'flag_green',
- 0.7
- );
- floorFlag.anchor(0, 1);
- if (gameOperation === 'minus')
- floorFlag.x -= self.floor.list[0].width;
- } else {
- let min = self.floor.selectedIndex,
- max = self.floor.correctIndex;
- if (min > max) {
- let aux = min;
- min = max;
- max = aux;
- }
- for (let i = min + 1; i <= max; i++) {
- self.floor.list[i].fillColor = colors.red;
- self.floor.list[i].alpha = 1;
- }
- }
- } else {
- if (self.control.isCorrect) {
- console.log(self.floor);
- const floorFlag = game.add.image(
- self.floor.list[self.floor.curIndex].x,
- self.tractor.y + 10,
- 'flag_green',
- 0.7
- );
- floorFlag.anchor(0, 1);
- if (gameOperation === 'minus')
- floorFlag.x -= self.floor.list[0].width;
- } else {
- let max = self.floor.selectedIndex,
- min = self.floor.curIndex;
- if (min < max) {
- for (let i = min + 1; i <= max; i++) {
- self.floor.list[i].fillColor = colors.red;
- self.floor.list[i].alpha = 1;
- }
- }
- }
- }
- const endSignX = (context.canvas.width - cardWidth) / 2 + cardWidth;
- return endSignX;
- },
- renderEndUI: () => {
- let btnColor = colors.green;
- let btnText = game.lang.continue;
- if (!self.control.isCorrect) {
- btnColor = colors.red;
- btnText = game.lang.retry;
- }
-
- self.ui.continue.button = game.add.geom.rect(
- context.canvas.width / 2,
- context.canvas.height / 2 + 100,
- 450,
- 100,
- btnColor
- );
- self.ui.continue.button.anchor(0.5, 0.5);
- self.ui.continue.text = game.add.text(
- context.canvas.width / 2,
- context.canvas.height / 2 + 16 + 100,
- btnText,
- textStyles.btn
- );
- },
-
- animateTractorHandler: () => {
-
- self.tractor.x += self.animation.speed;
- self.stack.list.forEach((block) => (block.x += self.animation.speed));
-
-
-
- const curBlockExtra =
- (self.default.width - self.stack.list[self.stack.curIndex].width) *
- self.control.direc;
- const canLowerBlocks =
- (gameOperation == 'plus' &&
- self.stack.list[0].x >= self.stack.curBlockEndX + curBlockExtra) ||
- (gameOperation == 'minus' &&
- self.stack.list[0].x <= self.stack.curBlockEndX + curBlockExtra);
- if (canLowerBlocks) {
- const endAnimation = self.utils.lowerBlocksHandler();
- if (!endAnimation) {
- self.animation.animateTractor = false;
- self.control.checkAnswer = true;
- }
- }
- },
- lowerBlocksHandler: () => {
- self.floor.curIndex += self.stack.list[self.stack.curIndex].blockValue;
- const tooManyStackBlocks = self.floor.curIndex > self.floor.selectedIndex;
- if (tooManyStackBlocks) return false;
-
- for (let i = 0; i <= self.floor.curIndex; i++) {
- self.floor.list[i].fillColor = 'transparent';
- }
-
- self.stack.list.forEach((block) => {
- block.y += self.default.height;
- });
-
- self.stack.list[self.stack.curIndex].alpha = 0;
- const isLastFloorBlock = self.floor.curIndex === self.floor.selectedIndex;
- const notEnoughStackBlocks =
- self.stack.curIndex === self.stack.list.length - 1;
- if (isLastFloorBlock || notEnoughStackBlocks) return false;
-
- self.stack.curIndex++;
- self.stack.curBlockEndX +=
- self.stack.list[self.stack.curIndex].width * self.control.direc;
- return true;
- },
- checkAnswerHandler: () => {
- game.timer.stop();
- game.animation.stop(self.tractor.animation[0]);
- if (gameMode === 'a') self.ui.arrow.alpha = 0;
- self.control.isCorrect =
- gameMode === 'a'
- ? self.floor.selectedIndex === self.floor.correctIndex
- : self.stack.selectedIndex === self.stack.correctIndex;
- const x = self.utils.renderOperationUI();
-
- if (self.control.isCorrect) {
- completedLevels++;
- if (audioStatus) game.audio.okSound.play();
- game.animation.play(self.tractor.animation[0]);
- game.add
- .image(x + 50, context.canvas.height / 3, 'answer_correct')
- .anchor(0.5, 0.5);
- if (isDebugMode) console.log('Completed Levels: ' + completedLevels);
- } else {
- if (audioStatus) game.audio.errorSound.play();
- game.add
- .image(x, context.canvas.height / 3, 'answer_wrong')
- .anchor(0.5, 0.5);
- }
- self.fetch.postScore();
- self.control.checkAnswer = false;
- self.animation.animateEnding = true;
- },
- animateEndingHandler: () => {
-
- self.control.count++;
-
- if (self.control.isCorrect) self.tractor.x += self.animation.speed;
- if (self.control.count === 100) {
- self.utils.renderEndUI();
- self.control.showEndInfo = true;
- if (self.control.isCorrect) canGoToNextMapPosition = true;
- else canGoToNextMapPosition = false;
- }
- },
- endLevel: () => {
- game.state.start('map');
- },
-
-
- showAnswer: () => {
-
- if (gameMode == 'a') {
- const aux = self.floor.list[0];
- self.ui.help.x =
- self.floor.correctX - (aux.width / 2) * self.control.direc;
- self.ui.help.y = self.default.y0;
-
- } else {
- const aux = self.stack.list[self.stack.correctIndex];
- self.ui.help.x = aux.x + (aux.width / 2) * self.control.direc;
- self.ui.help.y = aux.y;
- }
- if (!self.control.hasClicked) {
- self.ui.help.alpha = 0.7;
- }
- },
-
-
- clickHandler: (clickedIndex, curSet) => {
- if (!self.control.hasClicked && !self.animation.animateEnding) {
- document.body.style.cursor = 'auto';
-
- if (audioStatus) game.audio.popSound.play();
-
- navigation.disableIcon(navigation.showAnswerIcon);
-
- self.ui.message[0].alpha = 0;
-
- if (showFractions) {
- self.stack.list.forEach((block) => {
- block.fraction.labels.forEach((lbl) => {
- if (lbl) lbl.alpha = 0;
- });
- });
- }
-
- if (self.ui.help != undefined) self.ui.help.alpha = 0;
-
- for (let i = curSet.list.length - 1; i > clickedIndex; i--) {
- curSet.list[i].alpha = 0;
- }
-
- curSet.selectedIndex = clickedIndex;
- if (gameMode == 'a') {
- self.ui.arrow.alpha = 0;
- } else {
-
- self.stack.list.length = curSet.selectedIndex + 1;
- }
-
- game.animation.play(self.tractor.animation[0]);
- self.animation.animateTractor = true;
- self.control.hasClicked = true;
- }
- },
-
- overHandler: (cur) => {
- if (!self.control.hasClicked) {
- document.body.style.cursor = 'pointer';
- if (gameMode === 'a') {
- for (let i in self.floor.list) {
- self.floor.list[i].fillColor =
- i <= cur.blockIndex ? colors.blueBgInsideLevel : 'transparent';
- }
- self.floor.selectedIndex = cur.blockIndex;
- } else {
- for (let i in self.stack.list) {
- const alpha = i <= cur.blockIndex ? 1 : 0.4;
- self.stack.list[i].alpha = alpha;
- if (showFractions) {
- self.stack.list[i].fraction.labels.forEach((lbl) => {
- if (lbl) lbl.alpha = alpha;
- });
- }
- }
- self.stack.selectedIndex = cur.blockIndex;
- }
- }
- },
-
- outHandler: () => {
- if (!self.control.hasClicked) {
- document.body.style.cursor = 'auto';
- if (gameMode === 'a') {
- for (let i in self.floor.list) {
- self.floor.list[i].fillColor = 'transparent';
- }
- self.floor.selectedIndex = undefined;
- } else {
- for (let i in self.stack.list) {
- self.stack.list[i].alpha = 1;
- if (showFractions) {
- self.stack.list[i].fraction.labels.forEach((lbl) => {
- if (lbl) lbl.alpha = 1;
- });
- }
- }
- self.stack.selectedIndex = undefined;
- }
- }
- },
- },
- events: {
-
- onInputDown: (mouseEvent) => {
- const x = game.math.getMouse(mouseEvent).x;
- const y = game.math.getMouse(mouseEvent).y;
-
- const curSet = gameMode == 'a' ? self.floor : self.stack;
- for (let i in curSet.list) {
- if (game.math.isOverIcon(x, y, curSet.list[i])) {
- self.utils.clickHandler(+i, curSet);
- break;
- }
- }
-
- if (self.control.showEndInfo) {
- if (game.math.isOverIcon(x, y, self.ui.continue.button)) {
- if (audioStatus) game.audio.popSound.play();
- self.utils.endLevel();
- }
- }
- navigation.onInputDown(x, y);
- game.render.all();
- },
-
- onInputOver: (mouseEvent) => {
- const x = game.math.getMouse(mouseEvent).x;
- const y = game.math.getMouse(mouseEvent).y;
- let isOverFloor = false;
- let isOverStack = false;
- if (gameMode == 'a') {
- self.floor.list.forEach((cur) => {
-
- if (game.math.isOverIcon(x, y, cur)) {
- isOverFloor = true;
- self.utils.overHandler(cur);
- }
-
- if (
- !self.control.hasClicked &&
- !self.animation.animateEnding &&
- game.math.isOverIcon(x, self.default.y0, cur)
- ) {
- self.ui.arrow.x = x;
- }
- });
- if (!isOverFloor) self.utils.outHandler('a');
- }
- if (gameMode == 'b') {
-
- self.stack.list.forEach((cur) => {
- if (game.math.isOverIcon(x, y, cur)) {
- isOverStack = true;
- self.utils.overHandler(cur);
- }
- });
- if (!isOverStack) self.utils.outHandler('b');
- }
-
- if (self.control.showEndInfo) {
- if (game.math.isOverIcon(x, y, self.ui.continue.button)) {
-
- document.body.style.cursor = 'pointer';
- self.ui.continue.button.scale =
- self.ui.continue.button.initialScale * 1.1;
- self.ui.continue.text.style = textStyles.btnLg;
- } else {
-
- document.body.style.cursor = 'auto';
- self.ui.continue.button.scale =
- self.ui.continue.button.initialScale * 1;
- self.ui.continue.text.style = textStyles.btn;
- }
- }
- navigation.onInputOver(x, y);
- game.render.all();
- },
- },
- fetch: {
-
- postScore: () => {
-
- const data =
- '&line_game=' +
- gameShape +
- '&line_mode=' +
- gameMode +
- '&line_oper=' +
- gameOperation +
- '&line_leve=' +
- gameDifficulty +
- '&line_posi=' +
- curMapPosition +
- '&line_resu=' +
- self.control.isCorrect +
- '&line_time=' +
- game.timer.elapsed +
- '&line_deta=' +
- 'numBlocks:' +
- self.stack.list.length +
- ', valBlocks: ' +
- self.control.divisorsList +
- ' blockIndex: ' +
- self.stack.selectedIndex +
- ', floorIndex: ' +
- self.floor.selectedIndex;
-
- sendToDatabase(data);
- },
- },
- };
|