Aim State

mail@pastecode.io avatar
unknown
plain_text
5 months ago
2.9 kB
23
Indexable
extends PlayerState

#region Arrow
var arrow_scene: PackedScene = preload("res://scenes/test_arrow.tscn")
var arrow: Node3D
var arrow_spawn_point: Marker3D
#endregion

#region Vars
var camera_quaternion: Quaternion
var rotate_with_camera: bool = true
var exit_aim: bool = true
var charge_time: float
var can_shoot: bool
#endregion

var is_shooted: bool

@onready var string_hold: Timer = $StringHold

func enter(_msg := {}) -> void:
	is_shooted = false
	player.disable_input()
	arrow_spawn_point = get_node("../../ArrowSpawn")
	
	if not GameManager.player_has_arrows():
		fsm.sfx.no_arrows.play()
	
	setup_arrow_bar()
	show_arrow_bar()
	show_arrow_count()
	
	spawn_arrow()
	string_hold.start()
	
	GameManager.weapon_sockets.add_weapon(GameManager.weapon_sockets.left_hand)
	GameManager.weapon_sockets.spine.visible = false
	
	fsm.debug_message("Player entered [" + self.name + "] state")
	player.animation_tree.set("parameters/Transition/transition_request", "Bow-Aim")

func physics_update(delta: float) -> void:
	
	charge_time = abs(string_hold.time_left - string_hold.wait_time)
	
	update_arrow_bar()
	update_arrow_count()
	
	#region conditions
	if exit_aim:
		if Input.is_action_just_released("bow_aim"):
			fsm.change_to("Idle")
	
	if rotate_with_camera:
		camera_quaternion = player.yaw.quaternion
		player.quaternion = player.quaternion.slerp(camera_quaternion, player.rotation_speed * delta)
	#endregion
	
	if Input.is_action_just_pressed("attack"):
		is_shooted = true
		shoot_arrow()
	
	if is_shooted:
		fsm.change_to("Idle")	

func update_arrow_count() -> void:
	GameManager.UI.arrow_counter.text = str(GameManager.player_storage["Arrows"])

func show_arrow_count() -> void:
	GameManager.UI.arrow_count_node.visible = true

func hide_arrow_count() -> void:
	GameManager.UI.arrow_count_node.visible = false
	
func update_arrow_bar() -> void:
	GameManager.UI.arrow_bar.value = charge_time

func reset_arrow_bar() -> void:
	GameManager.UI.arrow_bar.value = 0

func setup_arrow_bar() -> void:
	GameManager.UI.arrow_bar.max_value = string_hold.wait_time

func show_arrow_bar() -> void:
	GameManager.UI.arrow_bar.visible = true

func hide_arrow_bar() -> void:
	GameManager.UI.arrow_bar.visible = false

func spawn_arrow() -> void:
	if GameManager.player_has_arrows():
		arrow = arrow_scene.instantiate() as RigidBody3D
		arrow.freeze = true
		arrow_spawn_point.add_child(arrow)

func remove_arrow() -> void:
	if arrow != null:
		arrow.queue_free()
		
func shoot_arrow() -> void:
	if GameManager.player_has_arrows():
		if arrow != null:
			arrow.freeze = false
			arrow.top_level = true
			arrow.apply_impulse(-GameManager.player.model.transform.basis.z * charge_time * 3)
			GameManager.player_storage["Arrows"] -= 1
		
func exit() -> void:
	reset_arrow_bar()
	player.enable_input()
	if !is_shooted and GameManager.player_has_arrows():
		remove_arrow()
		GameManager.print_message("Arrow was removed.")
		
Leave a Comment