-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameManager.java
More file actions
81 lines (63 loc) · 3 KB
/
GameManager.java
File metadata and controls
81 lines (63 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import greenfoot.*; // import every greefoot function, object and more
import java.util.*; // import java utils
import java.text.*; // import java text utils
public class GameManager extends Manager
{
private static boolean isGameStarted = false; // use to check internal if the game has started
private static boolean isGameOver = false; // use to check internal if the game is over (=> player died)
private static GreenfootSound gameMusic = new GreenfootSound("./music/gameMusic.mp3"); // the background music
private static ArrayList<String> lastPlayedTimes = new ArrayList<>(); // a list of the last 5 game times in ms
private static long gameStartedAt; // time in ms when game started
public static void start() {
NinjaEnemyManager.insert(); // add all ninajs to the ninaj enemy array
isGameOver = false;
isGameStarted = true;
gameStartedAt = System.currentTimeMillis();
MapManager.insertMap();
gameMusic.playLoop();
}
public static void stop() {
isGameStarted = false;
isGameOver = true;
// ensures that only 3 entries are in the array
if (lastPlayedTimes.size() == 3) {
lastPlayedTimes.remove(3);
}
lastPlayedTimes.add(getConvertedPlayTime());
NinjaEnemyManager.clear(); // clear the ninja enemy array
MapManager.insertMap();
gameMusic.stop();
}
public static ArrayList<String> getLastPlayedTimes() {
return lastPlayedTimes;
}
public static boolean isGameStarted() {
return isGameStarted;
}
public static boolean isGameOver() {
return isGameOver;
}
public static long getStartedAt() {
return gameStartedAt;
}
public static long getCurrentPlayTime() {
return System.currentTimeMillis();
}
public static String getConvertedPlayTime()
{
// calculates the elapsed game time in milliseconds by comparing the current time with the time the game started at
int gameDuration = (int) (System.currentTimeMillis() - gameStartedAt);
int hours = gameDuration / 3600000; // calculate the hours from the game time (1 hour = 3600000 milliseconds)
gameDuration -= hours * 3600000; // remove the milliseconds that have already been taken into account for the hours
int minutes = gameDuration / 60000; // calculate the minutes from the game time (1 minute = 60000 milliseconds)
gameDuration -= minutes * 60000; // remove the milliseconds that have already been taken into account for the minutes
int seconds = gameDuration / 1000; // calculate the seconds from the game time (1 second = 1000 milliseconds)
// format the game time
return new DecimalFormat("00").format(hours)
+ ":"
+ new DecimalFormat("00").format(minutes)
+ ":"
+ new DecimalFormat("00").format(seconds)
+ "h";
}
}