Skip to content

Commit f8152ab

Browse files
committed
The game timer was fixed.
1 parent 8b70557 commit f8152ab

File tree

18 files changed

+169
-154
lines changed

18 files changed

+169
-154
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Tiny Legend Reborn(Java-2D-RPG)
1+
# Tiny Adventure(Java 2D RPG)
22
<img width="718" alt="image" src="https://github.com/SEM24/Java-2D-Medieval-RPG/assets/71443826/0c8d83b8-bcec-4b8d-b0fc-329e2f4d16a3">
33
<img width="716" alt="image" src="https://github.com/SEM24/Java-2D-Medieval-RPG/assets/71443826/b9a1d38a-2ffb-4d5c-8718-15fc938eb6fb">
44

pom.xml

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,12 @@
55
<modelVersion>4.0.0</modelVersion>
66

77
<groupId>org.game</groupId>
8-
<artifactId>JavaGame</artifactId>
9-
<version>1.0-SNAPSHOT</version>
8+
<artifactId>TinyAdventure</artifactId>
9+
<version>1.0.0</version>
1010

1111
<properties>
1212
<maven.compiler.source>17</maven.compiler.source>
1313
<maven.compiler.target>17</maven.compiler.target>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515
</properties>
16-
<dependencies>
17-
<!-- <dependency>-->
18-
<!-- <groupId>com.formdev</groupId>-->
19-
<!-- <artifactId>flatlaf</artifactId>-->
20-
<!-- <version>3.1.1</version>-->
21-
<!-- </dependency>-->
22-
</dependencies>
2316
</project>

src/main/java/com/khomsi/game/GameApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ private void startGame() {
2323
System.setProperty("sun.java2d.opengl", "true");
2424
//can't resize window
2525
window.setResizable(false);
26-
window.setTitle("Tiny Legend Reborn");
26+
window.setTitle("Tiny Adventure");
2727
//Set icon for window
2828
ImageIcon logo = new ImageIcon(Objects.requireNonNull(getClass().getResource(iconMainPath)));
2929
window.setIconImage(logo.getImage());

src/main/java/com/khomsi/game/ai/PathFinder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ else if (openList.get(i).fCost == bestNodeFCost) {
142142
}
143143
}
144144
//if there is no node in the openList, end the loop
145-
if (openList.size() == 0) {
145+
if (openList.isEmpty()) {
146146
break;
147147
}
148148
//After the loop, we get the best node which is our next step

src/main/java/com/khomsi/game/data/DataInitializer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ public class DataInitializer implements Serializable {
3333
String mapObjectLootNames[][];
3434
boolean mapObjectOpened[][];
3535

36-
public Instant savedPlayTime;
36+
public Duration savedPlayTime;
37+
public Duration savedGameTimer;
3738
}

src/main/java/com/khomsi/game/data/SaveLoad.java

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import com.khomsi.game.main.GameManager;
44

55
import java.io.*;
6-
import java.time.Duration;
76

87
public class SaveLoad {
98
GameManager gameManager;
9+
// Path to save.dat file to check the statement
10+
private static final String FILE_PATH = "save.dat";
11+
public final File file = new File(FILE_PATH);
12+
public final boolean hasFile = file.exists();
1013

1114
public SaveLoad(GameManager gameManager) {
1215
this.gameManager = gameManager;
@@ -16,7 +19,7 @@ public SaveLoad(GameManager gameManager) {
1619
* Save method to save data info about player
1720
*/
1821
public void save() {
19-
try (ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("save.dat"))) {
22+
try (ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(FILE_PATH))) {
2023
DataInitializer initializer = new DataInitializer();
2124
initializer.level = gameManager.player.level;
2225
initializer.maxHp = gameManager.player.maxHp;
@@ -31,8 +34,9 @@ public void save() {
3134
initializer.coin = gameManager.player.coin;
3235
initializer.playerSkin = gameManager.player.playerSkin;
3336
//TODO currently doesn't work
34-
//Save play time
35-
initializer.savedPlayTime = gameManager.startTime;
37+
// Save play time and game timer
38+
initializer.savedPlayTime = gameManager.playTime;
39+
initializer.savedGameTimer = gameManager.getGameTimer();
3640

3741
//Player's inventory
3842
for (int i = 0; i < gameManager.player.inventory.size(); i++) {
@@ -77,7 +81,7 @@ public void save() {
7781
* Load method to get data info about player
7882
*/
7983
public boolean load() {
80-
try (ObjectInputStream is = new ObjectInputStream(new FileInputStream("save.dat"))) {
84+
try (ObjectInputStream is = new ObjectInputStream(new FileInputStream(FILE_PATH))) {
8185
//Read object from file
8286
DataInitializer initializer = (DataInitializer) is.readObject();
8387
gameManager.player.level = initializer.level;
@@ -92,8 +96,10 @@ public boolean load() {
9296
gameManager.player.nextLevelXp = initializer.nextLevelXp;
9397
gameManager.player.coin = initializer.coin;
9498
gameManager.player.playerSkin = initializer.playerSkin;
95-
// Load play time
96-
gameManager.startTime = initializer.savedPlayTime;
99+
100+
// Load play time and game timer
101+
gameManager.playTime = initializer.savedPlayTime;
102+
gameManager.gameTimer = initializer.savedGameTimer;
97103
//Load skin of player
98104
gameManager.player.loadImages();
99105

@@ -138,12 +144,22 @@ public boolean load() {
138144
return true;
139145
}
140146

141-
public Duration parseDuration(String durationString) {
142-
String[] parts = durationString.split(":");
143-
long hours = Long.parseLong(parts[0]);
144-
long minutes = Long.parseLong(parts[1]);
145-
long seconds = Long.parseLong(parts[2]);
146-
147-
return Duration.ofHours(hours).plusMinutes(minutes).plusSeconds(seconds);
147+
public void loadTitleData() {
148+
if (!hasFile) {
149+
// Set to default values or fallback values
150+
gameManager.levelForTitle = gameManager.player.level;
151+
gameManager.coinAmount = gameManager.player.coin;
152+
gameManager.playTime = gameManager.getGameTimer();
153+
} else {
154+
try (ObjectInputStream is = new ObjectInputStream(new FileInputStream(FILE_PATH))) {
155+
// Read object from file
156+
DataInitializer initializer = (DataInitializer) is.readObject();
157+
gameManager.levelForTitle = initializer.level;
158+
gameManager.coinAmount = initializer.coin;
159+
gameManager.playTime = initializer.savedPlayTime;
160+
} catch (IOException | ClassNotFoundException e) {
161+
System.err.println("Exception " + e.getMessage() + " in " + getClass().getSimpleName());
162+
}
163+
}
148164
}
149165
}

src/main/java/com/khomsi/game/entity/mobs/MobSlime.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ private void getImage() {
6262

6363
@Override
6464
public void setAction() {
65+
super.setAction();
6566
if (onPath) {
6667
//Check if it stops chasing
6768
checkStopChasing(gameManager.player, 10, 100);

src/main/java/com/khomsi/game/main/GameManager.java

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/main/java/com/khomsi/game/main/tools/KeyHandler.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import java.awt.event.KeyEvent;
66
import java.awt.event.KeyListener;
7-
import java.io.File;
87

98
public class KeyHandler implements KeyListener {
109
GameManager gameManager;
@@ -13,10 +12,6 @@ public class KeyHandler implements KeyListener {
1312
//Debug
1413
public boolean debugMode = false;
1514
public boolean godMode = false;
16-
// Path to save.dat file to check the statement
17-
private final String filePath = "save.dat";
18-
19-
public File file = new File(filePath);
2015

2116
public KeyHandler(GameManager gameManager) {
2217
this.gameManager = gameManager;
@@ -156,8 +151,8 @@ private void gameOverState(int code) {
156151
//fixme check the se
157152
gameManager.playMusic(0);
158153
} else if (gameManager.ui.commandNum == 1) {
159-
gameManager.ui.titleScreenState = 0;
160-
gameManager.gameState = GameManager.TITLE_STATE;
154+
gameManager.ui.titleScreenState = -1;
155+
gameManager.gameState = GameManager.START_STATE;
161156
gameManager.resetGame(true);
162157
}
163158
}
@@ -235,6 +230,7 @@ private void playerState(int code) {
235230
gameManager.player.speed += 1;
236231
}
237232
//Debug menu
233+
238234
//TODO disable on production
239235
if (code == KeyEvent.VK_F9) debugMode = !debugMode;
240236
if (code == KeyEvent.VK_F8) godMode = !godMode;
@@ -247,8 +243,9 @@ private void playerState(int code) {
247243
}
248244

249245
private void startState(int code) {
246+
gameManager.saveLoad.loadTitleData();
250247
if (code == KeyEvent.VK_ENTER) {
251-
if (!file.exists()) {
248+
if (!gameManager.saveLoad.hasFile) {
252249
gameManager.ui.titleScreenState = 0;
253250
gameManager.gameState = GameManager.TUTORIAL_STATE;
254251
} else {
@@ -269,6 +266,7 @@ private void tutorialState(int code) {
269266
}
270267
}
271268

269+
272270
private void titleState(int code) {
273271
if (gameManager.ui.titleScreenState == 1) {
274272
if (code == KeyEvent.VK_DOWN || code == KeyEvent.VK_S) {
@@ -302,15 +300,15 @@ private void titleState(int code) {
302300
}
303301

304302
if (!isEnterPressed && code == KeyEvent.VK_ENTER) {
303+
if (gameManager.saveLoad.hasFile) {
304+
gameManager.saveLoad.file.delete();
305+
gameManager.resetTimer();
306+
}
305307
//Set character's stats and skin, depends on chose
306308
switch (gameManager.ui.commandNum) {
307309
case 0 -> gameManager.player.createNewPlayer(0, 0, 3);
308310
case 1 -> gameManager.player.createNewPlayer(1, 0, 4);
309-
case 2 -> {
310-
if (file.exists()) {
311-
gameManager.ui.titleScreenState = 1;
312-
} else System.exit(0);
313-
}
311+
case 2 -> System.exit(0);
314312
}
315313
}
316314
}

src/main/java/com/khomsi/game/main/tools/Sound.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
public class Sound {
9-
Clip clip;
9+
public Clip clip;
1010
private final String resourcePath = "/sounds/";
1111
FloatControl fc;
1212
public int volumeScale = 3;
@@ -15,7 +15,7 @@ public class Sound {
1515

1616
public Sound() {
1717
//TODO make it automatic in future
18-
soundURL[0] = getResource("Main-Theme.wav");
18+
soundURL[0] = getResource("main-theme.wav");
1919
soundURL[1] = getResource("snd_pombark.wav");
2020
soundURL[2] = getResource("mus_sfx_a_grab.wav");
2121
soundURL[3] = getResource("mus_dununnn.wav");
@@ -38,6 +38,7 @@ public Sound() {
3838
soundURL[20] = getResource("Dungeon.wav");
3939
soundURL[21] = getResource("snd_switchpull_n.wav");
4040
soundURL[22] = getResource("boss_music.wav");
41+
soundURL[23] = getResource("fight-theme.wav");
4142
}
4243

4344
private URL getResource(String path) {

0 commit comments

Comments
 (0)