walking and idle animations

master
chimchooree 1 year ago
parent 83faf15858
commit 50a478fd5c

@ -1,4 +1,4 @@
[gd_scene load_steps=30 format=2] [gd_scene load_steps=31 format=2]
[ext_resource path="res://res/level/neighbor.png" type="Texture" id=1] [ext_resource path="res://res/level/neighbor.png" type="Texture" id=1]
[ext_resource path="res://res/character/girl.png" type="Texture" id=2] [ext_resource path="res://res/character/girl.png" type="Texture" id=2]
@ -108,26 +108,36 @@ flags = 4
atlas = ExtResource( 2 ) atlas = ExtResource( 2 )
region = Rect2( 0, 200, 33, 50 ) region = Rect2( 0, 200, 33, 50 )
[sub_resource type="AtlasTexture" id=23]
flags = 4
atlas = ExtResource( 2 )
region = Rect2( 0, 0, 33, 50 )
[sub_resource type="SpriteFrames" id=21] [sub_resource type="SpriteFrames" id=21]
animations = [ { animations = [ {
"frames": [ SubResource( 1 ), SubResource( 2 ), SubResource( 3 ), SubResource( 4 ), SubResource( 5 ) ], "frames": [ SubResource( 1 ), SubResource( 2 ), SubResource( 3 ), SubResource( 4 ), SubResource( 5 ) ],
"loop": true, "loop": true,
"name": "WalkRight", "name": "WalkRight",
"speed": 5.0 "speed": 10.0
}, { }, {
"frames": [ SubResource( 6 ), SubResource( 7 ), SubResource( 8 ), SubResource( 9 ), SubResource( 10 ) ], "frames": [ SubResource( 6 ), SubResource( 7 ), SubResource( 8 ), SubResource( 9 ), SubResource( 10 ) ],
"loop": true, "loop": true,
"name": "WalkLeft", "name": "WalkLeft",
"speed": 5.0 "speed": 10.0
}, { }, {
"frames": [ SubResource( 11 ), SubResource( 12 ), SubResource( 13 ), SubResource( 14 ), SubResource( 15 ) ], "frames": [ SubResource( 11 ), SubResource( 12 ), SubResource( 13 ), SubResource( 14 ), SubResource( 15 ) ],
"loop": true, "loop": true,
"name": "WalkUp", "name": "WalkUp",
"speed": 5.0 "speed": 10.0
}, { }, {
"frames": [ SubResource( 16 ), SubResource( 17 ), SubResource( 18 ), SubResource( 19 ), SubResource( 20 ) ], "frames": [ SubResource( 16 ), SubResource( 17 ), SubResource( 18 ), SubResource( 19 ), SubResource( 20 ) ],
"loop": true, "loop": true,
"name": "WalkDown", "name": "WalkDown",
"speed": 10.0
}, {
"frames": [ SubResource( 23 ) ],
"loop": true,
"name": "Idle",
"speed": 5.0 "speed": 5.0
} ] } ]
@ -156,7 +166,7 @@ script = ExtResource( 5 )
[node name="AnimatedSprite" type="AnimatedSprite" parent="World/Enter/Girl/KinematicBody2D"] [node name="AnimatedSprite" type="AnimatedSprite" parent="World/Enter/Girl/KinematicBody2D"]
frames = SubResource( 21 ) frames = SubResource( 21 )
animation = "WalkDown" animation = "Idle"
[node name="CollisionShape2D" type="CollisionShape2D" parent="World/Enter/Girl/KinematicBody2D"] [node name="CollisionShape2D" type="CollisionShape2D" parent="World/Enter/Girl/KinematicBody2D"]
position = Vector2( 1, -2 ) position = Vector2( 1, -2 )

@ -41,15 +41,19 @@ func movement_key_pressed(direction):
if direction == 1: # right if direction == 1: # right
velocity_sum.x = 0 velocity_sum.x = 0
velocity_sum.x += 1 velocity_sum.x += 1
get_user().animate("WalkRight")
elif direction == 2: # left elif direction == 2: # left
velocity_sum.x = 0 velocity_sum.x = 0
velocity_sum.x -= 1 velocity_sum.x -= 1
get_user().animate("WalkLeft")
elif direction == 3: # up elif direction == 3: # up
velocity_sum.y = 0 velocity_sum.y = 0
velocity_sum.y -= 1 velocity_sum.y -= 1
get_user().animate("WalkUp")
elif direction == 4: # down elif direction == 4: # down
velocity_sum.y = 0 velocity_sum.y = 0
velocity_sum.y += 1 velocity_sum.y += 1
get_user().animate("WalkDown")
# Velocity cannot be faster with more vectors # Velocity cannot be faster with more vectors
get_user().set_internal_velocity(velocity_sum) get_user().set_internal_velocity(velocity_sum)
@ -58,9 +62,10 @@ func movement_key_released(direction):
velocity_sum.x = 0 velocity_sum.x = 0
elif direction == 3 or direction == 4: # up/down elif direction == 3 or direction == 4: # up/down
velocity_sum.y = 0 velocity_sum.y = 0
get_user().animate("Idle")
get_user().set_internal_velocity(velocity_sum) get_user().set_internal_velocity(velocity_sum)
# Physics Process # Physics Process
func _physics_process(delta): func _physics_process(_delta):
## Keyboard Movement ## Keyboard Movement
get_input() get_input()

@ -3,6 +3,9 @@ extends Node
## Nodes ## Nodes
func get_body_node(): func get_body_node():
return $KinematicBody2D return $KinematicBody2D
func get_sprite_node():
return $KinematicBody2D/AnimatedSprite
## Parameters ## Parameters
func set_gpos(new_pos): func set_gpos(new_pos):
return get_body_node().set_gpos(new_pos) return get_body_node().set_gpos(new_pos)
@ -10,3 +13,7 @@ func get_gpos():
return get_body_node().get_gpos() return get_body_node().get_gpos()
func set_internal_velocity(new_vel): func set_internal_velocity(new_vel):
get_body_node().set_internal_velocity(new_vel) get_body_node().set_internal_velocity(new_vel)
## Logic
func animate(animation):
get_sprite_node().play(animation)

@ -26,7 +26,7 @@ func track_pos(new_pos):
positions.pop_front() positions.pop_front()
# Handle # Handle
func handle(character): func handle(_character):
return return
# var current_pos = character.get_gpos() # var current_pos = character.get_gpos()
# track_pos(current_pos) # track_pos(current_pos)

Loading…
Cancel
Save