Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
35 chris 1
unit app_game_highscore;
2
 
3
interface
4
 
5
uses sux_constant, sux_object, app_constant;
6
 
7
type
8
  TSScore = record
9
    name: SXStringL;
10
    score: SXInt;
11
  end;
12
  TSettings=record
13
    scores: array[0..SX_MAX_HIGHSCORE] of TSScore;
14
  end;
15
  SAGHighScore=class(SUXObject)
16
    settings:TSettings;
17
 
18
    function getPositionForScore(const score: SXInt): SXInt;
19
    procedure addEntry(const name: SXStringL; const score: SXInt);
20
    procedure reset;
21
    procedure save;
22
    procedure load;
23
 
24
    procedure initialize;
25
    constructor Create(const parent: TObject);
26
    destructor Destroy; override;
27
  end;
28
 
29
implementation
30
 
31
uses main, file_file;
32
 
33
 
34
// --- SAGHighScore
35
 
36
 
37
function SAGHighScore.getPositionForScore(const score: SXInt): SXInt;
38
var s: SXInt;
39
begin
40
  for s := 0 to SX_MAX_HIGHSCORE do
41
  if (score >= settings.scores[s].score) then
42
  begin
43
    result := s;
44
    exit;
45
  end;
46
 
47
  result := SX_NONE;
48
end;
49
 
50
 
51
procedure SAGHighScore.addEntry(const name: SXStringL; const score: SXInt);
52
var s, other: SXInt;
53
begin
54
  s := getPositionForScore(score);
55
  if (s = SX_NONE) then exit;
56
 
57
  // Move all following scores down one position.
58
  for other := SX_MAX_HIGHSCORE - 1 downto s do
59
  begin
60
    settings.scores[other + 1].name := settings.scores[other].name;
61
    settings.scores[other + 1].score := settings.scores[other].score;
62
  end;
63
 
64
  // Add new score at this position
65
  settings.scores[s].name := name;
66
  settings.scores[s].score := score;
67
end;
68
 
69
 
70
procedure SAGHighScore.reset;
71
var s: SXInt;
72
begin
73
  for s := 0 to SX_MAX_HIGHSCORE do
74
  begin
75
    settings.scores[s].name := 'empty';
76
    settings.scores[s].score := 0;
77
  end;
78
 
79
  addEntry('Meister', 9999999);
80
  addEntry('HAL', 1);
81
end;
82
 
83
 
84
procedure SAGHighScore.load;
85
var data: SUXFile;
86
    fullfilename: SXStringL;
87
    s: SXInt;
88
begin
89
  fullfilename := sx.fs.getFullFileName('data\highscore', 'data');
90
 
91
  // Generate a new highscore table if none exists so far.
92
  if (not sx.fs.exists(fullfilename, false)) then
93
  begin
94
    reset;
95
    save;
96
  end;
97
 
98
  data := SUXFile.Create(self, fullfilename, SX_FILE_BINARY, SX_FILE_ACCESS_READ);
99
  for s := 0 to SX_MAX_HIGHSCORE do
100
  begin
101
    data.readStringL(settings.scores[s].name);
102
    data.readInt(settings.scores[s].score);
103
  end;
104
  data.Free;
105
end;
106
 
107
 
108
procedure SAGHighScore.save;
109
var data: SUXFile;
110
    fullfilename: SXStringL;
111
    s: SXInt;
112
begin
113
  fullfilename := sx.fs.getFullFileName('data\highscore', 'data');
114
 
115
  data := SUXFile.Create(self, fullfilename, SX_FILE_BINARY, SX_FILE_ACCESS_WRITE);
116
  for s := 0 to SX_MAX_HIGHSCORE do
117
  begin
118
    data.writeStringL(settings.scores[s].name);
119
    data.writeInt(settings.scores[s].score);
120
  end;
121
  data.Free;
122
end;
123
 
124
 
125
 
126
 
127
procedure SAGHighScore.initialize;
128
begin
129
  load;
130
end;
131
 
132
 
133
constructor SAGHighScore.Create(const parent: TObject);
134
begin
135
  inherited Create(parent,'Game Highscore');
136
end;
137
 
138
 
139
destructor SAGHighScore.Destroy;
140
begin
141
  save;
142
 
143
  inherited;
144
end;
145
 
146
 
147
end.