diff --git a/scenes/player.tscn b/scenes/player.tscn index 2bdd068..c8d8081 100644 --- a/scenes/player.tscn +++ b/scenes/player.tscn @@ -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/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_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"] 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"] resource_name = "idle" length = 0.4 @@ -95,46 +135,6 @@ tracks/2/keys = { "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"] resource_name = "walk" length = 0.6 @@ -179,9 +179,9 @@ tracks/2/keys = { [sub_resource type="AnimationLibrary" id="AnimationLibrary_ldfyt"] _data = { "RESET": SubResource("Animation_rh7n7"), +"hit_ball": SubResource("Animation_gswgu"), "idle": SubResource("Animation_7igie"), "run": SubResource("Animation_xfiuh"), -"throw": SubResource("Animation_gswgu"), "walk": SubResource("Animation_pjkea") } @@ -189,7 +189,7 @@ _data = { size = Vector2(21, 10) [node name="Player" type="CharacterBody2D"] -collision_mask = 14 +collision_mask = 6 script = ExtResource("1_fmx2p") [node name="Sprite2D" type="Sprite2D" parent="."] @@ -225,9 +225,10 @@ script = ExtResource("7_0nfrc") [node name="Walk" type="Node" parent="StateMachine"] script = ExtResource("7_gcd3q") -[node name="Throw" type="Node" parent="StateMachine"] -script = ExtResource("9_atm3r") +[node name="HitBall" type="Node" parent="StateMachine"] +script = ExtResource("9_aqfsh") -[connection signal="collide_with_ball" from="." to="StateMachine/Idle" method="_on_player_collide_with_ball"] -[connection signal="animation_finished" from="AnimationPlayer" to="StateMachine/Throw" method="_on_animation_player_animation_finished"] +[connection signal="ball_starts_colliding" from="." to="StateMachine/Idle" method="_on_player_ball_starts_colliding"] +[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_exited" from="Area2D" to="." method="_on_area_2d_body_exited"] diff --git a/scripts/ball/ball.gd b/scripts/ball/ball.gd index bdd1208..8d8f0cf 100644 --- a/scripts/ball/ball.gd +++ b/scripts/ball/ball.gd @@ -11,12 +11,12 @@ func _ready(): target.y += Y_OFFSET var player = get_node("/root/Main/TileMap/Player") assert(player) - player.hit.connect(_on_player_hit) + player.hit_ball.connect(_on_player_hit_ball) func _physics_process(delta): 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() tile_map.reset_and_set_target_cell(rand_cell) target = tile_map.map_to_local(rand_cell) + Vector2(0, Y_OFFSET) diff --git a/scripts/player/player.gd b/scripts/player/player.gd index 68258bd..ebf7c54 100644 --- a/scripts/player/player.gd +++ b/scripts/player/player.gd @@ -1,7 +1,7 @@ class_name Player extends CharacterBody2D -signal hit +signal hit_ball signal ball_starts_colliding signal ball_stops_colliding const Y_SPAWN_OFFSET = -8 @@ -30,3 +30,15 @@ func flip_sprite(): sprite.flip_h = true elif Input.is_action_pressed("move_right"): 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 diff --git a/scripts/player/states/player_hit_ball_state.gd b/scripts/player/states/player_hit_ball_state.gd new file mode 100644 index 0000000..bf2150a --- /dev/null +++ b/scripts/player/states/player_hit_ball_state.gd @@ -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") diff --git a/scripts/player/states/player_hit_state.gd b/scripts/player/states/player_hit_state.gd deleted file mode 100644 index 2f4723d..0000000 --- a/scripts/player/states/player_hit_state.gd +++ /dev/null @@ -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 diff --git a/scripts/player/states/player_idle_state.gd b/scripts/player/states/player_idle_state.gd index 9221a9e..71ec8ec 100644 --- a/scripts/player/states/player_idle_state.gd +++ b/scripts/player/states/player_idle_state.gd @@ -5,11 +5,11 @@ var player_collide_with_ball = false func enter(_msg := {}): player.velocity = Vector2.ZERO - player.animation_player.play("idle") + player.play_idle_animation() func update(_delta): 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: state_machine.transition_to("Walk") diff --git a/scripts/player/states/player_walk_state.gd b/scripts/player/states/player_walk_state.gd index 56f468b..27de07f 100644 --- a/scripts/player/states/player_walk_state.gd +++ b/scripts/player/states/player_walk_state.gd @@ -2,17 +2,17 @@ class_name PlayerWalkState extends PlayerState func enter(_msg := {}): - player.animation_player.play("walk") + player.play_walk_animation() func physics_update(delta): var direction = player.get_input_direction() var velocity = direction * player.speed player.flip_sprite() - + var collision = player.move_and_collide(velocity * delta) if collision and Input.is_action_pressed("hit"): - state_machine.transition_to("Hit") + state_machine.transition_to("HitBall") if velocity == Vector2.ZERO: state_machine.transition_to("Idle")