-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScore.java
More file actions
157 lines (138 loc) · 3.57 KB
/
Score.java
File metadata and controls
157 lines (138 loc) · 3.57 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import java.awt.Font;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.io.File;
/**
* This Class Handles everything to do with the score,
* it keeps track of it while you play,
* it saves the high scores as you go along,
* it displays the high scores in the high score screen
* @author elan
*
*/
public class Score
{
/**
* Some static class variables
*/
private static int score;
private static int interumScore;
private static Integer[] highscores;
/**
* increases the score based on a number of factors,
* including current health level of the player and
* distance of the enemy away from the bottom of the screen,
* as well as type of enemy and the difficulty index for the level
*/
public static void increaseScore(Shooter player, Enemy enemy)
{
if(enemy instanceof Boss)
interumScore += ((enemy.getY()/(double)States.WINDOW_RESOLUTION) * 200) + player.getHP() + Level.difficultyIndex()*400;
else if(enemy instanceof LockOnEnemy)
interumScore += ((enemy.getY()/(double)States.WINDOW_RESOLUTION) * 200) + player.getHP() + Level.difficultyIndex()*10;
else
interumScore += ((enemy.getY()/(double)States.WINDOW_RESOLUTION) * 200) + player.getHP() + Level.difficultyIndex();
}
/**
* if the player has passed the level successfully
* the score is taken into permanent affect,
* otherwise it is reset for the level
*/
public static void consolidateScore()
{
score += interumScore;
resetInterumScore();
if(score > highscores[0])
{
highscores[0] = score;
updateHighScores();
}
}
/**
* @return the current score
*/
public static int getScore()
{
return interumScore;
}
/**
* Draws the score on the screen
*/
public static void render()
{
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.setFont(new Font(Font.SANS_SERIF, Font.BOLD, (int) (States.WINDOW_RESOLUTION*0.04)));
StdDraw.text(States.WINDOW_CENTRE, States.WINDOW_RESOLUTION - States.WINDOW_RESOLUTION/(double)30, "Score: " + (score + interumScore));
}
/**
* resets the score
*/
public static void resetScore()
{
score = 0;
}
/**
* resets the temparary score for the level
*/
public static void resetInterumScore()
{
interumScore = 0;
}
/**
* displays the high scores on the screen
*/
public static void displayHighScores()
{
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.setFont(new Font(Font.SANS_SERIF, Font.BOLD, (int) (States.WINDOW_RESOLUTION*0.04)));
for(int i = 0; i < 10; i++)
StdDraw.text(States.WINDOW_CENTRE, States.WINDOW_RESOLUTION - i*(States.WINDOW_RESOLUTION/(double)15) - States.WINDOW_RESOLUTION/(double)6, "" + (i + 1) + ". " + highscores[9 - i]);
}
/**
* loads the high scores from the file
* (or makes it if it doesnt exist)
*/
public static void loadHighScores()
{
highscores = new Integer[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int counter = 0;
try
{
Scanner read = new Scanner(new File("highscores.txt"));
while(read.hasNext() && counter < 10)
highscores[counter++] = read.nextInt();
read.close();
sort();
}
catch (FileNotFoundException e)
{
updateHighScores();
}
}
/**
* writes the new highscores to the file highscores.txt
*/
public static void updateHighScores()
{
sort();
try
{
PrintWriter write = new PrintWriter("highscores.txt");
for(int i = 0; i < 10; i++)
write.println(highscores[i]);
write.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
/**
* sorts the high scores
*/
public static void sort()
{
Quick.sort(highscores);
}
}