Skip to content

Commit d3b18d0

Browse files
committed
Guard: Fix flipping the sprite horizontally
In 57b0a2e I removed more than necessary: ``` if not is_zero_approx(velocity.x): animated_sprite_2d.flip_h = velocity.x < 0 ``` Bring it back, but now using CharacterSpriteBehavior and CharacterAnimationPlayerBehavior nodes. When the guard is alerted, disable the node controlling the AnimationPlayer for walking / being idle, so it can be disabled to play the alerted animation. Inside the scene file, the position of the idle animation subresource changes for no reason.
1 parent f461c18 commit d3b18d0

File tree

2 files changed

+53
-29
lines changed

2 files changed

+53
-29
lines changed

scenes/game_elements/characters/enemies/guard/components/guard.gd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ var _player: Player
9898
@onready var sight_ray_cast: RayCast2D = %SightRayCast
9999
## Control to hold debug info that can be toggled on or off.
100100
@onready var debug_info: Label = %DebugInfo
101+
102+
## Reference to the node controlling the AnimationPlayer for walking / being idle,
103+
## so it can be disabled to play the alerted animation.
104+
@onready
105+
# gdlint:ignore = max-line-length
106+
var character_animation_player_behavior: CharacterAnimationPlayerBehavior = %CharacterAnimationPlayerBehavior
107+
101108
## Handles the velocity and movement of the guard.
102109
@onready var guard_movement: GuardMovement = %GuardMovement
103110
@onready var animated_sprite_2d: AnimatedSprite2D = %AnimatedSprite2D
@@ -269,6 +276,7 @@ func _set_state(new_state: State) -> void:
269276
if not _alert_sound.playing:
270277
_alert_sound.play()
271278
State.ALERTED:
279+
character_animation_player_behavior.process_mode = Node.PROCESS_MODE_DISABLED
272280
if not _alert_sound.playing:
273281
_alert_sound.play()
274282
animation_player.play(&"alerted")

scenes/game_elements/characters/enemies/guard/guard.tscn

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
[ext_resource type="Script" uid="uid://cxsi2xqcdyw7g" path="res://scenes/game_elements/characters/enemies/guard/components/light.gd" id="4_lptvm"]
88
[ext_resource type="Script" uid="uid://bnbt0iw1a1w6" path="res://scenes/game_elements/characters/enemies/guard/components/detection_area.gd" id="4_mswbt"]
99
[ext_resource type="SpriteFrames" uid="uid://ovu5wqo15s5g" path="res://scenes/quests/story_quests/NO_EDIT/1_NO_EDIT_stealth/NO_EDIT_stealth_components/NO_EDIT_guard_enemy.tres" id="5_mswbt"]
10+
[ext_resource type="Script" uid="uid://dy68p7gf07pi3" path="res://scenes/game_logic/character_sprite_behavior.gd" id="7_klpct"]
11+
[ext_resource type="Script" uid="uid://b3hx1n2yl88qr" path="res://scenes/game_logic/character_animation_player_behavior.gd" id="9_8vt0k"]
1012

1113
[sub_resource type="CircleShape2D" id="CircleShape2D_g173s"]
1214
radius = 35.0
@@ -119,6 +121,35 @@ tracks/2/keys = {
119121
"values": [0, 1, 2, 3, 4, 5, 5]
120122
}
121123

124+
[sub_resource type="Animation" id="Animation_ls1y7"]
125+
resource_name = "idle"
126+
length = 1.1
127+
step = 0.1
128+
tracks/0/type = "value"
129+
tracks/0/imported = false
130+
tracks/0/enabled = true
131+
tracks/0/path = NodePath("AnimatedSprite2D:animation")
132+
tracks/0/interp = 1
133+
tracks/0/loop_wrap = true
134+
tracks/0/keys = {
135+
"times": PackedFloat32Array(0),
136+
"transitions": PackedFloat32Array(1),
137+
"update": 1,
138+
"values": [&"idle"]
139+
}
140+
tracks/1/type = "value"
141+
tracks/1/imported = false
142+
tracks/1/enabled = true
143+
tracks/1/path = NodePath("AnimatedSprite2D:frame")
144+
tracks/1/interp = 1
145+
tracks/1/loop_wrap = true
146+
tracks/1/keys = {
147+
"times": PackedFloat32Array(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.1),
148+
"transitions": PackedFloat32Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
149+
"update": 1,
150+
"values": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10]
151+
}
152+
122153
[sub_resource type="Animation" id="Animation_8vt0k"]
123154
resource_name = "walk"
124155
length = 0.6
@@ -161,35 +192,6 @@ tracks/2/keys = {
161192
"values": [0, 1, 2, 3, 4, 5, 5]
162193
}
163194

164-
[sub_resource type="Animation" id="Animation_ls1y7"]
165-
resource_name = "idle"
166-
length = 1.1
167-
step = 0.1
168-
tracks/0/type = "value"
169-
tracks/0/imported = false
170-
tracks/0/enabled = true
171-
tracks/0/path = NodePath("AnimatedSprite2D:animation")
172-
tracks/0/interp = 1
173-
tracks/0/loop_wrap = true
174-
tracks/0/keys = {
175-
"times": PackedFloat32Array(0),
176-
"transitions": PackedFloat32Array(1),
177-
"update": 1,
178-
"values": [&"idle"]
179-
}
180-
tracks/1/type = "value"
181-
tracks/1/imported = false
182-
tracks/1/enabled = true
183-
tracks/1/path = NodePath("AnimatedSprite2D:frame")
184-
tracks/1/interp = 1
185-
tracks/1/loop_wrap = true
186-
tracks/1/keys = {
187-
"times": PackedFloat32Array(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.1),
188-
"transitions": PackedFloat32Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
189-
"update": 1,
190-
"values": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10]
191-
}
192-
193195
[sub_resource type="AnimationLibrary" id="AnimationLibrary_innil"]
194196
_data = {
195197
&"RESET": SubResource("Animation_mswbt"),
@@ -250,6 +252,13 @@ sprite_frames = ExtResource("5_mswbt")
250252
animation = &"idle"
251253
autoplay = "idle"
252254

255+
[node name="CharacterSpriteBehavior" type="Node2D" parent="AnimatedSprite2D" node_paths=PackedStringArray("character", "sprite")]
256+
script = ExtResource("7_klpct")
257+
character = NodePath("../..")
258+
play_animations = false
259+
sprite = NodePath("..")
260+
metadata/_custom_type_script = "uid://dy68p7gf07pi3"
261+
253262
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
254263
shape = SubResource("CircleShape2D_nv25s")
255264

@@ -295,6 +304,13 @@ libraries = {
295304
&"": SubResource("AnimationLibrary_innil")
296305
}
297306

307+
[node name="CharacterAnimationPlayerBehavior" type="Node2D" parent="AnimationPlayer" node_paths=PackedStringArray("character", "animation_player")]
308+
unique_name_in_owner = true
309+
script = ExtResource("9_8vt0k")
310+
character = NodePath("../..")
311+
animation_player = NodePath("..")
312+
metadata/_custom_type_script = "uid://b3hx1n2yl88qr"
313+
298314
[node name="Sounds" type="Node2D" parent="."]
299315

300316
[node name="AlertSound" type="AudioStreamPlayer" parent="Sounds"]

0 commit comments

Comments
 (0)