forked from ericlangedijk/Lemmix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameScreen.LevelCode.pas
349 lines (293 loc) · 8.85 KB
/
GameScreen.LevelCode.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
unit GameScreen.LevelCode;
{$include lem_directives.inc}
interface
uses
LCLIntf, LCLType, Classes, Controls, Graphics, Forms, ClipBrd,
GR32, GR32_Image, GR32_Layers,
Base.Utils,
Dos.Structures,
Styles.Base,
Prog.Types, Prog.Base, Prog.Data, Prog.Strings, Prog.Cache, Prog.App,
GameScreen.Base;
type
TGameScreenLevelCode = class(TGameBaseScreen)
private
const INTERFAL_BLINK = 240;
private
BlinkTimer : TTicker;
LevelCode : string;
CursorPosition : Integer;
ValidLevelCode : Boolean;
YPositions : array[0..3] of Integer;
XPos : Integer;
Blinking : Boolean;
Typing : Boolean;
LastMessage : string;
LastCheatMessage : string;
// internal events
procedure Form_KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure Form_KeyPress(Sender: TObject; var Key: Char);
procedure Form_Close(Sender: TObject; var Action: TCloseAction );
procedure Application_Idle(Sender: TObject; var Done: Boolean);
// internal methods
function FindLevelByCode(const aCode: string): TLevelLoadingInformation;
function CheckLevelCode: Boolean;
function CheckCheatCode: Boolean;
procedure DrawChar(aCursorPos: Integer; aBlink: Boolean = False);
procedure DrawMessage(const S: string);
procedure UpdateCheatMessage;
public
constructor Create(aOwner: TComponent); override;
destructor Destroy; override;
procedure BuildScreen; override;
end;
implementation
uses
SysUtils, Form.Base;
{ TGameScreenLevelCode }
constructor TGameScreenLevelCode.Create(aOwner: TComponent);
begin
inherited;
LevelCode := '..........';
CursorPosition := 1;
ScreenImg.Enabled := False;
OnKeyDown := Form_KeyDown;
OnKeyPress := Form_KeyPress;
OnClose := Form_Close;
//BlinkSpeedMS := 240;
XPos := (640 - (10 * 16)) div 2;
YPositions[0] := 120;
YPositions[1] := 152;
YPositions[2] := 184;
YPositions[3] := 216;
InitializeImageSizeAndPosition(640, 380);
ExtractBackGround;
ExtractPurpleFont;
BlinkTimer.Reset(9);
BlinkTimer.Interval := INTERFAL_BLINK;
end;
destructor TGameScreenLevelCode.Destroy;
begin
Application.OnIdle := nil;
inherited;
end;
procedure TGameScreenLevelCode.BuildScreen;
var
Mainpal: TArrayOfColor32;
begin
ScreenImg.BeginUpdate;
try
MainPal := GetDosMainMenuPaletteColors32;
TileBackgroundBitmap(0, 0);
BackBuffer.Assign(ScreenImg.Bitmap); // save background
DrawPurpleText(ScreenImg.Bitmap, SEnterCode, XPos, 120);
DrawPurpleText(ScreenImg.Bitmap, LevelCode, XPos, YPositions[1]);
UpdateCheatMessage;
Application.OnIdle := Application_Idle;
finally
ScreenImg.EndUpdate;
end;
end;
procedure TGameScreenLevelCode.Application_Idle(Sender: TObject; var Done: Boolean);
var
CurrTick: Int64;
begin
if ScreenIsClosing then
Exit;
if Typing then
Exit;
Done := False;
Sleep(1); // relax CPU
// todo: optional non blinking: then drawcursor beneath levelcode (_)
CurrTick := QueryTimer;
if BlinkTimer.Check(CurrTick) then begin
BlinkTimer.Reset(CurrTick);
Blinking := not Blinking;
DrawChar(CursorPosition, Blinking);
end;
end;
function TGameScreenLevelCode.CheckCheatCode: Boolean;
var
S: string;
begin
S := stringreplace(LowerCase(LevelCode), '.', '', [rfReplaceAll]);
Result := SameText(S, SCheatCode);
end;
function TGameScreenLevelCode.CheckLevelCode: Boolean;
var
s: string;
// Sys: TLevelSystem;
Txt: string;
info: TLevelLoadingInformation;
// todo: cheats
begin
Result := False;
// Validated := True;
s := stringreplace(LevelCode, '.', '', [rfReplaceAll]);
info := FindLevelByCode(s);
if not Assigned(info) and App.Config.GetMiscOption(TMiscOption.UseCheatCodes) then
info := App.Style.LevelSystem.FindLevelBySectionNameAndNumber(LevelCode);
if Assigned(info) then begin
App.CurrentLevelInfo := info;
Txt := Format(SCodeForLevel_sd, [App.CurrentLevelInfo.Section.SectionName, App.CurrentLevelInfo.LevelIndex + 1]);
DrawMessage(Txt);
Exit(True);
end
else
DrawMessage(SIncorrectCode);
end;
procedure TGameScreenLevelCode.DrawChar(aCursorPos: Integer; aBlink: Boolean);
var
C: Char;
begin
if aBlink then
C := '_'
else
C := LevelCode[CursorPosition];
DrawPurpleText(ScreenImg.Bitmap, C, XPos + CursorPosition * 16 - 16, YPositions[1], BackBuffer);
end;
procedure TGameScreenLevelCode.DrawMessage(const S: string);
begin
if LastMessage <> '' then
DrawPurpleTextCentered(ScreenImg.Bitmap, LastMessage, YPositions[2], BackBuffer, True);
LastMessage := S;
if S = '' then
Exit;
DrawPurpleTextCentered(ScreenImg.Bitmap, S, YPositions[2]);
end;
procedure TGameScreenLevelCode.UpdateCheatMessage;
begin
Assert(App <> nil);
if LastCheatMessage <> '' then
DrawPurpleTextCentered(ScreenImg.Bitmap, LastCheatMessage, 350- 20, BackBuffer, True);
LastCheatMessage := '';
if not App.Config.GetMiscOption(TMiscOption.UseCheatCodes) then
Exit;
LastCheatMessage := 'Cheatcodes Enabled!';
DrawPurpleTextCentered(ScreenImg.Bitmap, LastCheatMessage, 350 - 20);
end;
function TGameScreenLevelCode.FindLevelByCode(const aCode: string): TLevelLoadingInformation;
// look in cache and find the level
var
items: TArray<TStyleCache.TLevelCacheItem>;
item: TStyleCache.TLevelCacheItem;
begin
Result := nil;
items := App.StyleCache.FindLevelsByCode(aCode);
if Length(items) = 0 then
Exit;
for item in items do begin
if item.StyleName = Consts.StyleName then begin
Result := App.Style.LevelSystem.FindLevelByIndex(item.SectionIndex, item.LevelIndex);
Exit;
end;
end;
end;
procedure TGameScreenLevelCode.Form_Close(Sender: TObject; var Action: TCloseAction);
begin
Application.OnIdle := nil;
end;
procedure TGameScreenLevelCode.Form_KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if ScreenIsClosing then
Exit;
if Shift = [] then
begin
case Key of
VK_ESCAPE: CloseScreen(TGameScreenType.Menu);
VK_RETURN:
begin
if CheckCheatCode then
begin
// toggle cheat enabled
App.Config.SetMiscOption(TMiscOption.UseCheatCodes, True);//not App.UseCheatCodes;
App.Config.SetMiscOption(TMiscOption.UseCheatScrollingInPreviewScreen, True);//not App.UseCheatScrollingInPreviewScreen;
UpdateCheatMessage;
//DrawMessage('cheatmode enabled');
Exit;
end;
if not ValidLevelCode then
begin
ValidLevelCode := CheckLevelCode;
if ValidLevelCode then
begin
CloseDelay := 1000;
DrawChar(CursorPosition, False);
CloseScreen(TGameScreenType.Menu);
end;
// closedelay:=1000;
// CloseScreen(gstMenu);
end
else
CloseScreen(TGameScreenType.Menu);
// if ValidLevelCode then
// App.WhichLevel := wlLevelCode;
end;
end;
end;
end;
procedure TGameScreenLevelCode.Form_KeyPress(Sender: TObject; var Key: Char);
var
OldC, C: Char;
OldPos: Integer;
s: string;
i: Integer;
begin
if ScreenIsClosing then
Exit;
Typing := True;
try
C := UpCase(Char(Key));
case C of
^V:
begin
s := Clipboard.AsText;
s := s.Trim;
if s.Length = 10 then begin
LevelCode := s;
for i := 1 to 10 do begin
CursorPosition := i;
DrawChar(i, False);
end;
end;
//beep;
end;
'A'..'Z', '0'..'9':
begin
DrawMessage('');
OldC := LevelCode[CursorPosition];
OldPos := CursorPosition;
LevelCode[CursorPosition] := C;
if CursorPosition < 10 then
begin
// maybe blinking: repair
DrawChar(CursorPosition, False);
// next pos
Inc(CursorPosition);
end;
if (OldPos <> CursorPosition) or (OldC <> C) then
begin
DrawChar(CursorPosition);
end;
ValidLevelCode := False;
end;
Chr(8):
begin
DrawMessage('');
// GoBack := True;
if CursorPosition > 1 then
begin
LevelCode[CursorPosition] := '.';
// maybe blinking: repair
DrawChar(CursorPosition, False);
if CursorPosition > 1 then
Dec(CursorPosition);
ValidLevelCode := False;
end;
end;
end; // case C
finally
Typing := False;
end;
end;
end.