Rev 26 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | daniel-mar | 1 | unit Main; |
2 | |||
3 | interface |
||
4 | |||
5 | uses |
||
6 | Windows, SysUtils, Classes, Graphics, Dialogs, StdCtrls, Menus, Controls, |
||
7 | ComCtrls, ExtCtrls, Forms, MMSystem, LevelFunctions, Registry; |
||
8 | |||
9 | type |
||
10 | TMainForm = class(TForm) |
||
11 | Playground: TPanel; |
||
12 | MainMenu: TMainMenu; |
||
13 | Help1: TMenuItem; |
||
14 | MExit: TMenuItem; |
||
15 | Statistics: TStatusBar; |
||
16 | Timer: TTimer; |
||
17 | MNewGame: TMenuItem; |
||
18 | Help2: TMenuItem; |
||
19 | MAbout: TMenuItem; |
||
20 | MHelp: TMenuItem; |
||
21 | N5: TMenuItem; |
||
22 | MJumpHistory: TMenuItem; |
||
23 | N2: TMenuItem; |
||
24 | N4: TMenuItem; |
||
25 | MHighScores: TMenuItem; |
||
26 | MRestartGame: TMenuItem; |
||
27 | MSettings: TMenuItem; |
||
28 | MEnableSound: TMenuItem; |
||
29 | MPauseTime: TMenuItem; |
||
30 | N1: TMenuItem; |
||
31 | MUndo: TMenuItem; |
||
32 | N3: TMenuItem; |
||
14 | daniel-mar | 33 | Aboutthislevel1: TMenuItem; |
1 | daniel-mar | 34 | procedure MExitClick(Sender: TObject); |
35 | procedure TimerTimer(Sender: TObject); |
||
36 | procedure MNewGameClick(Sender: TObject); |
||
37 | procedure MAboutClick(Sender: TObject); |
||
38 | procedure FormShow(Sender: TObject); |
||
39 | procedure FormCreate(Sender: TObject); |
||
40 | procedure FormDestroy(Sender: TObject); |
||
41 | procedure MJumpHistoryClick(Sender: TObject); |
||
42 | procedure MRestartGameClick(Sender: TObject); |
||
43 | procedure MHighScoresClick(Sender: TObject); |
||
44 | procedure FormClose(Sender: TObject; var Action: TCloseAction); |
||
45 | procedure MHelpClick(Sender: TObject); |
||
6 | daniel-mar | 46 | procedure MUndoClick(Sender: TObject); |
14 | daniel-mar | 47 | procedure Aboutthislevel1Click(Sender: TObject); |
48 | procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean); |
||
1 | daniel-mar | 49 | private |
18 | daniel-mar | 50 | NoCloseQuery: boolean; |
1 | daniel-mar | 51 | CountedSeconds: Integer; |
52 | LevelFile: String; |
||
6 | daniel-mar | 53 | PrevPlaygroundMatrixes: array of TPlayGroundMatrix; |
1 | daniel-mar | 54 | PlaygroundMatrix: TPlayGroundMatrix; |
55 | Points: Integer; |
||
56 | LevelTotalStones: Integer; |
||
57 | LevelRemovedStones: Integer; |
||
58 | JumpHistory: TStringList; |
||
11 | daniel-mar | 59 | Level: TLevel; |
1 | daniel-mar | 60 | procedure LoadSettings; |
61 | procedure SaveSettings; |
||
62 | procedure RestartLevel; |
||
63 | procedure SetNewPlayGroundMatrix(Matrix: TPlayGroundMatrix); |
||
64 | procedure RedrawStonesFromMatrix(Matrix: TPlayGroundMatrix); |
||
65 | function AskForLevel: String; |
||
66 | procedure StoneDraggingAllow(Stone: TImage; Allow: boolean); |
||
67 | procedure NewGame(Filename: string); |
||
68 | function LevelTime: String; |
||
69 | procedure DestroyLevel; |
||
70 | procedure RefreshTime; |
||
71 | procedure RefreshPoints; |
||
72 | procedure RefreshStonesRemoved; |
||
73 | procedure RemoveStone(x, y: integer; count_points: boolean); |
||
74 | procedure DoJump(SourceTag, DestTag: integer); |
||
75 | function MayJump(SourceTag, DestTag: integer): boolean; overload; |
||
19 | daniel-mar | 76 | procedure StoneDragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean); |
1 | daniel-mar | 77 | procedure StoneDragDrop(Sender, Source: TObject; X, Y: Integer); |
24 | daniel-mar | 78 | procedure DrawField(x, y: integer; var f: TField); |
26 | daniel-mar | 79 | function DrawStone(f: TField): TImage; |
80 | function DrawStoneBox(x, y, tag: integer; f: TField): TPanel; |
||
1 | daniel-mar | 81 | procedure LoadPictureForType(FieldType: TFieldType; Picture: TPicture); |
8 | daniel-mar | 82 | function GoalStatus: TGoalStatus; |
1 | daniel-mar | 83 | end; |
84 | |||
85 | var |
||
86 | MainForm: TMainForm; |
||
87 | |||
88 | implementation |
||
89 | |||
90 | uses |
||
19 | daniel-mar | 91 | About, Finish, Choice, Functions, History, HighScore, Help, Constants, Math; |
1 | daniel-mar | 92 | |
93 | {$R *.dfm} |
||
94 | |||
26 | daniel-mar | 95 | type |
96 | TFieldVclData = class(TObject) |
||
97 | public |
||
98 | Panel: TPanel; |
||
99 | Stone: TImage; |
||
100 | destructor Destroy; override; |
||
101 | end; |
||
102 | |||
21 | daniel-mar | 103 | { TMainForm } |
8 | daniel-mar | 104 | |
1 | daniel-mar | 105 | procedure TMainForm.RedrawStonesFromMatrix(Matrix: TPlayGroundMatrix); |
106 | var |
||
23 | daniel-mar | 107 | x, y: integer; |
1 | daniel-mar | 108 | begin |
23 | daniel-mar | 109 | for x := Low(Matrix.Fields) to High(Matrix.Fields) do |
1 | daniel-mar | 110 | begin |
23 | daniel-mar | 111 | for y := Low(Matrix.Fields[x]) to High(Matrix.Fields[x]) do |
1 | daniel-mar | 112 | begin |
26 | daniel-mar | 113 | if Assigned(Matrix.Fields[x,y].Data) and |
114 | Assigned(TFieldVclData(Matrix.Fields[x,y].Data).Stone) then |
||
1 | daniel-mar | 115 | begin |
26 | daniel-mar | 116 | LoadPictureForType(Matrix.Fields[x,y].FieldType, |
117 | TFieldVclData(Matrix.Fields[x,y].Data).Stone.Picture); |
||
118 | StoneDraggingAllow(TFieldVclData(Matrix.Fields[x,y].Data).Stone, |
||
119 | Matrix.Fields[x,y].FieldState <> fsAvailable); |
||
1 | daniel-mar | 120 | end; |
121 | end; |
||
122 | end; |
||
123 | end; |
||
124 | |||
125 | procedure TMainForm.DestroyLevel; |
||
6 | daniel-mar | 126 | var |
127 | i: Integer; |
||
1 | daniel-mar | 128 | begin |
21 | daniel-mar | 129 | MPauseTime.Checked := false; |
1 | daniel-mar | 130 | MPauseTime.Enabled := false; |
131 | Timer.Enabled := false; |
||
132 | |||
133 | MRestartGame.Enabled := false; |
||
134 | |||
135 | LevelFile := ''; |
||
136 | |||
137 | CountedSeconds := 0; |
||
138 | RefreshTime; |
||
139 | |||
140 | Points := 0; |
||
141 | RefreshPoints; |
||
142 | |||
143 | LevelRemovedStones := 0; |
||
144 | LevelTotalStones := 0; |
||
145 | RefreshStonesRemoved; |
||
146 | |||
147 | JumpHistory.Clear; |
||
148 | |||
21 | daniel-mar | 149 | PlayGroundMatrix.ClearMatrix(true); |
6 | daniel-mar | 150 | for i := 0 to Length(PrevPlaygroundMatrixes)-1 do |
21 | daniel-mar | 151 | PrevPlaygroundMatrixes[i].ClearMatrix(false); |
6 | daniel-mar | 152 | SetLength(PrevPlaygroundMatrixes, 0); |
153 | MUndo.Enabled := false; |
||
1 | daniel-mar | 154 | |
11 | daniel-mar | 155 | if Assigned(Level) then FreeAndNil(Level); |
1 | daniel-mar | 156 | end; |
157 | |||
158 | procedure TMainForm.LoadPictureForType(FieldType: TFieldType; Picture: TPicture); |
||
159 | begin |
||
160 | case FieldType of |
||
161 | ftEmpty: Picture.Bitmap.LoadFromResourceName(HInstance, RES_EMPTY); |
||
162 | ftGreen: Picture.Bitmap.LoadFromResourceName(HInstance, RES_GREEN); |
||
163 | ftYellow: Picture.Bitmap.LoadFromResourceName(HInstance, RES_YELLOW); |
||
164 | ftRed: Picture.Bitmap.LoadFromResourceName(HInstance, RES_RED); |
||
165 | end; |
||
166 | end; |
||
167 | |||
26 | daniel-mar | 168 | function TMainForm.DrawStone(f: TField): TImage; |
169 | var |
||
170 | panel: TPanel; |
||
1 | daniel-mar | 171 | begin |
26 | daniel-mar | 172 | panel := TFieldVclData(f.Data).Panel; |
173 | |||
1 | daniel-mar | 174 | result := TImage.Create(panel); |
175 | result.Parent := panel; |
||
26 | daniel-mar | 176 | LoadPictureForType(f.FieldType, result.Picture); |
1 | daniel-mar | 177 | result.Width := panel.Width - 2*MET_SHAPE_MARGIN; |
178 | result.Height := panel.Height - 2*MET_SHAPE_MARGIN; |
||
179 | result.Left := MET_SHAPE_MARGIN; |
||
180 | result.Top := MET_SHAPE_MARGIN; |
||
181 | result.Center := true; |
||
182 | result.Transparent := true; |
||
183 | |||
184 | result.Tag := panel.Tag; |
||
185 | result.OnDragOver := panel.OnDragOver; |
||
186 | result.OnDragDrop := panel.OnDragDrop; |
||
187 | |||
26 | daniel-mar | 188 | StoneDraggingAllow(result, f.FieldState <> fsAvailable); |
1 | daniel-mar | 189 | end; |
190 | |||
191 | procedure TMainForm.StoneDraggingAllow(Stone: TImage; Allow: boolean); |
||
192 | begin |
||
193 | if Allow then |
||
194 | begin |
||
195 | Stone.DragMode := dmAutomatic; |
||
196 | (Stone.Parent as TPanel).DragMode := dmAutomatic; |
||
197 | end |
||
198 | else |
||
199 | begin |
||
200 | Stone.DragMode := dmManual; |
||
201 | (Stone.Parent as TPanel).DragMode := dmManual; |
||
202 | end; |
||
203 | end; |
||
204 | |||
26 | daniel-mar | 205 | function TMainForm.DrawStoneBox(x, y, tag: integer; f: TField): TPanel; |
1 | daniel-mar | 206 | begin |
207 | result := TPanel.Create(Playground); |
||
208 | result.Parent := Playground; |
||
26 | daniel-mar | 209 | if f.Goal then |
1 | daniel-mar | 210 | begin |
211 | result.BevelInner := bvLowered; |
||
212 | end; |
||
213 | result.Color := Playground.Color; |
||
214 | result.BevelOuter := bvLowered; |
||
215 | result.Width := MET_FIELD_SIZE; |
||
216 | result.Height := MET_FIELD_SIZE; |
||
26 | daniel-mar | 217 | result.Left := x * (MET_FIELD_SIZE+MET_FIELD_SPACE) + MET_FIELD_SPACE + (f.Indent*MET_HALFTAB_SIZE); |
1 | daniel-mar | 218 | result.Top := y * (MET_FIELD_SIZE+MET_FIELD_SPACE) + MET_FIELD_SPACE; |
219 | |||
220 | result.Tag := tag; |
||
221 | result.OnDragOver := StoneDragOver; |
||
222 | result.OnDragDrop := StoneDragDrop; |
||
223 | end; |
||
224 | |||
225 | procedure TMainForm.MExitClick(Sender: TObject); |
||
226 | begin |
||
227 | Close; |
||
228 | end; |
||
229 | |||
230 | procedure TMainForm.RefreshTime; |
||
231 | begin |
||
232 | Statistics.Panels.Items[0].Text := Format(LNG_TIME, [LevelTime]); |
||
233 | end; |
||
234 | |||
235 | procedure TMainForm.RefreshStonesRemoved; |
||
21 | daniel-mar | 236 | resourcestring |
25 | daniel-mar | 237 | LNG_STONES_REMOVED = '%d of %d stones removed'; // Jumping stone not counted |
1 | daniel-mar | 238 | begin |
12 | daniel-mar | 239 | Statistics.Panels.Items[1].Text := Format(LNG_STONES_REMOVED, [LevelRemovedStones, LevelTotalStones-1]); |
1 | daniel-mar | 240 | end; |
241 | |||
242 | procedure TMainForm.RefreshPoints; |
||
243 | begin |
||
244 | Statistics.Panels.Items[2].Text := Format(LNG_POINTS, [Points]); |
||
245 | end; |
||
246 | |||
247 | procedure TMainForm.RemoveStone(x, y: integer; count_points: boolean); |
||
248 | begin |
||
249 | if count_points then |
||
250 | begin |
||
24 | daniel-mar | 251 | Inc(Points, FieldTypeWorth(PlayGroundMatrix.Fields[x,y].FieldType)); |
22 | daniel-mar | 252 | RefreshPoints; |
253 | |||
1 | daniel-mar | 254 | Inc(LevelRemovedStones); |
255 | RefreshStonesRemoved; |
||
256 | end; |
||
23 | daniel-mar | 257 | |
24 | daniel-mar | 258 | PlayGroundMatrix.Fields[x,y].FieldType := ftEmpty; |
26 | daniel-mar | 259 | LoadPictureForType(PlayGroundMatrix.Fields[x,y].FieldType, |
260 | TFieldVclData(PlayGroundMatrix.Fields[x,y].Data).Stone.Picture); |
||
261 | StoneDraggingAllow(TFieldVclData(PlayGroundMatrix.Fields[x,y].Data).Stone, false); |
||
1 | daniel-mar | 262 | end; |
263 | |||
14 | daniel-mar | 264 | procedure TMainForm.Aboutthislevel1Click(Sender: TObject); |
265 | var |
||
266 | mode: string; |
||
267 | goalYeSNo: string; |
||
268 | resourcestring |
||
269 | LNG_BOARD = 'Board: %s'; |
||
270 | LNG_MODE = 'Mode: %s'; |
||
271 | LNG_STONES_TOTAL = 'Stones: %d'; |
||
272 | LNG_GOAL_AVAILABLE = 'Target field defined'; |
||
273 | LNG_NO_GOAL = 'No target field'; |
||
274 | begin |
||
22 | daniel-mar | 275 | case Level.GameMode of |
276 | gmNormal: mode := 'Diagonal'; |
||
277 | gmDiagonal: mode := 'Normal'; |
||
278 | gmUndefined: mode := '?'; |
||
279 | end; |
||
14 | daniel-mar | 280 | |
281 | if GoalStatus = gsNoGoal then |
||
282 | goalYeSNo := LNG_NO_GOAL |
||
283 | else |
||
284 | goalYeSNo := LNG_GOAL_AVAILABLE; |
||
285 | |||
286 | ShowMessage(Format(LNG_BOARD, [ExtractFileNameWithoutExt(LevelFile)]) + #13#10 + |
||
287 | #13#10 + |
||
288 | Format(LNG_MODE, [mode]) + #13#10 + |
||
289 | Format(LNG_STONES_TOTAL, [LevelTotalStones]) + #13#10 + |
||
290 | goalYesNo); |
||
291 | end; |
||
292 | |||
1 | daniel-mar | 293 | procedure TMainForm.DoJump(SourceTag, DestTag: integer); |
21 | daniel-mar | 294 | resourcestring |
24 | daniel-mar | 295 | LNG_JUMP_LOG = '[%d, %d] -> [%d, %d];'; |
1 | daniel-mar | 296 | var |
23 | daniel-mar | 297 | d, s: TCoord; |
1 | daniel-mar | 298 | old_fieldtype: TFieldType; |
299 | res: Integer; |
||
300 | begin |
||
301 | if not MayJump(SourceTag, DestTag) then exit; |
||
302 | |||
23 | daniel-mar | 303 | s := PlaygroundMatrix.IndexToCoord(SourceTag); |
304 | d := PlaygroundMatrix.IndexToCoord(DestTag); |
||
1 | daniel-mar | 305 | |
24 | daniel-mar | 306 | JumpHistory.Add(Format(LNG_JUMP_LOG, [s.x+1, s.y+1, d.x+1, d.y+1])); |
1 | daniel-mar | 307 | |
6 | daniel-mar | 308 | {$REGION 'Stein entfernen und Punkte vergeben'} |
22 | daniel-mar | 309 | if Level.GameMode = gmDiagonal then |
1 | daniel-mar | 310 | begin |
24 | daniel-mar | 311 | if (s.X-2 = d.X) and (s.Y-2 = d.Y) and (PlayGroundMatrix.FieldState(s.X-1, s.Y-1) = fsOccupied) then RemoveStone(s.X-1, s.Y-1, true); |
312 | if (s.X-2 = d.X) and (s.Y+2 = d.Y) and (PlayGroundMatrix.FieldState(s.X-1, s.Y+1) = fsOccupied) then RemoveStone(s.X-1, s.Y+1, true); |
||
313 | if (s.X+2 = d.X) and (s.Y-2 = d.Y) and (PlayGroundMatrix.FieldState(s.X+1, s.Y-1) = fsOccupied) then RemoveStone(s.X+1, s.Y-1, true); |
||
314 | if (s.X+2 = d.X) and (s.Y+2 = d.Y) and (PlayGroundMatrix.FieldState(s.X+1, s.Y+1) = fsOccupied) then RemoveStone(s.X+1, s.Y+1, true); |
||
1 | daniel-mar | 315 | end; |
316 | |||
24 | daniel-mar | 317 | if (s.X+2 = d.X) and (s.Y = d.Y) and (PlayGroundMatrix.FieldState(s.X+1, s.Y ) = fsOccupied) then RemoveStone(s.X+1, s.Y, true); |
318 | if (s.X-2 = d.X) and (s.Y = d.Y) and (PlayGroundMatrix.FieldState(s.X-1, s.Y ) = fsOccupied) then RemoveStone(s.X-1, s.Y, true); |
||
319 | if (s.X = d.X) and (s.Y+2 = d.Y) and (PlayGroundMatrix.FieldState(s.X , s.Y+1) = fsOccupied) then RemoveStone(s.X, s.Y+1, true); |
||
320 | if (s.X = d.X) and (s.Y-2 = d.Y) and (PlayGroundMatrix.FieldState(s.X , s.Y-1) = fsOccupied) then RemoveStone(s.X, s.Y-1, true); |
||
6 | daniel-mar | 321 | {$ENDREGION} |
1 | daniel-mar | 322 | |
323 | // Den Timer erst nach dem ersten Zug starten |
||
324 | // oder nach einer Pause neustarten |
||
21 | daniel-mar | 325 | MPauseTime.Checked := false; |
326 | MPauseTime.Enabled := true; |
||
327 | Timer.Enabled := true; |
||
1 | daniel-mar | 328 | |
329 | // Sound abspielen |
||
330 | if MEnableSound.Checked then PlaySound(RES_JUMP, HInstance, SND_ASYNC or SND_NOWAIT or SND_RESOURCE); |
||
331 | |||
6 | daniel-mar | 332 | {$REGION 'Nun den Stein springen lassen'} |
21 | daniel-mar | 333 | old_fieldtype := PlayGroundMatrix.Fields[s.X, s.Y].FieldType; // Steinfarbe merken |
6 | daniel-mar | 334 | RemoveStone(s.X, s.Y, false); // Eigenen Stein entfernen. Keine Punkte zählen, da das unser eigener Stein ist, der springt |
21 | daniel-mar | 335 | PlayGroundMatrix.Fields[d.X, d.Y].FieldType := old_fieldtype; // Farbe wiederherstellen |
26 | daniel-mar | 336 | LoadPictureForType(PlayGroundMatrix.Fields[d.X, d.Y].FieldType, |
337 | TFieldVclData(PlayGroundMatrix.Fields[d.X, d.Y].Data).Stone.Picture); // Stein an neue Position malen |
||
338 | StoneDraggingAllow(TFieldVclData(PlayGroundMatrix.Fields[d.X, d.Y].Data).Stone, true); // Und die Drag-Eigenschaft erneuern |
||
6 | daniel-mar | 339 | {$ENDREGION} |
1 | daniel-mar | 340 | |
6 | daniel-mar | 341 | {$REGION 'Sind weitere Sprünge möglich oder ist das Spiel vorbei?'} |
22 | daniel-mar | 342 | if not PlayGroundMatrix.CanJump(Level.GameMode = gmDiagonal) then |
1 | daniel-mar | 343 | begin |
21 | daniel-mar | 344 | MPauseTime.Checked := false; |
1 | daniel-mar | 345 | MPauseTime.Enabled := false; |
346 | Timer.Enabled := false; |
||
347 | RefreshTime; |
||
4 | daniel-mar | 348 | if MEnableSound.Checked then |
349 | begin |
||
8 | daniel-mar | 350 | if LevelRemovedStones = LevelTotalStones-1 then |
351 | begin |
||
352 | if GoalStatus in [gsLastStoneInGoalRed, gsLastStoneInGoalYellow, gsLastStoneInGoalGreen] then |
||
353 | PlaySound(RES_WIN2, HInstance, SND_ASYNC or SND_NOWAIT or SND_RESOURCE) |
||
354 | else |
||
355 | PlaySound(RES_WIN1, HInstance, SND_ASYNC or SND_NOWAIT or SND_RESOURCE) |
||
356 | end |
||
4 | daniel-mar | 357 | else |
358 | PlaySound(RES_LOSE, HInstance, SND_ASYNC or SND_NOWAIT or SND_RESOURCE); |
||
359 | end; |
||
8 | daniel-mar | 360 | res := FinishForm.Execute(ExtractFileNameWithoutExt(LevelFile), Points, LevelTotalStones, LevelRemovedStones, CountedSeconds, GoalStatus, JumpHistory); |
1 | daniel-mar | 361 | if (res = mrOK) and FinishForm.ReplayCheckbox.Checked then RestartLevel; |
362 | end; |
||
6 | daniel-mar | 363 | {$ENDREGION} |
364 | |||
365 | SetLength(PrevPlaygroundMatrixes, Length(PrevPlaygroundMatrixes)+1); |
||
21 | daniel-mar | 366 | PrevPlaygroundMatrixes[Length(PrevPlaygroundMatrixes)-1] := PlaygroundMatrix.CloneMatrix; |
6 | daniel-mar | 367 | MUndo.Enabled := true; |
1 | daniel-mar | 368 | end; |
369 | |||
370 | function TMainForm.MayJump(SourceTag, DestTag: integer): boolean; |
||
371 | var |
||
23 | daniel-mar | 372 | s, d: TCoord; |
1 | daniel-mar | 373 | begin |
23 | daniel-mar | 374 | s := PlayGroundMatrix.IndexToCoord(SourceTag); |
375 | d := PlayGroundMatrix.IndexToCoord(DestTag); |
||
1 | daniel-mar | 376 | |
24 | daniel-mar | 377 | result := PlaygroundMatrix.CanJump(s, d, Level.GameMode = gmDiagonal); |
1 | daniel-mar | 378 | end; |
379 | |||
380 | procedure TMainForm.StoneDragDrop(Sender, Source: TObject; X, Y: Integer); |
||
381 | begin |
||
382 | DoJump(TComponent(Source).Tag, TComponent(Sender).Tag); |
||
383 | end; |
||
384 | |||
385 | procedure TMainForm.StoneDragOver(Sender, Source: TObject; X, |
||
386 | Y: Integer; State: TDragState; var Accept: Boolean); |
||
387 | begin |
||
388 | Accept := MayJump(TComponent(Source).Tag, TComponent(Sender).Tag); |
||
389 | end; |
||
390 | |||
24 | daniel-mar | 391 | procedure TMainForm.DrawField(x, y: integer; var f: TField); |
1 | daniel-mar | 392 | var |
393 | index: integer; |
||
394 | begin |
||
24 | daniel-mar | 395 | if f.FieldType = ftFullSpace then exit; |
1 | daniel-mar | 396 | |
23 | daniel-mar | 397 | index := PlaygroundMatrix.CoordToIndex(x, y); |
1 | daniel-mar | 398 | |
26 | daniel-mar | 399 | if not Assigned(f.Data) then f.Data := TFieldVclData.Create; |
400 | TFieldVclData(f.Data).Panel := DrawStoneBox(x, y, index, f); |
||
401 | TFieldVclData(f.Data).Stone := DrawStone(f); |
||
24 | daniel-mar | 402 | end; |
1 | daniel-mar | 403 | |
24 | daniel-mar | 404 | procedure TMainForm.TimerTimer(Sender: TObject); |
405 | begin |
||
406 | if MPauseTime.Checked then exit; |
||
407 | if mainform.Focused then Inc(CountedSeconds); |
||
408 | RefreshTime; |
||
1 | daniel-mar | 409 | end; |
410 | |||
24 | daniel-mar | 411 | function TMainForm.LevelTime: String; |
412 | begin |
||
413 | result := FormatDateTime('hh:nn:ss', CountedSeconds / SecsPerDay) |
||
414 | end; |
||
415 | |||
416 | procedure TMainForm.NewGame(Filename: string); |
||
417 | resourcestring |
||
418 | LNG_LVL_INVALID_NO_JUMP = 'Warning! The level is not playable. There are no jumps possible.'; |
||
1 | daniel-mar | 419 | var |
19 | daniel-mar | 420 | y, x: integer; |
421 | max_x, max_y: integer; |
||
422 | p: TPanel; |
||
1 | daniel-mar | 423 | begin |
24 | daniel-mar | 424 | DestroyLevel; |
425 | |||
426 | MPauseTime.Checked := true; |
||
427 | MPauseTime.Enabled := true; |
||
428 | Timer.Enabled := true; |
||
429 | MRestartGame.Enabled := true; |
||
430 | |||
431 | LevelFile := Filename; |
||
432 | Level := TLevel.Create(LevelFile); |
||
433 | |||
434 | Level.FillPlaygroundMatrix(PlaygroundMatrix, true); |
||
435 | if Length(PlaygroundMatrix.Fields) = 0 then Exit; |
||
436 | |||
1 | daniel-mar | 437 | PlayGround.Visible := false; |
438 | |||
439 | max_x := 0; |
||
19 | daniel-mar | 440 | max_y := 0; |
24 | daniel-mar | 441 | for x := Low(PlaygroundMatrix.Fields) to High(PlaygroundMatrix.Fields) do |
1 | daniel-mar | 442 | begin |
24 | daniel-mar | 443 | for y := Low(PlaygroundMatrix.Fields[x]) to High(PlaygroundMatrix.Fields[x]) do |
1 | daniel-mar | 444 | begin |
26 | daniel-mar | 445 | if PlaygroundMatrix.Fields[x,y].FieldState = fsOccupied then |
23 | daniel-mar | 446 | Inc(LevelTotalStones); |
24 | daniel-mar | 447 | DrawField(x, y, PlaygroundMatrix.Fields[x,y]); |
29 | daniel-mar | 448 | if Assigned(PlaygroundMatrix.Fields[x,y].Data) then |
19 | daniel-mar | 449 | begin |
29 | daniel-mar | 450 | p := TFieldVclData(PlaygroundMatrix.Fields[x,y].Data).Panel; |
451 | if Assigned(p) then |
||
452 | begin |
||
453 | max_x := Max(max_x, p.Left + p.Width); |
||
454 | max_y := Max(max_y, p.Top + p.Height); |
||
455 | end; |
||
19 | daniel-mar | 456 | end; |
1 | daniel-mar | 457 | end; |
458 | end; |
||
459 | |||
460 | PlayGround.Visible := true; |
||
461 | |||
462 | // Das Form an das Level anpassen |
||
19 | daniel-mar | 463 | PlayGround.Top := MET_OUTER_MARGIN; |
464 | PlayGround.Left := MET_OUTER_MARGIN; |
||
465 | PlayGround.Width := max_x; |
||
466 | PlayGround.Height := max_y; |
||
467 | ClientWidth := 2 * MET_OUTER_MARGIN + PlayGround.Width; |
||
468 | ClientHeight := 2 * MET_OUTER_MARGIN + PlayGround.Height + Statistics.Height; |
||
1 | daniel-mar | 469 | |
19 | daniel-mar | 470 | // If the board is too small, ClientWidth/ClientHeight will stop at a minimum value |
471 | // in this case, we make sure that the Playground is centered |
||
472 | PlayGround.Left := ClientWidth div 2 - Playground.Width div 2; |
||
473 | PlayGround.Top := (ClientHeight - Statistics.Height) div 2 - Playground.Height div 2; |
||
474 | |||
24 | daniel-mar | 475 | Statistics.Panels.Items[0].Width := Round(ClientWidth * MET_PERCENT_PNL_TIME); |
476 | Statistics.Panels.Items[1].Width := Round(ClientWidth * MET_PERCENT_PNL_STONES); |
||
1 | daniel-mar | 477 | |
6 | daniel-mar | 478 | SetLength(PrevPlaygroundMatrixes,1); |
21 | daniel-mar | 479 | PrevPlaygroundMatrixes[0] := PlayGroundMatrix.CloneMatrix; |
6 | daniel-mar | 480 | MUndo.Enabled := false; |
1 | daniel-mar | 481 | |
22 | daniel-mar | 482 | if not PlayGroundMatrix.CanJump(Level.GameMode = gmDiagonal) then |
1 | daniel-mar | 483 | begin |
9 | daniel-mar | 484 | MessageDlg(LNG_LVL_INVALID_NO_JUMP, mtError, [mbOk], 0); |
1 | daniel-mar | 485 | end; |
486 | RefreshTime; |
||
487 | RefreshStonesRemoved; |
||
488 | RefreshPoints; |
||
489 | end; |
||
490 | |||
491 | procedure TMainForm.MNewGameClick(Sender: TObject); |
||
492 | begin |
||
493 | LevelFile := AskForLevel; |
||
494 | if LevelFile <> '' then |
||
495 | begin |
||
496 | NewGame(LevelFile); |
||
497 | end; |
||
498 | end; |
||
499 | |||
500 | procedure TMainForm.MAboutClick(Sender: TObject); |
||
501 | begin |
||
502 | AboutBox.ShowModal; |
||
503 | end; |
||
504 | |||
505 | function TMainForm.AskForLevel: String; |
||
506 | begin |
||
507 | LevelChoice.ShowModal; |
||
508 | |||
509 | if LevelChoice.ModalResult <> mrOK then |
||
510 | begin |
||
511 | result := ''; |
||
512 | exit; |
||
513 | end; |
||
514 | |||
515 | result := LevelChoice.SelectedLevel; |
||
516 | end; |
||
517 | |||
518 | procedure TMainForm.FormShow(Sender: TObject); |
||
519 | begin |
||
520 | LevelFile := AskForLevel; |
||
521 | if LevelFile <> '' then |
||
522 | begin |
||
523 | NewGame(LevelFile); |
||
524 | end |
||
18 | daniel-mar | 525 | else |
526 | begin |
||
527 | NoCloseQuery := true; |
||
528 | Close; |
||
529 | end; |
||
1 | daniel-mar | 530 | end; |
531 | |||
8 | daniel-mar | 532 | function TMainForm.GoalStatus: TGoalStatus; |
533 | begin |
||
25 | daniel-mar | 534 | result := PlaygroundMatrix.GoalStatus(LevelTotalStones - LevelRemovedStones); |
8 | daniel-mar | 535 | end; |
536 | |||
1 | daniel-mar | 537 | procedure TMainForm.FormCreate(Sender: TObject); |
538 | begin |
||
539 | JumpHistory := TStringList.Create; |
||
540 | LoadSettings; |
||
541 | end; |
||
542 | |||
543 | procedure TMainForm.FormDestroy(Sender: TObject); |
||
544 | begin |
||
11 | daniel-mar | 545 | DestroyLevel; |
1 | daniel-mar | 546 | JumpHistory.Free; |
547 | end; |
||
548 | |||
549 | procedure TMainForm.MJumpHistoryClick(Sender: TObject); |
||
550 | begin |
||
551 | HistoryForm.JumpMemo.Lines.Assign(JumpHistory); |
||
552 | HistoryForm.ShowModal; |
||
553 | end; |
||
554 | |||
555 | procedure TMainForm.RestartLevel; |
||
10 | daniel-mar | 556 | var |
557 | i: Integer; |
||
1 | daniel-mar | 558 | begin |
21 | daniel-mar | 559 | MPauseTime.Checked := true; |
560 | MPauseTime.Enabled := true; |
||
561 | Timer.Enabled := true; |
||
1 | daniel-mar | 562 | |
563 | CountedSeconds := 0; |
||
564 | RefreshTime; |
||
565 | |||
566 | Points := 0; |
||
567 | RefreshPoints; |
||
568 | |||
569 | LevelRemovedStones := 0; |
||
570 | RefreshStonesRemoved; |
||
571 | |||
572 | JumpHistory.Clear; |
||
573 | |||
10 | daniel-mar | 574 | RedrawStonesFromMatrix(PrevPlaygroundMatrixes[0]); |
575 | SetNewPlayGroundMatrix(PrevPlaygroundMatrixes[0]); |
||
576 | for i := 1 to Length(PrevPlaygroundMatrixes)-1 do |
||
21 | daniel-mar | 577 | PrevPlaygroundMatrixes[i].ClearMatrix(false); |
10 | daniel-mar | 578 | SetLength(PrevPlaygroundMatrixes, 1); |
579 | |||
6 | daniel-mar | 580 | MUndo.Enabled := false; |
1 | daniel-mar | 581 | end; |
582 | |||
583 | procedure TMainForm.SetNewPlayGroundMatrix(Matrix: TPlayGroundMatrix); |
||
584 | begin |
||
21 | daniel-mar | 585 | PlayGroundMatrix.ClearMatrix(false); // Memory Leak verhindern |
586 | PlayGroundMatrix := Matrix.CloneMatrix; |
||
1 | daniel-mar | 587 | end; |
588 | |||
589 | procedure TMainForm.MRestartGameClick(Sender: TObject); |
||
590 | begin |
||
591 | RestartLevel; |
||
592 | end; |
||
593 | |||
6 | daniel-mar | 594 | procedure TMainForm.MUndoClick(Sender: TObject); |
595 | var |
||
596 | PrevWorth: integer; |
||
597 | NewWorth: integer; |
||
598 | begin |
||
599 | if Length(PrevPlaygroundMatrixes) > 1 then |
||
600 | begin |
||
21 | daniel-mar | 601 | PrevWorth := PrevPlaygroundMatrixes[Length(PrevPlaygroundMatrixes)-1].MatrixWorth; |
6 | daniel-mar | 602 | |
21 | daniel-mar | 603 | PrevPlaygroundMatrixes[Length(PrevPlaygroundMatrixes)-1].ClearMatrix(false); |
6 | daniel-mar | 604 | SetLength(PrevPlaygroundMatrixes, Length(PrevPlaygroundMatrixes)-1); |
605 | |||
21 | daniel-mar | 606 | NewWorth := PrevPlaygroundMatrixes[Length(PrevPlaygroundMatrixes)-1].MatrixWorth; |
6 | daniel-mar | 607 | RedrawStonesFromMatrix(PrevPlaygroundMatrixes[Length(PrevPlaygroundMatrixes)-1]); |
608 | SetNewPlayGroundMatrix(PrevPlaygroundMatrixes[Length(PrevPlaygroundMatrixes)-1]); |
||
609 | |||
610 | JumpHistory.Delete(JumpHistory.Count-1); |
||
611 | |||
612 | Dec(LevelRemovedStones); |
||
613 | RefreshStonesRemoved; |
||
614 | |||
615 | Dec(Points, NewWorth-PrevWorth); |
||
616 | RefreshPoints; |
||
7 | daniel-mar | 617 | |
618 | // Sound abspielen |
||
619 | if MEnableSound.Checked then PlaySound(RES_UNDO, HInstance, SND_ASYNC or SND_NOWAIT or SND_RESOURCE); |
||
6 | daniel-mar | 620 | end; |
621 | |||
622 | MUndo.Enabled := Length(PrevPlaygroundMatrixes) > 1; |
||
623 | end; |
||
624 | |||
1 | daniel-mar | 625 | procedure TMainForm.MHighScoresClick(Sender: TObject); |
626 | begin |
||
627 | HighScoreForm.Execute(ExtractFileNameWithoutExt(LevelFile)); |
||
628 | end; |
||
629 | |||
630 | procedure TMainForm.LoadSettings; |
||
631 | var |
||
632 | reg: TRegistry; |
||
633 | begin |
||
634 | reg := TRegistry.Create; |
||
635 | try |
||
636 | reg.RootKey := HKEY_CURRENT_USER; |
||
637 | if reg.OpenKeyReadOnly(REG_KEY) then |
||
638 | begin |
||
639 | if reg.ValueExists(REG_SOUND) then |
||
640 | MEnableSound.Checked := reg.ReadBool(REG_SOUND); |
||
641 | reg.CloseKey; |
||
642 | end; |
||
643 | finally |
||
644 | reg.Free; |
||
645 | end; |
||
646 | end; |
||
647 | |||
648 | procedure TMainForm.SaveSettings; |
||
649 | var |
||
650 | reg: TRegistry; |
||
651 | begin |
||
652 | reg := TRegistry.Create; |
||
653 | try |
||
654 | reg.RootKey := HKEY_CURRENT_USER; |
||
655 | if reg.OpenKey(REG_KEY, true) then |
||
656 | begin |
||
657 | reg.WriteBool(REG_SOUND, MEnableSound.Checked); |
||
658 | reg.CloseKey; |
||
659 | end; |
||
660 | finally |
||
661 | reg.Free; |
||
662 | end; |
||
663 | end; |
||
664 | |||
665 | procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction); |
||
666 | begin |
||
667 | SaveSettings; |
||
668 | if FinishForm.NameEdit.Text <> '' then |
||
669 | begin |
||
670 | FinishForm.SaveSettings; |
||
671 | end; |
||
672 | end; |
||
673 | |||
14 | daniel-mar | 674 | procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean); |
675 | resourcestring |
||
676 | LNG_REALLY_QUIT = 'Do you really want to quit?'; |
||
677 | begin |
||
18 | daniel-mar | 678 | CanClose := NoCloseQuery or (MessageDlg(LNG_REALLY_QUIT, mtConfirmation, mbYesNoCancel, 0) = mrYes); |
14 | daniel-mar | 679 | end; |
680 | |||
1 | daniel-mar | 681 | procedure TMainForm.MHelpClick(Sender: TObject); |
682 | begin |
||
683 | HelpForm.ShowModal; |
||
684 | end; |
||
685 | |||
26 | daniel-mar | 686 | { TFieldVclData } |
687 | |||
688 | destructor TFieldVclData.Destroy; |
||
689 | begin |
||
690 | if Assigned(Stone) then Stone.Free; |
||
691 | if Assigned(Panel) then Panel.Free; |
||
692 | inherited; |
||
693 | end; |
||
694 | |||
1 | daniel-mar | 695 | end. |