@@ -112,13 +112,20 @@ public class GameManager extends JPanel implements Runnable {
112112 public static final int OUTSIDE = 11 ;
113113 public static final int INDOOR = 12 ;
114114 public static final int DUNGEON = 13 ;
115- public static final int BOSS_DUNGEON = 13 ;
115+ public static final int BOSS_DUNGEON = 14 ;
116116 //Until player doesn't press shift, he doesn't run
117117 public boolean playerRun = false ;
118118 public boolean fullScreenOn = false ;
119119 public boolean bossBattleOn = false ;
120120 public Instant startTime ;
121121 public Duration playTime ;
122+ // Add a timer variable to keep track of the game time
123+ public Duration gameTimer = Duration .ZERO ;
124+ // Add a variable to keep track of the last update time
125+ private Instant lastUpdateTime = Instant .now ();
126+ public int coinAmount ;
127+ public int levelForTitle ;
128+
122129
123130 public GameManager () {
124131 //set size of this class
@@ -143,6 +150,7 @@ public void setupGame() {
143150 if (fullScreenOn )
144151 setFullScreen ();
145152 startTime = Instant .now ();
153+ gameTimer = Duration .ZERO ;
146154 playTime = Duration .ZERO ;
147155 }
148156
@@ -267,23 +275,29 @@ public void update() {
267275 animated .update ();
268276
269277 enManagement .update ();
270-
271- // Calculate play time
272- Instant currentTime = Instant .now ();
273- playTime = Duration .between (startTime , currentTime );
278+ calculateInGameTime ();
274279 }
275280 if (gameState == PAUSE_STATE ) {
276281 //Stop game
277282 }
278283 }
279284
285+ private void calculateInGameTime () {
286+ // Calculate time elapsed since the last update
287+ Instant currentTime = Instant .now ();
288+ Duration elapsedTime = Duration .between (lastUpdateTime , currentTime );
289+ lastUpdateTime = currentTime ;
290+ // Update game timer and play time
291+ playTime = playTime .plus (elapsedTime );
292+ gameTimer = gameTimer .plus (elapsedTime );
293+ }
294+
280295 public String formatDuration (Duration duration ) {
281296 long seconds = duration .getSeconds ();
282- long minutes = seconds / 60 ;
283- seconds %= 60 ;
284- long hours = minutes / 60 ;
285- minutes %= 60 ;
286- return String .format ("%02d:%02d:%02d" , hours , minutes , seconds );
297+ long absSeconds = Math .abs (seconds );
298+ String formattedDuration = String .format ("%02d:%02d:%02d" ,
299+ absSeconds / 3600 , (absSeconds % 3600 ) / 60 , absSeconds % 60 );
300+ return seconds < 0 ? "-" + formattedDuration : formattedDuration ;
287301 }
288302
289303 //method to draw the components on screen
@@ -375,9 +389,9 @@ private void debugFunction(Graphics2D graphics2D, long drawStart) {
375389 y += lineHeight ;
376390 graphics2D .drawString ("Col: " + (player .worldX + player .solidArea .x ) / TILE_SIZE , x , y );
377391 y += lineHeight ;
378- graphics2D .drawString ("Row: : " + (player .worldY + player .solidArea .y ) / TILE_SIZE , x , y );
392+ graphics2D .drawString ("Row: " + (player .worldY + player .solidArea .y ) / TILE_SIZE , x , y );
379393 y += lineHeight ;
380- graphics2D .drawString ("Invincible: : " + player .invincibleCounter , x , y );
394+ graphics2D .drawString ("Invincible: " + player .invincibleCounter , x , y );
381395 y += lineHeight ;
382396 graphics2D .drawString ("Draw Time: " + passed , x , y );
383397 y += lineHeight ;
@@ -387,7 +401,7 @@ private void debugFunction(Graphics2D graphics2D, long drawStart) {
387401 y += lineHeight ;
388402 graphics2D .drawString ("Press F8 to reload tiles" , x , y );
389403 y += lineHeight ;
390- graphics2D .drawString ("God mode:" + keyHandler .godMode , x , y );
404+ graphics2D .drawString ("God mode: " + keyHandler .godMode , x , y );
391405 //Show player bounds
392406 graphics2D .setColor (Color .RED );
393407 graphics2D .setStroke (new BasicStroke (2 ));
@@ -472,4 +486,13 @@ public void changeArea() {
472486 placeObjects .setMobs ();
473487 currentArea = nextArea ;
474488 }
489+
490+ public void resetTimer () {
491+ playTime = Duration .ZERO ;
492+ gameTimer = Duration .ZERO ;
493+ }
494+
495+ public Duration getGameTimer () {
496+ return gameTimer ;
497+ }
475498}
0 commit comments