-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHighscore.hx
129 lines (102 loc) · 2.64 KB
/
Highscore.hx
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
package;
/**
* ...
* @author Matthias Faust
*/
class Highscore {
var SPR_NONE:Sprite;
var scores:Array<HighscoreEntry>;
public function new() {
SPR_NONE = Gfx.getSprite(0, 0);
scores = [];
}
public function draw(x:Int, y:Int) {
var index:Int = 0;
var scoreString:String;
Gfx.drawTexture(x, y, 512, 100, SPR_NONE.uv, Color.WHITE);
for (s in scores) {
scoreString = Std.string(index + 1).lpad(" ", 2);
scoreString += " " + s.name.left(15).lpad(" ", 15);
scoreString += ": " + StringTools.lpad(Std.string(s.points), " ", 7);
scoreString += " (" + StringTools.lpad(Std.string(s.rooms), " ", 2) + ")";
Tobor.fontBig.drawString(x, y + index * 10, scoreString, Color.BLACK, Color.WHITE);
index++;
}
}
public function sort() {
scores.sort(function(a:HighscoreEntry, b:HighscoreEntry) {
if (a.points == b.points) {
if (a.rooms < b.rooms) {
return 1;
} else if (a.rooms > b.rooms) {
return -1;
} else {
return 0;
}
} else if (a.points < b.points) {
return 1;
} else if (a.points > b.points) {
return -1;
} else {
return 0;
}
});
while (scores.length > 10) {
scores.pop();
}
}
public function add(name:String, points:Int, rooms:Int) {
if (points <= 0) return;
scores.push(new HighscoreEntry(name, points, rooms));
sort();
}
public function save():String {
var data:Array<Dynamic> = [];
for (s in scores) {
data.push(s.save());
}
return haxe.Json.stringify(data);
}
public function load(fileData:String) {
scores = [];
if (fileData == "" || fileData == null) {
init();
return;
}
var data:Array<Dynamic> = haxe.Json.parse(fileData);
for (entry in data) {
var _name:String = Reflect.field(entry, "name");
var _points:Int = Reflect.field(entry, "points");
var _rooms:Int = Reflect.field(entry, "rooms");
add(_name, _points, _rooms);
}
sort();
}
public function init() {
add("Unglaublicher", 400000, 30);
add("Supermeister", 250000, 18);
add("Der Meister", 100000, 11);
add("Der Aufsteiger", 25000, 8);
add("Der Anfänger", 10000, 4);
}
}
class HighscoreEntry {
public var name:String = "";
public var points:Int = 0;
public var rooms:Int = 0;
public function new(name:String, points:Int, rooms:Int) {
set(name, points, rooms);
}
public function set(name:String, points:Int, rooms:Int) {
this.name = name;
this.points = points;
this.rooms = rooms;
}
public function save():Map<String, Dynamic> {
var data:Map<String, Dynamic> = new Map<String, Dynamic>();
data.set("name", name);
data.set("points", points);
data.set("rooms", rooms);
return data;
}
}