Skip to content

Commit 9c6f6c5

Browse files
authored
Merge pull request #1242 from endlessm/character-player-behavior
Add AnimationPlayer version of CharacterSpriteBehavior
2 parents dd87be5 + 6cca353 commit 9c6f6c5

4 files changed

Lines changed: 296 additions & 5 deletions

File tree

scenes/game_elements/characters/components/gym_area_test.tscn

Lines changed: 218 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[gd_scene load_steps=37 format=4 uid="uid://ikgvi75j5dls"]
1+
[gd_scene load_steps=44 format=4 uid="uid://ikgvi75j5dls"]
22

33
[ext_resource type="TileSet" uid="uid://07fq3rspk8ia" path="res://scenes/tileset.tres" id="1_5xpm5"]
44
[ext_resource type="SpriteFrames" uid="uid://07di3updrwh0" path="res://scenes/game_elements/characters/components/sprite_frames/storyweaver_purple.tres" id="3_6axvd"]
@@ -7,6 +7,7 @@
77
[ext_resource type="SpriteFrames" uid="uid://2ek86nvw6y28" path="res://scenes/game_elements/props/tree/components/tree_spriteframes_yellow.tres" id="5_7132a"]
88
[ext_resource type="PackedScene" uid="uid://bbsc3wssndtfe" path="res://scenes/game_elements/props/decoration/hiding_mushroom/hiding_mushroom.tscn" id="6_l45sp"]
99
[ext_resource type="PackedScene" uid="uid://dv4f232y8w8dv" path="res://scenes/game_elements/props/decoration/water_rock/water_rock.tscn" id="7_1aydi"]
10+
[ext_resource type="Script" uid="uid://b3hx1n2yl88qr" path="res://scenes/game_logic/character_animation_player_behavior.gd" id="9_1x76l"]
1011
[ext_resource type="SpriteFrames" uid="uid://b3r84ksew5djp" path="res://scenes/game_elements/characters/enemies/throwing_enemy/components/ink_drinker_frames_purple.tres" id="9_6nfeg"]
1112
[ext_resource type="Script" uid="uid://cx0sk4uhvnii0" path="res://scenes/game_logic/walk_behaviors/erratic_walk_behavior.gd" id="10_k43ew"]
1213
[ext_resource type="Script" uid="uid://q4jj6ou7yonf" path="res://scenes/game_logic/walk_behaviors/bounce_walk_behavior.gd" id="11_utmd2"]
@@ -17,6 +18,7 @@
1718
[ext_resource type="Script" uid="uid://dy68p7gf07pi3" path="res://scenes/game_logic/character_sprite_behavior.gd" id="15_utmd2"]
1819
[ext_resource type="Texture2D" uid="uid://dxaq5piwxqnht" path="res://scenes/game_elements/characters/player/components/dust.png" id="16_w4jvh"]
1920
[ext_resource type="Script" uid="uid://csev4hv57utxv" path="res://scenes/game_logic/walk_behaviors/character_speeds.gd" id="17_itker"]
21+
[ext_resource type="AudioStream" uid="uid://4ec6e2alns71" path="res://assets/third_party/sounds/characters/enemies/guard/GrassFoot.ogg" id="17_p4wnd"]
2022
[ext_resource type="SpriteFrames" uid="uid://r3dn5pxqk1sv" path="res://scenes/game_elements/characters/enemies/guard/components/storyvore_frames_gray.tres" id="17_w4jvh"]
2123
[ext_resource type="Script" uid="uid://id28maao3vdy" path="res://scenes/game_logic/walk_behaviors/path_walk_behavior.gd" id="18_wq1h3"]
2224
[ext_resource type="SpriteFrames" uid="uid://dnq8cw1cio2we" path="res://scenes/game_elements/characters/enemies/throwing_enemy/components/ink_drinker_frames_green.tres" id="20_jdub8"]
@@ -90,6 +92,204 @@ func _on_input_walk_behavior_running_changed(is_running: bool) -> void:
9092
emitting = is_running
9193
"
9294

95+
[sub_resource type="Animation" id="Animation_6nfeg"]
96+
length = 0.001
97+
tracks/0/type = "value"
98+
tracks/0/imported = false
99+
tracks/0/enabled = true
100+
tracks/0/path = NodePath("AnimatedSprite2D:animation")
101+
tracks/0/interp = 1
102+
tracks/0/loop_wrap = true
103+
tracks/0/keys = {
104+
"times": PackedFloat32Array(0),
105+
"transitions": PackedFloat32Array(1),
106+
"update": 1,
107+
"values": [&"idle"]
108+
}
109+
tracks/1/type = "value"
110+
tracks/1/imported = false
111+
tracks/1/enabled = true
112+
tracks/1/path = NodePath("AnimatedSprite2D:frame")
113+
tracks/1/interp = 1
114+
tracks/1/loop_wrap = true
115+
tracks/1/keys = {
116+
"times": PackedFloat32Array(0),
117+
"transitions": PackedFloat32Array(1),
118+
"update": 1,
119+
"values": [0]
120+
}
121+
tracks/2/type = "value"
122+
tracks/2/imported = false
123+
tracks/2/enabled = true
124+
tracks/2/path = NodePath("AudioStreamPlayer2D:volume_db")
125+
tracks/2/interp = 1
126+
tracks/2/loop_wrap = true
127+
tracks/2/keys = {
128+
"times": PackedFloat32Array(0),
129+
"transitions": PackedFloat32Array(1),
130+
"update": 0,
131+
"values": [-14.776]
132+
}
133+
134+
[sub_resource type="Animation" id="Animation_dx6fu"]
135+
resource_name = "idle"
136+
loop_mode = 1
137+
step = 0.1
138+
tracks/0/type = "value"
139+
tracks/0/imported = false
140+
tracks/0/enabled = true
141+
tracks/0/path = NodePath("AnimatedSprite2D:animation")
142+
tracks/0/interp = 1
143+
tracks/0/loop_wrap = true
144+
tracks/0/keys = {
145+
"times": PackedFloat32Array(0),
146+
"transitions": PackedFloat32Array(1),
147+
"update": 1,
148+
"values": [&"idle"]
149+
}
150+
tracks/1/type = "value"
151+
tracks/1/imported = false
152+
tracks/1/enabled = true
153+
tracks/1/path = NodePath("AnimatedSprite2D:frame")
154+
tracks/1/interp = 1
155+
tracks/1/loop_wrap = true
156+
tracks/1/keys = {
157+
"times": PackedFloat32Array(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1),
158+
"transitions": PackedFloat32Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
159+
"update": 1,
160+
"values": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9]
161+
}
162+
163+
[sub_resource type="Animation" id="Animation_makbw"]
164+
resource_name = "run"
165+
length = 0.300005
166+
loop_mode = 1
167+
step = 0.05
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": [&"walk"]
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.05, 0.1, 0.15, 0.2, 0.25, 0.3),
188+
"transitions": PackedFloat32Array(1, 1, 1, 1, 1, 1, 1),
189+
"update": 1,
190+
"values": [0, 1, 2, 3, 4, 5, 5]
191+
}
192+
tracks/2/type = "audio"
193+
tracks/2/imported = false
194+
tracks/2/enabled = true
195+
tracks/2/path = NodePath("AudioStreamPlayer2D")
196+
tracks/2/interp = 1
197+
tracks/2/loop_wrap = true
198+
tracks/2/keys = {
199+
"clips": [{
200+
"end_offset": 0.2353,
201+
"start_offset": 0.0954,
202+
"stream": ExtResource("17_p4wnd")
203+
}, {
204+
"end_offset": 0.2353,
205+
"start_offset": 0.0954,
206+
"stream": ExtResource("17_p4wnd")
207+
}],
208+
"times": PackedFloat32Array(0, 0.15)
209+
}
210+
tracks/2/use_blend = true
211+
tracks/3/type = "value"
212+
tracks/3/imported = false
213+
tracks/3/enabled = true
214+
tracks/3/path = NodePath("AudioStreamPlayer2D:volume_db")
215+
tracks/3/interp = 1
216+
tracks/3/loop_wrap = true
217+
tracks/3/keys = {
218+
"times": PackedFloat32Array(0),
219+
"transitions": PackedFloat32Array(1),
220+
"update": 0,
221+
"values": [-7.388]
222+
}
223+
224+
[sub_resource type="Animation" id="Animation_gv2vc"]
225+
resource_name = "walk"
226+
length = 0.60001
227+
loop_mode = 1
228+
step = 0.1
229+
tracks/0/type = "value"
230+
tracks/0/imported = false
231+
tracks/0/enabled = true
232+
tracks/0/path = NodePath("AnimatedSprite2D:animation")
233+
tracks/0/interp = 1
234+
tracks/0/loop_wrap = true
235+
tracks/0/keys = {
236+
"times": PackedFloat32Array(0),
237+
"transitions": PackedFloat32Array(1),
238+
"update": 1,
239+
"values": [&"walk"]
240+
}
241+
tracks/1/type = "value"
242+
tracks/1/imported = false
243+
tracks/1/enabled = true
244+
tracks/1/path = NodePath("AnimatedSprite2D:frame")
245+
tracks/1/interp = 1
246+
tracks/1/loop_wrap = true
247+
tracks/1/keys = {
248+
"times": PackedFloat32Array(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6),
249+
"transitions": PackedFloat32Array(1, 1, 1, 1, 1, 1, 1),
250+
"update": 1,
251+
"values": [0, 1, 2, 3, 4, 5, 5]
252+
}
253+
tracks/2/type = "audio"
254+
tracks/2/imported = false
255+
tracks/2/enabled = true
256+
tracks/2/path = NodePath("AudioStreamPlayer2D")
257+
tracks/2/interp = 1
258+
tracks/2/loop_wrap = true
259+
tracks/2/keys = {
260+
"clips": [{
261+
"end_offset": 0.0869,
262+
"start_offset": 0.0975,
263+
"stream": ExtResource("17_p4wnd")
264+
}, {
265+
"end_offset": 0.0869,
266+
"start_offset": 0.0975,
267+
"stream": ExtResource("17_p4wnd")
268+
}],
269+
"times": PackedFloat32Array(0, 0.3)
270+
}
271+
tracks/2/use_blend = true
272+
tracks/3/type = "value"
273+
tracks/3/imported = false
274+
tracks/3/enabled = true
275+
tracks/3/path = NodePath("AudioStreamPlayer2D:volume_db")
276+
tracks/3/interp = 1
277+
tracks/3/loop_wrap = true
278+
tracks/3/keys = {
279+
"times": PackedFloat32Array(0),
280+
"transitions": PackedFloat32Array(1),
281+
"update": 0,
282+
"values": [-23.869]
283+
}
284+
285+
[sub_resource type="AnimationLibrary" id="AnimationLibrary_6nfeg"]
286+
_data = {
287+
&"RESET": SubResource("Animation_6nfeg"),
288+
&"idle": SubResource("Animation_dx6fu"),
289+
&"run": SubResource("Animation_makbw"),
290+
&"walk": SubResource("Animation_gv2vc")
291+
}
292+
93293
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_itker"]
94294
radius = 13.0
95295
height = 66.0
@@ -304,9 +504,25 @@ animation = &"idle"
304504
[node name="CharacterSpriteBehavior" type="Node2D" parent="OnTheGround/CharacterBody2D4/AnimatedSprite2D" node_paths=PackedStringArray("character", "sprite")]
305505
script = ExtResource("15_utmd2")
306506
character = NodePath("../..")
507+
play_animations = false
307508
sprite = NodePath("..")
308509
metadata/_custom_type_script = "uid://dy68p7gf07pi3"
309510

511+
[node name="AnimationPlayer" type="AnimationPlayer" parent="OnTheGround/CharacterBody2D4"]
512+
libraries = {
513+
&"": SubResource("AnimationLibrary_6nfeg")
514+
}
515+
516+
[node name="CharacterAnimationPlayerBehavior" type="Node2D" parent="OnTheGround/CharacterBody2D4/AnimationPlayer" node_paths=PackedStringArray("character", "animation_player")]
517+
script = ExtResource("9_1x76l")
518+
character = NodePath("../..")
519+
animation_player = NodePath("..")
520+
metadata/_custom_type_script = "uid://b3hx1n2yl88qr"
521+
522+
[node name="AudioStreamPlayer2D" type="AudioStreamPlayer2D" parent="OnTheGround/CharacterBody2D4"]
523+
volume_db = -14.776
524+
bus = &"SFX"
525+
310526
[node name="InputWalkBehavior" type="Node2D" parent="OnTheGround/CharacterBody2D4" node_paths=PackedStringArray("character")]
311527
script = ExtResource("15_k43ew")
312528
character = NodePath("..")
@@ -381,5 +597,5 @@ character = NodePath("..")
381597
[node name="ScreenOverlay" type="CanvasLayer" parent="."]
382598

383599
[connection signal="running_changed" from="OnTheGround/CharacterBody2D4/InputWalkBehavior" to="OnTheGround/CharacterBody2D4/GPUParticles2D" method="_on_input_walk_behavior_running_changed"]
384-
[connection signal="running_changed" from="OnTheGround/CharacterBody2D4/InputWalkBehavior" to="OnTheGround/CharacterBody2D4/AnimatedSprite2D/CharacterSpriteBehavior" method="on_running_changed"]
600+
[connection signal="running_changed" from="OnTheGround/CharacterBody2D4/InputWalkBehavior" to="OnTheGround/CharacterBody2D4/AnimationPlayer/CharacterAnimationPlayerBehavior" method="on_running_changed"]
385601
[connection signal="running_changed" from="OnTheGround/CharacterBody2D6/NavigationFollowWalkBehavior" to="OnTheGround/CharacterBody2D6/AnimatedSprite2D/CharacterSpriteBehavior" method="on_running_changed"]
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# SPDX-FileCopyrightText: The Threadbare Authors
2+
# SPDX-License-Identifier: MPL-2.0
3+
@tool
4+
class_name CharacterAnimationPlayerBehavior
5+
extends Node2D
6+
## @experimental
7+
##
8+
## Play animations in [member animation_player] according
9+
## to the velocity of [member character].
10+
## [br][br]
11+
## For creating simple characters, consider using [CharacterSpriteBehavior] instead.
12+
13+
## The [member CharacterBody2D.velocity] is used to change the [member animation_player].
14+
## [br][br]
15+
## [b]Note:[/b] If the grandparent node is a CharacterBody2D and this isn't set,
16+
## the grandparent node will be automatically assigned to this variable.
17+
@export var character: CharacterBody2D
18+
19+
## The controlled animation player.
20+
## [br][br]
21+
## [b]Note:[/b] If the parent node is an AnimationPlayer and this isn't set,
22+
## the parent node will be automatically assigned to this variable.
23+
@export var animation_player: AnimationPlayer:
24+
set = _set_animation_player
25+
26+
var _is_character_running: bool = false
27+
28+
29+
func _enter_tree() -> void:
30+
if not animation_player and get_parent() is AnimationPlayer:
31+
animation_player = get_parent()
32+
if not character and get_parent() and get_parent().get_parent() is CharacterBody2D:
33+
character = get_parent().get_parent()
34+
35+
36+
func _set_animation_player(new_animation_player: AnimationPlayer) -> void:
37+
animation_player = new_animation_player
38+
update_configuration_warnings()
39+
40+
41+
func _get_configuration_warnings() -> PackedStringArray:
42+
var warnings: PackedStringArray
43+
if animation_player is not AnimationPlayer:
44+
warnings.append("Animation Player must be set.")
45+
return warnings
46+
47+
48+
func _ready() -> void:
49+
if Engine.is_editor_hint():
50+
set_process(false)
51+
return
52+
53+
54+
func _process(_delta: float) -> void:
55+
if not character:
56+
return
57+
58+
if character.velocity.is_zero_approx():
59+
animation_player.play(&"idle")
60+
else:
61+
if _is_character_running:
62+
animation_player.play(&"run")
63+
else:
64+
animation_player.play(&"walk")
65+
66+
67+
## You can connect this callback to a [member InputWalkBehavior.running_changed] signal.
68+
func on_running_changed(is_running: bool) -> void:
69+
_is_character_running = is_running
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uid://b3hx1n2yl88qr

scenes/game_logic/character_sprite_behavior.gd

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@ extends Node2D
99
## to the velocity of [member character].
1010

1111
## The [member CharacterBody2D.velocity] is used to change the [member sprite].
12+
## [br][br]
13+
## [b]Note:[/b] If the grandparent node is a CharacterBody2D and this isn't set,
14+
## the grandparent node will be automatically assigned to this variable.
1215
@export var character: CharacterBody2D
1316

1417
## Whether to play the sprite animations or not. If not, the only thing that will happen is that
1518
## the sprite will be flipped horizontally according to the velocity of [member character].
1619
## Use this when using more advanced animation through an AnimationPlayer node.
1720
@export var play_animations: bool = true
1821

19-
## The controlled sprite.[br][br]
20-
##
21-
## [b]Note:[/b] If the parent node is a AnimatedSprite2D and sprite isn't set,
22+
## The controlled sprite.
23+
## [br][br]
24+
## [b]Note:[/b] If the parent node is a AnimatedSprite2D and this isn't set,
2225
## the parent node will be automatically assigned to this variable.
2326
@export var sprite: AnimatedSprite2D:
2427
set = _set_sprite
@@ -29,6 +32,8 @@ var _is_character_running: bool = false
2932
func _enter_tree() -> void:
3033
if not sprite and get_parent() is AnimatedSprite2D:
3134
sprite = get_parent()
35+
if not character and get_parent() and get_parent().get_parent() is CharacterBody2D:
36+
character = get_parent().get_parent()
3237

3338

3439
func _set_sprite(new_sprite: AnimatedSprite2D) -> void:

0 commit comments

Comments
 (0)