mtd/PCs/hero.gd

251 lines
7.0 KiB
GDScript3
Raw Normal View History

extends CharacterBody3D
class_name Hero
signal ready_state_changed(state)
signal spawned
signal died
@export var hero_class: HeroClass
@export var camera : Camera3D
@export var gun_camera : Camera3D
@export var left_hand : Node3D
@export var right_hand : Node3D
@export var right_hand_animator : AnimationPlayer
@export var edit_tool : EditTool
@export var gauntlet_sprite : Sprite3D
@export var sprite : EightDirectionSprite3D
@export var interaction_raycast : RayCast3D
@export var inventory : Inventory
@export var card : CardInHand
@export var pause_menu_scene : PackedScene
@export var hud : HUD
@export var movement : PlayerMovement
@export var sprint_zoom_speed := 0.2
2023-11-16 00:07:41 +11:00
@export var player_name_tag : Label
var equipped_card : Card
var weapon : Weapon
var paused := false
var editing_mode := true
2023-11-16 00:07:41 +11:00
var profile : PlayerProfile
var ready_state := false :
set(value):
ready_state = value
ready_state_changed.emit(value)
var currency := 0 :
set(value):
currency = value
hud.set_currency_count(value)
get:
return currency
2023-11-09 17:56:08 +11:00
func set_zoom_factor(value):
movement.zoom_factor = value
func _ready() -> void:
if is_multiplayer_authority():
right_hand_animator.play("weapon_sway")
right_hand_animator.speed_scale = 0
camera.make_current()
sprite.queue_free()
2023-11-16 00:07:41 +11:00
player_name_tag.queue_free()
else:
camera.set_visible(false)
gun_camera.set_visible(false)
hud.set_visible(false)
if weapon != null:
weapon.set_raycast_origin(camera)
inventory.contents.append_array(hero_class.deck)
2023-11-16 00:07:41 +11:00
sprite.texture.atlas = hero_class.texture
check_left_hand_valid()
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
func _physics_process(_delta: float) -> void:
if !is_multiplayer_authority() or paused:
return
if movement.input_vector == Vector2.ZERO:
right_hand_animator.speed_scale = 0
elif movement.sprinting:
right_hand_animator.speed_scale = 1
else:
right_hand_animator.speed_scale = 0.6
func _process(delta: float) -> void:
if !is_multiplayer_authority() or paused:
return
if !movement.sprinting:
movement.zoom_factor += sprint_zoom_speed * 2.0 * delta
if movement.zoom_factor > 1.0:
movement.zoom_factor = 1.0
if editing_mode:
if interaction_raycast.is_colliding() and interaction_raycast.get_collider() is InteractButton:
hud.set_hover_text(interaction_raycast.get_collider().hover_text)
else:
hud.unset_hover_text()
if edit_tool.is_looking_at_tower_base:
card.view_tower()
else:
card.view_weapon()
if Input.is_action_just_pressed("Interact"):
edit_tool.interact()
if interaction_raycast.get_collider() is InteractButton:
var button = interaction_raycast.get_collider() as InteractButton
if currency >= button.press_cost:
button.press()
currency -= button.press_cost
if interaction_raycast.get_collider() is ItemCard:
inventory.add(interaction_raycast.get_collider().pick_up())
if Input.is_action_just_pressed("Equip In Gauntlet"):
equip_weapon()
if Input.is_action_just_pressed("Select Next Card"):
inventory.increment_selected()
if Input.is_action_just_pressed("Select Previous Card"):
inventory.decrement_selected()
if Input.is_action_just_pressed("Primary Fire"):
edit_tool.interact_key_held = true
if Input.is_action_just_released("Primary Fire"):
edit_tool.interact_key_held = false
if weapon != null:
weapon.release_trigger()
weapon.release_second_trigger()
else:
if weapon != null and Input.is_action_just_pressed("Primary Fire"):
weapon.hold_trigger()
if weapon != null and Input.is_action_just_released("Primary Fire"):
weapon.release_trigger()
if weapon != null and Input.is_action_pressed("Secondary Fire"):
weapon.hold_second_trigger()
if weapon != null and Input.is_action_just_released("Secondary Fire"):
weapon.release_second_trigger()
if weapon != null and Input.is_action_pressed("Primary Fire"):
movement.can_sprint = false
if weapon != null and Input.is_action_pressed("Secondary Fire"):
movement.can_sprint = false
if movement.sprinting:
movement.zoom_factor -= sprint_zoom_speed * delta
if movement.zoom_factor <= 1.0 - movement.sprint_zoom_factor:
movement.zoom_factor = 1.0 - movement.sprint_zoom_factor
camera.fov = Data.graphics.hfov * (1.0 / movement.zoom_factor)
if Input.is_action_just_pressed("View Map"):
hud.maximise_minimap(Game.level)
#Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
if Input.is_action_just_released("View Map"):
hud.minimize_minimap(self)
#Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
check_left_hand_valid()
func _unhandled_input(event: InputEvent) -> void:
if !is_multiplayer_authority() or paused:
return
if editing_mode and event.is_action_pressed("Ready"):
edit_tool.interact_key_held = false
if !ready_state:
ready_state = true
networked_set_ready_state.rpc(ready_state)
if event.is_action_pressed("Pause"):
var menu = pause_menu_scene.instantiate() as PauseMenu
pause()
menu.closed.connect(unpause)
hud.add_child(menu)
func unpause():
paused = false
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
2023-11-09 17:56:08 +11:00
movement.set_process(true)
movement.set_physics_process(true)
func pause():
paused = true
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
2023-11-09 17:56:08 +11:00
movement.set_process(false)
movement.set_physics_process(false)
func enter_editing_mode(value):
hud.set_wave_count(value + 1)
editing_mode = true
edit_tool.enabled = true
check_left_hand_valid()
if weapon != null:
weapon.release_trigger()
func exit_editing_mode(value):
hud.set_wave_count(value)
edit_tool.enabled = false
edit_tool.delete_tower_preview()
left_hand.set_visible(false)
editing_mode = false
func check_left_hand_valid():
if !editing_mode:
return
if inventory.contents.size() == 0:
left_hand.set_visible(false)
#gauntlet.texture.region = Rect2(64, 0, 64, 64)
else:
left_hand.set_visible(true)
#gauntlet.texture.region = Rect2(0, 0, 64, 64)
card.set_card(inventory.selected_item)
func equip_weapon():
if weapon != null:
unequip_weapon()
return
if inventory.contents.size() > 0:
equipped_card = inventory.remove()
networked_equip_weapon.rpc(Data.cards.find(equipped_card))
weapon = equipped_card.weapon_scene.instantiate()
weapon.name = "weapon"
weapon.set_multiplayer_authority(multiplayer.get_unique_id())
gauntlet_sprite.set_visible(false)
weapon.set_hero(self)
right_hand.add_child(weapon)
check_left_hand_valid()
func unequip_weapon():
networked_unequip_weapon.rpc()
gauntlet_sprite.set_visible(true)
weapon.queue_free()
inventory.add(equipped_card)
equipped_card = null
check_left_hand_valid()
#MULTIPLAYER NETWORKED FUNCTIONS
@rpc("reliable")
func networked_set_ready_state(state: bool):
ready_state = state
@rpc("reliable")
func networked_equip_weapon(card_index):
equipped_card = Data.cards[card_index]
weapon = equipped_card.weapon_scene.instantiate()
weapon.set_multiplayer_authority(multiplayer.get_remote_sender_id())
weapon.name = "weapon"
weapon.set_hero(self)
right_hand.add_child(weapon)
@rpc("reliable")
func networked_unequip_weapon():
weapon.queue_free()
inventory.add(equipped_card)
equipped_card = null