Skip to content

Commit

Permalink
add timer
Browse files Browse the repository at this point in the history
  • Loading branch information
parabolabam committed Oct 1, 2024
1 parent 3efc6b2 commit 3ecd433
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 24 deletions.
16 changes: 14 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<script type="text/javascript" src="js/lib/platform.js"></script>
<script type="text/javascript" src="js/lib/ios_fullscreen.js"></script>
<script type="text/javascript" src="js/lib/howler.min.js"></script>
<script type="text/javascript" src="js/lib/CTimeFormatter.js"></script>
<script type="text/javascript" src="js/lib/ctl_utils.js"></script>
<script type="text/javascript" src="js/lib/sprite_lib.js"></script>
<script type="text/javascript" src="js/lib/CTextButton.js"></script>
Expand Down Expand Up @@ -98,27 +99,38 @@
if(getParamValue('ctl-arcade') === "true"){
parent.__ctlArcadeStartSession();
}
window.parent.postMessage({ type: "START_F1_SESSION" }, "*");
//...ADD YOUR CODE HERE EVENTUALLY
});

$(oMain).on("end_session", function(evt) {
if(getParamValue('ctl-arcade') === "true"){
parent.__ctlArcadeEndSession();
}
window.parent.postMessage({ type: "END_F1_SESSION" }, "*");
//...ADD YOUR CODE HERE EVENTUALLY
});

$(oMain).on("restart_level", function(evt, iLevel) {
if(getParamValue('ctl-arcade') === "true"){
parent.__ctlArcadeRestartLevel({level:iLevel});
}
window.parent.postMessage({ type: "RESTART_F1_SESSION" }, "*");
//...ADD YOUR CODE HERE EVENTUALLY
});

$(oMain).on("save_score", function(evt,iScore, szMode) {
$(oMain).on("save_score", function(evt,iScore,szMode) {
if(getParamValue('ctl-arcade') === "true"){
parent.__ctlArcadeSaveScore({score:iScore, mode: szMode});
parent.__ctlArcadeSaveScore({score:iScore.score, mode: szMode});
}

window.parent.postMessage({
type: "SAVE_F1_SCORE",
score: iScore.score,
raceTime: iScore.raceTime
},
"*",
);
//...ADD YOUR CODE HERE EVENTUALLY
});

Expand Down
14 changes: 10 additions & 4 deletions js/CGameBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ var CGameBase = function (oData, iLevel) {

this._oLevelBuilder;

this.raceTimer;
this.raceTimer = 0;
this.raceTime = 0;

s_oGame = this;

Expand Down Expand Up @@ -301,6 +302,7 @@ CGameBase.prototype.resetParams = function () {
}

this._iGameState = STATE_GAME_START;
this.raceTime = 0;
this._iScore = 0;
this._iTimeElaps = LEVEL_INFO[this._iLevel].time;
this._iStartCountDown = START_COUNTDOWN;
Expand Down Expand Up @@ -452,12 +454,13 @@ CGameBase.prototype.update = function () {
this._oPlayer.update(iDt);

this.updateRace(iDt);
console.log({ s_iTimeElaps, s_iCntTime });
this.raceTimer =
this.raceTimer ||
setInterval(() => {
this._oInterface.refreshRaceTime(new Date().getTime());
}, 1000);
requestAnimationFrame(() =>
this._oInterface.refreshRaceTime(++this.raceTime)
);
}, 1);

break;
}
Expand All @@ -466,6 +469,9 @@ CGameBase.prototype.update = function () {
this._oPlayer.autoPilot();

this.updateOpponents(iDt);
clearInterval(this.raceTimer);
this.raceTimer = 0;
// this.resetParams();

break;
}
Expand Down
8 changes: 6 additions & 2 deletions js/CGameRace.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ CGameRace.prototype.playerLapCompleted = function(iCurLap){
this._iCurPlayerLap = iCurLap;
if(this._iCurPlayerLap >= this._iTotLap){
///END RACE
this._iCurPlayerLap = this._iTotLap;
this.trackCompleted();
}else {

Expand Down Expand Up @@ -218,7 +219,7 @@ CGameRace.prototype.trackCompleted = function(){
//////NEXTLEVELPANEL
if(iPlayerRank <= MIN_RANK_FOR_WIN){
this._iScore = Math.floor(POINTS_PER_RANK[iPlayerRank-1]);
var oNextLevelPanel = new CNextLevelPanel(iPlayerRank, this._iScore, this._iLevel);
var oNextLevelPanel = new CNextLevelPanel(iPlayerRank, this._iScore, this._iLevel, this.raceTime);



Expand All @@ -232,7 +233,10 @@ CGameRace.prototype.trackCompleted = function(){
iTotalScore += s_aLevelScore[i];

}
$(s_oMain).trigger("save_score", iTotalScore);
$(s_oMain).trigger("save_score", {
score: iTotalScore,
raceTime: this.raceTime,
});

}else{

Expand Down
11 changes: 3 additions & 8 deletions js/CLapIndicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function CLapIndicator(iX, iY, oParentContainer){

_oRaceTime = new CTLText(
_oContainer,
iTextX * 2.5,
iTextX * 3.5,
iTextY - iHeight / 2,
iWidth,
iHeight,
Expand All @@ -56,7 +56,7 @@ function CLapIndicator(iX, iY, oParentContainer){
1,
2,
2,
sprintf("%s:%s", 0, 0),
sprintf("%s:%s:%s", "00", "00", "000"),
true,
true,
false,
Expand All @@ -81,12 +81,7 @@ function CLapIndicator(iX, iY, oParentContainer){
};

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);
_oRaceTime.refreshText(formatTime(iMilliseconds));
};

this.setVisible = function(bVal){
Expand Down
16 changes: 8 additions & 8 deletions js/CNextLevelPanel.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function CNextLevelPanel(iRank, iScore, iLevel){
function CNextLevelPanel(iRank, iScore, iLevel, raceTime){

var _oPanelContainer;
var _oFade;
Expand Down Expand Up @@ -73,17 +73,17 @@ function CNextLevelPanel(iRank, iScore, iLevel){
oScoreContainer.y = -50;
_oPanelContainer.addChild(oScoreContainer);

var oSprite = s_oSpriteLibrary.getSprite('star');
var oStar = createBitmap(oSprite);
oStar.regX = oSprite.width/2;
oStar.regY = oSprite.height/2;
oScoreContainer.addChild(oStar);
// var oSprite = s_oSpriteLibrary.getSprite('star');
// var oStar = createBitmap(oSprite);
// oStar.regX = oSprite.width/2;
// oStar.regY = oSprite.height/2;
// oScoreContainer.addChild(oStar);

var oScoreText = new CTLText(oScoreContainer,
50, -20, 150, 50,
50, "left", "#fff", PRIMARY_FONT, 1,
0, 0,
"0",
formatTime(raceTime),
true, true, false,
false );
oScoreText.setStroke(10,"#000");
Expand All @@ -95,7 +95,7 @@ function CNextLevelPanel(iRank, iScore, iLevel){


new createjs.Tween.get(_oPanelContainer).to({y:CANVAS_HEIGHT/2},500, createjs.Ease.quartIn).call(function(){
oScoreText.roll(iScore, 5000,createjs.Ease.cubicOut);
// oScoreText.roll(formatTime(raceTime), 5000,createjs.Ease.cubicOut);
});


Expand Down
12 changes: 12 additions & 0 deletions js/lib/CTimeFormatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function formatTime(iMilliseconds) {
const iTotalSeconds = Math.floor(iMilliseconds / 1000);
const iMinutes = Math.floor(iTotalSeconds / 60);
const iSeconds = iTotalSeconds % 60;
const iMillis = iMilliseconds % 1000;

const szMinutes = String(iMinutes).padStart(2, "0");
const szSeconds = String(iSeconds).padStart(2, "0");
const szMillis = String(iMillis).padStart(3, "0");

return `${szMinutes}:${szSeconds}:${szMillis}`;
}

0 comments on commit 3ecd433

Please sign in to comment.