-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathENTITIES.PAS
1121 lines (946 loc) · 34.6 KB
/
ENTITIES.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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{$A+,B-,E+,F-,G+,I-,N+,P-,Q-,R-,S-,T-,V-,X+}
unit Entities;
interface
uses FixedP, MathFP;
const
PLAYER_WALK_SPEED = FP_0_6;
PLAYER_TACK_PUSH_FORCE = FP_8;
ENTITY_FRICTION = FP_0_2;
FORCE_FRICTION = FP_0_7;
PRICK_RADIUS = 4;
SCORE_UP_PARTICLE_SPEED = -trunc(0.075 * FP_FLOAT_SHIFT);
OW_PARTICLE_SPEED = -trunc(0.1 * FP_FLOAT_SHIFT);
{ cooldowns/times are all specified in terms of frame ticks }
STAB_COOLDOWN = 80;
STABBED_DEBUFF_TIME = 5000;
SPLASHED_DEBUFF_TIME = 5000;
type
Direction = (South, North, East, West);
{ used to statically define an animation sequence }
AnimationDesc = record
frames : array[0..5] of word; { 6 frames total ever ... }
count : word; { number of frames (max of 6) }
delay : word; { frame timer/delay between switching to next frame }
time : word; { total time. = count * delay }
loops : bytebool; { true = animation loops }
base : word; { spritesheet index of first frame. if
multi-directional, this should be the index of the
first frame for the south direction }
dirLength : word; { number of frames from the start of one direction
to the start of the next direction. if not a
multi-directional animation, this must be set to 0 }
end;
PAnimationDesc = ^AnimationDesc;
{ state needed for a currently running animation sequence. will be used
in conjunction with an AnimationDesc that defines the animation sequence
itself }
AnimationState = record
complete : bytebool; { true = current animation sequence is complete }
frameIndex : word; { index of the current animation sequence frame }
time : word; { current frame timer }
end;
PAnimationState = ^AnimationState;
{ general entity properties. an instance of this should be included
in the actual entity's specific record-type. not all entity types
actually use all of these properties. }
Entity = record
position : Vec2FP;
velocity : Vec2FP;
force : Vec2FP;
direction : Direction;
animation : AnimationState;
noCollision : bytebool;
end;
{ particle entity properties. sprite-animation-based particles
have their lifetime directly linked to the length of the animation.
TODO: non-sprite-animation-based particles. }
Particle = record
active : bytebool;
entity : Entity;
animation : PAnimationDesc; { if non-nil, particle is a
sprite-animation-based particle }
end;
FruitKind = (Tomato, Grapes);
FruitState = (Plant, Growing, Grown, Popped);
{ fruit entity properties }
Fruit = record
entity : Entity;
kind : FruitKind;
state : FruitState;
{ the meaning of these two values depends on the 'state' value }
counter : word;
value : word;
isGold : bytebool;
end;
PlayerState = (Idle, Walking, Stabbing, Victory, Defeat);
{ holds player state. duh. }
Player = record
entity : Entity;
fruitPref : FruitKind;
state : PlayerState;
stabCooldown : word;
stabbedDebuffTime : word;
splashedDebuffTime : word;
skipRenderFlag : bytebool;
score : word;
end;
PPlayer = ^Player;
procedure ResetAnimationState(var animation : AnimationState);
function GetAnimationFrame(const state : AnimationState;
const desc : AnimationDesc;
direction : Direction) : word;
procedure UpdateAnimation(var entity : Entity;
const animation : AnimationDesc);
function DoEntitiesOverlap(const a, b : Entity) : boolean;
function DoesEntityOverlap(const entity : Entity;
x1, y1, x2, y2 : integer) : boolean;
function IsEntityPositionValid(const entity : Entity) : boolean;
function MoveEntity(var entity : Entity) : boolean;
function IsEntityStopped(const entity : Entity) : boolean;
procedure UpdateEntity(var entity : Entity);
procedure SetPlayerState(var player : Player; state : PlayerState);
procedure GetThumbTackPointCoords(const player : Player; var out_x, out_y : integer);
procedure GetThumbTackRenderCoords(const player : Player; var out_x, out_y : integer);
procedure UpdatePlayer(var player : Player);
procedure InitPlayer(var player : Player; x, y : integer; fruit: FruitKind);
procedure MovePlayer(var player : Player; dir : Direction);
procedure StabPlayer(var player : Player);
procedure DoThumbTackStabAt(px, py : integer; player : PPlayer);
function SpawnRandomFruit : integer;
procedure PopAllFruit(kind : FruitKind; player : PPlayer);
procedure PopFruitAt(x, y : integer; player : PPlayer);
procedure UpdateAllFruit;
function GetUnusedParticleIndex : integer;
function SpawnTomatoSplash(x, y : integer) : integer;
function SpawnGrapesSplash(x, y : integer) : integer;
function SpawnPlantSplash(x, y : integer) : integer;
function SpawnStabFlash(x, y : integer) : integer;
function SpawnScoreUp(x, y : integer; kind : FruitKind) : integer;
function SpawnOw(x, y : integer) : integer;
procedure UpdateAllParticles;
implementation
uses Math, Toolbox, Maps, Assets, Shared;
procedure ResetAnimationState(var animation : AnimationState);
{ resets the given animation state, so it can be used to start an
animation sequence from the very beginning }
begin
with animation do begin
complete := false;
frameIndex := 0;
time := 0;
end;
end;
function GetAnimationFrame(const state : AnimationState;
const desc : AnimationDesc;
direction : Direction) : word;
{ returns the current spritesheet bitmap index that should be blitted
to draw an entity based on it's animation state and facing direction }
begin
with desc do begin
GetAnimationFrame := frames[state.frameIndex]
+ (ord(direction) * dirLength)
+ base;
end;
end;
procedure UpdateAnimation(var entity : Entity;
const animation : AnimationDesc);
{ cycles the entity's animation state. the passed AnimationDesc should be
the corresponding animation descriptor/definition for the animation state
that the entity is currently in }
begin
with entity.animation do begin
if not complete then begin
inc(time, frameTicks);
if time >= animation.delay then begin
{ move to next frame in the current animation sequence }
time := 0;
if frameIndex = (animation.count-1) then begin
{ we're at the last frame in the current animation sequence }
if not animation.loops then begin
complete := true;
end else
frameIndex := 0;
end else
inc(frameIndex);
end;
end;
end;
end;
{ ------------------------------------------------------------------------ }
function DoEntitiesOverlap(const a, b : Entity) : boolean;
{ returns true if the given entities overlap fully or partially }
const
EDGE = 2;
var
ax1, ay1, ax2, ay2 : integer;
bx1, by1, bx2, by2 : integer;
begin
DoEntitiesOverlap := false;
with a.position do begin
ax1 := FixToInt(x)+EDGE;
ay1 := FixToInt(y)+EDGE;
ax2 := ax1 + (ENTITY_SIZE-1)-EDGE;
ay2 := ay1 + (ENTITY_SIZE-1)-EDGE;
end;
with b.position do begin
bx1 := FixToInt(x)+EDGE;
by1 := FixToInt(y)+EDGE;
bx2 := bx1 + (ENTITY_SIZE-1)-EDGE;
by2 := by1 + (ENTITY_SIZE-1)-EDGE;
end;
if (ay1 < by1) and (ay2 < by1) then
exit;
if (ay1 > by2) and (ay2 > by2) then
exit;
if (ax1 < bx1) and (ax2 < bx1) then
exit;
if (ax1 > bx2) and (ax2 > bx2) then
exit;
DoEntitiesOverlap := true;
end;
function DoesEntityOverlap(const entity : Entity;
x1, y1, x2, y2 : integer) : boolean;
{ returns true if the entity partially or fully overlaps with the given
area (specified in pixel coordinates) }
const
EDGE = 2;
var
ex1, ey1, ex2, ey2 : integer;
begin
DoesEntityOverlap := false;
with entity.position do begin
ex1 := FixToInt(x)+EDGE;
ey1 := FixToInt(y)+EDGE;
ex2 := ex1 + (ENTITY_SIZE-1)-EDGE;
ey2 := ey1 + (ENTITY_SIZE-1)-EDGE;
end;
if (ey1 < y1) and (ey2 < y1) then
exit;
if (ey1 > y2) and (ey2 > y2) then
exit;
if (ex1 < x1) and (ex2 < x1) then
exit;
if (ex1 > x2) and (ex2 > x2) then
exit;
DoesEntityOverlap := true;
end;
function IsEntityPositionValid(const entity : Entity) : boolean;
{ returns true if the given entity is currently located in a position
that is "valid", meaning completely free of collisions with anything. }
begin
IsEntityPositionValid := true;
with entity.position do begin
{ any collision with the map? either collidable tiles or fruit ... }
if IsMapCollision(FixToInt(x), FixToInt(y)) then
IsEntityPositionValid := false
else begin
{ TODO: this seems a bit clumsy ...? }
{ make sure we don't compare against ourself. since this function
operates on generic Entity's, we kinda have to check this way }
if @entity = @player1.entity then begin
IsEntityPositionValid := (not DoEntitiesOverlap(entity,
player2.entity));
end else begin
IsEntityPositionValid := (not DoEntitiesOverlap(entity,
player1.entity));
end;
end;
end;
end;
function MoveEntity(var entity : Entity) : boolean;
{ updates the entity's X and Y position based on their current movement
velocity and any force velocity. checks for map collisions and prevents
movement along the X and/or Y axis if any collisions are found. returns
true if the entity collided against something. }
const
{ number of movement+collision sub-steps to divide this movement into }
NUM_STEPS = 2;
{ reciprocal (also as fixed point) just so that we can use Vec2FP_Scale }
STEP_SCALE = trunc((1 / NUM_STEPS) * FP_FLOAT_SHIFT);
var
stepVelocity : Vec2FP;
i : integer;
begin
MoveEntity := false;
with entity do begin
{ calculate the sub-step velocity for the below loop ... }
Vec2FP_Add(stepVelocity, velocity, force);
if (stepVelocity.x = 0) and (stepVelocity.y = 0) then exit;
{ if this entity skips collision checks, just move it and return }
if noCollision then begin
Vec2FP_AddTo(position, stepVelocity);
exit;
end;
{ we're dividing the movement and collision checks into sub-steps!
this is a possibly-hacky solution to fix issues with any frame-timings
that might result in us (without using sub-steps) moving entities
more than 1 pixel per loop. this could cause problems! e.g. the player
might not be able to move into a 1-tile-wide gap because their
movement keeps skipping over 1 pixel too much ... }
Vec2FP_ScaleThis(stepVelocity, STEP_SCALE);
for i := 1 to NUM_STEPS do begin
{ add velocity X to player's position, then test for collisions using
this new position. if a collision occurs, we cannot move the player
in the X direction by this amount, so we back it out }
inc(position.x, stepVelocity.x);
if not IsEntityPositionValid(entity) then begin
MoveEntity := true;
dec(position.x, stepVelocity.x);
end;
{ same thing for the velocity Y now. note that, if no collision occured
in the X direction, the position that is tested for here will also
include the velocity X component ... }
inc(position.y, stepVelocity.y);
if not IsEntityPositionValid(entity) then begin
MoveEntity := true;
dec(position.y, stepVelocity.y);
end;
end;
end;
end;
function IsEntityStopped(const entity : Entity) : boolean;
{ returns true if the entity's movement velocity is slow enough that they
could be considered stopped. this does not take into account the entity's
force velocity }
const
THRESHOLD = trunc(0.05 * FP_FLOAT_SHIFT);
begin
with entity.velocity do begin
IsEntityStopped := (abs(x) < THRESHOLD) and (abs(y) < THRESHOLD);
end;
end;
procedure UpdateEntity(var entity : Entity);
{ updates general entity state. this includes applying velocity/force
vectors to the entity's position and also applying friction to those
velocity/force vecotrs too }
begin
{ move entity in the direction of their velocity and any combined force
that is currently being applied to them. also handles collision. }
MoveEntity(entity);
with entity do begin
{ slow both the velocity and force down by friction }
{ TODO: probably for the force vector, we should use something other
than friction ... ? some per-force specific value maybe? }
Vec2FP_ScaleThis(velocity, ENTITY_FRICTION);
Vec2FP_ScaleThis(force, FORCE_FRICTION);
end;
end;
{ ------------------------------------------------------------------------ }
procedure SetPlayerState(var player : Player; state : PlayerState);
{ switches the player state to the one specified, also resetting the
player's current animation state }
begin
if state = player.state then exit;
player.state := state;
ResetAnimationState(player.entity.animation);
end;
procedure GetThumbTackPointCoords(const player : Player;
var out_x, out_y : integer);
{ computes the pixel coordinate of where the player's thumb tack's point
should be (assuming they are currently stabbing). }
var
dir : Direction;
begin
{ lol, perhaps just computing, say, 8-16 pixels out in a line directly
centered on the player and outward in their facing direction and using
that as the point coordinate would be best ... ?
this all seems silly now that i've written it ... }
with player.entity do begin
dir := direction;
with position do begin
out_x := FixToInt(x)
+ thumbTackRenderOffsetsX[ord(dir)]
+ thumbTackPointOffsetsX[ord(dir)];
out_y := FixToInt(y)
+ thumbTackRenderOffsetsY[ord(dir)]
+ thumbTackPointOffsetsY[ord(dir)];
end;
end;
end;
procedure GetThumbTackRenderCoords(const player : Player;
var out_x, out_y : integer);
{ computes the pixel coordinate of where the player's thumb tack sprite
should be (assuming they are currently stabbing). }
var
dir : Direction;
begin
with player.entity do begin
dir := direction;
with position do begin
out_x := FixToInt(x) + thumbTackRenderOffsetsX[ord(dir)];
out_y := FixToInt(y) + thumbTackRenderOffsetsY[ord(dir)];
end;
end;
end;
procedure UpdatePlayer(var player : Player);
{ updates player (and general entity) state. this includes entity
movement, as well as player animation state }
var
animation : ^AnimationDesc;
i : integer;
px, py : integer;
dir : Direction;
begin
with player do begin
{ do general entity updates first ... this will handle movement via
any velocity/force vectors }
UpdateEntity(entity);
if state = Stabbing then begin
if (entity.animation.frameIndex = 0)
and (entity.animation.time = 0)
and (not entity.animation.complete) then begin
{ only for the very first frame of the stabbing animation,
check for any fruit that collide with the thumb tack's pointy
end and should be popped }
dir := entity.direction;
with entity.position do begin
GetThumbTackPointCoords(player, px, py);
DoThumbTackStabAt(px, py, @player);
end;
end;
if entity.animation.complete then begin
{ keep player in the stabbing state until that animation has
completed. }
SetPlayerState(player, Idle);
stabCooldown := STAB_COOLDOWN;
{ stab/attack cooldown time quadrupled when afflicted by either
the 'stabbed' or 'splashed' cooldown }
if (stabbedDebuffTime > 0) or (splashedDebuffTime > 0) then
stabCooldown := stabCooldown * 4;
end;
end else if (state <> Victory) and (state <> Defeat) then begin
{ set player idle/walking based on their velocity.
note that this check ignores their force velocity! }
if IsEntityStopped(entity) then
SetPlayerState(player, Idle)
else
SetPlayerState(player, Walking);
end;
UpdateAnimation(entity, playerAnimations[ord(state)]);
{ update cooldowns / debuff timers }
if stabCooldown > 0 then
if stabCooldown > frameTicks then
dec(stabCooldown, frameTicks)
else
stabCooldown := 0;
if stabbedDebuffTime > 0 then
if stabbedDebuffTime > frameTicks then
dec(stabbedDebuffTime, frameTicks)
else
stabbedDebuffTime := 0;
if splashedDebuffTime > 0 then
if splashedDebuffTime > frameTicks then
dec(splashedDebuffTime, frameTicks)
else
splashedDebuffTime := 0;
if (stabbedDebuffTime > 0) or (splashedDebuffTime > 0) then
skipRenderFlag := not skipRenderFlag;
end;
end;
procedure InitPlayer(var player : Player; x, y : integer; fruit: FruitKind);
begin
MemFill(@player, 0, SizeOf(Player));
player.entity.position.x := IntToFix(x);
player.entity.position.y := IntToFix(y);
player.entity.direction := South;
player.fruitPref := fruit;
case fruit of
Tomato: tomatoPlayer := @player;
Grapes: grapesPlayer := @player;
end;
SetPlayerState(player, Idle);
end;
procedure MovePlayer(var player : Player; dir : Direction);
{ sets the given player in motion in the given direction. this function
does not actually adjust the players position in any way. it only sets
their velocity }
var
speed : fixed;
begin
with player do begin
if stabCooldown > 0 then exit;
if (state <> Idle) and (state <> Walking) then exit;
{ movement speed is halved when afflicted by 'splashed' }
if splashedDebuffTime > 0 then
speed := FixMul(PLAYER_WALK_SPEED, FP_0_5)
{ movement speed is cut by 30% when afflicted by 'stabbed' }
else if stabbedDebuffTime > 0 then
speed := FixMul(PLAYER_WALK_SPEED, FP_0_7)
else
speed := PLAYER_WALK_SPEED;
case dir of
North: begin
with entity do begin
dec(velocity.y, speed);
direction := North;
end;
end;
South: begin
with entity do begin
inc(velocity.Y, speed);
direction := South;
end;
end;
West: begin
with entity do begin
dec(velocity.x, speed);
direction := West;
end;
end;
East: begin
with entity do begin
inc(velocity.x, speed);
direction := East;
end;
end;
end;
end;
end;
procedure StabPlayer(var player : Player);
{ switches the player into the 'stabbing' state, which will start the
animation as well as bring out the player's thumb tack (during the
next player update anyway) }
begin
with player do begin
if stabCooldown > 0 then exit;
if (state <> Idle) and (state <> Walking) then exit;
SetPlayerState(player, Stabbing);
end;
end;
procedure DoThumbTackStabAt(px, py : integer; player : PPlayer);
{ determines what, if anything, a thumb tack stab with the pixel coordinates
of the point provided, collided with and what should happen. the player
passed should be the player who owns the thumb tack. }
var
otherPlayer : PPlayer;
dir : Direction;
begin
{ always pop any fruit / destroy any plants at this position }
PopFruitAt(px, py, player);
{ did we also hit the other player? }
{ determine which player is which ... }
if player = @player1 then
otherPlayer := @player2
else
otherPlayer := @player1;
{ now check if this thumb tack point collided with that other player }
if DoesEntityOverlap(otherPlayer^.entity,
px - PRICK_RADIUS,
py - PRICK_RADIUS,
px + PRICK_RADIUS,
py + PRICK_RADIUS) then begin
{ we hit the other player. push the other player in the direction
that this player is facing }
dir := player^.entity.direction;
with otherPlayer^.entity do begin
case dir of
North: AngleToVec2DFP(BIN_ANGLE_270, force);
South: AngleToVec2DFP(BIN_ANGLE_90, force);
West: AngleToVec2DFP(BIN_ANGLE_180, force);
East: AngleToVec2DFP(0, force);
end;
Vec2FP_ScaleThis(force, PLAYER_TACK_PUSH_FORCE);
end;
{ also apply the 'stabbed' debuff to the other player }
otherPlayer^.stabbedDebuffTime := STABBED_DEBUFF_TIME;
{ finally, spawn a 'ow' particle as another indication that a player
was stabbed }
with otherPlayer^.entity.position do begin
SpawnOw(FixToInt(x), FixToInt(y));
end;
end;
end;
{ ------------------------------------------------------------------------ }
procedure SetFruitState(var fruit : Fruit; state : FruitState);
begin
if state = fruit.state then exit;
fruit.state := state;
fruit.counter := 0;
fruit.value := 0;
ResetAnimationState(fruit.entity.animation);
end;
function GetPlantRandomLifeTime : word;
const
MINIMUM_TIME = 3000;
STEP_SIZE = 2000;
begin
GetPlantRandomLifeTime := MINIMUM_TIME
+ ((1+random(5)) * STEP_SIZE)
- random(STEP_SIZE);
end;
function SpawnRandomFruit : integer;
{ spawns a new fruit (starting it off as a plant) in any random available
dirt tile on the map. spawning may fail (if a free dirt tile could not
be found randomly). returns the dirtTiles index of the new fruit, or
-1 if spawning failed }
var
idx : integer;
begin
SpawnRandomFruit := -1;
{ find a random spot to spawn a new fruit in }
idx := GetRandomUnusedDirtTileIndex;
if idx = -1 then exit;
SpawnRandomFruit := idx;
with dirtTiles[idx] do begin
{ important! this marks the dirt tile as being 'used' }
hasFruit := true;
inc(numActiveDirtTiles);
{ zero out the fruit, and then fill in its starter properties }
MemFill(@fruit, 0, SizeOf(fruit));
SetFruitState(fruit, Plant);
if random(2) = 0 then
fruit.kind := Tomato
else
fruit.kind := Grapes;
fruit.value := GetPlantRandomLifeTime;
fruit.isGold := (random(100) < GOLD_FRUIT_SPAWN_CHANCE);
fruit.entity.position.x := IntToFix(x*16);
fruit.entity.position.y := IntToFix(y*16);
end;
end;
procedure PopFruitIn(var tile : DirtTile; player : PPlayer);
{ switches any fruit entity located within the given dirt tile into the
'popped' state, as well as starting up any relevant animations. if
there is no fruit in this tile, or the fruit is not in the 'grown'
state yet, nothing happens. the player passed in here will be the one
given 'credit' for popping the fruit (or if nil, no credit is given to
any player). }
var
fx, fy : integer;
begin
with tile do begin
if not hasFruit then exit;
case fruit.state of
Plant, Growing: begin
{ no score credit for stabbing a fruit plant, or a growing
but not yet full grown fruit. just despawn it. }
hasFruit := false;
dec(numActiveDirtTiles);
with fruit.entity.position do begin
SpawnPlantSplash(FixToInt(x), FixToInt(y));
end;
end;
Grown: begin
{ stabbing fully grown fruit }
SetFruitState(fruit, Popped);
with fruit.entity.position do begin
fx := FixToInt(x);
fy := FixToInt(y);
{ if the stabbing player's fruit choice matches the popped
fruit, then give the player score credit }
if fruit.kind = player^.fruitPref then begin
SpawnScoreUp(fx, fy, fruit.kind);
inc(player^.score);
end;
if fruit.isGold then
PopAllFruit(fruit.kind, player);
SpawnStabFlash(fx, fy);
end;
end;
end;
end;
end;
procedure PopAllFruit(kind : FruitKind; player : PPlayer);
var
idx : integer;
begin
for idx := 0 to numDirtTiles-1 do begin
with dirtTiles[idx] do begin
if not hasFruit then continue;
if (fruit.kind = kind) and (fruit.state = Grown) then begin
PopFruitIn(dirtTiles[idx], player);
end;
end;
end;
end;
procedure PopFruitAt(x, y : integer; player : PPlayer);
{ switches any fruit located near the given x/y coordinates into the
'popped' state, assuming that the fruit are already in the 'grown'
state (otherwise, they will not be changed). these x/y coordinates
would normally be the pixel coordinates corresponding to a player's
thumb tack point. the player passed in here will be the one given
'credit' for popping the fruit (or if nil, no credit is given to
any player). }
var
left, right, top, bottom : integer;
cx, cy : integer;
dirtTile : PDirtTile;
begin
{ get the map tile x/y region to check for dirt tiles within }
left := (x - PRICK_RADIUS) div TILE_SIZE;
right := (x + PRICK_RADIUS) div TILE_SIZE;
top := (y - PRICK_RADIUS) div TILE_SIZE;
bottom := (y + PRICK_RADIUS) div TILE_SIZE;
if left < 0 then left := 0;
if right > MAP_RIGHT then right := MAP_RIGHT;
if top < 0 then top := 0;
if bottom > MAP_BOTTOM then bottom := MAP_BOTTOM;
{ for all dirt tiles located within this region, pop the fruit
in them }
for cy := top to bottom do begin
for cx := left to right do begin
dirtTile := dirtTileMapping[(cy * SCREEN_MAP_WIDTH) + cx];
if dirtTile <> nil then begin
PopFruitIn(dirtTile^, player);
end;
end;
end;
end;
procedure UpdateAllFruit;
{ updates the state of all fruit currently active within dirtTiles }
const
GROW_STEP_TIME = 40;
SPRITE_MAX_SIZE = 16;
var
i, fx, fy : integer;
begin
{ periodically spawn more fruit }
if (fruitSpawnTimer >= 1000)
and (numActiveDirtTiles < map.header.maxFruit) then begin
SpawnRandomFruit;
fruitSpawnTimer := 0;
end;
for i := 0 to numDirtTiles-1 do begin
with dirtTiles[i] do begin
if not hasFruit then continue;
case fruit.state of
Plant: begin
{ count time into the plant has been a plant for 'value' time
at which point it should 'grow' into a fruit }
with fruit do begin
inc(counter, frameTicks);
if counter >= value then begin
SetFruitState(fruit, Growing);
value := 2;
end;
end;
end;
Growing: begin
{ the fruit "grows" by scaling it's size up from zero to it's
normal pixel sprite size. the size increments by 1 every
so often }
with fruit do begin
if value < SPRITE_MAX_SIZE then begin
inc(counter, frameTicks);
if counter >= GROW_STEP_TIME then begin
inc(value);
counter := 0;
end;
end else begin
SetFruitState(fruit, Grown);
end;
end;
end;
Grown: begin
end;
Popped: begin
{ when the popped "animation" (not really an animation, just
abusing a 1-frame sequence with long delay as a timer)
completes, we can deactivate this dirt tile and fruit }
if fruit.entity.animation.complete then begin
hasFruit := false;
dec(numActiveDirtTiles);
with fruit.entity.position do begin
fx := FixToInt(x);
fy := FixToInt(y);
end;
if fruit.kind = Tomato then begin
SpawnTomatoSplash(fx, fy);
{ if the grapes-preference player is nearby this tomato
splash, then spawn an extra tomato splash at their exact
position and afflict them with the 'splashed' debuff }
with grapesPlayer^ do begin
if DoesEntityOverlap(entity,
fx-32,
fy-32,
fx+48,
fy+48) then begin
with entity.position do
SpawnTomatoSplash(FixToInt(x), FixToInt(y));
splashedDebuffTime := SPLASHED_DEBUFF_TIME;
end;
end;
end else begin
SpawnGrapesSplash(fx, fy);
{ if the tomato-preference player is nearby this grapes
splash, then spawn an extra grapes splash at their exact
position and afflict them with the 'splashed' debuff }
with tomatoPlayer^ do begin
if DoesEntityOverlap(entity,
fx-32,
fy-32,
fx+48,
fy+48) then begin
with entity.position do
SpawnGrapesSplash(FixToInt(x), FixToInt(y));
splashedDebuffTime := SPLASHED_DEBUFF_TIME;
end;
end;
end;
continue;
end;
end;
end;
with fruit do begin
UpdateAnimation(entity, fruitAnimations[ord(state)]);
end;
end;
end;
end;
{ ------------------------------------------------------------------------ }
function GetUnusedParticleIndex : integer;
{ returns the index of the next unused/inactive particle. returns -1 if
there is no free index }
var
i : integer;
begin
GetUnusedParticleIndex := -1;
for i := 0 to MAX_PARTICLES-1 do begin
if not particles[i].active then begin
GetUnusedParticleIndex := i;
exit;
end;
end;
end;
function InitNewParticle(x, y : integer) : integer;
var
i : integer;
begin
InitNewParticle := -1;
i := GetUnusedParticleIndex;
if i = -1 then exit;
MemFill(@particles[i], 0, SizeOf(Particle));
with particles[i] do begin
active := true;
with entity do begin
noCollision := true;
position.x := IntToFix(x);
position.y := IntToFix(y);
end;
end;
InitNewParticle := i;
end;
function SpawnTomatoSplash(x, y : integer) : integer;
{ spawns a new 'tomato splash' particle at the given coordinates.
returns the index of the spawned particle if successful, or -1 if there
was no free particle index }
var
i : integer;
begin
SpawnTomatoSplash := -1;
i := InitNewParticle(x, y);
if i = -1 then exit;
with particles[i] do begin
animation := @tomatoSplashAnimation;
end;
SpawnTomatoSplash := i;
end;
function SpawnGrapesSplash(x, y : integer) : integer;
{ spawns a new 'grapes splash' particle at the given coordinates.
returns the index of the spawned particle if successful, or -1 if there
was no free particle index }
var
i : integer;
begin
SpawnGrapesSplash := -1;
i := InitNewParticle(x, y);
if i = -1 then exit;
with particles[i] do begin
animation := @grapesSplashAnimation;
end;
SpawnGrapesSplash := i;