Compare commits
No commits in common. "4429984b2b08a0404e667a51ca1fb84068722ac1" and "1f5b08e39fa814696005c4814a1865ba96fce52f" have entirely different histories.
4429984b2b
...
1f5b08e39f
8 changed files with 63 additions and 23 deletions
|
|
@ -11,9 +11,8 @@ class_name StateMachine extends Node
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
for state_node: State in find_children("*", "State"):
|
for state_node: State in find_children("*", "State"):
|
||||||
# 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)
|
state_node.finished.connect(_transition_to_next_state)
|
||||||
|
|
||||||
await owner.ready
|
await owner.ready
|
||||||
state.enter("")
|
state.enter("")
|
||||||
|
|
||||||
|
|
@ -39,12 +38,6 @@ func _get_initial_state() -> State:
|
||||||
|
|
||||||
## Transitions the active state out after receiving a finished signal.
|
## Transitions the active state out after receiving a finished signal.
|
||||||
func _transition_to_next_state(target_state_path: String, data: Dictionary = {}) -> void:
|
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):
|
if not has_node(target_state_path):
|
||||||
printerr(owner.name + ": Trying to transition to state " + target_state_path + " but it does not exist.")
|
printerr(owner.name + ": Trying to transition to state " + target_state_path + " but it does not exist.")
|
||||||
return
|
return
|
||||||
|
|
|
||||||
11
scenes/hit/hit.gd
Normal file
11
scenes/hit/hit.gd
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
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
scenes/hit/hit.gd.uid
Normal file
1
scenes/hit/hit.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
uid://c0m6xhbtgbgtu
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
[gd_scene load_steps=3 format=3 uid="uid://px67runjx6ex"]
|
[gd_scene load_steps=4 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"]
|
[ext_resource type="Texture2D" uid="uid://c6a62gvw7218s" path="res://scenes/hit/hit.png" id="1_wjo4f"]
|
||||||
|
|
||||||
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_fxeki"]
|
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_fxeki"]
|
||||||
|
|
@ -9,6 +10,7 @@ height = 18.0
|
||||||
[node name="Hit" type="Area2D" groups=["hit"]]
|
[node name="Hit" type="Area2D" groups=["hit"]]
|
||||||
collision_layer = 64
|
collision_layer = 64
|
||||||
collision_mask = 0
|
collision_mask = 0
|
||||||
|
script = ExtResource("1_fxeki")
|
||||||
|
|
||||||
[node name="Sprite2D" type="Sprite2D" parent="."]
|
[node name="Sprite2D" type="Sprite2D" parent="."]
|
||||||
position = Vector2(7, -4)
|
position = Vector2(7, -4)
|
||||||
|
|
@ -19,3 +21,8 @@ position = Vector2(5, -1)
|
||||||
rotation = 1.5707964
|
rotation = 1.5707964
|
||||||
shape = SubResource("CapsuleShape2D_fxeki")
|
shape = SubResource("CapsuleShape2D_fxeki")
|
||||||
debug_color = Color(0.6205005, 0.42311785, 0.8432588, 0.41960785)
|
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,14 +1,13 @@
|
||||||
class_name Player extends CharacterBody2D
|
class_name Player extends CharacterBody2D
|
||||||
|
|
||||||
const DEADZONE: float = 0.1
|
|
||||||
|
|
||||||
@export var id: int = 1
|
@export var id: int = 1
|
||||||
@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
|
@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 anim_dir: String
|
||||||
var def_dir: String
|
var def_dir: String
|
||||||
|
|
||||||
|
|
@ -21,3 +20,34 @@ 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')
|
||||||
var y_mov: float = Input.get_action_strength('move_down') - Input.get_action_strength('move_up')
|
var y_mov: float = Input.get_action_strength('move_down') - Input.get_action_strength('move_up')
|
||||||
return Vector2(x_mov, y_mov)
|
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="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://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://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://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"]
|
[sub_resource type="AtlasTexture" id="AtlasTexture_g2els"]
|
||||||
atlas = ExtResource("2_3li8b")
|
atlas = ExtResource("2_3li8b")
|
||||||
|
|
@ -160,9 +160,5 @@ script = ExtResource("5_75vfm")
|
||||||
metadata/_custom_type_script = "uid://b2sr7p80gdjii"
|
metadata/_custom_type_script = "uid://b2sr7p80gdjii"
|
||||||
|
|
||||||
[node name="Hitting" type="Node" parent="StateMachine"]
|
[node name="Hitting" type="Node" parent="StateMachine"]
|
||||||
script = ExtResource("6_75vfm")
|
script = ExtResource("4_p47bc")
|
||||||
metadata/_custom_type_script = "uid://b2sr7p80gdjii"
|
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:
|
func _state_physics_update(_delta: float) -> void:
|
||||||
var movement_vector: Vector2 = player.get_movement_vector()
|
var movement_vector: Vector2 = player.get_movement_vector()
|
||||||
|
|
||||||
if movement_vector.length() > player.DEADZONE:
|
if movement_vector.length() >= 1:
|
||||||
finished.emit(RUNNING)
|
finished.emit(RUNNING)
|
||||||
return
|
return
|
||||||
elif Input.is_action_pressed("hit_left"):
|
elif Input.is_action_pressed("hit_left"):
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ func _state_physics_update(_delta: float) -> void:
|
||||||
|
|
||||||
player.move_and_slide()
|
player.move_and_slide()
|
||||||
|
|
||||||
if movement_vector.length() <= player.DEADZONE:
|
if movement_vector.length() == 0:
|
||||||
finished.emit(IDLE)
|
finished.emit(IDLE)
|
||||||
return
|
return
|
||||||
elif Input.is_action_just_pressed("hit_left"):
|
elif Input.is_action_just_pressed("hit_left"):
|
||||||
|
|
@ -30,6 +30,8 @@ func _state_physics_update(_delta: float) -> void:
|
||||||
|
|
||||||
player.sprite.play(player.anim_dir)
|
player.sprite.play(player.anim_dir)
|
||||||
|
|
||||||
|
print("physupdate")
|
||||||
|
|
||||||
func _exit() -> void:
|
func _exit() -> void:
|
||||||
player.sprite.stop()
|
player.sprite.stop()
|
||||||
print("exit")
|
print("exit")
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue