implement idle and running states for players
This commit is contained in:
parent
16fba6caed
commit
1f5b08e39f
6 changed files with 89 additions and 33 deletions
|
|
@ -4,6 +4,7 @@ class_name Player extends CharacterBody2D
|
|||
@export var max_speed: float = 90
|
||||
|
||||
@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
|
||||
@onready var state_machine: StateMachine = $StateMachine
|
||||
|
||||
var is_hitting: bool = false
|
||||
var was_moving: bool = false
|
||||
|
|
@ -14,38 +15,6 @@ func _ready() -> void:
|
|||
assert(id == 1 or id == 2, "id ("+str(id)+") is invalid")
|
||||
anim_dir = 'up' if id == 1 else 'down'
|
||||
def_dir = 'up' if id == 1 else 'down'
|
||||
sprite.play(def_dir)
|
||||
sprite.frame = 1
|
||||
sprite.pause()
|
||||
|
||||
func _physics_process(_delta: float) -> void:
|
||||
var movement_vector: Vector2 = get_movement_vector()
|
||||
var direction: Vector2 = movement_vector.normalized() if not is_hitting else Vector2.ZERO
|
||||
velocity = direction * max_speed
|
||||
|
||||
move_and_slide()
|
||||
|
||||
var is_moving: bool = velocity.length() > 10
|
||||
|
||||
if Input.is_action_pressed("hit_left") and not is_hitting:
|
||||
hit("left")
|
||||
elif Input.is_action_pressed("hit_right") and not is_hitting:
|
||||
hit("right")
|
||||
|
||||
if is_moving and not is_hitting:
|
||||
if abs(velocity.x) > abs(velocity.y):
|
||||
anim_dir = 'right' if velocity.x > 0 else 'left'
|
||||
else:
|
||||
anim_dir = 'down' if velocity.y > 0 else 'up'
|
||||
|
||||
sprite.play(anim_dir)
|
||||
else:
|
||||
if was_moving and not is_hitting:
|
||||
sprite.play(def_dir)
|
||||
sprite.frame = 1
|
||||
sprite.pause()
|
||||
|
||||
was_moving = is_moving
|
||||
|
||||
func get_movement_vector() -> Vector2:
|
||||
var x_mov: float = Input.get_action_strength('move_right') - Input.get_action_strength('move_left')
|
||||
|
|
|
|||
|
|
@ -1,7 +1,11 @@
|
|||
[gd_scene load_steps=17 format=3 uid="uid://642nm6c4mpxx"]
|
||||
[gd_scene load_steps=21 format=3 uid="uid://642nm6c4mpxx"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://rdqmsvofiots" path="res://scenes/player/player.gd" id="1_v6fml"]
|
||||
[ext_resource type="Texture2D" uid="uid://b8ptokcqwpdud" path="res://scenes/player/saffron.png" id="2_3li8b"]
|
||||
[ext_resource type="Script" uid="uid://dqjaxgmyxq3rx" path="res://scenes/classes/state_machine.gd" id="3_lvxji"]
|
||||
[ext_resource type="Script" uid="uid://delyni51vg6us" path="res://scenes/player/states/idle.gd" id="4_75vfm"]
|
||||
[ext_resource type="Script" uid="uid://b2sr7p80gdjii" path="res://scenes/classes/player_state.gd" id="4_p47bc"]
|
||||
[ext_resource type="Script" uid="uid://dxfs1b8fuk7kv" path="res://scenes/player/states/running.gd" id="5_75vfm"]
|
||||
|
||||
[sub_resource type="AtlasTexture" id="AtlasTexture_g2els"]
|
||||
atlas = ExtResource("2_3li8b")
|
||||
|
|
@ -141,3 +145,20 @@ offset = Vector2(0, -16)
|
|||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||
position = Vector2(0, -8)
|
||||
shape = SubResource("CapsuleShape2D_oprun")
|
||||
|
||||
[node name="StateMachine" type="Node" parent="." node_paths=PackedStringArray("initial_state")]
|
||||
script = ExtResource("3_lvxji")
|
||||
initial_state = NodePath("Idle")
|
||||
metadata/_custom_type_script = "uid://dqjaxgmyxq3rx"
|
||||
|
||||
[node name="Idle" type="Node" parent="StateMachine"]
|
||||
script = ExtResource("4_75vfm")
|
||||
metadata/_custom_type_script = "uid://b2sr7p80gdjii"
|
||||
|
||||
[node name="Running" type="Node" parent="StateMachine"]
|
||||
script = ExtResource("5_75vfm")
|
||||
metadata/_custom_type_script = "uid://b2sr7p80gdjii"
|
||||
|
||||
[node name="Hitting" type="Node" parent="StateMachine"]
|
||||
script = ExtResource("4_p47bc")
|
||||
metadata/_custom_type_script = "uid://b2sr7p80gdjii"
|
||||
|
|
|
|||
27
scenes/player/states/idle.gd
Normal file
27
scenes/player/states/idle.gd
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
extends PlayerState
|
||||
|
||||
|
||||
func _enter(_previous_state_path: String, _data: Dictionary = {}) -> void:
|
||||
print("entering idle")
|
||||
player.velocity = Vector2.ZERO
|
||||
player.move_and_slide()
|
||||
|
||||
player.sprite.play(player.def_dir)
|
||||
player.sprite.frame = 1
|
||||
player.sprite.pause()
|
||||
|
||||
print("entered")
|
||||
|
||||
|
||||
func _state_physics_update(_delta: float) -> void:
|
||||
var movement_vector: Vector2 = player.get_movement_vector()
|
||||
|
||||
if movement_vector.length() >= 1:
|
||||
finished.emit(RUNNING)
|
||||
return
|
||||
elif Input.is_action_pressed("hit_left"):
|
||||
finished.emit(HITTING, {hit_dir = "left"})
|
||||
return
|
||||
elif Input.is_action_pressed("hit_right"):
|
||||
finished.emit(HITTING, {hit_dir = "right"})
|
||||
return
|
||||
1
scenes/player/states/idle.gd.uid
Normal file
1
scenes/player/states/idle.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://delyni51vg6us
|
||||
37
scenes/player/states/running.gd
Normal file
37
scenes/player/states/running.gd
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
extends PlayerState
|
||||
|
||||
|
||||
func _enter(_previous_state_path: String, _data: Dictionary = {}) -> void:
|
||||
print("entering running")
|
||||
player.move_and_slide()
|
||||
|
||||
|
||||
func _state_physics_update(_delta: float) -> void:
|
||||
var movement_vector: Vector2 = player.get_movement_vector()
|
||||
var direction: Vector2 = movement_vector.normalized()
|
||||
player.velocity = direction * player.max_speed
|
||||
|
||||
player.move_and_slide()
|
||||
|
||||
if movement_vector.length() == 0:
|
||||
finished.emit(IDLE)
|
||||
return
|
||||
elif Input.is_action_just_pressed("hit_left"):
|
||||
finished.emit(HITTING, {hit_dir = "left"})
|
||||
return
|
||||
elif Input.is_action_just_pressed("hit_right"):
|
||||
finished.emit(HITTING, {hit_dir = "right"})
|
||||
return
|
||||
|
||||
if abs(player.velocity.x) > abs(player.velocity.y):
|
||||
player.anim_dir = 'right' if player.velocity.x > 0 else 'left'
|
||||
else:
|
||||
player.anim_dir = 'down' if player.velocity.y > 0 else 'up'
|
||||
|
||||
player.sprite.play(player.anim_dir)
|
||||
|
||||
print("physupdate")
|
||||
|
||||
func _exit() -> void:
|
||||
player.sprite.stop()
|
||||
print("exit")
|
||||
1
scenes/player/states/running.gd.uid
Normal file
1
scenes/player/states/running.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://dxfs1b8fuk7kv
|
||||
Loading…
Add table
Reference in a new issue