-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
uGameData.pas
149 lines (131 loc) · 3.7 KB
/
uGameData.pas
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
unit uGameData;
interface
var
GameScore, GameLevel, GameLevelMax: cardinal;
function isGamePaused: boolean;
function isGameStarted: boolean;
procedure PauseGame;
procedure GameStart;
procedure GameResume;
procedure GameOver;
implementation
uses
system.ioutils, uCommonTools, fSceneGame, uCharacterSprite;
var
GameStarted: boolean;
function getGameFilePath: string;
begin
result := tpath.Combine(getFolderName, 'game.dat');
end;
procedure save;
var
f: file;
NbCharactersToCreate: integer;
i, j: integer;
SceneGame: TfrmSceneGame;
begin
NbCharactersToCreate := 0;
SceneGame := TfrmSceneGame.getsceneref as TfrmSceneGame;
for i := 0 to length(SceneGame.CharacterLayers) - 1 do
for j := SceneGame.CharacterLayers[i].ChildrenCount - 1 downto 0 do
if (SceneGame.CharacterLayers[i].Children[j] is tcharactersprite) then
inc(NbCharactersToCreate);
assignfile(f, getGameFilePath);
{$I-}
rewrite(f, 1);
{$I+}
blockwrite(f, GameScore, sizeof(GameScore));
blockwrite(f, GameLevel, sizeof(GameLevel));
blockwrite(f, GameLevelMax, sizeof(GameLevelMax));
blockwrite(f, NbCharactersToCreate, sizeof(NbCharactersToCreate));
closefile(f);
end;
procedure load;
var
f: file;
NbCharactersToCreate: integer;
i: integer;
SceneGame: TfrmSceneGame;
begin
GameScore := 0;
GameLevel := 1;
GameLevelMax := 1;
NbCharactersToCreate := 0;
if tfile.Exists(getGameFilePath) then
begin
assignfile(f, getGameFilePath);
{$I-}
reset(f, 1);
{$I+}
if not eof(f) then
blockread(f, GameScore, sizeof(GameScore));
if not eof(f) then
blockread(f, GameLevel, sizeof(GameLevel));
if not eof(f) then
blockread(f, GameLevelMax, sizeof(GameLevelMax));
if not eof(f) then
blockread(f, NbCharactersToCreate, sizeof(NbCharactersToCreate));
closefile(f);
end;
SceneGame := TfrmSceneGame.getsceneref as TfrmSceneGame;
for i := 0 to NbCharactersToCreate do
tcharactersprite.initSprite(SceneGame, SceneGame.onBernieClick,
SceneGame.onNotBernieClick, SceneGame.onMissedBernieClick);
end;
function isGamePaused: boolean;
begin
result := tfile.Exists(getGameFilePath);
end;
function isGameStarted: boolean;
begin
result := GameStarted;
end;
procedure PauseGame;
begin
if GameStarted then
begin
GameStarted := false;
save;
end;
end;
procedure GameStart;
var
SceneGame: TfrmSceneGame;
i, j: integer;
begin
GameStarted := true;
SceneGame := TfrmSceneGame.getsceneref as TfrmSceneGame;
for i := 0 to length(SceneGame.CharacterLayers) - 1 do
for j := SceneGame.CharacterLayers[i].ChildrenCount - 1 downto 0 do
if (SceneGame.CharacterLayers[i].Children[j] is tcharactersprite) then
(SceneGame.CharacterLayers[i].Children[j] as tcharactersprite).Free;
GameScore := 0;
GameLevel := 1;
GameLevelMax := 1;
SceneGame.SceneGameInitAndStart;
TfrmSceneGame.SceneShow<TfrmSceneGame>;
end;
procedure GameResume;
var
SceneGame: TfrmSceneGame;
i, j: integer;
begin
GameStarted := true;
SceneGame := TfrmSceneGame.getsceneref as TfrmSceneGame;
for i := 0 to length(SceneGame.CharacterLayers) - 1 do
for j := SceneGame.CharacterLayers[i].ChildrenCount - 1 downto 0 do
if (SceneGame.CharacterLayers[i].Children[j] is tcharactersprite) then
(SceneGame.CharacterLayers[i].Children[j] as tcharactersprite).Free;
load;
SceneGame.SceneGameInitAndStart;
TfrmSceneGame.SceneShow<TfrmSceneGame>;
end;
procedure GameOver;
begin
GameStarted := false;
if tfile.Exists(getGameFilePath) then
tfile.Delete(getGameFilePath);
end;
initialization
GameStarted := false;
end.