Clean up player scripts

This commit is contained in:
Mathilde Grapin 2023-06-28 21:29:55 +02:00
parent a1e8f9b34d
commit a08afcb147
7 changed files with 75 additions and 75 deletions

View file

@ -8,11 +8,51 @@
[ext_resource type="Script" path="res://scripts/state_machine/state_machine.gd" id="6_hl63m"] [ext_resource type="Script" path="res://scripts/state_machine/state_machine.gd" id="6_hl63m"]
[ext_resource type="Script" path="res://scripts/player/states/player_idle_state.gd" id="7_0nfrc"] [ext_resource type="Script" path="res://scripts/player/states/player_idle_state.gd" id="7_0nfrc"]
[ext_resource type="Script" path="res://scripts/player/states/player_walk_state.gd" id="7_gcd3q"] [ext_resource type="Script" path="res://scripts/player/states/player_walk_state.gd" id="7_gcd3q"]
[ext_resource type="Script" path="res://scripts/player/states/player_throw_state.gd" id="9_atm3r"] [ext_resource type="Script" path="res://scripts/player/states/player_hit_ball_state.gd" id="9_aqfsh"]
[sub_resource type="Animation" id="Animation_rh7n7"] [sub_resource type="Animation" id="Animation_rh7n7"]
length = 0.001 length = 0.001
[sub_resource type="Animation" id="Animation_gswgu"]
resource_name = "hit_ball"
length = 0.4
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("Sprite2D:texture")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [ExtResource("5_2s1mg")]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath("Sprite2D:hframes")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [4]
}
tracks/2/type = "value"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath("Sprite2D:frame")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(0, 0.1, 0.2, 0.3),
"transitions": PackedFloat32Array(1, 1, 1, 1),
"update": 1,
"values": [0, 1, 2, 3]
}
[sub_resource type="Animation" id="Animation_7igie"] [sub_resource type="Animation" id="Animation_7igie"]
resource_name = "idle" resource_name = "idle"
length = 0.4 length = 0.4
@ -95,46 +135,6 @@ tracks/2/keys = {
"values": [0, 1, 2, 3, 4, 5] "values": [0, 1, 2, 3, 4, 5]
} }
[sub_resource type="Animation" id="Animation_gswgu"]
resource_name = "throw"
length = 0.4
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("Sprite2D:texture")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [ExtResource("5_2s1mg")]
}
tracks/1/type = "value"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath("Sprite2D:hframes")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [4]
}
tracks/2/type = "value"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath("Sprite2D:frame")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(0, 0.1, 0.2, 0.3),
"transitions": PackedFloat32Array(1, 1, 1, 1),
"update": 1,
"values": [0, 1, 2, 3]
}
[sub_resource type="Animation" id="Animation_pjkea"] [sub_resource type="Animation" id="Animation_pjkea"]
resource_name = "walk" resource_name = "walk"
length = 0.6 length = 0.6
@ -179,9 +179,9 @@ tracks/2/keys = {
[sub_resource type="AnimationLibrary" id="AnimationLibrary_ldfyt"] [sub_resource type="AnimationLibrary" id="AnimationLibrary_ldfyt"]
_data = { _data = {
"RESET": SubResource("Animation_rh7n7"), "RESET": SubResource("Animation_rh7n7"),
"hit_ball": SubResource("Animation_gswgu"),
"idle": SubResource("Animation_7igie"), "idle": SubResource("Animation_7igie"),
"run": SubResource("Animation_xfiuh"), "run": SubResource("Animation_xfiuh"),
"throw": SubResource("Animation_gswgu"),
"walk": SubResource("Animation_pjkea") "walk": SubResource("Animation_pjkea")
} }
@ -189,7 +189,7 @@ _data = {
size = Vector2(21, 10) size = Vector2(21, 10)
[node name="Player" type="CharacterBody2D"] [node name="Player" type="CharacterBody2D"]
collision_mask = 14 collision_mask = 6
script = ExtResource("1_fmx2p") script = ExtResource("1_fmx2p")
[node name="Sprite2D" type="Sprite2D" parent="."] [node name="Sprite2D" type="Sprite2D" parent="."]
@ -225,9 +225,10 @@ script = ExtResource("7_0nfrc")
[node name="Walk" type="Node" parent="StateMachine"] [node name="Walk" type="Node" parent="StateMachine"]
script = ExtResource("7_gcd3q") script = ExtResource("7_gcd3q")
[node name="Throw" type="Node" parent="StateMachine"] [node name="HitBall" type="Node" parent="StateMachine"]
script = ExtResource("9_atm3r") script = ExtResource("9_aqfsh")
[connection signal="collide_with_ball" from="." to="StateMachine/Idle" method="_on_player_collide_with_ball"] [connection signal="ball_starts_colliding" from="." to="StateMachine/Idle" method="_on_player_ball_starts_colliding"]
[connection signal="animation_finished" from="AnimationPlayer" to="StateMachine/Throw" method="_on_animation_player_animation_finished"] [connection signal="ball_stops_colliding" from="." to="StateMachine/Idle" method="_on_player_ball_stops_colliding"]
[connection signal="body_entered" from="Area2D" to="." method="_on_area_2d_body_entered"] [connection signal="body_entered" from="Area2D" to="." method="_on_area_2d_body_entered"]
[connection signal="body_exited" from="Area2D" to="." method="_on_area_2d_body_exited"]

View file

@ -11,12 +11,12 @@ func _ready():
target.y += Y_OFFSET target.y += Y_OFFSET
var player = get_node("/root/Main/TileMap/Player") var player = get_node("/root/Main/TileMap/Player")
assert(player) assert(player)
player.hit.connect(_on_player_hit) player.hit_ball.connect(_on_player_hit_ball)
func _physics_process(delta): func _physics_process(delta):
position = position.move_toward(target, delta * speed) position = position.move_toward(target, delta * speed)
func _on_player_hit(): func _on_player_hit_ball():
var rand_cell: Vector2i = tile_map.get_random_top_cell() var rand_cell: Vector2i = tile_map.get_random_top_cell()
tile_map.reset_and_set_target_cell(rand_cell) tile_map.reset_and_set_target_cell(rand_cell)
target = tile_map.map_to_local(rand_cell) + Vector2(0, Y_OFFSET) target = tile_map.map_to_local(rand_cell) + Vector2(0, Y_OFFSET)

View file

@ -1,7 +1,7 @@
class_name Player class_name Player
extends CharacterBody2D extends CharacterBody2D
signal hit signal hit_ball
signal ball_starts_colliding signal ball_starts_colliding
signal ball_stops_colliding signal ball_stops_colliding
const Y_SPAWN_OFFSET = -8 const Y_SPAWN_OFFSET = -8
@ -30,3 +30,15 @@ func flip_sprite():
sprite.flip_h = true sprite.flip_h = true
elif Input.is_action_pressed("move_right"): elif Input.is_action_pressed("move_right"):
sprite.flip_h = false sprite.flip_h = false
func play_idle_animation():
animation_player.play("idle")
func play_walk_animation():
animation_player.play("walk")
func play_hit_ball_animation():
animation_player.speed_scale = 0.8
animation_player.play("hit_ball")
await animation_player.animation_finished
animation_player.speed_scale = 1

View file

@ -0,0 +1,7 @@
class_name PlayerHitBallState
extends PlayerState
func enter(_msg := {}):
player.hit_ball.emit()
await player.play_hit_ball_animation()
state_machine.transition_to("Idle")

View file

@ -1,20 +0,0 @@
class_name PlayerThrowState
extends PlayerState
var is_animation_finished = false
func enter(_msg := {}):
player.animation_player.speed_scale = 0.8
player.animation_player.play("throw")
player.hit.emit()
func update(_delta: float):
if is_animation_finished:
state_machine.transition_to("Idle")
func exit():
is_animation_finished = false
player.animation_player.speed_scale = 1
func _on_animation_player_animation_finished(_anim_name):
is_animation_finished = true

View file

@ -5,11 +5,11 @@ var player_collide_with_ball = false
func enter(_msg := {}): func enter(_msg := {}):
player.velocity = Vector2.ZERO player.velocity = Vector2.ZERO
player.animation_player.play("idle") player.play_idle_animation()
func update(_delta): func update(_delta):
if player_collide_with_ball && Input.is_action_pressed("hit"): if player_collide_with_ball && Input.is_action_pressed("hit"):
state_machine.transition_to("Hit") state_machine.transition_to("HitBall")
if player.get_input_direction() != Vector2.ZERO: if player.get_input_direction() != Vector2.ZERO:
state_machine.transition_to("Walk") state_machine.transition_to("Walk")

View file

@ -2,7 +2,7 @@ class_name PlayerWalkState
extends PlayerState extends PlayerState
func enter(_msg := {}): func enter(_msg := {}):
player.animation_player.play("walk") player.play_walk_animation()
func physics_update(delta): func physics_update(delta):
var direction = player.get_input_direction() var direction = player.get_input_direction()
@ -12,7 +12,7 @@ func physics_update(delta):
var collision = player.move_and_collide(velocity * delta) var collision = player.move_and_collide(velocity * delta)
if collision and Input.is_action_pressed("hit"): if collision and Input.is_action_pressed("hit"):
state_machine.transition_to("Hit") state_machine.transition_to("HitBall")
if velocity == Vector2.ZERO: if velocity == Vector2.ZERO:
state_machine.transition_to("Idle") state_machine.transition_to("Idle")