Godot: Character controller with animations

This commit is contained in:
Joseph Ferano 2023-10-01 23:26:16 +07:00
parent ac0875103d
commit 420742aba6
4 changed files with 339 additions and 234 deletions

View File

@ -1,18 +1,61 @@
extends CharacterBody2D extends CharacterBody2D
var gravity = 15 var gravity = 800
const SPEED = 60.0
const JUMP_VELOCITY = -400.0
@onready var sprite = $Sprites
var land_time = 0
var jumping = false
var side = 1
func _physics_process(delta): func _physics_process(delta):
if !is_on_floor(): if not is_on_floor():
velocity.y += 15 velocity.y += gravity * delta
elif jumping:
jumping = false
sprite.play("Land")
land_time = 0.33
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
sprite.play("Jump")
jumping = true
sprite.pause()
sprite.set_frame_and_progress(0, 0)
if jumping:
if velocity.y > 50:
sprite.set_frame_and_progress(2,0)
elif velocity.y > -150:
sprite.set_frame_and_progress(1, 0)
var speed = SPEED
if Input.is_key_pressed(KEY_SHIFT):
speed *= 3
var direction = Input.get_axis("ui_left", "ui_right")
if direction:
velocity.x = direction * speed
else:
velocity.x = move_toward(velocity.x, 0, speed)
if direction != 0:
sprite.flip_h = direction == -1
if land_time >= 0:
land_time -= delta
# If shift left/right then 2 * speed, play run
if land_time <= 0 and not jumping:
if velocity.x != 0:
if Input.is_key_pressed(KEY_SHIFT):
sprite.play("Run")
else:
sprite.play("Walk")
else:
sprite.play("Idle")
move_and_slide() move_and_slide()
$AnimationPlayer.play('Idle')
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass

View File

@ -1,6 +1,6 @@
[gd_scene load_steps=4 format=3 uid="uid://dr8vg4b1eh6i"] [gd_scene load_steps=4 format=3 uid="uid://dr8vg4b1eh6i"]
[ext_resource type="PackedScene" uid="uid://dv08bvquqpc8d" path="res://Scenes/Player.tscn" id="1_ibeo8"] [ext_resource type="PackedScene" uid="uid://bjc6dwxaakqdg" path="res://Scenes/Player.tscn" id="1_ibeo8"]
[sub_resource type="QuadMesh" id="QuadMesh_n5vip"] [sub_resource type="QuadMesh" id="QuadMesh_n5vip"]
@ -10,7 +10,7 @@ size = Vector2(300, 20)
[node name="Node2D" type="Node2D"] [node name="Node2D" type="Node2D"]
[node name="Player" parent="." instance=ExtResource("1_ibeo8")] [node name="Player" parent="." instance=ExtResource("1_ibeo8")]
position = Vector2(80, 82) position = Vector2(163, 99)
[node name="StaticBody2D" type="StaticBody2D" parent="."] [node name="StaticBody2D" type="StaticBody2D" parent="."]
position = Vector2(198, 184) position = Vector2(198, 184)

View File

@ -1,236 +1,296 @@
[gd_scene load_steps=13 format=3 uid="uid://dv08bvquqpc8d"] [gd_scene load_steps=44 format=3 uid="uid://bjc6dwxaakqdg"]
[ext_resource type="Texture2D" uid="uid://wfwjlevruveu" path="res://Import/Sprites/Character Idle 48x48.png" id="1_7u4sn"] [ext_resource type="Script" path="res://Code/Character.gd" id="1_23c20"]
[ext_resource type="Texture2D" uid="uid://bge22c82umdui" path="res://Import/Sprites/PlayerWalk 48x48.png" id="1_h6xei"] [ext_resource type="Texture2D" uid="uid://wfwjlevruveu" path="res://Import/Sprites/Character Idle 48x48.png" id="2_8h572"]
[ext_resource type="Script" path="res://Code/Character.gd" id="1_v1ece"] [ext_resource type="Texture2D" uid="uid://bge22c82umdui" path="res://Import/Sprites/PlayerWalk 48x48.png" id="3_xxdqr"]
[ext_resource type="Texture2D" uid="uid://8ikuchoh7pyd" path="res://Import/Sprites/player jump 48x48.png" id="3_ye2je"] [ext_resource type="Texture2D" uid="uid://y8syonuki304" path="res://Import/Sprites/run cycle 48x48.png" id="4_rqcgh"]
[ext_resource type="Texture2D" uid="uid://y8syonuki304" path="res://Import/Sprites/run cycle 48x48.png" id="4_2cq86"] [ext_resource type="Texture2D" uid="uid://8ikuchoh7pyd" path="res://Import/Sprites/player jump 48x48.png" id="5_f2sni"]
[ext_resource type="Texture2D" uid="uid://diiqvgeqnmn1c" path="res://Import/Sprites/player land 48x48.png" id="6_ba2s3"]
[sub_resource type="Animation" id="Animation_vrt4p"] [sub_resource type="AtlasTexture" id="AtlasTexture_t3ih3"]
resource_name = "Idle" atlas = ExtResource("2_8h572")
length = 1.3 region = Rect2(0, 0, 48, 48)
step = 0.05
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath(".:texture")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [ExtResource("1_7u4sn")]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath(".:hframes")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [10]
}
tracks/2/type = "value"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath(".:frame")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8),
"transitions": PackedFloat32Array(1, 1, 1, 1, 1, 1, 1, 1, 1),
"update": 1,
"values": [0, 1, 2, 3, 4, 5, 5, 6, 7]
}
[sub_resource type="Animation" id="Animation_2yfkp"] [sub_resource type="AtlasTexture" id="AtlasTexture_s5g6o"]
resource_name = "Jump" atlas = ExtResource("2_8h572")
tracks/0/type = "value" region = Rect2(48, 0, 48, 48)
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath(".:texture")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [ExtResource("3_ye2je")]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath(".:hframes")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [3]
}
tracks/2/type = "value"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath(".:frame")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(0, 0.1, 0.2),
"transitions": PackedFloat32Array(1, 1, 1),
"update": 1,
"values": [0, 1, 2]
}
[sub_resource type="Animation" id="Animation_1l7dh"] [sub_resource type="AtlasTexture" id="AtlasTexture_b6frn"]
length = 0.001 atlas = ExtResource("2_8h572")
tracks/0/type = "value" region = Rect2(96, 0, 48, 48)
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath(".:frame")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [0]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath(".:texture")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [ExtResource("1_h6xei")]
}
tracks/2/type = "value"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath(".:hframes")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [10]
}
[sub_resource type="Animation" id="Animation_oejr4"] [sub_resource type="AtlasTexture" id="AtlasTexture_kmcgv"]
resource_name = "Run" atlas = ExtResource("2_8h572")
length = 0.75 region = Rect2(144, 0, 48, 48)
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath(".:texture")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [ExtResource("4_2cq86")]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath(".:hframes")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [8]
}
tracks/2/type = "value"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath(".:frame")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7),
"transitions": PackedFloat32Array(1, 1, 1, 1, 1, 1, 1, 1),
"update": 1,
"values": [0, 1, 2, 3, 4, 5, 6, 7]
}
[sub_resource type="Animation" id="Animation_dl063"] [sub_resource type="AtlasTexture" id="AtlasTexture_ms05f"]
resource_name = "Walk" atlas = ExtResource("2_8h572")
tracks/0/type = "value" region = Rect2(192, 0, 48, 48)
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath(".:texture")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [ExtResource("1_h6xei")]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath(".:hframes")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [8]
}
tracks/2/type = "value"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath(".:frame")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6),
"transitions": PackedFloat32Array(1, 1, 1, 1, 1, 1, 1),
"update": 1,
"values": [0, 1, 2, 3, 4, 5, 6]
}
[sub_resource type="AnimationLibrary" id="AnimationLibrary_6srtp"] [sub_resource type="AtlasTexture" id="AtlasTexture_t3rvs"]
_data = { atlas = ExtResource("2_8h572")
"Idle": SubResource("Animation_vrt4p"), region = Rect2(240, 0, 48, 48)
"Land": SubResource("Animation_2yfkp"),
"RESET": SubResource("Animation_1l7dh"), [sub_resource type="AtlasTexture" id="AtlasTexture_n46sx"]
"Run": SubResource("Animation_oejr4"), atlas = ExtResource("2_8h572")
"Walk": SubResource("Animation_dl063") region = Rect2(288, 0, 48, 48)
}
[sub_resource type="AtlasTexture" id="AtlasTexture_48smm"]
atlas = ExtResource("2_8h572")
region = Rect2(336, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_tjhup"]
atlas = ExtResource("2_8h572")
region = Rect2(384, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_c84of"]
atlas = ExtResource("2_8h572")
region = Rect2(432, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_i67oc"]
atlas = ExtResource("5_f2sni")
region = Rect2(0, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_x3ooa"]
atlas = ExtResource("5_f2sni")
region = Rect2(48, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_7bpxf"]
atlas = ExtResource("5_f2sni")
region = Rect2(96, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_fexn1"]
atlas = ExtResource("6_ba2s3")
region = Rect2(0, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_3uk3c"]
atlas = ExtResource("6_ba2s3")
region = Rect2(48, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_iiovw"]
atlas = ExtResource("6_ba2s3")
region = Rect2(96, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_sx58l"]
atlas = ExtResource("6_ba2s3")
region = Rect2(144, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_6l2e7"]
atlas = ExtResource("6_ba2s3")
region = Rect2(240, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_acowd"]
atlas = ExtResource("6_ba2s3")
region = Rect2(336, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_3u4f5"]
atlas = ExtResource("4_rqcgh")
region = Rect2(0, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_dpm2p"]
atlas = ExtResource("4_rqcgh")
region = Rect2(48, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_qh1us"]
atlas = ExtResource("4_rqcgh")
region = Rect2(96, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_7kb6t"]
atlas = ExtResource("4_rqcgh")
region = Rect2(144, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_p5dlr"]
atlas = ExtResource("4_rqcgh")
region = Rect2(192, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_g7jsr"]
atlas = ExtResource("4_rqcgh")
region = Rect2(240, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_aq6al"]
atlas = ExtResource("4_rqcgh")
region = Rect2(288, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_ftoh7"]
atlas = ExtResource("4_rqcgh")
region = Rect2(336, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_a247p"]
atlas = ExtResource("3_xxdqr")
region = Rect2(0, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_24aks"]
atlas = ExtResource("3_xxdqr")
region = Rect2(48, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_hs577"]
atlas = ExtResource("3_xxdqr")
region = Rect2(96, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_1sf80"]
atlas = ExtResource("3_xxdqr")
region = Rect2(144, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_h515b"]
atlas = ExtResource("3_xxdqr")
region = Rect2(192, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_nx0f0"]
atlas = ExtResource("3_xxdqr")
region = Rect2(240, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_6ow76"]
atlas = ExtResource("3_xxdqr")
region = Rect2(288, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_ygn7k"]
atlas = ExtResource("3_xxdqr")
region = Rect2(336, 0, 48, 48)
[sub_resource type="SpriteFrames" id="SpriteFrames_gvqqb"]
animations = [{
"frames": [{
"duration": 1.0,
"texture": SubResource("AtlasTexture_t3ih3")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_s5g6o")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_b6frn")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_kmcgv")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_ms05f")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_t3rvs")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_n46sx")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_48smm")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_tjhup")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_c84of")
}],
"loop": true,
"name": &"Idle",
"speed": 5.0
}, {
"frames": [{
"duration": 1.0,
"texture": SubResource("AtlasTexture_i67oc")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_x3ooa")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_7bpxf")
}],
"loop": true,
"name": &"Jump",
"speed": 5.0
}, {
"frames": [{
"duration": 1.0,
"texture": SubResource("AtlasTexture_fexn1")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_3uk3c")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_iiovw")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_sx58l")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_6l2e7")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_acowd")
}],
"loop": true,
"name": &"Land",
"speed": 15.0
}, {
"frames": [{
"duration": 1.0,
"texture": SubResource("AtlasTexture_3u4f5")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_dpm2p")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_qh1us")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_7kb6t")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_p5dlr")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_g7jsr")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_aq6al")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_ftoh7")
}],
"loop": true,
"name": &"Run",
"speed": 15.0
}, {
"frames": [{
"duration": 1.0,
"texture": SubResource("AtlasTexture_a247p")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_24aks")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_hs577")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_1sf80")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_h515b")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_nx0f0")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_6ow76")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_ygn7k")
}],
"loop": true,
"name": &"Walk",
"speed": 10.0
}]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_wc2sw"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_wc2sw"]
size = Vector2(16, 29) size = Vector2(16, 29)
[node name="Player" type="CharacterBody2D"] [node name="Player" type="CharacterBody2D"]
script = ExtResource("1_v1ece") script = ExtResource("1_23c20")
[node name="AnimationPlayer" type="AnimationPlayer" parent="."] [node name="Sprites" type="AnimatedSprite2D" parent="."]
root_node = NodePath("../Sprites")
autoplay = "Idle"
libraries = {
"": SubResource("AnimationLibrary_6srtp")
}
[node name="Sprites" type="Sprite2D" parent="."]
texture_filter = 1 texture_filter = 1
position = Vector2(0, -16) position = Vector2(0, -16)
texture = ExtResource("1_h6xei") sprite_frames = SubResource("SpriteFrames_gvqqb")
hframes = 10 animation = &"Run"
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2(0, -14.5) position = Vector2(0, -14.5)

View File

@ -19,4 +19,6 @@ config/icon="res://icon.svg"
window/size/viewport_width=500 window/size/viewport_width=500
window/size/viewport_height=300 window/size/viewport_height=300
window/stretch/scale=2.0 window/size/window_width_override=1600
window/size/window_height_override=1050
window/stretch/scale=3.0