diff --git a/js/CGameBase.js b/js/CGameBase.js index de4b909..f9254de 100644 --- a/js/CGameBase.js +++ b/js/CGameBase.js @@ -1,104 +1,104 @@ -var CGameBase = function(oData, iLevel){ - this._bStartGame; - this._bDamaged; - this._bCollision; - - this._iStartCountDown; - this._iTimeElaps; - this._iLevel; - this._iScore; - this._iGameState; - this._iCurRotationScreenDir; - this._iCurFrameRotation; - this._iCurRotation; - - this._aSegments; - - this._oInterface; - this._oEndPanel = null; - this._oParent; - - this._oPrevSegment; - this._oPlayer; - this._oRoad; - - this._oGrassDrawingLevel; - this._oRoadDrawingLevel; - this._oRumbleDrawingLevel; - this._oLaneDrawingLevel; - - this._oElementContainer; - this._oHorizon; - this._iHorizonOffset; - - this._aCars; - this._aEnemy; - - this._oLevelBuilder; - - s_oGame = this; - - this._init(iLevel); -}; - -CGameBase.prototype._init = function(iLevel){ - - this._iLevel = iLevel; - - this._aSegments = new Array(); - - this._oGameContainer = new createjs.Container(); - this._oGameContainer.x = CANVAS_WIDTH/2; - this._oGameContainer.y = CANVAS_HEIGHT; - this._oGameContainer.regX = CANVAS_WIDTH/2; - this._oGameContainer.regY = CANVAS_HEIGHT; - s_oStage.addChild(this._oGameContainer); - - var iWorldNum = Math.floor(this._iLevel/NUM_TRACKS_PER_WORLD); - var szPatch = "w"+iWorldNum+"_patch"; -/* +var CGameBase = function (oData, iLevel) { + this._bStartGame; + this._bDamaged; + this._bCollision; + + this._iStartCountDown; + this._iTimeElaps; + this._iLevel; + this._iScore; + this._iGameState; + this._iCurRotationScreenDir; + this._iCurFrameRotation; + this._iCurRotation; + + this._aSegments; + + this._oInterface; + this._oEndPanel = null; + this._oParent; + + this._oPrevSegment; + this._oPlayer; + this._oRoad; + + this._oGrassDrawingLevel; + this._oRoadDrawingLevel; + this._oRumbleDrawingLevel; + this._oLaneDrawingLevel; + + this._oElementContainer; + this._oHorizon; + this._iHorizonOffset; + + this._aCars; + this._aEnemy; + + this._oLevelBuilder; + + this.raceTimer; + + s_oGame = this; + + this._init(iLevel); +}; + +CGameBase.prototype._init = function (iLevel) { + this._iLevel = iLevel; + + this._aSegments = new Array(); + + this._oGameContainer = new createjs.Container(); + this._oGameContainer.x = CANVAS_WIDTH / 2; + this._oGameContainer.y = CANVAS_HEIGHT; + this._oGameContainer.regX = CANVAS_WIDTH / 2; + this._oGameContainer.regY = CANVAS_HEIGHT; + s_oStage.addChild(this._oGameContainer); + + var iWorldNum = Math.floor(this._iLevel / NUM_TRACKS_PER_WORLD); + var szPatch = "w" + iWorldNum + "_patch"; + /* var oSprite = s_oSpriteLibrary.getSprite(szPatch); var oPatch = createBitmap(oSprite); this._oGameContainer.addChild(oPatch); */ - this._oHorizon = new CHorizon(this._iLevel, this._oGameContainer); - - this._oRoadDrawingContainer = new createjs.Container(); - //this._oRoadDrawingContainer.cache(START_X_ROTATION_VIEW, START_Y_ROTATION_VIEW, WIDTH_ROTATION_VIEW, HEIGHT_ROTATION_VIEW); - this._oGameContainer.addChild(this._oRoadDrawingContainer); - - this._oGrassDrawingLevel = new createjs.Shape(); - //this._oGrassDrawingLevel.cache(START_X_ROTATION_VIEW, START_Y_ROTATION_VIEW, WIDTH_ROTATION_VIEW, HEIGHT_ROTATION_VIEW); - //this._oGameContainer.addChild(this._oGrassDrawingLevel); - this._oRoadDrawingContainer.addChild(this._oGrassDrawingLevel); - - this._oRoadDrawingLevel = new createjs.Shape(); - //this._oRoadDrawingLevel.cache(START_X_ROTATION_VIEW, START_Y_ROTATION_VIEW, WIDTH_ROTATION_VIEW, HEIGHT_ROTATION_VIEW); - //this._oGameContainer.addChild(this._oRoadDrawingLevel); - this._oRoadDrawingContainer.addChild(this._oRoadDrawingLevel); - - - this._oRumbleDrawingLevel = new createjs.Shape(); - //this._oRumbleDrawingLevel.cache(START_X_ROTATION_VIEW, START_Y_ROTATION_VIEW, WIDTH_ROTATION_VIEW, HEIGHT_ROTATION_VIEW); - //this._oGameContainer.addChild(this._oRumbleDrawingLevel); - this._oRoadDrawingContainer.addChild(this._oRumbleDrawingLevel); - - this._oLaneDrawingLevel = new createjs.Shape(); - //this._oLaneDrawingLevel.cache(START_X_ROTATION_VIEW, START_Y_ROTATION_VIEW, WIDTH_ROTATION_VIEW, HEIGHT_ROTATION_VIEW); - //this._oGameContainer.addChild(this._oLaneDrawingLevel); - this._oRoadDrawingContainer.addChild(this._oLaneDrawingLevel); - - this._oRumbleStrokeLevel = new createjs.Shape(); - //this._oRumbleStrokeLevel.cache(START_X_ROTATION_VIEW, START_Y_ROTATION_VIEW, WIDTH_ROTATION_VIEW, HEIGHT_ROTATION_VIEW); - this._oRoadDrawingContainer.addChild(this._oRumbleStrokeLevel); - - /* + this._oHorizon = new CHorizon(this._iLevel, this._oGameContainer); + + this._oRoadDrawingContainer = new createjs.Container(); + //this._oRoadDrawingContainer.cache(START_X_ROTATION_VIEW, START_Y_ROTATION_VIEW, WIDTH_ROTATION_VIEW, HEIGHT_ROTATION_VIEW); + this._oGameContainer.addChild(this._oRoadDrawingContainer); + + this._oGrassDrawingLevel = new createjs.Shape(); + //this._oGrassDrawingLevel.cache(START_X_ROTATION_VIEW, START_Y_ROTATION_VIEW, WIDTH_ROTATION_VIEW, HEIGHT_ROTATION_VIEW); + //this._oGameContainer.addChild(this._oGrassDrawingLevel); + this._oRoadDrawingContainer.addChild(this._oGrassDrawingLevel); + + this._oRoadDrawingLevel = new createjs.Shape(); + //this._oRoadDrawingLevel.cache(START_X_ROTATION_VIEW, START_Y_ROTATION_VIEW, WIDTH_ROTATION_VIEW, HEIGHT_ROTATION_VIEW); + //this._oGameContainer.addChild(this._oRoadDrawingLevel); + this._oRoadDrawingContainer.addChild(this._oRoadDrawingLevel); + + this._oRumbleDrawingLevel = new createjs.Shape(); + //this._oRumbleDrawingLevel.cache(START_X_ROTATION_VIEW, START_Y_ROTATION_VIEW, WIDTH_ROTATION_VIEW, HEIGHT_ROTATION_VIEW); + //this._oGameContainer.addChild(this._oRumbleDrawingLevel); + this._oRoadDrawingContainer.addChild(this._oRumbleDrawingLevel); + + this._oLaneDrawingLevel = new createjs.Shape(); + //this._oLaneDrawingLevel.cache(START_X_ROTATION_VIEW, START_Y_ROTATION_VIEW, WIDTH_ROTATION_VIEW, HEIGHT_ROTATION_VIEW); + //this._oGameContainer.addChild(this._oLaneDrawingLevel); + this._oRoadDrawingContainer.addChild(this._oLaneDrawingLevel); + + this._oRumbleStrokeLevel = new createjs.Shape(); + //this._oRumbleStrokeLevel.cache(START_X_ROTATION_VIEW, START_Y_ROTATION_VIEW, WIDTH_ROTATION_VIEW, HEIGHT_ROTATION_VIEW); + this._oRoadDrawingContainer.addChild(this._oRumbleStrokeLevel); + + /* this._oRumbleStrokeLevel = new createjs.Shape(); this._oRumbleStrokeLevel.cache(START_X_ROTATION_VIEW, START_Y_ROTATION_VIEW, WIDTH_ROTATION_VIEW, HEIGHT_ROTATION_VIEW); this._oGameContainer.addChild(this._oRumbleStrokeLevel); */ - - /* + + /* var iWorld = Math.floor(this._iLevel/NUM_WORLDS); var oSprite = s_oSpriteLibrary.getSprite("fog_"+iWorld); var oFog = createBitmap(oSprite); @@ -106,520 +106,550 @@ CGameBase.prototype._init = function(iLevel){ oFog.y = CANVAS_HEIGHT/2-200; this._oGameContainer.addChild(oFog); */ - this._oElementContainer = new createjs.Container(); - this._oGameContainer.addChild(this._oElementContainer); - - this._oRoad = new CRoad(this._oRoadDrawingLevel, this._oElementContainer, this._iLevel, this._oRumbleDrawingLevel, this._oGrassDrawingLevel, this._oLaneDrawingLevel, this._oRumbleStrokeLevel); - - this._oPlayer = new CPlayer(CANVAS_WIDTH/2, CANVAS_HEIGHT-180, this._oElementContainer); - this._oPlayer.addEventListener(ON_PLAYER_PASSED_LAP, this.playerLapPassed, this); - //this._oPlayer.addEventListener(ON_PLAYER_HIT, this._onPlayerHit, this); - //this._oPlayer.addEventListener(ON_PLAYER_HIT_WHILE_COLLIDED, this._onPlayerHitWhileCollided, this); - - this._aSegments = this._oRoad.getSegments(); - TRACK_LENGTH = this._oRoad.getTrackLength(); - - this._oInterface = new CInterface(); - - var oCursor = this._oPlayer.getCursor(); - this._oInterface.addMiniMapCursor(oCursor, this._oPlayer); - - this._aCars = new Array(); - this._aEnemy = new Array(); - this._oLevelBuilder = new CLevelBuilder(this._oPlayer, this._aCars, this._aEnemy, this._oElementContainer, iLevel); - - this.resetParams(); - - - //console.log( this._aSegments.length ); - - this.refreshButtonPos(); - - ////////////////PLACE BEFORE STARTING LANE - ///NO NEGATIVE NUMBERS, SO LET'S GET LAST SEGMENT, AND SUBTRACT A QTY - /* + this._oElementContainer = new createjs.Container(); + this._oGameContainer.addChild(this._oElementContainer); + + this._oRoad = new CRoad( + this._oRoadDrawingLevel, + this._oElementContainer, + this._iLevel, + this._oRumbleDrawingLevel, + this._oGrassDrawingLevel, + this._oLaneDrawingLevel, + this._oRumbleStrokeLevel + ); + + this._oPlayer = new CPlayer( + CANVAS_WIDTH / 2, + CANVAS_HEIGHT - 180, + this._oElementContainer + ); + this._oPlayer.addEventListener( + ON_PLAYER_PASSED_LAP, + this.playerLapPassed, + this + ); + //this._oPlayer.addEventListener(ON_PLAYER_HIT, this._onPlayerHit, this); + //this._oPlayer.addEventListener(ON_PLAYER_HIT_WHILE_COLLIDED, this._onPlayerHitWhileCollided, this); + + this._aSegments = this._oRoad.getSegments(); + TRACK_LENGTH = this._oRoad.getTrackLength(); + + this._oInterface = new CInterface(); + + var oCursor = this._oPlayer.getCursor(); + this._oInterface.addMiniMapCursor(oCursor, this._oPlayer); + + this._aCars = new Array(); + this._aEnemy = new Array(); + this._oLevelBuilder = new CLevelBuilder( + this._oPlayer, + this._aCars, + this._aEnemy, + this._oElementContainer, + iLevel + ); + + this.resetParams(); + + //console.log( this._aSegments.length ); + + this.refreshButtonPos(); + + ////////////////PLACE BEFORE STARTING LANE + ///NO NEGATIVE NUMBERS, SO LET'S GET LAST SEGMENT, AND SUBTRACT A QTY + /* var iStartPos = 7200; this._oPlayer.setPosition(iStartPos*SEGMENT_LENGTH); this._oPlayer.setXOffset(-0.4); this._oPrevSegment = this._oPlayer.getPlayerSegment(); */ - - }; +CGameBase.prototype.refreshButtonPos = function () { + this._oInterface.refreshButtonPos(); -CGameBase.prototype.refreshButtonPos = function(){ - this._oInterface.refreshButtonPos(); - - var iOffsetRatio = 0.5; - this._oGameContainer.y = CANVAS_HEIGHT-s_iOffsetY*iOffsetRatio; - + var iOffsetRatio = 0.5; + this._oGameContainer.y = CANVAS_HEIGHT - s_iOffsetY * iOffsetRatio; }; -CGameBase.prototype.getWorldCameraPos = function(){ - var oZPosition = this._oPlayer.getPosition().z; - - - var oBase = this._aSegments[(this.findSegment(oZPosition).index)]; - - var baseSegmentPos = oBase.p1.world; - var iPercent = (oZPosition - baseSegmentPos.z)/SEGMENT_LENGTH; - - var iX = Util.interpolate(oBase.p1.world.x, oBase.p2.world.x, iPercent); - //WE DON'T NEED Y NOW - var iY = 0//Util.interpolate(oBase.p1.world.z, oBase.p2.world.z, iPercent); +CGameBase.prototype.getWorldCameraPos = function () { + var oZPosition = this._oPlayer.getPosition().z; - ///this._iHorizonOffset SHOULD BE INCREMENTED HERE, - ///BUT IT'S OK FOR PERFORMANCE IMPROVEMENTS + var oBase = this._aSegments[this.findSegment(oZPosition).index]; - return {x: iX+this._iHorizonOffset, y: iY}; -}; + var baseSegmentPos = oBase.p1.world; + var iPercent = (oZPosition - baseSegmentPos.z) / SEGMENT_LENGTH; + + var iX = Util.interpolate(oBase.p1.world.x, oBase.p2.world.x, iPercent); + //WE DON'T NEED Y NOW + var iY = 0; //Util.interpolate(oBase.p1.world.z, oBase.p2.world.z, iPercent); + + ///this._iHorizonOffset SHOULD BE INCREMENTED HERE, + ///BUT IT'S OK FOR PERFORMANCE IMPROVEMENTS -CGameBase.prototype.playerLapPassed = function(){ - this._iHorizonOffset += this._aSegments[(this._aSegments.length-1)].p1.world.x; + return { x: iX + this._iHorizonOffset, y: iY }; }; +CGameBase.prototype.playerLapPassed = function () { + this._iHorizonOffset += + this._aSegments[this._aSegments.length - 1].p1.world.x; +}; -function _onKeyboardUp(evt){ - evt.preventDefault(); - s_oGame.onKeyUp(evt.keyCode); +function _onKeyboardUp(evt) { + evt.preventDefault(); + s_oGame.onKeyUp(evt.keyCode); } -function _onKeyboardDown(evt){ - if(!evt){ - evt = window.event; - } - evt.preventDefault(); +function _onKeyboardDown(evt) { + if (!evt) { + evt = window.event; + } + evt.preventDefault(); - s_oGame.onKeyDown(evt.keyCode); + s_oGame.onKeyDown(evt.keyCode); } -CGameBase.prototype.onKeyUp = function(iKey) { - if(!this._bStartGame){ - return; +CGameBase.prototype.onKeyUp = function (iKey) { + if (!this._bStartGame) { + return; + } + + switch (iKey) { + // left + case KEY_LEFT_A: + case KEY_LEFT: { + this._oPlayer.stopLeft(); + break; } - - switch(iKey) { - // left - case KEY_LEFT_A: - case KEY_LEFT: { - this._oPlayer.stopLeft(); - break; - } - case KEY_UP_W: - case KEY_UP: { - this._oPlayer.stopAccelerate(); - break; - } - // right - case KEY_RIGHT_D: - case KEY_RIGHT: { - this._oPlayer.stopRight(); - break; - } - case KEY_DOWN_S: - case KEY_DOWN: { - this._oPlayer.stopBrake(); - break; - } + case KEY_UP_W: + case KEY_UP: { + this._oPlayer.stopAccelerate(); + break; } -}; - -CGameBase.prototype.onKeyDown = function(iKey) { - if(!this._bStartGame){ - return; + // right + case KEY_RIGHT_D: + case KEY_RIGHT: { + this._oPlayer.stopRight(); + break; } - - switch(iKey) { - // left - case KEY_LEFT_A: - case KEY_LEFT: { - this._oPlayer.moveLeft(); - break; - } - case KEY_UP_W: - case KEY_UP: { - this._oPlayer.moveAccelerate(); - break; - } - // right - case KEY_RIGHT_D: - case KEY_RIGHT: { - this._oPlayer.moveRight(); - break; - } - case KEY_DOWN_S: - case KEY_DOWN: { - this._oPlayer.moveBrake(); - break; - } - case KEY_SPACE: { - break; - } + case KEY_DOWN_S: + case KEY_DOWN: { + this._oPlayer.stopBrake(); + break; } + } }; -CGameBase.prototype.resetParams = function(){ - this._bStartGame = false; - stopSound(s_aSounds["game_soundtrack"]); - - if(this._iLevel === 0/*false*/){ - var oHelpPanel = new CHelpPanel(); - } else { - this._bStartGame = true; - stopSound(s_aSounds["menu_soundtrack"]); - playSound("game_soundtrack", SOUNDTRACK_VOLUME_IN_GAME, true); - - $(s_oMain).trigger("start_level",this._iLevel); - - var oFade = new createjs.Shape(); - oFade.graphics.beginFill("black").drawRect(0,0,CANVAS_WIDTH,CANVAS_HEIGHT); - oFade.alpha = 1; - s_oStage.addChild(oFade); - - new createjs.Tween.get(oFade).to({alpha:0},750).call(function(){ - s_oStage.removeChild(oFade); - }); +CGameBase.prototype.onKeyDown = function (iKey) { + if (!this._bStartGame) { + return; + } + switch (iKey) { + // left + case KEY_LEFT_A: + case KEY_LEFT: { + this._oPlayer.moveLeft(); + break; + } + case KEY_UP_W: + case KEY_UP: { + this._oPlayer.moveAccelerate(); + break; + } + // right + case KEY_RIGHT_D: + case KEY_RIGHT: { + this._oPlayer.moveRight(); + break; + } + case KEY_DOWN_S: + case KEY_DOWN: { + this._oPlayer.moveBrake(); + break; + } + case KEY_SPACE: { + break; } + } +}; - this._iGameState = STATE_GAME_START; - this._iScore = 0; - this._iTimeElaps = LEVEL_INFO[this._iLevel].time; - this._iStartCountDown = START_COUNTDOWN; - this._iCurRotationScreenDir = 0; - this._iCurFrameRotation = 0; - this._iCurRotation = 0; - this._iHorizonOffset = 0; +CGameBase.prototype.resetParams = function () { + this._bStartGame = false; + stopSound(s_aSounds["game_soundtrack"]); - this._oHorizon.restart(); + if (this._iLevel === 0 /*false*/) { + var oHelpPanel = new CHelpPanel(); + } else { + this._bStartGame = true; + stopSound(s_aSounds["menu_soundtrack"]); + playSound("game_soundtrack", SOUNDTRACK_VOLUME_IN_GAME, true); + $(s_oMain).trigger("start_level", this._iLevel); - var iWorld = Math.floor(this._iLevel/NUM_WORLDS); - var iTrack = this._iLevel%NUM_TRACKS_PER_WORLD; - this._oInterface.setLevelInfo(iTrack); + var oFade = new createjs.Shape(); + oFade.graphics + .beginFill("black") + .drawRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT); + oFade.alpha = 1; + s_oStage.addChild(oFade); + new createjs.Tween.get(oFade).to({ alpha: 0 }, 750).call(function () { + s_oStage.removeChild(oFade); + }); + } - if(!s_bMobile){ - document.onkeydown = _onKeyboardDown; - document.onkeyup = _onKeyboardUp; - } + this._iGameState = STATE_GAME_START; + this._iScore = 0; + this._iTimeElaps = LEVEL_INFO[this._iLevel].time; + this._iStartCountDown = START_COUNTDOWN; + this._iCurRotationScreenDir = 0; + this._iCurFrameRotation = 0; + this._iCurRotation = 0; + this._iHorizonOffset = 0; - this._oPrevSegment = this._oPlayer.getPlayerSegment(); - this._oPlayer.reset(); + this._oHorizon.restart(); + + var iWorld = Math.floor(this._iLevel / NUM_WORLDS); + var iTrack = this._iLevel % NUM_TRACKS_PER_WORLD; + this._oInterface.setLevelInfo(iTrack); + if (!s_bMobile) { + document.onkeydown = _onKeyboardDown; + document.onkeyup = _onKeyboardUp; + } + this._oPrevSegment = this._oPlayer.getPlayerSegment(); + this._oPlayer.reset(); }; CGameBase.prototype.restartGame = function () { - $(s_oMain).trigger("show_interlevel_ad"); - $(s_oMain).trigger("restart_level", this._iLevel); - - this.resetParams(); -}; + $(s_oMain).trigger("show_interlevel_ad"); + $(s_oMain).trigger("restart_level", this._iLevel); -CGameBase.prototype.unload = function(){ + this.resetParams(); +}; - this._oInterface.unload(); - if(this._oEndPanel !== null){ - this._oEndPanel.unload(); - } +CGameBase.prototype.unload = function () { + this._oInterface.unload(); + if (this._oEndPanel !== null) { + this._oEndPanel.unload(); + } - stopSound(s_aSounds["menu_soundtrack"]); - stopSound(s_aSounds["game_soundtrack"]); - stopSound(s_aSounds["engine"]); - stopSound(s_aSounds["brake"]); - stopSound(s_aSounds["engine_reverse"]); - stopSound(s_aSounds["engine_stall"]); + stopSound(s_aSounds["menu_soundtrack"]); + stopSound(s_aSounds["game_soundtrack"]); + stopSound(s_aSounds["engine"]); + stopSound(s_aSounds["brake"]); + stopSound(s_aSounds["engine_reverse"]); + stopSound(s_aSounds["engine_stall"]); - createjs.Tween.removeAllTweens(); - s_oStage.removeAllChildren(); + createjs.Tween.removeAllTweens(); + s_oStage.removeAllChildren(); }; -CGameBase.prototype.checkDamage = function(){ - if(this._oPlayer.getCurSpeed() > PLAYER_MIN_SPEED_DAMAGE){ - ///// WE SHOULD USE A LIMITED TIME OF INVULNERABILITY TO PREVENT MULTIPLE DAMAGE - this.setPlayerDamage(0,null); - } +CGameBase.prototype.checkDamage = function () { + if (this._oPlayer.getCurSpeed() > PLAYER_MIN_SPEED_DAMAGE) { + ///// WE SHOULD USE A LIMITED TIME OF INVULNERABILITY TO PREVENT MULTIPLE DAMAGE + this.setPlayerDamage(0, null); + } }; -CGameBase.prototype.endDamageTime = function(){ - this._bDamaged = false; +CGameBase.prototype.endDamageTime = function () { + this._bDamaged = false; }; -CGameBase.prototype._countDown = function(){ - this._iStartCountDown -= s_iTimeElaps; - this._oInterface.refreshCountdown(this._iStartCountDown); - if(this._iStartCountDown <= 0){ - this._iStartCountDown = 0; - this._iGameState = STATE_GAME_RACE; - this._oInterface.countDownGo(); +CGameBase.prototype._countDown = function () { + this._iStartCountDown -= s_iTimeElaps; + this._oInterface.refreshCountdown(this._iStartCountDown); + if (this._iStartCountDown <= 0) { + this._iStartCountDown = 0; + this._iGameState = STATE_GAME_RACE; + this._oInterface.countDownGo(); - //this._oPlayer.setPlayerAnim("start"); - } + //this._oPlayer.setPlayerAnim("start"); + } }; -CGameBase.prototype.nextLevel = function(){ - s_oGame._iLevel++; - if(s_oGame._iLevel < NUM_TRACKS_PER_WORLD*NUM_WORLDS){ - s_oGame.unload(); - s_oGame._init(s_oGame._iLevel); - s_oGame._startGame(); - - } else { - s_oGame.gameOver(); - } +CGameBase.prototype.nextLevel = function () { + s_oGame._iLevel++; + if (s_oGame._iLevel < NUM_TRACKS_PER_WORLD * NUM_WORLDS) { + s_oGame.unload(); + s_oGame._init(s_oGame._iLevel); + s_oGame._startGame(); + } else { + s_oGame.gameOver(); + } }; -CGameBase.prototype.trackLose = function(){ - this._iGameState = STATE_GAME_END; - s_oGame.stopPlayer(); +CGameBase.prototype.trackLose = function () { + this._iGameState = STATE_GAME_END; + s_oGame.stopPlayer(); - $(s_oMain).trigger("end_level",this._iLevel); - $(s_oMain).trigger("show_interlevel_ad"); + $(s_oMain).trigger("end_level", this._iLevel); + $(s_oMain).trigger("show_interlevel_ad"); - var oLosePanel = new CLosePanel(s_oSpriteLibrary.getSprite('msg_box')); - oLosePanel.show(this._iScore); + var oLosePanel = new CLosePanel(s_oSpriteLibrary.getSprite("msg_box")); + oLosePanel.show(this._iScore); }; -CGameBase.prototype.stopPlayer = function(){ - this._oPlayer.stopAll(); +CGameBase.prototype.stopPlayer = function () { + this._oPlayer.stopAll(); - if(!s_bMobile){ - document.onkeydown = null; - document.onkeyup = null; - } + if (!s_bMobile) { + document.onkeydown = null; + document.onkeyup = null; + } }; -CGameBase.prototype.onExit = function(){ - s_oGame.unload(); - $(s_oMain).trigger("end_session"); - - playSound("menu_soundtrack", 1, true); +CGameBase.prototype.onExit = function () { + s_oGame.unload(); + $(s_oMain).trigger("end_session"); - s_oMain.gotoMenu(); + playSound("menu_soundtrack", 1, true); + s_oMain.gotoMenu(); }; CGameBase.prototype._onExitHelp = function () { - this._bStartGame = true; - stopSound(s_aSounds["menu_soundtrack"]); - playSound("game_soundtrack", SOUNDTRACK_VOLUME_IN_GAME, true); + this._bStartGame = true; + stopSound(s_aSounds["menu_soundtrack"]); + playSound("game_soundtrack", SOUNDTRACK_VOLUME_IN_GAME, true); - $(s_oMain).trigger("start_level",1); + $(s_oMain).trigger("start_level", 1); }; -CGameBase.prototype.gameOver = function(){ - this._oEndPanel = new CEndPanel(s_oSpriteLibrary.getSprite('msg_box')); - this._oEndPanel.show(this._iScore); +CGameBase.prototype.gameOver = function () { + this._oEndPanel = new CEndPanel(s_oSpriteLibrary.getSprite("msg_box")); + this._oEndPanel.show(this._iScore); }; -CGameBase.prototype.setPause = function(){ - this._bStartGame = false; - this._oPlayer.stopAll(); +CGameBase.prototype.setPause = function () { + this._bStartGame = false; + this._oPlayer.stopAll(); }; -CGameBase.prototype.setResume = function(){ - s_oGame._bStartGame = true; +CGameBase.prototype.setResume = function () { + s_oGame._bStartGame = true; }; -CGameBase.prototype.update = function(){ - var iDt = 1/s_iCurFps; +CGameBase.prototype.update = function () { + var iDt = 1 / s_iCurFps; - switch(this._iGameState){ - case STATE_GAME_START:{ - if(this._bStartGame){ - this._countDown(); - } + switch (this._iGameState) { + case STATE_GAME_START: { + if (this._bStartGame) { + this._countDown(); + } - break; - } - case STATE_GAME_RACE:{ - if(!this._bStartGame){ - return; - } - - this._oPlayer.update(iDt); - - this.updateRace(iDt); - - break; - } - case STATE_GAME_END:{ + break; + } + case STATE_GAME_RACE: { + if (!this._bStartGame) { + return; + } - this._oPlayer.update(iDt); - this._oPlayer.autoPilot(); + this._oPlayer.update(iDt); - this.updateOpponents(iDt); + this.updateRace(iDt); + console.log({ s_iTimeElaps, s_iCntTime }); + this.raceTimer = + this.raceTimer || + setInterval(() => { + this._oInterface.refreshRaceTime(new Date().getTime()); + }, 1000); - break; - } + break; } + case STATE_GAME_END: { + this._oPlayer.update(iDt); + this._oPlayer.autoPilot(); + this.updateOpponents(iDt); - this._oInterface.refreshSpeed(this._oPlayer.getCurSpeed()*PLAYER_SPEED_CONVERSION_RATIO); - this._oInterface.refreshMiniMap(this._oPlayer.getPlayerSegment().index); + break; + } + } - - this._oRoad.update(this._oPlayer.getPosition()); + this._oInterface.refreshSpeed( + this._oPlayer.getCurSpeed() * PLAYER_SPEED_CONVERSION_RATIO + ); + this._oInterface.refreshMiniMap(this._oPlayer.getPlayerSegment().index); - this._oHorizon.move(this.getWorldCameraPos()/*, this._oRoad.getLastVisibleSegment().clip*/); + this._oRoad.update(this._oPlayer.getPosition()); + this._oHorizon.move( + this.getWorldCameraPos() /*, this._oRoad.getLastVisibleSegment().clip*/ + ); - this._bCollision = false; - ///CHECK COLLISION - this._checkCollisions(); - - this._oPrevSegment = this._oPlayer.getPlayerSegment(); + this._bCollision = false; + ///CHECK COLLISION + this._checkCollisions(); + this._oPrevSegment = this._oPlayer.getPlayerSegment(); }; -CGameBase.prototype._checkCollisions = function(){ - var iPlayerSegment = this._oPlayer.getPlayerSegment().index; - iPlayerSegment += PLAYER_COLLISION_FRONT_DISTANCE; +CGameBase.prototype._checkCollisions = function () { + var iPlayerSegment = this._oPlayer.getPlayerSegment().index; + iPlayerSegment += PLAYER_COLLISION_FRONT_DISTANCE; - var aSegmentsToCheck = new Array(); - for(var i=this._oPrevSegment.index; i<=iPlayerSegment; i++){ - var iIndex = i%this._aSegments.length; - aSegmentsToCheck.push(this._aSegments[iIndex]); - } + var aSegmentsToCheck = new Array(); + for (var i = this._oPrevSegment.index; i <= iPlayerSegment; i++) { + var iIndex = i % this._aSegments.length; + aSegmentsToCheck.push(this._aSegments[iIndex]); + } - ////THIS ITERATION PREVENT ENEMY PIERCING ON ELEMENT, LIKE A CCD - for(var i=0; i PLAYER_MIN_SPEED_DAMAGE; - if(bSpeedToCheckDamage){ + var bStateCondition = + oEnemy.getState() !== STATE_RUNNER_DEATH && + oEnemy.getState() !== STATE_RUNNER_HURTED; - this.checkDamage(); - this._oPlayer.setCurSpeed( oEnemy.getSpeed() * (oEnemy.getSpeed()/this._oPlayer.getCurSpeed()) ); + if (bOverlapXCondition && bStateCondition) { + var bSpeedToCheckDamage = + this._oPlayer.getCurSpeed() - oEnemy.getSpeed() > + PLAYER_MIN_SPEED_DAMAGE; + if (bSpeedToCheckDamage) { + this.checkDamage(); + this._oPlayer.setCurSpeed( + oEnemy.getSpeed() * (oEnemy.getSpeed() / this._oPlayer.getCurSpeed()) + ); - this._bCollision = true; - this._oPlayer.stopEngineSound(); + this._bCollision = true; + this._oPlayer.stopEngineSound(); - this.setEnemyDamage(oEnemy, null); + this.setEnemyDamage(oEnemy, null); + break; + } else { + var iRelativeXPos = this.getRelativeEnemyPosToPlayer(oEnemy); + var iRelativeZPos = this.getRelativeEnemyZPosToPlayer(oEnemy); + oEnemy.lateralCollision(iRelativeXPos); + this._oPlayer.lateralCollision(iRelativeXPos, oEnemy); - break; - }else{ - var iRelativeXPos = this.getRelativeEnemyPosToPlayer(oEnemy); - var iRelativeZPos = this.getRelativeEnemyZPosToPlayer(oEnemy); + this.setPlayerDamage(0, iRelativeXPos, iRelativeZPos); - oEnemy.lateralCollision(iRelativeXPos); - this._oPlayer.lateralCollision(iRelativeXPos, oEnemy); - - - this.setPlayerDamage(0, iRelativeXPos, iRelativeZPos); - - return; - } - } + return; + } } + } }; +CGameBase.prototype.getRelativeEnemyPosToPlayer = function (oEnemy) { + var oXPosition = this._oPlayer.getPosition().x; + var iRelativeXPos = oEnemy.getOffset() - oXPosition; - -CGameBase.prototype.getRelativeEnemyPosToPlayer = function(oEnemy){ - var oXPosition = this._oPlayer.getPosition().x; - var iRelativeXPos = oEnemy.getOffset() - oXPosition; - - return iRelativeXPos; + return iRelativeXPos; }; -CGameBase.prototype.getRelativeEnemyZPosToPlayer = function(oEnemy){ - var oZPosition = this._oPlayer.getPosition().z; - var iRelativeZPos = oEnemy.getZ() - oZPosition; +CGameBase.prototype.getRelativeEnemyZPosToPlayer = function (oEnemy) { + var oZPosition = this._oPlayer.getPosition().z; + var iRelativeZPos = oEnemy.getZ() - oZPosition; - return iRelativeZPos; + return iRelativeZPos; }; -CGameBase.prototype.setPlayerDamage = function(iAmount, iRelativeXPos, iRelativeZ){ - if(this._bDamaged){ - return; - } - this._bDamaged = true; +CGameBase.prototype.setPlayerDamage = function ( + iAmount, + iRelativeXPos, + iRelativeZ +) { + if (this._bDamaged) { + return; + } + this._bDamaged = true; - this._oPlayer.damageAnim(iRelativeXPos, iRelativeZ); + this._oPlayer.damageAnim(iRelativeXPos, iRelativeZ); - var oTremble = new CTremble(s_oStage, 250, 20, 5); + var oTremble = new CTremble(s_oStage, 250, 20, 5); }; -CGameBase.prototype.playerCollide = function(){ - return this._bCollision; +CGameBase.prototype.playerCollide = function () { + return this._bCollision; }; -CGameBase.prototype.rumbleCondition = function(segment){ - return segment.curve > 1 || segment.curve < -1; +CGameBase.prototype.rumbleCondition = function (segment) { + return segment.curve > 1 || segment.curve < -1; }; - -CGameBase.prototype.findSegment = function(z) { - return this._oRoad.findSegment(z); +CGameBase.prototype.findSegment = function (z) { + return this._oRoad.findSegment(z); }; -CGameBase.prototype.getSegments = function(){ - return this._aSegments; +CGameBase.prototype.getSegments = function () { + return this._aSegments; }; -CGameBase.prototype.getValidSegment = function(iIndex){ - //LOOKBEHIND FUNCTION HAVE PROBLEMS, SO WE USING THIS FUNCTION - var iStartIndex = iIndex%this._aSegments.length; - var oSegment; - if(iStartIndex>0){ - oSegment = this._aSegments[iStartIndex]; - }else { - ////END OF TRACK - var iNewStartIndex = this._aSegments.length + iStartIndex -1; - oSegment = this._aSegments[iNewStartIndex]; - } +CGameBase.prototype.getValidSegment = function (iIndex) { + //LOOKBEHIND FUNCTION HAVE PROBLEMS, SO WE USING THIS FUNCTION + var iStartIndex = iIndex % this._aSegments.length; + var oSegment; + if (iStartIndex > 0) { + oSegment = this._aSegments[iStartIndex]; + } else { + ////END OF TRACK + var iNewStartIndex = this._aSegments.length + iStartIndex - 1; + oSegment = this._aSegments[iNewStartIndex]; + } - return oSegment; + return oSegment; }; -CGameBase.prototype.getPlayer = function(){ - return this._oPlayer; +CGameBase.prototype.getPlayer = function () { + return this._oPlayer; }; diff --git a/js/CInterface.js b/js/CInterface.js index 95bb019..06cc645 100644 --- a/js/CInterface.js +++ b/js/CInterface.js @@ -30,195 +30,309 @@ function CInterface(){ var _oRankContainer; var _oRankIndicator; var _pStartRankPos; - - this._init = function(){ - var aButtonList = new Array(); - - var oInterfaceContainer = new createjs.Container(); - s_oStage.addChild(oInterfaceContainer); - - var oSprite = s_oSpriteLibrary.getSprite('but_exit'); - _oButExit = new CGfxButton(0, 0, oSprite, oInterfaceContainer); - _oButExit.addEventListener(ON_MOUSE_UP, this._onExit, this); - - if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){ - var oSprite = s_oSpriteLibrary.getSprite('audio_icon'); - _oAudioToggle = new CToggle(0,0,oSprite,s_bAudioActive); - _oAudioToggle.addEventListener(ON_MOUSE_UP, this._onAudioToggle, this); - } - - var doc = window.document; - var docEl = doc.documentElement; - _fRequestFullScreen = docEl.requestFullscreen || docEl.mozRequestFullScreen || docEl.webkitRequestFullScreen || docEl.msRequestFullscreen; - _fCancelFullScreen = doc.exitFullscreen || doc.mozCancelFullScreen || doc.webkitExitFullscreen || doc.msExitFullscreen; + let raceTime = { + prev: 0, + curr: 0, + }; - if(ENABLE_FULLSCREEN === false){ - _fRequestFullScreen = false; - } + this._init = function () { + var aButtonList = new Array(); + + var oInterfaceContainer = new createjs.Container(); + s_oStage.addChild(oInterfaceContainer); + + var oSprite = s_oSpriteLibrary.getSprite("but_exit"); + _oButExit = new CGfxButton(0, 0, oSprite, oInterfaceContainer); + _oButExit.addEventListener(ON_MOUSE_UP, this._onExit, this); + + if (DISABLE_SOUND_MOBILE === false || s_bMobile === false) { + var oSprite = s_oSpriteLibrary.getSprite("audio_icon"); + _oAudioToggle = new CToggle(0, 0, oSprite, s_bAudioActive); + _oAudioToggle.addEventListener(ON_MOUSE_UP, this._onAudioToggle, this); + } + + var doc = window.document; + var docEl = doc.documentElement; + _fRequestFullScreen = + docEl.requestFullscreen || + docEl.mozRequestFullScreen || + docEl.webkitRequestFullScreen || + docEl.msRequestFullscreen; + _fCancelFullScreen = + doc.exitFullscreen || + doc.mozCancelFullScreen || + doc.webkitExitFullscreen || + doc.msExitFullscreen; + + if (ENABLE_FULLSCREEN === false) { + _fRequestFullScreen = false; + } + + if (_fRequestFullScreen && screenfull.isEnabled) { + oSprite = s_oSpriteLibrary.getSprite("but_fullscreen"); + _oButFullscreen = new CToggle( + 0, + 0, + oSprite, + s_bFullscreen, + oInterfaceContainer + ); + _oButFullscreen.addEventListener( + ON_MOUSE_UP, + this._onFullscreenRelease, + this + ); + aButtonList.push(_oButFullscreen); + } + + var oSprite = s_oSpriteLibrary.getSprite("but_settings"); + _pStartPosGUI = { + x: CANVAS_WIDTH - oSprite.width / 2 - 10, + y: oSprite.height / 2 + 10, + }; + _oGUIExpandible = new CGUIExpandible( + _pStartPosGUI.x, + _pStartPosGUI.y, + oSprite, + oInterfaceContainer + ); + _oGUIExpandible.addButton(_oButExit); + + if (DISABLE_SOUND_MOBILE === false || s_bMobile === false) { + _oGUIExpandible.addButton(_oAudioToggle); + } + + if (_fRequestFullScreen && screenfull.isEnabled) { + _oGUIExpandible.addButton(_oButFullscreen); + } + + ///////SET SPEED INDICATOR + _oTachometer = new CTachometer( + 206, + CANVAS_HEIGHT - 12, + oInterfaceContainer + ); + + ///////SET MINIMAP + _pStartPosMiniMap = { x: CANVAS_WIDTH - 162, y: CANVAS_HEIGHT - 226 }; + _oMiniMap = new CMiniMap( + _pStartPosMiniMap.x, + _pStartPosMiniMap.y, + oInterfaceContainer + ); + _oMiniMap.setPath(s_oGame.getSegments()); + + ///////LAP INDICATOR + _oLapIndicator = new CLapIndicator(CANVAS_WIDTH / 2, 10, s_oStage); + + ///////RANK INDICATOR + _pStartRankPos = { x: 20, y: 0 }; + _oRankContainer = new createjs.Container(); + _oRankContainer.x = _pStartRankPos.x; + _oRankContainer.y = _pStartRankPos.y; + oInterfaceContainer.addChild(_oRankContainer); + + _oRankIndicator = new CRankIndicator(150, 70, _oRankContainer, 150); + + _oCountdownTextStroke = new createjs.Text( + "", + " 300px " + PRIMARY_FONT, + "#000" + ); + _oCountdownTextStroke.x = CANVAS_WIDTH / 2; + _oCountdownTextStroke.y = CANVAS_HEIGHT / 2; + _oCountdownTextStroke.textAlign = "center"; + _oCountdownTextStroke.textBaseline = "middle"; + _oCountdownTextStroke.lineWidth = 200; + _oCountdownTextStroke.outline = 20; + oInterfaceContainer.addChild(_oCountdownTextStroke); + + _oCountdownText = new createjs.Text("", " 300px " + PRIMARY_FONT, "#fff"); + _oCountdownText.x = CANVAS_WIDTH / 2; + _oCountdownText.y = CANVAS_HEIGHT / 2; + _oCountdownText.textAlign = "center"; + _oCountdownText.textBaseline = "middle"; + _oCountdownText.lineWidth = 200; + oInterfaceContainer.addChild(_oCountdownText); + + if (s_bMobile) { + var iY = CANVAS_HEIGHT - 300; + var iX = 180; + var scaleFactor = 1; // Коэффициент увеличения + + ///////////// MOVEMENT + var oSprite = s_oSpriteLibrary.getSprite("key_up"); + _pStartPosUp = { + x: CANVAS_WIDTH - iX + oSprite.width / 2, + y: iY - 350, + }; + _oButUp = new CGfxButton( + _pStartPosUp.x, + _pStartPosUp.y, + oSprite, + oInterfaceContainer + ); + _oButUp.addEventListenerWithParams( + ON_MOUSE_UP, + s_oGame.onKeyUp, + s_oGame, + KEY_UP + ); + _oButUp.addEventListenerWithParams( + ON_MOUSE_DOWN, + s_oGame.onKeyDown, + s_oGame, + KEY_UP + ); + + var oSprite = s_oSpriteLibrary.getSprite("key_down"); + _pStartPosDown = { + x: CANVAS_WIDTH / 2, + y: CANVAS_HEIGHT - oSprite.height + 100, + }; // Центр экрана по X + _oButDown = new CGfxButton( + _pStartPosDown.x, + _pStartPosDown.y, + oSprite, + oInterfaceContainer + ); + _oButDown.addEventListenerWithParams( + ON_MOUSE_UP, + s_oGame.onKeyUp, + s_oGame, + KEY_DOWN + ); + _oButDown.addEventListenerWithParams( + ON_MOUSE_DOWN, + s_oGame.onKeyDown, + s_oGame, + KEY_DOWN + ); + + ///////////// DIRECTION + var oSprite = s_oSpriteLibrary.getSprite("key_left"); + oSprite.scaleX = scaleFactor; // Увеличиваем ширину + oSprite.scaleY = scaleFactor; // Увеличиваем высоту + _pStartPosLeft = { x: iX - oSprite.width / 2, y: iY - 160 }; + _oButLeft = new CGfxButton( + _pStartPosLeft.x, + _pStartPosLeft.y, + oSprite, + oInterfaceContainer + ); + _oButLeft.addEventListenerWithParams( + ON_MOUSE_UP, + s_oGame.onKeyUp, + s_oGame, + KEY_LEFT + ); + _oButLeft.addEventListenerWithParams( + ON_MOUSE_DOWN, + s_oGame.onKeyDown, + s_oGame, + KEY_LEFT + ); + + var oSprite = s_oSpriteLibrary.getSprite("key_right"); + _pStartPosRight = { + x: CANVAS_WIDTH - iX + oSprite.width / 2, + y: iY - 160, + }; + _oButRight = new CGfxButton( + _pStartPosRight.x, + _pStartPosRight.y, + oSprite, + oInterfaceContainer + ); + _oButRight.addEventListenerWithParams( + ON_MOUSE_UP, + s_oGame.onKeyUp, + s_oGame, + KEY_RIGHT + ); + _oButRight.addEventListenerWithParams( + ON_MOUSE_DOWN, + s_oGame.onKeyDown, + s_oGame, + KEY_RIGHT + ); + } + + _aNumCountDown = new Array(); + for (var i = 0; i <= 3; i++) { + _aNumCountDown[i] = false; + } + + this.refreshButtonPos(); + }; - if (_fRequestFullScreen && screenfull.isEnabled){ - oSprite = s_oSpriteLibrary.getSprite("but_fullscreen") - _oButFullscreen = new CToggle(0,0,oSprite,s_bFullscreen, oInterfaceContainer); - _oButFullscreen.addEventListener(ON_MOUSE_UP,this._onFullscreenRelease,this); - aButtonList.push(_oButFullscreen); - } + this.unload = function () { + _oGUIExpandible.unload(); - var oSprite = s_oSpriteLibrary.getSprite('but_settings'); - _pStartPosGUI = {x:CANVAS_WIDTH - (oSprite.width/2) -10,y:(oSprite.height/2) +10}; - _oGUIExpandible = new CGUIExpandible(_pStartPosGUI.x, _pStartPosGUI.y, oSprite, oInterfaceContainer); - _oGUIExpandible.addButton(_oButExit); - - if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){ - _oGUIExpandible.addButton(_oAudioToggle); - } - - if (_fRequestFullScreen && screenfull.isEnabled){ - _oGUIExpandible.addButton(_oButFullscreen); - } + s_oInterface = null; + if (s_bMobile) { + _oButUp.unload(); + _oButDown.unload(); + _oButLeft.unload(); + _oButRight.unload(); + } + }; - ///////SET SPEED INDICATOR - _oTachometer = new CTachometer(206, CANVAS_HEIGHT-12, oInterfaceContainer); - - ///////SET MINIMAP - _pStartPosMiniMap = {x: CANVAS_WIDTH-162, y: CANVAS_HEIGHT-226}; - _oMiniMap = new CMiniMap(_pStartPosMiniMap.x, _pStartPosMiniMap.y, oInterfaceContainer); - _oMiniMap.setPath(s_oGame.getSegments()); + this.refreshButtonPos = function () { + _oGUIExpandible.refreshPos(); - ///////LAP INDICATOR - _oLapIndicator = new CLapIndicator(CANVAS_WIDTH/2, 10, s_oStage); + if (s_bMobile) { + _oButDown.setPosition(_pStartPosDown.x, _pStartPosDown.y); // Убираем оффсеты для кнопки "тормоз" + _oButLeft.setPosition( + _pStartPosLeft.x + s_iOffsetX, + _pStartPosLeft.y - s_iOffsetY + ); + _oButRight.setPosition( + _pStartPosRight.x - s_iOffsetX, + _pStartPosRight.y - s_iOffsetY + ); + } - ///////RANK INDICATOR - _pStartRankPos = {x:20, y:0} - _oRankContainer = new createjs.Container(); - _oRankContainer.x = _pStartRankPos.x; - _oRankContainer.y = _pStartRankPos.y; - oInterfaceContainer.addChild(_oRankContainer); + _oTachometer.updateOffset(s_iOffsetX, s_iOffsetY); - - _oRankIndicator = new CRankIndicator(150, 70, _oRankContainer, 150); - - _oCountdownTextStroke = new createjs.Text(""," 300px "+PRIMARY_FONT, "#000"); - _oCountdownTextStroke.x = CANVAS_WIDTH/2; - _oCountdownTextStroke.y = CANVAS_HEIGHT/2; - _oCountdownTextStroke.textAlign = "center"; - _oCountdownTextStroke.textBaseline = "middle"; - _oCountdownTextStroke.lineWidth = 200; - _oCountdownTextStroke.outline = 20; - oInterfaceContainer.addChild(_oCountdownTextStroke); - - _oCountdownText = new createjs.Text(""," 300px "+PRIMARY_FONT, "#fff"); - _oCountdownText.x = CANVAS_WIDTH/2; - _oCountdownText.y = CANVAS_HEIGHT/2; - _oCountdownText.textAlign = "center"; - _oCountdownText.textBaseline = "middle"; - _oCountdownText.lineWidth = 200; - oInterfaceContainer.addChild(_oCountdownText); - - if(s_bMobile){ - var iY = CANVAS_HEIGHT-300; - var iX = 180; - var scaleFactor = 1; // Коэффициент увеличения - - ///////////// MOVEMENT - var oSprite = s_oSpriteLibrary.getSprite('key_up'); - _pStartPosUp = {x: CANVAS_WIDTH-iX +oSprite.width/2, y: iY-350}; - _oButUp = new CGfxButton(_pStartPosUp.x, _pStartPosUp.y, oSprite, oInterfaceContainer); - _oButUp.addEventListenerWithParams(ON_MOUSE_UP, s_oGame.onKeyUp, s_oGame, KEY_UP); - _oButUp.addEventListenerWithParams(ON_MOUSE_DOWN, s_oGame.onKeyDown, s_oGame, KEY_UP); - - var oSprite = s_oSpriteLibrary.getSprite('key_down'); - _pStartPosDown = {x: CANVAS_WIDTH / 2 , y: CANVAS_HEIGHT - oSprite.height +100}; // Центр экрана по X - _oButDown = new CGfxButton(_pStartPosDown.x, _pStartPosDown.y, oSprite, oInterfaceContainer); - _oButDown.addEventListenerWithParams(ON_MOUSE_UP, s_oGame.onKeyUp, s_oGame, KEY_DOWN); - _oButDown.addEventListenerWithParams(ON_MOUSE_DOWN, s_oGame.onKeyDown, s_oGame, KEY_DOWN); - - ///////////// DIRECTION - var oSprite = s_oSpriteLibrary.getSprite('key_left'); - oSprite.scaleX = scaleFactor; // Увеличиваем ширину -oSprite.scaleY = scaleFactor; // Увеличиваем высоту - _pStartPosLeft = {x:iX-oSprite.width/2, y: iY-160}; - _oButLeft = new CGfxButton(_pStartPosLeft.x, _pStartPosLeft.y, oSprite, oInterfaceContainer); - _oButLeft.addEventListenerWithParams(ON_MOUSE_UP, s_oGame.onKeyUp, s_oGame, KEY_LEFT); - _oButLeft.addEventListenerWithParams(ON_MOUSE_DOWN, s_oGame.onKeyDown, s_oGame, KEY_LEFT); - - var oSprite = s_oSpriteLibrary.getSprite('key_right'); - _pStartPosRight = {x: CANVAS_WIDTH-iX +oSprite.width/2, y: iY-160}; - _oButRight = new CGfxButton(_pStartPosRight.x, _pStartPosRight.y, oSprite, oInterfaceContainer); - _oButRight.addEventListenerWithParams(ON_MOUSE_UP, s_oGame.onKeyUp, s_oGame, KEY_RIGHT); - _oButRight.addEventListenerWithParams(ON_MOUSE_DOWN, s_oGame.onKeyDown, s_oGame, KEY_RIGHT); - - } + _oMiniMap.setPosition( + _pStartPosMiniMap.x - s_iOffsetX, + _pStartPosMiniMap.y - s_iOffsetY + ); - _aNumCountDown = new Array(); - for(var i=0; i<=3; i++){ - _aNumCountDown[i] = false; - } + _oLapIndicator.setPosition(); - this.refreshButtonPos(); + _oRankContainer.x = _pStartRankPos.x + s_iOffsetX; + _oRankContainer.y = _pStartRankPos.y + s_iOffsetY; }; - - this.unload = function(){ - _oGUIExpandible.unload(); - s_oInterface = null; - - if(s_bMobile){ - _oButUp.unload(); - _oButDown.unload(); - _oButLeft.unload(); - _oButRight.unload(); - } - + this.resetFullscreenBut = function () { + if (_fRequestFullScreen && screenfull.isEnabled) { + _oButFullscreen.setActive(s_bFullscreen); + } }; - - this.refreshButtonPos = function(){ - _oGUIExpandible.refreshPos(); - - if(s_bMobile){ - - _oButDown.setPosition(_pStartPosDown.x, _pStartPosDown.y); // Убираем оффсеты для кнопки "тормоз" - _oButLeft.setPosition(_pStartPosLeft.x + s_iOffsetX, _pStartPosLeft.y - s_iOffsetY); - _oButRight.setPosition(_pStartPosRight.x - s_iOffsetX, _pStartPosRight.y - s_iOffsetY); - } - - _oTachometer.updateOffset(s_iOffsetX, s_iOffsetY); - _oMiniMap.setPosition(_pStartPosMiniMap.x- s_iOffsetX, _pStartPosMiniMap.y - s_iOffsetY); - - _oLapIndicator.setPosition(); + this._onFullscreenRelease = function () { + if (s_bFullscreen) { + _fCancelFullScreen.call(window.document); + } else { + _fRequestFullScreen.call(window.document.documentElement); + } - _oRankContainer.x = _pStartRankPos.x + s_iOffsetX; - _oRankContainer.y = _pStartRankPos.y + s_iOffsetY; - - - - }; - - this.resetFullscreenBut = function(){ - if (_fRequestFullScreen && screenfull.isEnabled){ - _oButFullscreen.setActive(s_bFullscreen); - } + sizeHandler(); }; - - this._onFullscreenRelease = function(){ - if(s_bFullscreen) { - _fCancelFullScreen.call(window.document); - }else{ - _fRequestFullScreen.call(window.document.documentElement); - } - - sizeHandler(); + this.refreshFPS = function (iValue) { + _oTimer.setTime(iValue); }; - - this.refreshFPS = function(iValue){ - _oTimer.setTime(iValue); + this.refreshLap = function (iCur, iTot) { + _oLapIndicator.refreshLap(iCur, iTot); }; - this.refreshLap = function(iCur, iTot){ - _oLapIndicator.refreshLap(iCur, iTot); + this.refreshRaceTime = function (iValue) { + _oLapIndicator.refreshRaceTime(iValue); }; this.refreshRankPosition = function(iPos){ diff --git a/js/CLapIndicator.js b/js/CLapIndicator.js index f8530a0..bd96f11 100644 --- a/js/CLapIndicator.js +++ b/js/CLapIndicator.js @@ -4,47 +4,89 @@ function CLapIndicator(iX, iY, oParentContainer){ var _pStartPos; - this._init = function(){ - _pStartPos = {x: iX, y:iY}; - - _oContainer = new createjs.Container(); - _oContainer.x = iX; - _oContainer.y = iY; - oParentContainer.addChild(_oContainer); - - var oSprite = s_oSpriteLibrary.getSprite('lap_panel'); - var oBg = createBitmap(oSprite); - oBg.regX = oSprite.width/2; - oBg.regY = 0; - _oContainer.addChild(oBg); - - var iWidth = 140; - var iHeight = 70; - var iTextX = 50; - var iTextY = 54; - _oText = new CTLText(_oContainer, - iTextX-iWidth/2, iTextY-iHeight/2, iWidth, iHeight, - 70, "center", "#fff", PRIMARY_FONT, 1, - 2, 2, - sprintf(TEXT_LAP, 0, 0), - true, true, false, - false ); - _oText.setStroke(10,"#000"); + this._init = function () { + _pStartPos = { x: iX, y: iY }; + + _oContainer = new createjs.Container(); + _oContainer.x = iX; + _oContainer.y = iY; + oParentContainer.addChild(_oContainer); + + var oSprite = s_oSpriteLibrary.getSprite("lap_panel"); + var oBg = createBitmap(oSprite); + oBg.regX = oSprite.width / 2; + oBg.regY = 0; + _oContainer.addChild(oBg); + + var iWidth = 140; + var iHeight = 70; + var iTextX = 50; + var iTextY = 54; + _oText = new CTLText( + _oContainer, + iTextX - iWidth / 2, + iTextY - iHeight / 2, + iWidth, + iHeight, + 70, + "center", + "#fff", + PRIMARY_FONT, + 1, + 2, + 2, + sprintf(TEXT_LAP, 0, 0), + true, + true, + false, + false + ); + _oText.setStroke(10, "#000"); + + _oRaceTime = new CTLText( + _oContainer, + iTextX * 2.5, + iTextY - iHeight / 2, + iWidth, + iHeight, + 70, + "center", + "#fff", + PRIMARY_FONT, + 1, + 2, + 2, + sprintf("%s:%s", 0, 0), + true, + true, + false, + false + ); + _oRaceTime.setStroke(10, "#000"); }; - - this.unload = function(){ - oParentContainer.removeChild(_oContainer); + + this.unload = function () { + oParentContainer.removeChild(_oContainer); }; - - this.setPosition = function(){ - _oContainer.x = _pStartPos.x; - _oContainer.y = _pStartPos.y + s_iOffsetY; + + this.setPosition = function () { + _oContainer.x = _pStartPos.x; + _oContainer.y = _pStartPos.y + s_iOffsetY; }; - - this.refreshLap = function(iCur, iTot){ - var szText = sprintf(TEXT_LAP, iCur, iTot); - - _oText.refreshText(szText); + + this.refreshLap = function (iCur, iTot) { + var szText = sprintf(TEXT_LAP, iCur, iTot); + + _oText.refreshText(szText); + }; + + this.refreshRaceTime = function (iMilliseconds) { + var iSeconds = Math.floor(iMilliseconds / 1000); + var iMinutes = Math.floor(iSeconds / 60); + iSeconds -= iMinutes * 60; + + var szText = sprintf("%s:%s", iMinutes, iSeconds); + _oRaceTime.refreshText(szText); }; this.setVisible = function(bVal){