WIP clean up player

This commit is contained in:
Mathilde Grapin 2023-06-28 00:07:13 +02:00
parent 97f9ec7fa1
commit a1e8f9b34d
4 changed files with 31 additions and 30 deletions

View file

@ -2,20 +2,31 @@ class_name Player
extends CharacterBody2D extends CharacterBody2D
signal hit signal hit
signal collide_with_ball signal ball_starts_colliding
signal ball_stops_colliding
const Y_SPAWN_OFFSET = -8
@export var speed = 120 @export var speed = 120
var y_spawn_offset = -8
@onready var animation_player = $AnimationPlayer @onready var animation_player = $AnimationPlayer
@onready var sprite = $Sprite2D @onready var sprite = $Sprite2D
@onready var tile_map: TileMap = get_parent() @onready var tile_map: TileMap = get_parent()
func _ready(): func _ready():
var spawn_cell: Vector2i = tile_map.get_bottom_spawn_cell() var spawn_cell: Vector2i = tile_map.get_bottom_spawn_cell()
position = tile_map.map_to_local(spawn_cell) position = tile_map.map_to_local(spawn_cell) + Vector2(0, Y_SPAWN_OFFSET)
position.y += y_spawn_offset
func _on_area_2d_body_entered(_body):
func _on_area_2d_body_entered(_body: Node2D):
# As players Area2D only collide with balls # As players Area2D only collide with balls
# We only enter this function after colliding with a ball # 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

View file

@ -1,23 +1,21 @@
class_name PlayerIdleState class_name PlayerIdleState
extends PlayerState extends PlayerState
var collide_with_ball = false 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.animation_player.play("idle")
func update(_delta): func update(_delta):
if collide_with_ball && Input.is_action_pressed("hit"): if player_collide_with_ball && Input.is_action_pressed("hit"):
collide_with_ball = false state_machine.transition_to("Hit")
state_machine.transition_to("Throw")
if get_input_direction() != Vector2.ZERO: if player.get_input_direction() != Vector2.ZERO:
state_machine.transition_to("Walk") state_machine.transition_to("Walk")
func get_input_direction(): func _on_player_ball_starts_colliding():
return Input.get_vector("move_left", "move_right", "move_up", "move_down") player_collide_with_ball = true
func _on_player_ball_stops_colliding():
func _on_player_collide_with_ball(): player_collide_with_ball = false
collide_with_ball = true

View file

@ -5,22 +5,14 @@ func enter(_msg := {}):
player.animation_player.play("walk") player.animation_player.play("walk")
func physics_update(delta): func physics_update(delta):
var direction = get_input_direction() var direction = player.get_input_direction()
var velocity = direction * player.speed var velocity = direction * player.speed
player.flip_sprite()
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("Throw") state_machine.transition_to("Hit")
if velocity == Vector2.ZERO: if velocity == Vector2.ZERO:
state_machine.transition_to("Idle") 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