11package states ;
22
3+ import progress .PlayTimePlugin ;
4+ import ui .font .BitmapText ;
35import flixel .FlxSprite ;
46import ldtk .Level ;
57import states .substate .UpgradeCutscene ;
@@ -52,6 +54,9 @@ class PlayState extends FlxTransitionableState {
5254
5355 public var player : Player ;
5456
57+ public var timerCam = new FlxCamera ();
58+ public var timerTxt = new BitmapText (" 0:00" );
59+
5560 public var baseTerrainCam : FlxCamera ;
5661 public var colorCams : Map <Color , FlxCamera > = [];
5762 public var objectCam : FlxCamera ;
@@ -75,6 +80,9 @@ class PlayState extends FlxTransitionableState {
7580 var deltaMod = 1.0 ;
7681 var deathDeltaMod = 0.1 ;
7782
83+ // This is our cumulative time in-game
84+ var baseTime = 0.0 ;
85+ // This is our time on this specific level
7886 public var levelTime = 0.0 ;
7987
8088 var resetQueued = false ;
@@ -109,6 +117,22 @@ class PlayState extends FlxTransitionableState {
109117 objectCam = makeShaderCamera (EMPTY );
110118 FlxG .cameras .add (objectCam , false );
111119
120+ timerCam .bgColor = FlxColor .TRANSPARENT ;
121+ FlxG .cameras .add (timerCam , false );
122+ timerTxt .scrollFactor .set ();
123+ timerTxt .camera = timerCam ;
124+ timerTxt .autoSize = false ;
125+ timerTxt .width = FlxG .width ;
126+ timerTxt .alignment = CENTER ;
127+ timerTxt .updateHitbox ();
128+ timerTxt .screenCenter (X );
129+ timerTxt .setBorderStyle (OUTLINE , FlxColor .BLACK );
130+
131+ if (Collected .isGameComplete ()) {
132+ // Only show timer once they've completed the game
133+ add (timerTxt );
134+ }
135+
112136 // XXX: need the substate to use the right cameras... but it's not set yet, so we go to this
113137 // variable to get it
114138 if (_requestedSubState != null ) {
@@ -150,6 +174,7 @@ class PlayState extends FlxTransitionableState {
150174 var checkpointRoom = Collected .getCheckpointLevel ();
151175 var checkpointEntity = Collected .getCheckpointEntity ();
152176 if (checkpointRoom != null && checkpointEntity != null ) {
177+ PlayTimePlugin .ME .accumulated = Collected .getTime ();
153178 loadLevel (checkpointRoom , checkpointEntity );
154179 } else {
155180 var spawnLevel = levels.ldtk. Level .project .all_worlds .Default .levels .filter ((l ) -> return l .l_Objects .all_Spawn .length > 0 );
@@ -162,7 +187,7 @@ class PlayState extends FlxTransitionableState {
162187 }
163188 #end
164189
165- FlxG . watch . add ( this , " levelTime " , " Timer: " ) ;
190+ PlayTimePlugin . ME . timerRunning = true ;
166191 }
167192
168193 override function draw () {
@@ -220,8 +245,8 @@ class PlayState extends FlxTransitionableState {
220245 lastLevel = levelID ;
221246 lastSpawnEntity = entityID ;
222247
223- Collected .addTime (levelTime );
224- levelTime = 0 ;
248+ // Collected.addTime(levelTime);
249+ Collected . setTime ( PlayTimePlugin . ME . accumulated ) ;
225250
226251 Collected .setLastCheckpoint (levelID , entityID );
227252
@@ -541,9 +566,10 @@ class PlayState extends FlxTransitionableState {
541566
542567 super .update (elapsed );
543568
544- // if (player.inControl) {
545- levelTime + = originalDelta ;
546- // }
569+ // Time travels at normal speed as far as timing goes
570+ // levelTime += originalDelta;
571+
572+ updateTimer ();
547573
548574 for (o in pendingObjects ) {
549575 o .add_to_group (objects );
@@ -570,6 +596,12 @@ class PlayState extends FlxTransitionableState {
570596 #end
571597 }
572598
599+ public function updateTimer () {
600+ var rawTime = PlayTimePlugin .ME .accumulated ;
601+ var ngConsistentTime = Math .round (rawTime * 1000 );
602+ timerTxt .text = CreditsState .formatTime (ngConsistentTime , false );
603+ }
604+
573605 var tmpScreenPoint = FlxPoint .get ();
574606 function camSeesWorldPoint (cam : FlxCamera , x : Float , y : Float ): Bool {
575607 tmpScreenPoint = objectCam .project (FlxPoint .weak (x , y ));
0 commit comments