forked from bchipman/cs113_game
-
Notifications
You must be signed in to change notification settings - Fork 1
/
classes.py
992 lines (846 loc) · 37.6 KB
/
classes.py
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
import copy
import math
import random
from collections import defaultdict
from collections import namedtuple
import pygame
from pygame.locals import *
import globals as GL
from globals import *
from skills import *
# -------------------------------------------------------------------------
class Rect2(pygame.Rect):
def __init__(self, *args, **kargs):
if args != tuple():
if all_isinstance(args, tuple) and len(args) is 2:
super().__init__(args[0], args[1])
if all_isinstance(args, tuple) and len(args) is 1:
super().__init__(args[0][0], args[0][1], args[0][2], args[0][3])
elif all_isinstance(args, int):
super().__init__(args[0], args[1], args[2], args[3])
else:
if all_in('left,top,width,height'.split(','), kargs.keys()):
super().__init__(kargs['left'], kargs['top'], kargs['width'], kargs['height'])
elif all_in(('topleft', 'size'), kargs):
super().__init__(kargs['topleft'], kargs['size'])
for i in 'left,top,width,height,topleft,size'.split(','):
try:
kargs.pop(i)
except KeyError:
pass
for k, v in kargs.items():
exec('self.{} = {}'.format(k, repr(v)))
def p_collidelist(self, li):
# follows same logic as pygame.Rect.collidelist, but customized to look at center coords
for i in range(len(li)):
if li[i].left < self.centerx < li[i].right and li[i].top < self.centery < li[i].bottom:
return i
return -1
def p_collidelistall(self, li):
# follows same logic as pygame.Rect.collidelistall, but customized to look at center coords
hit_indices = []
for i, r in enumerate(li):
# if self.collidepoint(r.center): #Using this is causing particles to pass through terrain
if li[i].left < self.centerx < li[i].right and li[i].top < self.centery < li[i].bottom:
hit_indices.append(i)
return hit_indices
def __getattr__(self, name):
if name == 'hits_to_destroy':
return -1
elif name == 'spawn_point':
return None
# -------------------------------------------------------------------------
class Player(Rect2):
def __init__(self, id, topleft, sprite=None):
self.id = id # 1 for player1, 2 for player2
# position
super().__init__(topleft, (30, 40))
self.topleft_initial = self.topleft
# speed
self.dx = 10 if self.id == 1 else -10 # initial speed
self.dx_initial = self.dx
self.dy = 4 # initial fall rate
self.dx_max, self.dy_max = 12, 15 # max speed, max fall rate
# acceleration - player input
self.dx_movement = 2 # +/- applied when player moves
self.dy_jump = 35 # applied when player jumps
self.dx_wall_jump = 15 # +/- applied when player wall jumps
# acceleration - physics
self.dx_friction = 0.5 # applied every frame
self.dy_gravity = 4 # applied every frame
# misc.
self.touching_ground = False # for jumping
self.hit_wall_from = None # for wall jumping
self.conditions = {STUN: [], SLOW: [], SNARE: [], DOT: [], SILENCE: [], WOUNDED: [],
WEAKENED: [], SPEED: [], SHIELD: [], INVIGORATED: [], EMPOWERED: []}
# character stats
self.hit_points = self.hit_points_max = 100
self.energy = self.energy_max = 10
self.level = 10
self.current_exp = 0
# used for skill pickup
self.pickup_time = -1
self.pickup_slot = None
self.pickup_skill = None
self.overlapping_skill = None
# skills
self.attack_id = 1
self.skill1_id = self.skill2_id = self.skill3_id = self.ult_id = 0
# for debugging/testing:
# self.attack_id = random.randint(1,3)
# self.skill1_id = random.randint(100,115)
# self.skill2_id = random.randint(100,115)
# self.skill3_id = random.randint(100,115)
# self.ult_id = random.randint(1000,1003)
# specific testing:
self.attack_id = 10
self.skill1_id = 111
self.skill2_id = 102
self.skill3_id = 123
self.ult_id = 1005
# attacking
self.facing_direction = RIGHT if self.id == 1 else LEFT
self.facing_direction_initial = self.facing_direction
self.attack_cooldown_expired = True
self.new_particle = None
# scrolling text
self.st_buffer = []
# sprite information
self.state = STAND
self.previous_state = STAND
self.sprite = sprite
self.wait_frames = 0
self.animation_key = -1
self.attack_state = ONEHAND
self.attack_frame = 1
@property
def input(self):
if self.id == 1:
return GL.INPUT1
elif self.id == 2:
return GL.INPUT2
#play the skill's sound
def play_sound(self, path, i):
print(SKILLS_TABLE[i]['sound'])
if SKILLS_TABLE[i]['sound'] is not 'none' and AUDIO.sound_on:
global SOUNDS
sound = SOUNDS.get(path)
if sound == None:
sound = pygame.mixer.Sound(path)
SOUNDS[path] = sound
sound.play()
@property
def skills(self):
return [self.attack_id, self.skill1_id, self.skill2_id, self.skill3_id, self.ult_id]
def copy(self):
return Player(self.left, self.top, self.width, self.height)
def shield_trigger(self, damage_taken):
"""Handles how shield works
Call this after any damage is taken"""
if self.hit_points < self.hit_points_max and self.conditions[SHIELD]:
sorted(self.conditions[SHIELD], key=lambda k: k.remaining) # Will subtract from lowest remaining time shield first
for s in self.conditions[SHIELD]:
damage_taken = s.exchange(damage_taken)
if damage_taken == 0:
break
def is_dead(self):
return self.hit_points <= 0
def handle_exp(self,exp_gain,time):
if self.level < 10:
self.current_exp += exp_gain
if self.current_exp >= LEVEL_THRESHOLDS[self.level]:
self.level += 1
self.st_buffer.append((ST_LEVEL_UP,"LEVEL " + str(self.level)+ "!",time+1000 ))
def move_ip(self, dxdy):
super().move_ip(dxdy)
def distance_from(self, other):
a = self.centerx - other.centerx
b = self.centery - other.centery
return math.sqrt(a * a + b * b)
def __call__(self, arena_map):
self._handle_facing_direction()
if not self.conditions[STUN] and not self.conditions[SILENCE]:
self._handle_inputs(arena_map)
self._handle_acceleration()
self._handle_movement(arena_map)
self._determine_state()
def _handle_facing_direction(self):
if self.attack_cooldown_expired and not self.conditions[STUN]:
self.facing_direction = RIGHT if self.input.RIGHT \
else LEFT if self.input.LEFT \
else self.facing_direction
def _handle_acceleration(self):
def _apply_accel_left_right_input():
self.dx += self.dx_movement if self.input.RIGHT \
else -self.dx_movement if self.input.LEFT \
else 0
if self.input.RIGHT or self.input.LEFT:
self.pickup_time = -1
def _apply_friction():
self.dx += self.dx_friction if self.dx < 0 \
else -self.dx_friction if self.dx > 0 \
else 0
def _apply_accel_jump_input():
if self.input.JUMP:
self.pickup_time = -1
if not isinstance(self, Monster):
self.dy -= self.dy_jump if self.touching_ground or self.hit_wall_from \
else 0
if not self.touching_ground:
self.dx += self.dx_wall_jump if self.hit_wall_from == LEFT \
else -self.dx_wall_jump if self.hit_wall_from == RIGHT \
else 0
else:
if self.touching_ground:
self.dy -= self.dy_jump
#self.dy -= self.dy_jump if self.touching_ground \
# else 0
def _apply_gravity():
if -5 < self.dy < 8: # This helps make the jump arc smoother at the top
self.dy += self.dy_gravity * 0.5
else:
self.dy += self.dy_gravity
def _apply_limits():
self.dx = eval('{:+}'.format(self.dx)[0] + str(min(abs(self.dx), self.dx_max)))
self.dy = min(self.dy, self.dy_max)
self.dy = max(self.dy, -self.dy_jump)
if self.attack_cooldown_expired and not self.conditions[STUN]:
# These can only be used if not attacking
_apply_accel_left_right_input()
_apply_accel_jump_input()
_apply_friction()
_apply_gravity()
_apply_limits()
def _handle_movement(self, arena):
def _move():
self.ptop = self.top
self.pleft = self.left
if self.conditions[SNARE]:
self.move_ip((0, 0))
elif self.conditions[SLOW] or self.conditions[SPEED]:
max_slow = max(self.conditions[SLOW], key=lambda x: x.magnitude).magnitude if \
self.conditions[SLOW] else 0
max_speed = max(self.conditions[SPEED], key=lambda y: y.magnitude).magnitude if \
self.conditions[SPEED] else 0
delta = 1.0 + max_speed - max_slow
self.move_ip((self.dx * delta, (self.dy * delta) if self.dy < 0 else self.dy))
else:
self.move_ip((self.dx, self.dy))
def _check_for_collisions():
self.hit_wall_from, self.touching_ground = None, False # reset every frame
for terrain in arena.rects:
if not terrain.spawn_point:
# Check if touching ground
if (terrain.left < self.left < terrain.right or terrain.left < self.right < terrain.right) or (self.left < terrain.left < self.right or self.left < terrain.right < self.right):
if self.top < terrain.top < self.bottom:
if self.ptop + self.height - 5 <= terrain.top:
self.bottom = terrain.top
self.dy, self.touching_ground = 0, True
if isinstance(self, Monster):
self.hit_wall_from = False
self.touching_ground = True
if self.top < terrain.bottom < self.bottom and self.dy < 0:
if self.ptop >= terrain.bottom:
self.top = terrain.bottom
self.dy *= 0.4 # Prevents immediate drop when player hits ceiling
if (terrain.top < self.bottom < terrain.bottom or terrain.top < self.top < terrain.bottom) or (self.top < terrain.top < self.bottom or self.top < terrain.bottom < self.bottom):
if self.left < terrain.right < self.right and self.dx <= 0:
self.left = terrain.right
self.hit_wall_from = LEFT
self.dx = 0
# Sliding
if self.dy > 0 and not isinstance(self,Monster):
self.dy = 0
elif self.left < terrain.left < self.right and self.dx >= 0:
self.right = terrain.left
self.hit_wall_from = RIGHT
self.dx = 0
# Sliding
if self.dy > 0 and not isinstance(self,Monster):
self.dy = 0
def _check_for_skill_pick_ups(arena):
if not isinstance(self, Monster):
self.overlapping_skill = None
for skill in arena.dropped_skills:
if self.colliderect(skill):
# skill_type = get_skill_type(skill.id)
# if skill_type == WEAK:
# self.attack_id = skill.id
# elif skill_type == MEDIUM:
# n = random.choice((1, 2, 3))
# exec('self.skill{}_id = skill.id'.format(str(n)))
# elif skill_type == ULTIMATE:
# self.ult_id = skill.id
# arena.dropped_skills.remove(skill)
self.overlapping_skill = skill
_move() # move then check for collisions
_check_for_collisions()
_check_for_skill_pick_ups(arena)
out_of_arena_fix(self) # otherwise, player can jump up and over arena
# Handles attacks, skill buttons, and meditate
# If multiple pushed, priority is:
# ultimate > skill3 > skill2 > skill1 > attack > meditate
# Dropping skills and picking up skills can be handled here later on
def _handle_inputs(self, arena):
i, button = self._priority_inputs()
if self.input.DROP_SKILL and i != -1: # Drop skill pressed
self.__dict__[button] = 0 if button != ATTACKBUTTON else 1
elif not self.input.DROP_SKILL: # Drop skill not pressed
# If a valid skill is pressed
if (isinstance(i,str) or i > 0) and self.attack_cooldown_expired:
self.pickup_time = -1
if self.energy >= SKILLS_TABLE[i]['energy']:
self.attack_state = SKILLS_TABLE[i]['state']
self.attack_frame = SKILLS_TABLE[i]['frame']
if not(self.attack_state == RUN or self.attack_state == BREATH):
self.state = RESET
self.energy -= SKILLS_TABLE[i]['energy']
self.play_sound(SKILLS_TABLE[i]['sound'], i)
self.new_particle = SKILLS_TABLE[i]['start'](i, self, self.input.UP, self.input.DOWN)
if SKILLS_TABLE[i]['cooldown']:
self.attack_cooldown_expired = False
pygame.time.set_timer(TIME_TICK_EVENT + self.id, SKILLS_TABLE[i]['cooldown'])
if i == -1:
pygame.time.set_timer(PLAYER2_LOCK_EVENT + self.id, SKILLS_TABLE[-1]['cooldown'])
# If skill is 0, aka empty, and sitting over a skill
elif (i == 0 or i == 1) and self.overlapping_skill:
if button == ATTACKBUTTON and (0 < self.overlapping_skill.id < 99):
if self.overlapping_skill in arena.dropped_skills:
self.__dict__[ATTACKBUTTON] = self.overlapping_skill.id
arena.dropped_skills.remove(self.overlapping_skill)
elif button in (SKILL1BUTTON,SKILL2BUTTON, SKILL3BUTTON) and (100 <= self.overlapping_skill.id <= 999):
if self.overlapping_skill in arena.dropped_skills:
self.__dict__[button] = self.overlapping_skill.id
arena.dropped_skills.remove(self.overlapping_skill)
elif button == ULTBUTTON and (self.overlapping_skill.id >= 1000):
if self.overlapping_skill in arena.dropped_skills:
self.__dict__[ULTBUTTON] = self.overlapping_skill.id
arena.dropped_skills.remove(self.overlapping_skill)
# If hasnt tried to pick up skill
# print(self.pickup_time)
# if self.pickup_time == -1:
# print("flag1")
# self.pickup_time = 0
# self.pickup_slot = button
# self.pickup_skill = self.overlapping_skill
# print(self.pickup_skill.id)
# elif self.pickup_time != -1:
# if self.pickup_skill != self.overlapping_skill or button != self.pickup_slot:
# print("flag2")
# self.pickup_time = 0
# self.pickup_slot = button
# self.pickup_skill = self.overlapping_skill
# elif self.pickup_time == 9:
# print("flag3")
# self.pickup_time = 0
# self.__dict__[self.pickup_slot] = self.pickup_skill.id
# else:
# print("flag4")
# pygame.time.set_timer(USEREVENT+2+self.id, 250)
def _priority_inputs(self):
if self.input.ULT:
return self.ult_id, ULTBUTTON
elif self.input.SKILL3:
return self.skill3_id, SKILL3BUTTON
elif self.input.SKILL2:
return self.skill2_id, SKILL2BUTTON
elif self.input.SKILL1:
return self.skill1_id, SKILL1BUTTON
elif self.input.ATTACK:
return self.attack_id, ATTACKBUTTON
return -1, ''
def _determine_state(self):
"""Determines the player state to be used for animations"""
self.previous_state = self.state
if self.hit_points <= 0:
self.state = DEATH
elif not self.attack_cooldown_expired:
self.state = ATTACK # or cast
elif self.dy < 0:
self.state = JUMP
elif not self.touching_ground:
# if self.hit_wall_from:
# self.state = SLIDE
# Needed if we have slide animation
self.state = FALL
elif self.input.RIGHT:
self.state = RWALK
elif self.input.LEFT:
self.state = LWALK
else:
self.state = STAND
# -------------------------------------------------------------------------
class Monster(Player):
def __init__(self, info, topleft, player1, player2, color=ORANGE):
super().__init__(0, topleft, (info.w, info.h))
self.dx_max, self.dy_max = info.dx, info.dy
self.hit_points = self.hit_points_max = info.hp
self.chasing_time = info.chase
self.idle_time = info.idle
self.dx_friction, self.dy_gravity, self.dy_jump = 0.5, 2, 30
self.p1, self.p2 = player1, player2
self.target, self.status = None, IDLE
self.last_status_change = 0
self.ai_input = AI_Input()
self.color = color
self.kind = info.kind
self.dmg = info.dmg
self.exp_value = info.exp_value
self.has_hit = []
self.has_hit_time = []
self.hit_by = {1: False, 2: False}
self.hit_by_time = {1: 0, 2: 0}
@property
def input(self):
return self.ai_input
def _pick_new_target(self):
d1 = self.distance_from(self.p1)
d2 = self.distance_from(self.p2)
if d1 != d2 and random.randint(1, 50) <= 30:
if d1 > d2:
self.target = self.p1
elif d2 < d2:
self.target = self.p2
else:
if random.randint(1, 2) == 1:
self.target = self.p1
else:
self.target = self.p2
def _handle_last_hit(self,time):
for i,v in enumerate(self.has_hit_time):
if (v+2000) <= time:
del self.has_hit[i]
del self.has_hit_time[i]
if self.hit_by_time[1] + 2000 <= time:
self.hit_by[1] = False
if self.hit_by_time[2] + 2000 <= time:
self.hit_by[2] = False
def _switch_mode(self, time):
time_spent_in_status = time - self.last_status_change
if self.status == CHASING and time_spent_in_status > self.chasing_time:
self.last_status_change = time
self.status = IDLE
elif self.status == IDLE and time_spent_in_status > self.idle_time:
self.last_status_change = time
self.status = CHASING
self._pick_new_target()
def _ai(self, time):
self._switch_mode(time)
if self.status == CHASING and self.target is not None:
self.input.refresh()
if self.target.centerx >= self.centerx:
self.input.RIGHT = True
else:
self.input.LEFT = True
if self.target.centery < self.centery:
if random.randint(1, 50) == 1:
self.input.JUMP = True
else:
self.input.JUMP = False
if random.randint(1, 30) < 5:
if self.input.RIGHT:
self.input.RIGHT = False
self.input.LEFT = True
else:
self.input.RIGHT = True
self.input.LEFT = False
if random.randint(1, 100) == 2:
self.input.JUMP = True
def __call__(self, time, arena_map):
self._handle_last_hit(time)
self._ai(time)
self._handle_facing_direction()
self._handle_acceleration()
self._handle_movement(arena_map)
def on_hit(self, target, time):
if target not in self.has_hit:
if self.kind != ULTIMATE and not self.hit_by[target.id]:
self.has_hit.append(target)
self.has_hit_time.append(time)
handle_damage(target, self.dmg, time)
elif self.kind == ULTIMATE:
self.has_hit.append(target)
self.has_hit_time.append(time)
handle_damage(target, self.dmg, time)
# -------------------------------------------------------------------------
class AI_Input():
def __init__(self):
self.RIGHT = False
self.LEFT = False
self.JUMP = False
def refresh(self):
self.RIGHT = self.LEFT = self.JUMP = False
# -------------------------------------------------------------------------
class Arena:
def __init__(self, arena_info):
self.background = arena_info.background
self.max_monsters = arena_info.max_monsters
self.possible_monsters = tuple(MONSTER_TABLE.keys()) if arena_info.possible_monsters == ALL \
else arena_info.possible_monsters
self.floor = Rect2(0, arena_info.floor_y, 1280, 50, color=None)
self.left_wall = Rect2(0, 0, arena_info.left_wall_x, 600, color=None)
self.right_wall = Rect2(arena_info.right_wall_x, 0, 1280 - arena_info.right_wall_x, 600, color=None)
play_area_color = SKYBLUE if arena_info.background is None else None
play_area = Rect2(self.left_wall.right, 0, self.right_wall.left - self.left_wall.right, self.floor.top, color=play_area_color)
self.p1_spawn = (arena_info.p1_spawn[0] + play_area.left, arena_info.p1_spawn[1])
self.p2_spawn = (arena_info.p2_spawn[0] + play_area.left, arena_info.p1_spawn[1])
platforms = [Rect2(tuple(terr)[0:4], color=terr.color, hits_to_destroy=terr.hits_to_destroy, spawn_point=terr.spawn_point) for terr in arena_info.platforms]
rects = [play_area, self.floor, self.left_wall, self.right_wall] + platforms
for rect in rects[4:]: # don't shift the first 4 rects
rect.move_ip((play_area.left, 0)) # to account for play area starting 65 pixels from left
self.play_area_rect = rects[0]
self.rects = rects[1:]
self.dropped_skills = []
def __iter__(self):
# currently only time iteration is used is when the rects are drawn
for rect in [self.play_area_rect] + self.rects:
yield rect
@property
def spawn_points(self):
return filter(lambda x: x.spawn_point, self)
@property
def destructible_terrain(self):
return filter(lambda x: x.hits_to_destroy > 0, self)
@property
def random_spawn_point(self):
return random.choice(list(self.spawn_points))
# -------------------------------------------------------------------------
class Particle(Rect2):
def __init__(self, sid, player):
self.sid = sid
self.left = 0
self.top = 0
self.width = SKILLS_TABLE[sid]['width']
self.height = SKILLS_TABLE[sid]['height']
self.cooldown = SKILLS_TABLE[sid]['cooldown']
self.duration = SKILLS_TABLE[sid]['duration']
self.color = SKILLS_TABLE[sid]['color']
self.spawn_time = 0
self.expired = False
self.dmg = SKILLS_TABLE[sid]['dmg']
self.energy = SKILLS_TABLE[sid]['energy']
self.belongs_to = player
self.conditions = []
self.on_hit_f = None
self.on_expire_f = None
self.on_terrain_f = None
self.persistent_f = None
self.special_f = None
if 'conditions' in SKILLS_TABLE[sid].keys():
for c in SKILLS_TABLE[sid]['conditions']:
self.conditions.append(c)
if 'on_hit_f' in SKILLS_TABLE[sid].keys():
self.on_hit_f = SKILLS_TABLE[sid]['on_hit_f']
if 'on_expire_f' in SKILLS_TABLE[sid].keys():
self.on_expire_f = SKILLS_TABLE[sid]['on_expire_f']
if 'on_terrain_f' in SKILLS_TABLE[sid].keys():
self.on_terrain_f = SKILLS_TABLE[sid]['on_terrain_f']
if 'persistent_f' in SKILLS_TABLE[sid].keys():
self.persistent_f = SKILLS_TABLE[sid]['persistent_f']
if 'special_path' in SKILLS_TABLE[sid].keys():
self.special_f = SKILLS_TABLE[sid]['special_path']
# -------------------------------------------------------------------------
class MeleeParticle(Particle):
def __init__(self, sid, player):
# super().__init__(particle.width, particle.height, particle.radius, particle.cooldown, particle.duration, particle.color)
super().__init__(sid, player)
self.arc = self.progress = SKILLS_TABLE[sid]['arc']
self.radius = SKILLS_TABLE[sid]['start_radius']
self.max_radius = SKILLS_TABLE[sid]['max_radius']
self.has_hit = [] # Need this to keep track of what it has hit;
# melee particles are not delete upon hitting
# a target, so we need to know who it has hit
# to prevent the same target being hit multiple
# times
self.has_hit_time = []
self.extend = SKILLS_TABLE[sid]['extend']
self.dradius = (self.max_radius - self.radius)*35/self.duration
self.direction = player.facing_direction
# def update(self, time, player):
# Let the particle know how it belongs to so it can
# rotate around that player and also in collision
# detection, will not hit the player who made particle
def update(self, time):
if self.spawn_time == 0:
self.spawn_time = time
for i,v in enumerate(self.has_hit_time):
if (v+1000) <= time:
del self.has_hit[i]
del self.has_hit_time[i]
elapsed_time = time - self.spawn_time
self.expired = (elapsed_time >= self.duration)
r = (elapsed_time / self.duration)
self.progress = (1-r)*self.arc
if self.special_f:
self.centerx, self.centery = self.special_f(self,time)
else:
if self.extend:
self.width += self.dradius
self.radius += self.dradius/2
else:
self.radius += self.dradius
if self.direction == RIGHT:
self.centerx = self.belongs_to.centerx + self.radius * math.cos((1 - r) * self.arc)
else:
self.centerx = self.belongs_to.centerx - self.radius * math.cos((1 - r) * self.arc)
self.centery = self.belongs_to.centery - 10 - self.radius * math.sin((1 - r) * self.arc)
if self.persistent_f:
self.persistent_f(self,time)
def on_hit(self, target, time): # DON'T delete time; will use later
if target != self.belongs_to and target not in self.has_hit:
self.has_hit.append(target)
self.has_hit_time.append(time)
handle_damage(target, self.dmg, time)
for c in self.conditions:
c.begin(time, target)
# On hitting monster, small pushback
if isinstance(target, Monster):
target.centerx += -10 * target.dx
target.dx *= -1
target.hit_by[self.belongs_to.id] = True
target.hit_by_time[self.belongs_to.id] = time
target.last_hit_by = self.belongs_to
if self.on_hit_f:
self.on_hit_f(self,target,time)
# -------------------------------------------------------------------------
class RangeParticle(Particle):
def __init__(self, sid, player, up, down):
super().__init__(sid,player)
self.has_special = False
self.direction = player.facing_direction
self.originx = player.centerx # Where the particle started
self.originy = player.centery # These might be useful later on
# If has special path, upload function to special_f
self.dx = SKILLS_TABLE[sid]['speed']
self.ddx = SKILLS_TABLE[sid]['acceleration']
# if player pressed up
if up:
self.dy = SKILLS_TABLE[sid]['speed'] * -1
self.ddy = SKILLS_TABLE[sid]['acceleration'] * -1
elif down:
self.dy = SKILLS_TABLE[sid]['speed']
self.ddy = SKILLS_TABLE[sid]['acceleration']
elif not up and not down:
self.dy = 0
self.ddy = 0
# initial position
if player.facing_direction == RIGHT:
self.centerx = player.centerx + 40
else:
self.centerx = player.centerx - 40
self.centery = player.centery
if self.direction == RIGHT:
self.centerx -= 30
else:
self.centerx += 30
self.dx *= -1
self.ddx *= -1
def update(self, time):
if self.spawn_time == 0:
self.spawn_time = time
elapsed_time = time - self.spawn_time
self.expired = (elapsed_time >= self.duration)
if self.special_f:
self.centerx,self.centery = self.special_f(self,time)
else:
self.dx += self.ddx
self.dy += self.ddy
self.centerx += self.dx
self.centery += self.dy
if self.persistent_f:
self.persistent_f(self,time)
def on_hit(self, target, time): # DONT delete time; will use later
if target != self.belongs_to:
handle_damage(target, self.dmg, time)
for c in self.conditions:
c.begin(time, target)
# On hitting monster, small pushback
if isinstance(target, Monster):
target.centerx += -5 * target.dx
target.dx *= -1
target.hit_by[self.belongs_to.id] = True
target.hit_by_time[self.belongs_to.id] = time
target.last_hit_by = self.belongs_to
if self.on_hit_f:
self.on_hit_f(self, target, time)
# -------------------------------------------------------------------------
class FieldParticle(Particle):
def __init__(self, sid, player):
super().__init__(sid, player)
self.radius = SKILLS_TABLE[sid]['radius']
self.frequency = None
self.persistent_pulse_f = None #(particle, time, target)
self.originx = self.centerx = player.centerx
self.originy = self.centery = player.centery
self.has_hit = [] # Need this to keep track of what it has hit;
# melee particles are not delete upon hitting
# a target, so we need to know who it has hit
# to prevent the same target being hit multiple
# times
self.has_hit_time = []
if 'persistent_pulse_f' in SKILLS_TABLE[sid].keys():
self.persistent_pulse_f = SKILLS_TABLE[sid]['persistent_pulse_f']
if 'frequency' in SKILLS_TABLE[sid].keys():
self.frequency = SKILLS_TABLE[sid]['frequency']
def update(self, time):
if self.spawn_time == 0:
self.spawn_time = time
for i,v in enumerate(self.has_hit_time):
if (v+200) <= time:
del self.has_hit[i]
del self.has_hit_time[i]
elapsed_time = time - self.spawn_time
self.expired = (elapsed_time >= self.duration)
if self.persistent_f:
self.persistent_f(self,time)
def is_in_field(self, target):
return target.distance_from(self) <= self.radius
def on_hit(self, target, time):
# If pulse function exists
if target != self.belongs_to and self.frequency and target not in self.has_hit:
self.has_hit.append(target)
self.has_hit_time.append(time)
# On pulse
if (time - self.spawn_time)%self.frequency == 0:
handle_damage(target, self.dmg, time)
for c in self.conditions:
c.begin(time, target)
if isinstance(target, Monster):
target.centerx += -5 * target.dx
target.dx *= -1
target.hit_by[self.belongs_to.id] = True
target.hit_by_time[self.belongs_to.id] = time
target.last_hit_by = self.belongs_to
if self.on_hit_f:
self.on_hit_f(self, target, time)
# If persistent pulse function exists
if target != self.belongs_to and self.persistent_pulse_f:
self.peristent_pulse_f(self,target,time)
# -------------------------------------------------------------------------
class GameTime:
def __init__(self):
self.qsec = 0
def __call__(self):
return self.qsec
def inc(self):
self.qsec += 1
@property
def msec(self):
return self.qsec * 250
def as_seconds(self):
return self.qsec / 4
def as_half_seconds(self):
return self.qsec / 2
def as_quarter_seconds(self):
return self.qsec
def __str__(self):
sec = self.qsec / 4
return '{:>2}:{:0>2}'.format(str(int(sec / 60)), str(int(sec % 60)))
# ------------------------------------------------------------------------
class Condition:
def __init__(self, duration):
self.start = -1
self.duration = duration
def begin(self, time, target):
c = copy.copy(self)
c.start = time
c.target = target
target.conditions[c.type].append(c)
def is_expired(self,time):
if self.start == -1:
self.start = time
return self.duration <= (time - self.start)
# ---Debuffs-----------------------------------------------------------------
class Stun(Condition):
def __init__(self, duration):
super().__init__(duration)
self.type = STUN
class Slow(Condition):
# Magnitude = 0 to 1
def __init__(self, duration, magnitude):
super().__init__(duration)
self.magnitude = magnitude
self.type = SLOW
class Snare(Condition):
def __init__(self, duration):
super().__init__(duration)
self.type = SNARE
class Dot(Condition):
# Magnitude = Dot flat dmg value
# Frequency = Every x seconds; make frequency a factor of 250 ms
def __init__(self, magnitude, ticks, frequency):
super().__init__(ticks * frequency)
self.magnitude = magnitude
self.frequency = frequency
self.last_tick = self.start
self.ticks = ticks
self.type = DOT
def begin(self, time, target):
c = copy.copy(self)
c.start = time
c.target = target
c.last_tick = time
target.conditions[c.type].append(c)
def is_expired(self, time):
t = time - self.last_tick
if t >= self.frequency:
self.last_tick = time
handle_damage(self.target, self.magnitude, time)
self.ticks -= 1
return self.ticks <= 0
class Silence(Condition):
def __init__(self, duration):
super().__init__(duration)
self.type = SILENCE
class Wounded(Condition):
"""Reduces HP regen"""
def __init__(self, duration):
super().__init__(duration)
self.type = WOUNDED
class Weakened(Condition):
"""Reduces Energy regen"""
def __init__(self, duration):
super().__init__(duration)
self.type = WEAKENED
# ---Buffs-------------------------------------------------------------------
class Speed(Condition):
def __init__(self, duration, magnitude):
super().__init__(duration)
self.type = SPEED
self.magnitude = magnitude
class Shield(Condition):
def __init__(self, duration, magnitude):
super().__init__(duration)
self.magnitude = magnitude
self.type = SHIELD
self.remaining = self.duration # used for sorting
def is_expired(self,time):
if self.start == -1:
self.start = time
self.remaining = self.duration - time - self.start
if self.duration <= (time - self.start):
return True
elif self.magnitude <= 0:
return True
return False
def exchange(self,damage_taken):
if self.magnitude > 0:
if self.magnitude > damage_taken:
self.target.hit_points += damage_taken
self.magnitude -= damage_taken
return 0
elif self.magnitude <= damage_taken:
self.target.hit_points += self.magnitude
temp = damage_taken - self.magnitude
self.magnitude -= damage_taken
return temp
class Invigorated(Condition):
"""Increases HP regen"""
def __init__(self, duration):
super().__init__(duration)
self.type = INVIGORATED
class Empowered(Condition):
"""Increases energy regen"""
def __init__(self, duration):
super().__init__(duration)
self.type = EMPOWERED