class_name PlayerStateMoving extends PlayerState var is_moving := false var target_position: Vector2 func _state_enter() -> void: is_moving = false func _state_exit() -> void: is_moving = false func _physics_process(delta: float) -> void: var map: TileMapLayer = Game.world.get_current_map().get_node("TileMapLayer") if not is_moving: if Input.is_action_pressed("ui_up") and not player.ray_cast_up.is_colliding(): is_moving = true player.current_direction = Vector2.UP player.sprite.play("walk_up") elif Input.is_action_pressed("ui_down") and not player.ray_cast_down.is_colliding(): is_moving = true player.current_direction = Vector2.DOWN player.sprite.play("walk_down") elif Input.is_action_pressed("ui_left") and not player.ray_cast_left.is_colliding(): is_moving = true player.current_direction = Vector2.LEFT player.sprite.play("walk_left") elif Input.is_action_pressed("ui_right") and not player.ray_cast_right.is_colliding(): is_moving = true player.current_direction = Vector2.RIGHT player.sprite.play("walk_right") else: player.set_state(player.state_idle) var target_tile := map.local_to_map(player.global_position) + Vector2i(player.current_direction) target_position = map.map_to_local(target_tile) if is_moving: player.global_position = player.global_position.move_toward( target_position, delta * player.current_speed ) get_viewport().get_camera_2d().global_position = player.global_position if player.global_position == target_position: is_moving = false