-
Notifications
You must be signed in to change notification settings - Fork 1
/
Model_Lib.pas
598 lines (529 loc) · 13.2 KB
/
Model_Lib.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
unit Model_Lib;
interface
uses
Windows,
OpenGL,
Constants_lib,
Particle_Lib,
Func_Lib,
Engine_Reg,
Graph_Lib,
ObjAnim_Lib,
ObjSound_Lib;
const
ANIM_WALK = 0;
ANIM_CROUCH = 1;
ANIM_DIED = 2;
ANIM_WIDLE = 3;
ANIM_CIDLE = 4;
ANIM_PWALK = 5;
ANIM_PCROUCH = 6;
ANIM_PWIDLE = 7;
ANIM_PCIDLE = 8;
const
MAGIC = 'TMDL';
Version = 5;
type
TModelSound = class
Target : boolean; // Ñëåäèì çà èãðîêîì (çâóêè â ïîçèöèè êàìåðû)
Jump : TSound;
Death1 : TSound;
Death2 : TSound;
Death3 : TSound;
Pain_25 : TSound;
Pain_50 : TSound;
Pain_75 : TSound;
Pain_100 : TSound;
Step : TSound;
constructor Create;
procedure LoadFromModel(const Model: string);
procedure Death(X, Y: single);
procedure Pain(X, Y: single; Health: SmallInt);
end;
TModelAnim = record
Body : TObjTex;
Mask : TObjTex;
Width : Byte;
Height : Byte;
end;
TModel = class
constructor Create;
private
FCrouch : boolean;
FDied : boolean;
procedure SetFrame(index: integer);
function GetFrame: integer;
procedure SetCrouch(Value: boolean);
procedure SetDied(Value: boolean);
public
FrameIdx : Byte;
Color, railcolor: TRGB;
Anim : array [0..8] of TModelAnim;
CurAnim : Byte;
Step : array [0..255] of boolean;
Steps : boolean;
X, Y : single;
Sound : TModelSound;
ModelName : string;
function LoadFromFile(const ModelName: string; simple: boolean = false): boolean;
procedure NextFrame; // ñëåäóþùèé êàäð
procedure PrevFrame; // ïðåäûäóùèé êàäð
function FrameCount : integer; // êîëè÷åñòâî êàäðîâ â òåêóùåé àíèìàöèè
procedure Update;
procedure Draw(PowerUP: boolean = false);
// Ïóôêà!
procedure DrawWeapon(angle : single = 90;
crouch : boolean = false;
weapon : Byte = 1;
frame : Byte = 0;
PowerUP : boolean = false;
d : single = 1);
property FrameIndex: integer read GetFrame write SetFrame;
property Crouch : boolean read FCrouch write SetCrouch;
property Died : boolean read FDied write SetDied;
end;
TSkin = record
Name : string;
Color : TRGB;
end;
const
TransC : TRGBA = (R: 255; G: 255; B: 255; A: 0);
Skins : array [1..8] of TSkin = (
(Name: 'blue'; Color: (R: 0; G: 0; B: 255)),
(Name: 'red'; Color: (R: 255; G: 0; B: 0)),
(Name: 'green'; Color: (R: 0; G: 255; B: 0)),
(Name: 'white'; Color: (R: 255; G: 255; B: 255)),
(Name: 'black'; Color: (R: 128; G: 128; B: 128)),
(Name: 'yellow'; Color: (R: 255; G: 255; B: 0)),
(Name: 'purple'; Color: (R: 255; G: 0; B: 255)),
(Name: 'aqua'; Color: (R: 0; G: 255; B: 255)));
implementation
uses
Player_Lib, ItemObj_Lib;
/// ModelSound ///
constructor TModelSound.Create;
begin
Jump := TSound.Create;
Death1 := TSound.Create;
Pain_25 := TSound.Create;
Pain_50 := TSound.Create;
Pain_75 := TSound.Create;
Pain_100 := TSound.Create;
Step := TSound.Create;
end;
procedure TModelSound.LoadFromModel(const Model: string);
begin
Jump := TSound.Create('models\' + Model + '\jump1.wav', false);
Death1 := TSound.Create('models\' + Model + '\death1.wav', false);
Death2 := TSound.Create('models\' + Model + '\death2.wav', false);
Death3 := TSound.Create('models\' + Model + '\death3.wav', false);
Pain_25 := TSound.Create('models\' + Model + '\pain25_1.wav', false);
Pain_50 := TSound.Create('models\' + Model + '\pain50_1.wav', false);
Pain_75 := TSound.Create('models\' + Model + '\pain75_1.wav', false);
Pain_100 := TSound.Create('models\' + Model + '\pain100_1.wav', false);
Step := TSound.Create('models\' + Model + '\step.wav', false);
end;
procedure TModelSound.Death(X, Y: single);
begin
case trunc(randomf*3) of
0 : Death1.Play(X, Y);
1 : Death2.Play(X, Y);
2 : Death3.Play(X, Y);
end;
end;
procedure TModelSound.Pain(X, Y: single; Health: SmallInt);
begin
if Health <= 25 then
Pain_25.Play(X, Y, target)
else
if Health <= 50 then
Pain_50.Play(X, Y, target)
else
if Health <= 75 then
Pain_75.Play(X, Y, target)
else
Pain_100.Play(X, Y, target);
end;
/// NFKModel ///
constructor TModel.Create;
var
i : integer;
begin
Sound := TModelSound.Create;
for i := 0 to 6 do
with Anim[i] do
begin
Body := TObjTex.Create;
Mask := TObjTex.Create;
Width := 0;
Height := 0;
end;
CurAnim := 0;
end;
function TModel.LoadFromFile(const ModelName: string; simple: boolean = false): boolean;
var
i : integer;
n, s : string;
str : string;
F : File of Byte;
tBody : PaRGBA;
tMask : PByteArray;
w, h : WORD;
m : array [0..3] of Char;
b : Byte;
FCount, FWait : Byte;
FObj : TFrameObj;
x, y : integer;
procedure SmoothMask;
var
c : WORD;
idx : integer;
x, y : integer;
pMask : PByteArray;
procedure pix(x, y: SmallInt);
begin
if (x > -1) and (x < integer(w)) and
(y > -1) and (y < integer(h)) then
inc(c, tMask[y*w + x])
else
dec(idx);
end;
begin
GetMem(pMask, w*h);
for y := 0 to h - 1 do
for x := 0 to w - 1 do
begin
idx := 8;
c := 0;
pix(x - 1, y - 1);
pix(x - 1, y);
pix(x - 1, y + 1);
pix(x + 1, y - 1);
pix(x + 1, y);
pix(x + 1, y + 1);
pix(x, y - 1);
pix(x, y + 1);
pMask[y*w + x] := c div idx;
end;
FreeMem(tMask);
tMask := pMask;
end;
begin
Result := false;
i := Pos('+', ModelName);
if i <> 0 then
begin
n := LowerCase(Copy(ModelName, 1, i - 1));
s := LowerCase(Copy(ModelName, i + 1, Length(ModelName)));
end
else
begin
s := 'default';
n := LowerCase(ModelName);
end;
if s <> 'default' then
begin
w := 0;
for i := 1 to Length(Skins) do
if s = Skins[i].Name then
begin
w := 1;
break;
end;
if w = 0 then
Exit;
end;
try
if not FileExists(Engine_ModDir + 'models\' + n + '\' + n + '.tml') then
Exit;
if not Simple then
Sound.LoadFromModel(n);
FileMode := 64;
AssignFile(F, Engine_ModDir + 'models\' + n + '\' + n + '.tml');
Reset(F);
BlockRead(F, m, 4);
BlockRead(F, b, 1);
if (m <> MAGIC) or (b <> Version) then
begin
CloseFile(F);
Exit;
end;
BlockRead(F, Color, 3);
for i := 0 to 4 do
with Anim[i] do
begin
if Simple and (i > 0) then
break;
BlockRead(F, FCount, 1);
BlockRead(F, FWait, 1);
BlockRead(F, w, 2);
BlockRead(F, h, 2);
if not ((w = 0) or (h = 0)) then
begin
GetMem(tBody, w*h*4);
GetMem(tMask, w*h);
BlockRead(F, tBody[0], w*h*4);
BlockRead(F, tMask[0], w*h);
str := n + ' ' + IntToStr(i) + ' * body';
FObj := TexExists(str);
if FObj <> nil then
begin
FObj.flag := true;
Body.Tex := FObj;
Body.FWait := FWait + 1;
end
else
Body := TObjTex.Create(str, w, h, FWait + 1, true, false, nil, 0, tBody, 32, FCount);
str := n + ' ' + IntToStr(i) + ' * mask';
FObj := TexExists(str);
if FObj <> nil then
begin
FObj.flag := true;
Mask.Tex := FObj;
Mask.FWait := FWait;
end
else
Mask := TObjTex.Create(str, w, h, FWait + 1, true, false, nil, 0, tMask, 8, FCount);
if not Simple then
if i in [ANIM_WALK, ANIM_CROUCH] then
begin
for y := 0 to h - 1 do
for x := 0 to w - 1 do
tMask[y*w + x] := tBody[y*w + x].A;
SmoothMask;
if i = ANIM_WALK then
str := n + ' ' + IntToStr(i) + ' * pwalk'
else
str := n + ' ' + IntToStr(i) + ' * pcrouch';
FObj := TexExists(str);
if FObj <> nil then
begin
FObj.flag := true;
Anim[i + 3].Mask.Tex := FObj;
Anim[i + 3].Mask.FWait := 0;
end
else
Anim[i + 3].Mask := TObjTex.Create(str, w, h, 0, true, false, nil, 0, tMask, 8, FCount);
end;
Width := w div FCount;
Height := h;
FreeMem(tBody);
FreeMem(tMask);
end;
end;
BlockRead(F, Step[0], Anim[ANIM_WALK].Body.FrameCount);
CloseFile(F);
Result := true;
except
log('a');
end;
if s <> 'default' then
for i := 1 to Length(Skins) do
if s = Skins[i].Name then
begin
Color := Skins[i].Color;
Exit;
end;
end;
procedure TModel.NextFrame;
begin
FrameIndex := FrameIndex + 1;
end;
procedure TModel.PrevFrame;
begin
FrameIndex := FrameIndex - 1;
end;
function TModel.FrameCount: integer;
begin
Result := Anim[CurAnim].Body.FrameCount
end;
procedure TModel.Update;
begin
if Died then
CurAnim := ANIM_DIED
else
if Crouch then
CurAnim := ANIM_CROUCH
else
CurAnim := ANIM_WALK;
if Anim[CurAnim].Body.Wait > 0 then
dec(Anim[CurAnim].Body.Wait);
Anim[CurAnim].Body.FrameIndex := FrameIdx;
Anim[CurAnim].Mask.FrameIndex := FrameIdx;
end;
procedure TModel.Draw(PowerUP: boolean = false);
const
ls = 48;
var
wx, wy, yy : single;
tx, ty : single;
w, h : integer;
Frame : PTexData;
procedure DrawBox;
begin
glBegin(GL_QUADS);
glTexCoord2f(0, ty); glVertex2f(-wx, - wy + 24 - yy);
glTexCoord2f(tx, ty); glVertex2f( wx, - wy + 24 - yy);
glTexCoord2f(tx, 0); glVertex2f( wx, 24 + yy);
glTexCoord2f(0, 0); glVertex2f(-wx, 24 + yy);
glEnd;
end;
var
c : array [0..3] of single;
begin
glGetFloatv(GL_CURRENT_COLOR, @c);
yy := 0;
with Anim[CurAnim] do
begin
wx := Width/2; // Ðàçìåðû ôðåéìà
wy := Height;
Anim[CurAnim].Body.FrameIndex := FrameIdx;
Anim[CurAnim].Mask.FrameIndex := FrameIdx;
Frame := Anim[CurAnim].Body.CurFrame;
end;
if Frame <> nil then
begin
xglTex_Enable(Frame);
W := Frame^.Width; // Ðàçìåðû òåêñòóðû ôðåéìà
H := Frame^.Height;
end
else
begin
xglTex_Disable;
W := 64;
H := 64;
end;
tx := (wx + wx - 1)/W;
ty := (wy - 1)/H;
xglAlphaBlend(1);
if PowerUp then
glColor4f(1, 1, 1, 1)
else
glColor4f(1, 1, 1, c[3]);
DrawBox;
Frame := Anim[CurAnim].Mask.CurFrame;
xglTex_Enable(Frame);
if PowerUp then
glColor4ub(Color.R, Color.G, Color.B, 255)
else
glColor4ub(Color.R, Color.G, Color.B, trunc(c[3] * 255)); // àëüôà äëÿ èíâèç èãðîêîâ
DrawBox;
if PowerUP then
begin
xglTex_Disable;
Anim[CurAnim + 3].Mask.FrameIndex := FrameIdx;
xglTex_Enable(Anim[CurAnim + 3].Mask.CurFrame);
xglAlphaBlend(2);
// wx := wx + wx*0.1;
// yy := 24*0.1;
glColor4fv(@c);
DrawBox;
xglTex_Enable(@light_1);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2f(-ls, -ls);
glTexCoord2f(1, 0); glVertex2f( ls, -ls);
glTexCoord2f(1, 1); glVertex2f( ls, ls);
glTexCoord2f(0, 1); glVertex2f(-ls, ls);
glEnd;
xglAlphaBlend(1);
end;
end;
procedure TModel.DrawWeapon(angle: single; crouch: boolean; weapon: Byte; frame: Byte; PowerUP: boolean; d: single);
var
c : array [0..3] of single;
procedure DrawBox;
begin
glBegin(GL_QUADS);
glTexCoord2f(0, 1); glVertex2f(-6, -8);
glTexCoord2f(1, 1); glVertex2f(26, -8);
glTexCoord2f(1, 0); glVertex2f(26, 8);
glTexCoord2f(0, 0); glVertex2f(-6, 8);
glEnd;
end;
begin
if weapon = WPN_RAILGUN then frame := 0;
glGetFloatv(GL_CURRENT_COLOR, @c);
if WeaponExists(weapon) then
with WeaponObjs[weapon] do
xglTex_Enable(FIREanim[frame])
else
xglTex_Disable; // Exit íå ïèøó, ò.ê. áåëûé êâàäðàò ëó÷øå ÷åì ñîâñåì íè÷åãî
if trunc(angle) > 180 then
angle := trunc(angle) mod 180 - 90
else
angle := trunc(angle) - 90;
glTranslatef(0, GetShotY(crouch), 0);
glRotatef(angle, 0, 0, 1);
xglAlphaBlend(1);
if PowerUp then
glColor4f(1, 1, 1, 1)
else
glColor4f(1, 1, 1, c[3]); // àëüôà äëÿ èíâèç èãðîêîâ
DrawBox;
if weapon = WPN_RAILGUN then
begin
if WeaponExists(weapon) then
with WeaponObjs[weapon] do
xglTex_Enable(FIREanim[1])
else
xglTex_Disable; // Exit íå ïèøó, ò.ê. áåëûé êâàäðàò ëó÷øå ÷åì ñîâñåì íè÷åãî
glColor4f(railcolor.r/255 * (1 - d) + d,
railcolor.g/255 * (1 - d) + d,
railcolor.b/255 * (1 - d) + d, c[3]);
DrawBox;
end;
glColor4fv(@c);
// Îòðèñîâêà PowerUP íàëîæåíèÿ
if PowerUP then
begin
xglAlphaBlend(2);
glColor4fv(@c);
if WeaponExists(weapon) then
with WeaponObjs[weapon] do
xglTex_Enable(Mask[frame]);
DrawBox;
xglAlphaBlend(1);
end;
end;
procedure TModel.SetFrame(index: integer);
var
FFrame : integer;
begin
with Anim[CurAnim].Body do
begin
if Wait > 0 then Exit;
FFrame := index;
if FFrame > FrameCount - 1 then
if CurAnim = ANIM_DIED then
FFrame := FrameCount - 1
else
FFrame := 1;
if CurAnim <> ANIM_DIED then
if FFrame < 1 then
FFrame := FrameCount - 1;
if CurAnim = ANIM_WALK then
if Steps and Step[FFrame] and cg_steps then
Sound.Step.Play(X, Y);
Wait := FWait;
FrameIdx := FFrame;
end;
end;
function TModel.GetFrame: integer;
begin
Result := FrameIdx;
end;
procedure TModel.SetCrouch(Value: boolean);
begin
if FCrouch <> Value then
with Anim[CurAnim] do
begin
FCrouch := Value;
FrameIdx := 0;
end;
end;
procedure TModel.SetDied(Value: boolean);
begin
FDied := Value;
Crouch := false;
end;
end.