Compare commits
4 commits
37d5fa981d
...
1f5b08e39f
| Author | SHA1 | Date | |
|---|---|---|---|
| 1f5b08e39f | |||
| 16fba6caed | |||
| 969d264b6e | |||
| cd9e52cf9b |
9 changed files with 136 additions and 46 deletions
|
|
@ -12,21 +12,21 @@ func _ready() -> void:
|
||||||
assert(player != null, "The PlayerState state type must be used only in the player scene. It needs the owner to be a Player node.")
|
assert(player != null, "The PlayerState state type must be used only in the player scene. It needs the owner to be a Player node.")
|
||||||
|
|
||||||
|
|
||||||
func enter(previous_state_path: String, data: Dictionary = {}) -> void:
|
func _enter(_previous_state_path: String, _data: Dictionary = {}) -> void:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
func exit() -> void:
|
func _exit() -> void:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
func handle_input(_event: InputEvent) -> void:
|
func _handle_input(_event: InputEvent) -> void:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
func state_update(_delta: float) -> void:
|
func _state_update(_delta: float) -> void:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
func state_physics_update(_delta: float) -> void:
|
func _state_physics_update(_delta: float) -> void:
|
||||||
pass
|
pass
|
||||||
|
|
|
||||||
|
|
@ -6,19 +6,53 @@
|
||||||
@warning_ignore("unused_signal")
|
@warning_ignore("unused_signal")
|
||||||
signal finished(next_state_path: String, data: Dictionary)
|
signal finished(next_state_path: String, data: Dictionary)
|
||||||
|
|
||||||
|
|
||||||
|
## For 'ghost frame' update fix
|
||||||
|
var is_active: bool = false :
|
||||||
|
set(value):
|
||||||
|
is_active = value
|
||||||
|
get:
|
||||||
|
return is_active
|
||||||
|
|
||||||
|
|
||||||
## Called by the state machine when receiving unhandled input events.
|
## Called by the state machine when receiving unhandled input events.
|
||||||
@abstract func handle_input(_event: InputEvent) -> void
|
@abstract func _handle_input(event: InputEvent) -> void
|
||||||
|
|
||||||
## Called by the state machine on the engine's main loop tick.
|
## Called by the state machine on the engine's main loop tick.
|
||||||
@abstract func state_update(_delta: float) -> void
|
@abstract func _state_update(delta: float) -> void
|
||||||
|
|
||||||
## Called by the state machine on the engine's physics update tick.
|
## Called by the state machine on the engine's physics update tick.
|
||||||
@abstract func state_physics_update(_delta: float) -> void
|
@abstract func _state_physics_update(delta: float) -> void
|
||||||
|
|
||||||
## Called by the state machine upon changing the active state. The `data` parameter
|
## Called by the state machine upon changing the active state. The `data` parameter
|
||||||
## is a dictionary with arbitrary data the state can use to initialize itself.
|
## is a dictionary with arbitrary data the state can use to initialize itself.
|
||||||
@abstract func enter(previous_state_path: String, data: Dictionary = {}) -> void
|
@abstract func _enter(previous_state_path: String, data: Dictionary = {}) -> void
|
||||||
|
|
||||||
## Called by the state machine before changing the active state. Use this function
|
## Called by the state machine before changing the active state. Use this function
|
||||||
## to clean up the state.
|
## to clean up the state.
|
||||||
@abstract func exit() -> void
|
@abstract func _exit() -> void
|
||||||
|
|
||||||
|
|
||||||
|
func handle_input(event: InputEvent) -> void:
|
||||||
|
if not is_active: return
|
||||||
|
_handle_input(event)
|
||||||
|
|
||||||
|
|
||||||
|
func state_update(delta: float) -> void:
|
||||||
|
if not is_active: return
|
||||||
|
_state_update(delta)
|
||||||
|
|
||||||
|
|
||||||
|
func state_physics_update(delta: float) -> void:
|
||||||
|
if not is_active: return
|
||||||
|
_state_physics_update(delta)
|
||||||
|
|
||||||
|
|
||||||
|
func enter(previous_state_path: String, data: Dictionary = {}) -> void:
|
||||||
|
is_active = true
|
||||||
|
_enter(previous_state_path, data)
|
||||||
|
|
||||||
|
|
||||||
|
func exit() -> void:
|
||||||
|
is_active = false
|
||||||
|
_exit()
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
## Virtual base class for all nodes that deal directly with states.
|
## Virtual base class for all nodes that deal directly with states.
|
||||||
## Extend this class and override its methods to implement a state machine.
|
## Extend this class and override its methods to implement a state machine.
|
||||||
@abstract class_name StateMachine extends Node
|
class_name StateMachine extends Node
|
||||||
|
|
||||||
## The initial state of the state machine. If not set, the first child node is used.
|
## The initial state of the state machine. If not set, the first child node is used.
|
||||||
@export var initial_state: State
|
@export var initial_state: State
|
||||||
|
|
@ -22,11 +22,11 @@ func _unhandled_input(event: InputEvent) -> void:
|
||||||
|
|
||||||
|
|
||||||
func _process(delta: float) -> void:
|
func _process(delta: float) -> void:
|
||||||
state.update(delta)
|
state.state_update(delta)
|
||||||
|
|
||||||
|
|
||||||
func _physics_process(delta: float) -> void:
|
func _physics_process(delta: float) -> void:
|
||||||
state.physics_update(delta)
|
state.state_physics_update(delta)
|
||||||
|
|
||||||
|
|
||||||
## Called when initial state is not specified.
|
## Called when initial state is not specified.
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ class_name Player extends CharacterBody2D
|
||||||
@export var max_speed: float = 90
|
@export var max_speed: float = 90
|
||||||
|
|
||||||
@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
|
@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
|
||||||
|
@onready var state_machine: StateMachine = $StateMachine
|
||||||
|
|
||||||
var is_hitting: bool = false
|
var is_hitting: bool = false
|
||||||
var was_moving: 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")
|
assert(id == 1 or id == 2, "id ("+str(id)+") is invalid")
|
||||||
anim_dir = 'up' if id == 1 else 'down'
|
anim_dir = 'up' if id == 1 else 'down'
|
||||||
def_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:
|
func get_movement_vector() -> Vector2:
|
||||||
var x_mov: float = Input.get_action_strength('move_right') - Input.get_action_strength('move_left')
|
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="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="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"]
|
[sub_resource type="AtlasTexture" id="AtlasTexture_g2els"]
|
||||||
atlas = ExtResource("2_3li8b")
|
atlas = ExtResource("2_3li8b")
|
||||||
|
|
@ -141,3 +145,20 @@ offset = Vector2(0, -16)
|
||||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||||
position = Vector2(0, -8)
|
position = Vector2(0, -8)
|
||||||
shape = SubResource("CapsuleShape2D_oprun")
|
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