diff --git a/scripts/player/player.gd b/scripts/player/player.gd index 7fa2cc0..68258bd 100644 --- a/scripts/player/player.gd +++ b/scripts/player/player.gd @@ -2,20 +2,31 @@ class_name Player extends CharacterBody2D signal hit -signal collide_with_ball +signal ball_starts_colliding +signal ball_stops_colliding +const Y_SPAWN_OFFSET = -8 @export var speed = 120 -var y_spawn_offset = -8 @onready var animation_player = $AnimationPlayer @onready var sprite = $Sprite2D @onready var tile_map: TileMap = get_parent() func _ready(): var spawn_cell: Vector2i = tile_map.get_bottom_spawn_cell() - position = tile_map.map_to_local(spawn_cell) - position.y += y_spawn_offset + position = tile_map.map_to_local(spawn_cell) + Vector2(0, Y_SPAWN_OFFSET) - -func _on_area_2d_body_entered(_body: Node2D): +func _on_area_2d_body_entered(_body): # As player’s Area2D only collide with balls # We only enter this function after colliding with a ball - collide_with_ball.emit() + ball_starts_colliding.emit() + +func _on_area_2d_body_exited(_body): + ball_stops_colliding.emit() + +func get_input_direction(): + return Input.get_vector("move_left", "move_right", "move_up", "move_down") + +func flip_sprite(): + if Input.is_action_pressed("move_left"): + sprite.flip_h = true + elif Input.is_action_pressed("move_right"): + sprite.flip_h = false diff --git a/scripts/player/states/player_throw_state.gd b/scripts/player/states/player_hit_state.gd similarity index 100% rename from scripts/player/states/player_throw_state.gd rename to scripts/player/states/player_hit_state.gd diff --git a/scripts/player/states/player_idle_state.gd b/scripts/player/states/player_idle_state.gd index c8f8fbb..9221a9e 100644 --- a/scripts/player/states/player_idle_state.gd +++ b/scripts/player/states/player_idle_state.gd @@ -1,23 +1,21 @@ class_name PlayerIdleState extends PlayerState -var collide_with_ball = false +var player_collide_with_ball = false func enter(_msg := {}): player.velocity = Vector2.ZERO player.animation_player.play("idle") func update(_delta): - if collide_with_ball && Input.is_action_pressed("hit"): - collide_with_ball = false - state_machine.transition_to("Throw") - - if get_input_direction() != Vector2.ZERO: + if player_collide_with_ball && Input.is_action_pressed("hit"): + state_machine.transition_to("Hit") + + if player.get_input_direction() != Vector2.ZERO: state_machine.transition_to("Walk") -func get_input_direction(): - return Input.get_vector("move_left", "move_right", "move_up", "move_down") +func _on_player_ball_starts_colliding(): + player_collide_with_ball = true - -func _on_player_collide_with_ball(): - collide_with_ball = true +func _on_player_ball_stops_colliding(): + player_collide_with_ball = false diff --git a/scripts/player/states/player_walk_state.gd b/scripts/player/states/player_walk_state.gd index c091ea3..56f468b 100644 --- a/scripts/player/states/player_walk_state.gd +++ b/scripts/player/states/player_walk_state.gd @@ -5,22 +5,14 @@ func enter(_msg := {}): player.animation_player.play("walk") func physics_update(delta): - var direction = get_input_direction() + 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("Throw") + state_machine.transition_to("Hit") if velocity == Vector2.ZERO: state_machine.transition_to("Idle") - -func get_input_direction(): - var direction = Input.get_vector("move_left", "move_right", "move_up", "move_down") - - if Input.is_action_pressed("move_left"): - player.sprite.flip_h = true - elif Input.is_action_pressed("move_right"): - player.sprite.flip_h = false - - return direction