Skip to content

Commit 058b012

Browse files
committed
add comments
1 parent 86fe33f commit 058b012

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

combo-demo/player/Player.gd

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,26 @@ enum { LIGHT_ATTACK, HEAVY_ATTACK }
66

77
const SPEED_MOVE := 400.0
88

9+
# This data structure stores the attack sequence of each combo as a key, and the
10+
# corresponding animations and other data as values.
911
const COMBOS = {
10-
[LIGHT_ATTACK, LIGHT_ATTACK, LIGHT_ATTACK]: ["light_combo_1", "light_combo_2", "light_combo_3"],
11-
[HEAVY_ATTACK, HEAVY_ATTACK]: ["heavy_combo_1", "heavy_combo_2"],
12-
[LIGHT_ATTACK, LIGHT_ATTACK, HEAVY_ATTACK]: ["light_combo_1", "light_combo_2", "heavy_combo_2"],
12+
[LIGHT_ATTACK, LIGHT_ATTACK, LIGHT_ATTACK]:
13+
# Using a dictionary for each attack's data allows us to add any other data
14+
# we need per-attack, like damage, status effects...
15+
[{animation = "light_combo_1"}, {animation = "light_combo_2"}, {animation = "light_combo_3"}],
16+
[HEAVY_ATTACK, HEAVY_ATTACK]: [{animation = "heavy_combo_1"}, {animation = "heavy_combo_2"}],
17+
[LIGHT_ATTACK, LIGHT_ATTACK, HEAVY_ATTACK]:
18+
[{animation = "light_combo_1"}, {animation = "light_combo_2"}, {animation = "heavy_combo_2"}],
1319
}
1420

21+
# There's a limited timeframe to register the next attack for each playing
22+
# attack animation. This variable gets set to true from the animation to allow
23+
# the player to use the next attack.
1524
var accept_next_attack := true setget set_accept_next_attack
1625

1726
var _state = States.MOVE
27+
# We store the list of attacks the player used as part of the current combo
28+
# here. See the _attack() function to see how we use this.
1829
var _combo_current := []
1930

2031
onready var _skin := $Skin
@@ -35,6 +46,7 @@ func _unhandled_input(event: InputEvent) -> void:
3546
_attack(HEAVY_ATTACK)
3647

3748

49+
# This handles forward and back movement when the player isn't attacking.
3850
func _physics_process(delta: float) -> void:
3951
if _state != States.MOVE:
4052
return
@@ -47,40 +59,56 @@ func _physics_process(delta: float) -> void:
4759
move_and_slide(velocity)
4860

4961

62+
# This is the function that handles combos. It appends attacks to the
63+
# _combo_current array and, if it finds a matching combo, plays the
64+
# corresponding attack animation.
5065
func _attack(attack_type: int) -> void:
5166
assert(
5267
attack_type in [LIGHT_ATTACK, HEAVY_ATTACK],
5368
"Unsupported attack type, please use an attack type from the enum."
5469
)
5570

71+
# We don't want to accept another attack until the attack animation allows
72+
# it.
5673
accept_next_attack = false
5774

58-
_combo_current.append(attack_type)
5975
_state = States.ATTACK
76+
_combo_current.append(attack_type)
6077

61-
_animation_player.stop()
62-
78+
# We check every attack of the current combo against every attack of every
79+
# combo in the COMBOS data structure until we find a match.
80+
#
81+
# If we have a match, we assign the next attack's animation to the variable
82+
# below, which tells us if we should continue or end the combo.
6383
var next_attack_animation := ""
6484
var current_attack_count := _combo_current.size()
6585
for combo in COMBOS:
86+
# To avoid indexing errors, we skip any combo shorter than the current combo.
6687
if current_attack_count > combo.size():
6788
continue
6889

90+
# We compare each value in the current combo with the values in our
91+
# reference combo. If any one value doesn't match, then we skip to the
92+
# next combo.
6993
var is_match := true
7094
for index in current_attack_count:
7195
if _combo_current[index] != combo[index]:
7296
is_match = false
7397
break
7498

99+
# If the current combo matches the reference combo, we store the
100+
# animation to play next.
75101
if is_match:
76-
next_attack_animation = COMBOS[combo][current_attack_count - 1]
102+
next_attack_animation = COMBOS[combo][current_attack_count - 1].animation
77103

78104
if next_attack_animation:
79105
_animation_player.play(next_attack_animation)
80106
else:
81107
_end_combo()
82108

83109

110+
# This function resets all combo-related variables, to allow the player to start
111+
# another combo.
84112
func _end_combo() -> void:
85113
_state = States.MOVE
86114
_animation_player.play("idle")

0 commit comments

Comments
 (0)