Compare commits
7 commits
1f5b08e39f
...
4429984b2b
| Author | SHA1 | Date | |
|---|---|---|---|
| 4429984b2b | |||
| a780839f4a | |||
| 4b21537f52 | |||
| 43500b3ca5 | |||
| 77a6bea4d6 | |||
| d7b4a2e90c | |||
| 8379f06e24 |
8 changed files with 23 additions and 63 deletions
|
|
@ -11,10 +11,11 @@ class_name StateMachine extends Node
|
|||
|
||||
func _ready() -> void:
|
||||
for state_node: State in find_children("*", "State"):
|
||||
state_node.finished.connect(_transition_to_next_state)
|
||||
|
||||
await owner.ready
|
||||
state.enter("")
|
||||
# fixes duplicate connections (not sure why)
|
||||
if not state_node.finished.is_connected(_transition_to_next_state):
|
||||
state_node.finished.connect(_transition_to_next_state)
|
||||
await owner.ready
|
||||
state.enter("")
|
||||
|
||||
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
|
|
@ -38,6 +39,12 @@ func _get_initial_state() -> State:
|
|||
|
||||
## Transitions the active state out after receiving a finished signal.
|
||||
func _transition_to_next_state(target_state_path: String, data: Dictionary = {}) -> void:
|
||||
print("+++ TRANSITION CALLED: ", target_state_path)
|
||||
print("+++ has node? ", has_node(target_state_path))
|
||||
print("+++ all children: ", get_children().map(func(c: Node) -> StringName: return c.name))
|
||||
|
||||
assert(has_node(target_state_path), owner.name + ": Trying to transition to state " + target_state_path + " but it does not exist.")
|
||||
|
||||
if not has_node(target_state_path):
|
||||
printerr(owner.name + ": Trying to transition to state " + target_state_path + " but it does not exist.")
|
||||
return
|
||||
|
|
|
|||
|
|
@ -1,11 +0,0 @@
|
|||
extends Area2D
|
||||
|
||||
@onready var timer: Timer = $Timer
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
timer.connect("timeout", _despawn)
|
||||
|
||||
|
||||
func _despawn() -> void:
|
||||
queue_free()
|
||||
|
|
@ -1 +0,0 @@
|
|||
uid://c0m6xhbtgbgtu
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
[gd_scene load_steps=4 format=3 uid="uid://px67runjx6ex"]
|
||||
[gd_scene load_steps=3 format=3 uid="uid://px67runjx6ex"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://c0m6xhbtgbgtu" path="res://scenes/hit/hit.gd" id="1_fxeki"]
|
||||
[ext_resource type="Texture2D" uid="uid://c6a62gvw7218s" path="res://scenes/hit/hit.png" id="1_wjo4f"]
|
||||
|
||||
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_fxeki"]
|
||||
|
|
@ -10,7 +9,6 @@ height = 18.0
|
|||
[node name="Hit" type="Area2D" groups=["hit"]]
|
||||
collision_layer = 64
|
||||
collision_mask = 0
|
||||
script = ExtResource("1_fxeki")
|
||||
|
||||
[node name="Sprite2D" type="Sprite2D" parent="."]
|
||||
position = Vector2(7, -4)
|
||||
|
|
@ -21,8 +19,3 @@ position = Vector2(5, -1)
|
|||
rotation = 1.5707964
|
||||
shape = SubResource("CapsuleShape2D_fxeki")
|
||||
debug_color = Color(0.6205005, 0.42311785, 0.8432588, 0.41960785)
|
||||
|
||||
[node name="Timer" type="Timer" parent="."]
|
||||
wait_time = 0.5
|
||||
one_shot = true
|
||||
autostart = true
|
||||
|
|
|
|||
|
|
@ -1,13 +1,14 @@
|
|||
class_name Player extends CharacterBody2D
|
||||
|
||||
const DEADZONE: float = 0.1
|
||||
|
||||
@export var id: int = 1
|
||||
@export var max_speed: float = 90
|
||||
|
||||
@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
|
||||
@onready var state_machine: StateMachine = $StateMachine
|
||||
@onready var hit_timer: Timer = $HitTimer
|
||||
|
||||
var is_hitting: bool = false
|
||||
var was_moving: bool = false
|
||||
var anim_dir: String
|
||||
var def_dir: String
|
||||
|
||||
|
|
@ -20,34 +21,3 @@ func get_movement_vector() -> Vector2:
|
|||
var x_mov: float = Input.get_action_strength('move_right') - Input.get_action_strength('move_left')
|
||||
var y_mov: float = Input.get_action_strength('move_down') - Input.get_action_strength('move_up')
|
||||
return Vector2(x_mov, y_mov)
|
||||
|
||||
func hit(dir: String) -> void:
|
||||
if not is_hitting:
|
||||
sprite.play('up' if id == 1 else 'down')
|
||||
sprite.frame = 1
|
||||
sprite.pause()
|
||||
|
||||
var sprite_texture: Texture2D = sprite.sprite_frames.get_frame_texture('up', 1)
|
||||
var hit_node: Area2D = preload("res://scenes/hit/hit.tscn").instantiate()
|
||||
|
||||
# flip entire node horizontally if spawning left
|
||||
# flip vertically if player 2
|
||||
hit_node.scale = Vector2(
|
||||
-1 if dir == "left" else 1,
|
||||
1 if id == 1 else -1
|
||||
)
|
||||
add_child(hit_node)
|
||||
|
||||
# set position according to frame width and height
|
||||
@warning_ignore("integer_division")
|
||||
hit_node.global_position = global_position + Vector2(
|
||||
(sprite_texture.get_width()/2)*(-1 if dir == "left" else 1),
|
||||
(sprite_texture.get_height()*-1) if id == 1 else 4
|
||||
)
|
||||
|
||||
hit_node.timer.connect("timeout", _on_hit_end)
|
||||
|
||||
is_hitting = true
|
||||
|
||||
func _on_hit_end() -> void:
|
||||
is_hitting = false
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@
|
|||
[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"]
|
||||
[ext_resource type="Script" uid="uid://n7v8leojhykt" path="res://scenes/player/states/hitting.gd" id="6_75vfm"]
|
||||
|
||||
[sub_resource type="AtlasTexture" id="AtlasTexture_g2els"]
|
||||
atlas = ExtResource("2_3li8b")
|
||||
|
|
@ -160,5 +160,9 @@ script = ExtResource("5_75vfm")
|
|||
metadata/_custom_type_script = "uid://b2sr7p80gdjii"
|
||||
|
||||
[node name="Hitting" type="Node" parent="StateMachine"]
|
||||
script = ExtResource("4_p47bc")
|
||||
script = ExtResource("6_75vfm")
|
||||
metadata/_custom_type_script = "uid://b2sr7p80gdjii"
|
||||
|
||||
[node name="HitTimer" type="Timer" parent="."]
|
||||
wait_time = 0.5
|
||||
one_shot = true
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ func _enter(_previous_state_path: String, _data: Dictionary = {}) -> void:
|
|||
func _state_physics_update(_delta: float) -> void:
|
||||
var movement_vector: Vector2 = player.get_movement_vector()
|
||||
|
||||
if movement_vector.length() >= 1:
|
||||
if movement_vector.length() > player.DEADZONE:
|
||||
finished.emit(RUNNING)
|
||||
return
|
||||
elif Input.is_action_pressed("hit_left"):
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ func _state_physics_update(_delta: float) -> void:
|
|||
|
||||
player.move_and_slide()
|
||||
|
||||
if movement_vector.length() == 0:
|
||||
if movement_vector.length() <= player.DEADZONE:
|
||||
finished.emit(IDLE)
|
||||
return
|
||||
elif Input.is_action_just_pressed("hit_left"):
|
||||
|
|
@ -29,8 +29,6 @@ func _state_physics_update(_delta: float) -> void:
|
|||
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()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue