-
Notifications
You must be signed in to change notification settings - Fork 7
/
game.gd
1655 lines (1432 loc) · 47.3 KB
/
game.gd
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
extends Node2D
class_name Game
const GHOST_FRAMES = 90
const SUPER_FREEZE_TICKS = 20
const GHOST_ACTIONABLE_FREEZE_TICKS = 10
const CAMERA_MAX_Y_DIST = 210
const QUITTER_FOCUS_TICKS = 60
const CLASH_DAMAGE_DIFF = 25
const CAMERA_PADDING = 20
export(int) var char_distance = 200
export(int) var stage_width = 1100
export(int) var max_char_distance = 600
export(int) var time = 3000
signal player_actionable()
signal player_actionable_network()
signal simulation_continue()
signal playback_requested()
signal game_ended()
signal game_won(winner)
signal ghost_finished()
signal make_afterimage()
signal ghost_my_turn()
signal forfeit_started(id)
signal actions_submitted()
signal turn_started()
signal zoom_changed()
var p1_data
var p2_data
var p1_turn = false
var p2_turn = false
onready var camera: GoodCamera = $Camera2D
onready var objects_node = $Objects
onready var fx_node = $Fx
var mouse_pressed = false
var current_tick = -1
var max_replay_tick = 0
var game_started = false
var undoing = false
var singleplayer = false
var parry_freeze = false
var clashing_enabled = false
var game_paused = false
var buffer_playback = false
var buffer_edit = false
var game_end_tick = 0
var frame_passed = false
var game_finished = false
var ghost_cleaned = true
var asymmetrical_clashing = false
var forfeit = false
var quitter_focus = false
var quitter_focus_ticks = 0
var advance_frame_input = false
var frame_by_frame = false
var network_simulate_ready = true
var gravity_enabled = true
var is_ghost = false
var is_afterimage = false
var ghost_hidden = false
var ghost_game
var ghost_speed = 3
var ghost_tick = 0
var forfeit_player = null
var match_data = null
var simulated_once = false
var started_multiplayer = false
var prediction_enabled = true
var p1 = null
var p2 = null
var p1_username = null
var p2_username = null
var my_id = 1
var draw_stage = true
var snapping_camera = false
var waiting_for_player_prev = false
var spectate_tick = -1
var global_gravity_modifier = "1.0"
var camera_snap_position = Vector2()
var objects: Array = []
var objs_map = {
}
var effects: Array = []
var drag_position = null
var real_tick = 0
var super_freeze_ticks = 0
var super_active = false
var prediction_effect = false
var p1_super = false
var p2_super = false
var hit_freeze = false
var ghost_freeze = false
var player_actionable = true
var network_sync_tick = -100
var ceiling_height = 400
var has_ceiling = true
var mouse_position = Vector2()
var ghost_simulated_ticks = 0
var is_in_replay = false
var ghost_actionable_freeze_ticks = 0
var ghost_p1_actionable = false
var ghost_p2_actionable = false
var made_afterimage = false
var p1_ghost_ready_tick
var p2_ghost_ready_tick
var spectating = false
var ghost_time = 0.0
var camera_zoom = 1.0
#var has_ghost_frozen_yet = false
func get_ticks_left():
return time - Utils.int_min(current_tick, time)
func _ready():
if is_ghost:
hide()
for object in objects_node.get_children():
object.free()
for fx in fx_node.get_children():
fx.free()
$GhostStartTimer.start()
ghost_time = Time.get_unix_time_from_system()
else:
emit_signal("simulation_continue")
func _spawn_particle_effect(particle_effect: PackedScene, pos: Vector2, dir= Vector2.RIGHT):
var obj = particle_effect.instance()
add_child(obj)
obj.tick()
var facing = -1 if dir.x < 0 else 1
obj.position = pos
if facing < 0:
obj.rotation = (dir * Vector2(-1, -1)).angle()
else:
obj.rotation = dir.angle()
obj.scale.x = facing
remove_child(obj)
on_particle_effect_spawned(obj)
func connect_signals(object):
object.connect("object_spawned", self, "on_object_spawned")
object.connect("particle_effect_spawned", self, "on_particle_effect_spawned")
func copy_to(game: Game):
if !game_started:
return
# var p1_pos = p1.get_pos()
# var p2_pos = p2.get_pos()
# print(p1_pos)
# print(p2_pos)
# game.p1.set_pos(p1_pos.x, p1_pos.y)
# game.p2.set_pos(p2_pos.x, p2_pos.y)
p1.chara.copy_to(game.p1.chara)
p2.chara.copy_to(game.p2.chara)
game.p1.update_data()
game.p2.update_data()
p1.copy_to(game.p1)
p2.copy_to(game.p2)
game.p1.hp = p1.hp
game.p2.hp = p2.hp
clean_objects()
for object in game.objects:
object.free()
for fx in game.effects:
fx.free()
for object in objects:
if is_instance_valid(object):
if !object.disabled:
var new_obj = load(object.filename).instance()
game.on_object_spawned(new_obj)
object.copy_to(new_obj)
else:
game.objs_map[str(game.objs_map.size() + 1)] = null
game.camera.limit_left = camera.limit_left
game.camera.limit_right = camera.limit_right
func _on_super_started(ticks, player):
if is_ghost:
return
if ticks == 0:
ticks = 0
var state = player.current_state()
if state.get("super_freeze_ticks") != null:
if state.super_freeze_ticks > ticks:
ticks = state.super_freeze_ticks
super_freeze_ticks = ticks
super_active = true
if player == p1:
p1_super = true
if player == p2:
p2_super = true
func get_screen_position(player_id):
var screen_center = camera.get_camera_screen_center()
var player_position = get_player(player_id).get_center_position_float()
var result = player_position - screen_center
return result / camera.zoom.x
func get_player(id) -> Fighter:
if id == 1:
return p1
if id == 2:
return p2
return null
func on_particle_effect_spawned(fx: ParticleEffect):
if ReplayManager.resimulating:
fx.queue_free()
return
effects.append(fx)
fx_node.add_child(fx)
fx.connect("tree_exited", self, "_on_fx_exit_tree", [fx])
func on_object_spawned(obj: BaseObj):
objects.append(obj)
objects_node.add_child(obj)
obj.has_ceiling = has_ceiling
obj.ceiling_height = ceiling_height
obj.obj_name = str(objs_map.size() + 1)
obj.logic_rng = BetterRng.new()
obj.logic_rng_static = BetterRng.new()
var seed_ = hash(match_data.seed + (objs_map.size() + 1))
obj.logic_rng.seed = seed_
obj.logic_rng_seed = seed_
obj.logic_rng_static.seed = match_data.seed
obj.logic_rng_static_seed = match_data.seed
objs_map[obj.obj_name if obj.obj_name else obj.name] = obj
obj.objs_map = objs_map
obj.connect("tree_exited", self, "_on_obj_exit_tree", [obj])
obj.connect("hitbox_refreshed", self, "on_hitbox_refreshed")
obj.connect("global_hitlag", self, "on_global_hitlag")
obj.gravity_enabled = gravity_enabled
obj.set_gravity_modifier(global_gravity_modifier)
obj.fighter_owner = get_player(obj.id)
obj.update_data()
for particle in obj.particles.get_children():
effects.append(particle)
connect_signals(obj)
func _on_fx_exit_tree(fx):
effects.erase(fx)
func _on_obj_exit_tree(obj):
objects.erase(obj)
func on_hitbox_refreshed(hitbox_name):
p1.parried_hitboxes.erase(hitbox_name)
p2.parried_hitboxes.erase(hitbox_name)
pass
func on_clash():
super_freeze_ticks = 5
parry_freeze = true
pass
func on_parry():
super_freeze_ticks = 10
parry_freeze = true
func on_block():
super_freeze_ticks = 7
parry_freeze = true
func on_global_hitlag(amount):
if is_ghost:
return
super_freeze_ticks = amount
parry_freeze = true
hit_freeze = true
func forfeit(id):
if forfeit:
return
forfeit = true
get_player(id).forfeit()
get_player((id % 2) + 1).on_action_selected("Continue", null, null)
emit_signal("forfeit_started", id)
quitter_focus = true
forfeit_player = get_player(id)
quitter_focus_ticks = QUITTER_FOCUS_TICKS
func start_game(singleplayer: bool, match_data: Dictionary):
self.match_data = match_data
if match_data.has("spectating"):
spectating = match_data.spectating
if is_ghost:
spectating = false
if Global.name_paths.has(match_data.selected_characters[1]["name"]):
p1 = load(Global.name_paths[match_data.selected_characters[1]["name"]]).instance()
else:
return false
if Global.name_paths.has(match_data.selected_characters[2]["name"]):
p2 = load(Global.name_paths[match_data.selected_characters[2]["name"]]).instance()
else:
return false
p1.connect("parried", self, "on_parry")
p2.connect("parried", self, "on_parry")
p1.connect("clashed", self, "on_clash")
p2.connect("clashed", self, "on_clash")
# p1.connect("blocked", self, "on_block")
# p2.connect("blocked", self, "on_block")
p1.connect("predicted", self, "on_prediction", [p1])
p2.connect("predicted", self, "on_prediction", [p2])
stage_width = Utils.int_clamp(match_data.stage_width, 100, 50000)
if match_data.has("game_length"):
time = match_data["game_length"]
if match_data.has("frame_by_frame"):
frame_by_frame = match_data.frame_by_frame
if match_data.has("char_distance"):
char_distance = match_data["char_distance"]
if match_data.has("clashing_enabled"):
clashing_enabled = match_data["clashing_enabled"]
if match_data.has("asymmetrical_clashing"):
asymmetrical_clashing = match_data["asymmetrical_clashing"]
if match_data.has("global_gravity_modifier"):
global_gravity_modifier = match_data["global_gravity_modifier"]
if match_data.has("has_ceiling"):
has_ceiling = match_data["has_ceiling"]
if match_data.has("ceiling_height"):
ceiling_height = match_data["ceiling_height"]
if match_data.has("prediction_enabled"):
prediction_enabled = match_data["prediction_enabled"]
p1.has_ceiling = has_ceiling
p2.has_ceiling = has_ceiling
p1.ceiling_height = ceiling_height
p2.ceiling_height = ceiling_height
# if !is_ghost:
# print("seed: ", match_data.seed)
p1.name = "P1"
p2.name = "P2"
p1.logic_rng = BetterRng.new()
p2.logic_rng = BetterRng.new()
p1.logic_rng_static = BetterRng.new()
p2.logic_rng_static = BetterRng.new()
p1.logic_rng.seed = hash(match_data.seed)
p1.logic_rng_seed = hash(match_data.seed)
p2.logic_rng.seed = hash(match_data.seed + 1)
p2.logic_rng_seed = hash(match_data.seed + 1)
p1.logic_rng_static.seed = hash(match_data.seed)
p1.logic_rng_static_seed = hash(match_data.seed)
p2.logic_rng_static.seed = hash(match_data.seed + 1)
p2.logic_rng_static_seed = hash(match_data.seed + 1)
p2.id = 2
p1.is_ghost = is_ghost
p2.is_ghost = is_ghost
p1.set_gravity_modifier(global_gravity_modifier)
p2.set_gravity_modifier(global_gravity_modifier)
if !is_ghost:
Global.current_game = self
for value in match_data:
for player in [p1, p2]:
if player.get(value) != null:
player.set(value, match_data[value])
$Players.add_child(p1)
$Players.add_child(p2)
p1.set_color(Color("aca2ff"))
p2.set_color(Color("ff7a81"))
p1.init()
p2.init()
if match_data.has("selected_styles"):
var style1 = match_data.selected_styles[1]
var style2 = match_data.selected_styles[2]
# if Custom.can_use_style(1, style1):
if is_ghost or Custom.can_use_style(1, style1):
p1.apply_style(style1)
# if Custom.can_use_style(2, style1):
if is_ghost or Custom.can_use_style(2, style1):
p2.apply_style(style2)
if match_data.has("gravity_enabled"):
gravity_enabled = match_data.gravity_enabled
p1.gravity_enabled = match_data.gravity_enabled
p2.gravity_enabled = match_data.gravity_enabled
p1.connect("undo", self, "set", ["undoing", true])
p2.connect("undo", self, "set", ["undoing", true])
p1.connect("super_started", self, "_on_super_started", [p1])
p2.connect("super_started", self, "_on_super_started", [p2])
p1.connect("global_hitlag", self, "on_global_hitlag")
p2.connect("global_hitlag", self, "on_global_hitlag")
connect_signals(p1)
connect_signals(p2)
objs_map = {
"P1": p1,
"P2": p2,
}
p1.objs_map = objs_map
p2.objs_map = objs_map
snapping_camera = true
self.singleplayer = singleplayer
if singleplayer:
if match_data["p2_dummy"]:
p2.dummy = true
pass
elif !is_ghost:
Network.game = self
if !singleplayer:
started_multiplayer = true
if Network.multiplayer_active:
p1_username = Network.pid_to_username(1)
p2_username = Network.pid_to_username(2)
my_id = Network.player_id
current_tick = -1
if !is_ghost:
if ReplayManager.playback:
get_max_replay_tick()
elif !match_data.has("replay"):
ReplayManager.init()
else:
get_max_replay_tick()
if ReplayManager.frames[1].size() > 0 or ReplayManager.frames[2].size() > 0:
ReplayManager.playback = true
var height = 0
if match_data.has("char_height"):
height = -match_data.char_height
p1.set_pos(-char_distance, height)
p2.set_pos(char_distance, height)
p1.stage_width = stage_width
p2.stage_width = stage_width
if stage_width >= 320:
camera.limit_left = -stage_width - CAMERA_PADDING
camera.limit_right = stage_width + CAMERA_PADDING
# p1.set_pos(0, 0)
# p2.set_pos(0, -100)
p1.opponent = p2
p2.opponent = p1
p2.set_facing(-1)
p1.update_data()
p2.update_data()
p1_data = p1.data
p2_data = p2.data
apply_hitboxes([p1,p2])
if !ReplayManager.resimulating:
show_state()
if ReplayManager.playback and !ReplayManager.resimulating and !is_ghost:
yield(get_tree().create_timer(0.5 if !ReplayManager.replaying_ingame else 0.25), "timeout")
game_started = true
if !is_ghost:
if SteamLobby.is_fighting():
SteamLobby.on_match_started()
if match_data.has("starting_meter"):
var meter_amount = p1.fixed.round(p1.fixed.mul(str(Fighter.MAX_SUPER_METER), match_data.starting_meter))
p1.gain_super_meter(meter_amount)
p2.gain_super_meter(meter_amount)
func on_prediction(ticks=7, player=null):
_on_super_started(ticks, player)
prediction_effect = true
pass
func update_data():
p1.update_data()
p2.update_data()
p1_data = p1.data
p2_data = p2.data
func get_max_replay_tick():
# if spectating:
# max_replay_tick = spectate_tick
# if max_replay_tick >= current_tick:
# ReplayManager.playback = true
# return
max_replay_tick = 0
for tick in ReplayManager.frames[1].keys():
if tick > max_replay_tick:
max_replay_tick = tick
for tick in ReplayManager.frames[2].keys():
if tick > max_replay_tick:
max_replay_tick = tick
return max_replay_tick
func clean_objects():
var invalid_objects = []
for object in objects:
if !is_instance_valid(object):
invalid_objects.append(object)
for object in invalid_objects:
objects.erase(object)
func initialize_objects():
for object in objects:
if !object.initialized:
object.init()
func process_fx():
for fx in effects:
if is_instance_valid(fx):
fx.tick()
func tick():
if is_ghost and not prediction_enabled:
return
if quitter_focus and quitter_focus_ticks > 0:
if (QUITTER_FOCUS_TICKS - quitter_focus_ticks) % 10 == 0:
if forfeit_player:
forfeit_player.toggle_quit_graphic()
quitter_focus_ticks -= 1
return
else :
if forfeit_player:
forfeit_player.toggle_quit_graphic(false)
quitter_focus = false
frame_passed = true
if not singleplayer:
if not is_ghost:
Network.reset_action_inputs()
clean_objects()
for object in objects:
if object.disabled:
continue
if not object.initialized:
object.init()
object.tick()
var pos = object.get_pos()
if pos.x < - stage_width:
object.set_pos( - stage_width, pos.y)
elif pos.x > stage_width:
object.set_pos(stage_width, pos.y)
if has_ceiling and pos.y <= - ceiling_height:
object.set_y( - ceiling_height)
object.on_hit_ceiling()
process_fx()
current_tick += 1
p1.current_tick = current_tick
p2.current_tick = current_tick
p1.lowest_tick = - 1
p2.lowest_tick = - 1
var players = resolve_port_priority()
players[0].tick_before()
players[1].tick_before()
players[0].update_advantage()
players[1].update_advantage()
players[0].tick()
players[1].tick()
resolve_same_x_coordinate()
initialize_objects()
p1_data = p1.data
p2_data = p2.data
resolve_collisions(players[0], players[1])
apply_hitboxes(players)
p1_data = p1.data
p2_data = p2.data
if (p1.state_interruptable or p1.dummy_interruptable) and not p1.busy_interrupt:
p2.reset_combo()
if (p2.state_interruptable or p2.dummy_interruptable) and not p2.busy_interrupt:
p1.reset_combo()
if is_ghost:
if not ghost_hidden:
if not visible and current_tick >= 0:
show()
return
if not game_finished:
if ReplayManager.playback:
if not ReplayManager.resimulating:
is_in_replay = true
if current_tick > max_replay_tick and not (ReplayManager.frames.has("finished") and ReplayManager.frames.finished):
ReplayManager.set_deferred("playback", false)
else :
if current_tick > (ReplayManager.resim_tick if ReplayManager.resim_tick >= 0 else max_replay_tick - 2):
if not Network.multiplayer_active:
ReplayManager.playback = false
ReplayManager.resimulating = false
camera.reset_shake()
else :
ReplayManager.frames.finished = true
if should_game_end():
if started_multiplayer:
if not ReplayManager.playback:
Network.autosave_match_replay(match_data, p1_username, p2_username)
end_game()
var priorities = [
funcref(self,"state_priority"),
funcref(self,"comboing"),
funcref(self,"attacks"),
funcref(self,"lower_sadness"),
funcref(self,"forward_movement"),
funcref(self,"lower_health")
]
func resolve_port_priority(id=false):
var priority = 0
var p1_state = p1.current_state()
var p2_state = p2.current_state()
for p in priorities:
priority = p.call_func(p1_state,p2_state)
if(priority>0):
break
priority = max(1,priority)
return ([p1,p2] if priority==1 else [p2,p1]) if !id else id
func state_priority(p1_state, p2_state):
if p1_state.tick_priority < p2_state.tick_priority:
return 1
if p2_state.tick_priority < p1_state.tick_priority:
return 2
return 0
func comboing(p1_state,p2_state):
if(p1_state.is_hurt_state):
return 2
if(p2_state.is_hurt_state):
return 1
return 0
func attacks(p1_state,p2_state):
var p1_hitboxes = []
var p2_hitboxes = []
for c in p1_state.get_children():
if(c is Hitbox):
p1_hitboxes.append(c)
for c in p2_state.get_children():
if(c is Hitbox):
p2_hitboxes.append(c)
if(p1_hitboxes.size()==0 and p2_hitboxes.size()==0):
return 0
if(p1_hitboxes.size()==0):
return 2
if(p2_hitboxes.size()==0):
return 1
var p1_start_tick = 999
var p2_start_tick = 999
var p1_damage = 0
var p2_damage = 0
for h in p1_hitboxes:
if(h.start_tick<p1_start_tick):
p1_start_tick = h.start_tick
p1_damage = h.damage
for h in p2_hitboxes:
if(h.start_tick<p2_start_tick):
p2_start_tick = h.start_tick
p2_damage = h.damage
if(p1_start_tick!=p2_start_tick):
return 1 if p1_start_tick>p2_start_tick else 2
if(p1_damage!=p2_damage):
return 1 if p1_damage>p2_damage else 2
return 0
func lower_sadness(_1,_2):
if(abs(p1.penalty-p2.penalty)<10):
return 0
return 1 if p1.penalty<p2.penalty else 2
func forward_movement(p1_state,p2_state):
if(p1_state.beats_backdash and !p2_state.beats_backdash):
return 1
elif(p1_state.beats_backdash):
pass
elif(p2_state.beats_backdash):
return 2
return 0
func lower_health(_1,_2):
var p1_hp = p1.hp/p1.MAX_HEALTH
var p2_hp = p2.hp/p2.MAX_HEALTH
if(p1_hp==p2_hp):
return 0
return 1 if p1_hp<p2_hp else 2
func int_abs(n: int):
if n < 0:
n *= -1
return n
func int_clamp(n: int, min_: int, max_: int):
if n > min_ and n < max_:
return n
if n <= min_:
return min_
if n >= max_:
return max_
func should_game_end():
return (current_tick > time or p1.hp <= 0 or p2.hp <= 0)
func resolve_same_x_coordinate():
# prevent both players occupying the same x coordinate
var p1_pos = p1.get_pos()
var p2_pos = p2.get_pos()
if p1_pos.x == p2_pos.x:
var player_to_move = p1 if current_tick % 2 == 0 else p2
var direction_to_move = 1 if current_tick % 2 == 0 else -1
var x = p1_pos.x
if x < 0:
direction_to_move = 1
if p1.get_facing_int() == -1:
player_to_move = p1
elif p2.get_facing_int() == -1:
player_to_move = p2
elif x > 0:
direction_to_move = -1
if p1.get_facing_int() == 1:
player_to_move = p1
elif p2.get_facing_int() == 1:
player_to_move = p2
player_to_move.set_x(player_to_move.get_pos().x + direction_to_move)
player_to_move.update_data()
func resolve_collisions(p1, p2, step=0):
p1.update_collision_boxes()
p2.update_collision_boxes()
var x_pos = p1.data.object_data.position_x
var opp_x_pos = p2.data.object_data.position_x
var p1_right_edge = (x_pos + p1.collision_box.width + p1.collision_box.x)
var p1_left_edge = (x_pos - p1.collision_box.width + p1.collision_box.x)
var p2_right_edge = (opp_x_pos + p2.collision_box.width + p2.collision_box.x)
var p2_left_edge = (opp_x_pos - p2.collision_box.width + p2.collision_box.x)
var edge_distance
if x_pos < opp_x_pos:
edge_distance = int_abs(p2_right_edge - p1_left_edge)
else:
edge_distance = int_abs(p1_right_edge - p2_left_edge)
if p1.is_colliding_with_opponent() and p2.is_colliding_with_opponent() and p1.collision_box.overlaps(p2.collision_box):
var push_p1_left = (p1.get_facing_int() == 1)
if p1.reverse_state:
push_p1_left = !push_p1_left
var push_p2_left = (p1.get_facing_int() == -1)
if p2.reverse_state:
push_p2_left = !push_p2_left
if push_p1_left:
var edge = p1_right_edge
var opp_edge = p2_left_edge
if opp_edge < edge:
var overlap = int_abs(opp_edge - edge)
p1.set_x(x_pos - overlap / 2)
p2.set_x(opp_x_pos + (overlap / 2))
elif push_p2_left:
var edge = p1_left_edge
var opp_edge = p2_right_edge
if opp_edge > edge:
var overlap = int_abs(opp_edge - edge)
p1.set_x(x_pos + overlap / 2)
p2.set_x(opp_x_pos - (overlap / 2))
if edge_distance > max_char_distance:
var midpoint = (x_pos + opp_x_pos) / 2
var left = int_clamp(midpoint - max_char_distance / 2, -stage_width, stage_width)
var right = int_clamp(midpoint + max_char_distance / 2, -stage_width, stage_width)
p1.set_x(int_clamp(x_pos, left + (p1.collision_box.width + p1.collision_box.x), right + (-p1.collision_box.width + p1.collision_box.x)))
p2.set_x(int_clamp(opp_x_pos, left + (p2.collision_box.width + p2.collision_box.x), right + (-p2.collision_box.width + p2.collision_box.x)))
$Camera2D.reset_smoothing()
var p1_y_pos = p1.data.object_data.position_y
var p2_y_pos = p2.data.object_data.position_y
if has_ceiling:
if p1_y_pos - p1.collision_box.height * 2 < -ceiling_height:
p1.set_y(-ceiling_height + p1.collision_box.height * 2)
var vel = p1.get_vel()
p1.set_vel(vel.x, "0")
if p2_y_pos - p2.collision_box.height * 2 < -ceiling_height:
p2.set_y(-ceiling_height + p2.collision_box.height * 2)
var vel = p2.get_vel()
p2.set_vel(vel.x, "0")
if step < 5:
if !p1.clipping_wall and x_pos - p1.collision_box.width < -stage_width:
p1.set_x(-stage_width + p1.collision_box.width)
p1.update_data()
p2.update_data()
return resolve_collisions(p1, p2, step+1)
elif !p1.clipping_wall and x_pos + p1.collision_box.width > stage_width:
p1.set_x(stage_width - p1.collision_box.width)
p1.update_data()
p2.update_data()
return resolve_collisions(p1, p2, step+1)
if !p2.clipping_wall and opp_x_pos - p2.collision_box.width < -stage_width:
p2.set_x(-stage_width + p2.collision_box.width)
p1.update_data()
p2.update_data()
return resolve_collisions(p1, p2, step+1)
elif !p2.clipping_wall and opp_x_pos + p2.collision_box.width > stage_width:
p2.set_x(stage_width - p2.collision_box.width)
p1.update_data()
p2.update_data()
return resolve_collisions(p1, p2, step+1)
if p1.is_colliding_with_opponent() and p2.is_colliding_with_opponent() and p1.collision_box.overlaps(p2.collision_box):
p1.update_data()
p2.update_data()
return resolve_collisions(p1, p2, step+1)
func apply_hitboxes(players):
var px1 = players[0]
var px2 = players[1]
var p1_hitboxes = px1.get_active_hitboxes()
var p2_hitboxes = px2.get_active_hitboxes()
var p1_pos = px1.get_pos()
var p2_pos = px2.get_pos()
for hitbox in p1_hitboxes:
hitbox.update_position(p1_pos.x, p1_pos.y)
for hitbox in p2_hitboxes:
hitbox.update_position(p2_pos.x, p2_pos.y)
var p2_hit_by = get_colliding_hitbox(p1_hitboxes, px2.hurtbox) if not px2.invulnerable else null
var p1_hit_by = get_colliding_hitbox(p2_hitboxes, px1.hurtbox) if not px1.invulnerable else null
var p1_hit = false
var p2_hit = false
var p1_throwing = false
var p2_throwing = false
if p1_hit_by:
if not (p1_hit_by is ThrowBox):
p1_hit = true
else:
p2_throwing = true
if p2_hit_by:
if not (p2_hit_by is ThrowBox):
p2_hit = true
else:
p1_throwing = true
var clash_position = Vector2()
var clashed = false
if clashing_enabled:
for p1_hitbox in p1_hitboxes:
if p1_hitbox is ThrowBox:
continue
if not p1_hitbox.can_clash:
continue
var p2_hitbox = get_colliding_hitbox(p2_hitboxes, p1_hitbox)
if p2_hitbox:
if p2_hitbox is ThrowBox:
continue
if not p2_hitbox.can_clash:
continue
var valid_clash = false
if asymmetrical_clashing:
if p1_hit and not p2_hit:
if p1_hitbox.damage - p2_hitbox.damage < CLASH_DAMAGE_DIFF:
valid_clash = true
if p2_hit and not p1_hit:
if p2_hitbox.damage - p1_hitbox.damage < CLASH_DAMAGE_DIFF:
valid_clash = true
if ( not p1_hit and not p2_hit) or (p1_hit and p2_hit):
if Utils.int_abs(p2_hitbox.damage - p1_hitbox.damage) < CLASH_DAMAGE_DIFF:
valid_clash = true
elif p1_hitbox.damage > p2_hitbox.damage:
p1_hit = false
clash_position = p2_hitbox.get_center_float()
_spawn_particle_effect(preload("res://fx/ClashEffect.tscn"), clash_position)
elif p1_hitbox.damage < p2_hitbox.damage:
clash_position = p1_hitbox.get_center_float()
_spawn_particle_effect(preload("res://fx/ClashEffect.tscn"), clash_position)
p2_hit = false
if valid_clash:
clashed = true
clash_position = p2_hitbox.get_overlap_center_float(p1_hitbox)
break
if clashed:
px1.clash()
px2.clash()
px1.add_penalty(-25)
px2.add_penalty(-25)
_spawn_particle_effect(preload("res://fx/ClashEffect.tscn"), clash_position)
else:
if p1_hit:
if !(p1_throwing and !p1_hit_by.beats_grab):
p1_hit_by.hit(px1)
else:
p1_hit = false
if p2_hit:
if !(p2_throwing and !p2_hit_by.beats_grab):
p2_hit_by.hit(px2)
else:
p2_hit = false
var players_hittable = true
if not p2_hit and not p1_hit:
if p2_throwing and p1_throwing and px1.current_state().throw_techable and px2.current_state().throw_techable:
px1.state_machine.queue_state("ThrowTech")
px2.state_machine.queue_state("ThrowTech")
players_hittable = false
elif p2_throwing and p1_throwing and not px1.current_state().throw_techable and not px2.current_state().throw_techable:
players_hittable = false
elif p1_throwing:
if px1.current_state().throw_techable and px2.current_state().throw_techable:
px1.state_machine.queue_state("ThrowTech")
px2.state_machine.queue_state("ThrowTech")
players_hittable = false
var can_hit = true
if px2.is_grounded() and not p2_hit_by.hits_vs_grounded:
can_hit = false
if not px2.is_grounded() and not p2_hit_by.hits_vs_aerial:
can_hit = false
if !players_hittable:
can_hit = false
# if px2.is_bracing() and px2.current_state().counter_type == CounterAttack.CounterType.Grab:
# can_hit = false
# px1.state_machine.queue_state("ThrowTech")
# px2.state_machine.queue_state("ThrowTech")
# return
if can_hit:
p2_hit_by.hit(px2)
if p2_hit_by.throw_state:
px1.state_machine.queue_state(p2_hit_by.throw_state)
players_hittable = false
elif p2_throwing:
if px1.current_state().throw_techable and px2.current_state().throw_techable:
px1.state_machine.queue_state("ThrowTech")
px2.state_machine.queue_state("ThrowTech")
players_hittable = false
var can_hit = true