giants-godot/player/TPSController.gd

185 lines
5.8 KiB
GDScript3
Raw Normal View History

2021-09-22 18:04:03 +02:00
extends Spatial
class_name TPSController
2021-10-15 23:42:06 +02:00
# debug camera
export var reaper_path: NodePath
export var vimp_path: NodePath
export var character_path: NodePath
onready var character: Character = get_node(character_path)
var head: Spatial
var cam: Camera
var default_camera_trans: Transform
2021-09-22 18:04:03 +02:00
var is_in_menu: bool = false
2021-09-23 21:19:11 +02:00
export var FOV = 80.0
export var HUD_scene: PackedScene
onready var HUD: Control
2021-10-15 23:42:06 +02:00
onready var debug_label: Label = $DebugText
2021-09-23 21:19:11 +02:00
var mouse_axis: Vector2 = Vector2.ZERO
2021-10-15 23:42:06 +02:00
export var mouse_sensitivity: float = 10.0
var cursor_no_enemy: Control
var cursor_enemy: Control
2021-09-23 21:19:11 +02:00
2021-10-15 23:42:06 +02:00
func set_character(c: Character):
if cam:
cam.current = false
character = c
cam = character.back_camera
default_camera_trans = cam.transform
head = character.head
2021-09-23 21:19:11 +02:00
cam.fov = FOV
2021-10-15 23:42:06 +02:00
cam.current = true
func _ready():
set_character(character)
2021-09-23 21:19:11 +02:00
if not Engine.editor_hint:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
HUD = HUD_scene.instance()
add_child(HUD)
2021-10-12 23:11:23 +02:00
character.connect("health_changed", self, "update_health")
2021-10-15 23:42:06 +02:00
if HUD.has_node("cursor_enemy"):
cursor_enemy = HUD.get_node("cursor_enemy")
if HUD.has_node("cursor_no_enemy"):
cursor_enemy = HUD.get_node("cursor_no_enemy")
2021-09-22 18:04:03 +02:00
func get_target() -> Dictionary:
2021-09-23 21:19:11 +02:00
var weapon = character.current_weapon
2021-09-22 18:04:03 +02:00
var ray_from = cam.global_transform.origin
var ray_to = cam.global_transform.basis.z * -20000
var space_state = get_world().direct_space_state
var result = space_state.intersect_ray(ray_from, ray_to, [self, weapon])
if not result:
2021-09-23 21:19:11 +02:00
return Globals.create_empty_collision_dictionnary(ray_to)
2021-09-22 18:04:03 +02:00
else:
return result
2021-09-23 21:19:11 +02:00
func _physics_process(delta):
if Engine.editor_hint:
return
debug_label.text = "FPS: " + str(Engine.get_frames_per_second()) + " | Objects: " + str(Performance.get_monitor(Performance.OBJECT_COUNT))
# calculate and export target
var target = get_target()
Globals.target_position = target
# cursor change
2021-10-15 23:42:06 +02:00
if cursor_no_enemy and cursor_enemy:
if target["collider"] and target["collider"] is Entity:
cursor_no_enemy.visible = false
cursor_enemy.visible = true
else:
cursor_enemy.visible = false
cursor_no_enemy.visible = true
2021-09-23 21:19:11 +02:00
# update camera
if character.sprinting:
cam.set_fov(lerp(cam.fov, FOV * 1.2, delta * 8))
else:
cam.set_fov(lerp(cam.fov, FOV, delta * 8))
2021-10-15 23:42:06 +02:00
# head.global_transform.origin = character.global_transform.origin
2021-10-01 15:50:16 +02:00
2021-09-23 21:19:11 +02:00
func camera_rotation() -> void:
if mouse_axis.length() > 0:
var _smoothness := 80
# Get mouse delta
var horizontal: float = -(mouse_axis.x * mouse_sensitivity) / _smoothness
var vertical: float = -(mouse_axis.y * mouse_sensitivity) / _smoothness
mouse_axis = Vector2.ZERO
character.rotate_y(deg2rad(horizontal))
var temp_rot: Vector3 = head.rotation_degrees
2021-10-15 23:42:06 +02:00
temp_rot += Vector3(vertical, 0, 0)
# temp_rot += Vector3(vertical, horizontal, 0)
# clamp vertical rotation
2021-09-23 21:19:11 +02:00
temp_rot.x = clamp(temp_rot.x, -75, 75)
head.rotation_degrees = temp_rot
func _input(event: InputEvent) -> void:
if Engine.editor_hint:
return
if not Globals.process_3d_inputs:
return
if event is InputEventMouseMotion:
mouse_axis = event.relative
if Input.is_action_pressed("camera_front"):
var camera_front_transform = default_camera_trans
camera_front_transform = default_camera_trans.translated(Vector3(5, 0, -5))
camera_front_transform = camera_front_transform.rotated(Vector3.UP, deg2rad(-180))
cam.transform = camera_front_transform
2021-10-01 15:50:16 +02:00
if Input.is_action_just_released("camera_front"):
2021-09-23 21:19:11 +02:00
cam.transform = default_camera_trans
if Input.is_action_pressed("inp_fire"):
2021-09-23 21:19:11 +02:00
character.fire_pressed()
if Input.is_action_just_released("inp_fire"):
character.fire_released()
if Input.is_action_just_pressed("weapon_forward"):
character.switch_next_weapon_slot()
if Input.is_action_just_pressed("ui_cancel"):
2021-09-23 21:19:11 +02:00
var ingame_menu = preload("res://ui/menus/ingame_menu.tscn").instance()
Input.set_mouse_mode(Input.MOUSE_MODE_CONFINED)
add_child(ingame_menu)
ingame_menu.connect("tree_exited", self, "_ingamemenu_closed")
Globals.process_3d_inputs = false
2021-10-15 23:42:06 +02:00
if Input.is_action_just_pressed("camera_reaper"):
set_character(get_node(reaper_path))
if Input.is_action_just_pressed("camera_vimp"):
set_character(get_node(vimp_path))
if Input.is_action_just_pressed("move_jump"):
character.jump()
if Input.is_action_just_pressed("move_sprint"):
character.set_sprint(true)
if Input.is_action_just_released("move_sprint"):
character.set_sprint(false)
if character is Kabuto and Input.is_action_just_pressed("kabuto_sprint"):
character.start_sprint()
2021-09-23 21:19:11 +02:00
2021-10-01 15:50:16 +02:00
func _ingamemenu_closed():
Globals.process_3d_inputs = true
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
2021-09-23 21:19:11 +02:00
func _process(_delta: float) -> void:
if Engine.editor_hint:
return
if Globals.process_3d_inputs:
camera_rotation()
2021-10-15 23:42:06 +02:00
character.move_axis.x = Input.get_action_strength("move_forward") - Input.get_action_strength("move_backward")
character.move_axis.y = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
2021-09-23 21:19:11 +02:00
update_hud()
func update_hud() -> void:
#for i in range(5):
# var weapon_slot = HUD.find_node("Weapon%s" % i)
# if weapon_slot:
# for child in weapon_slot.get_children():
# weapon_slot.remove_child(child)
2021-09-23 21:19:11 +02:00
for weapon in character.weapons:
var weapon_slot = character.get_weapon_slot(weapon)
var hud_weapon_slot = HUD.find_node("Weapon%s" % weapon_slot)
var weapon_icon: Control = weapon.ui_icon
if not weapon_icon.get_parent() == hud_weapon_slot:
hud_weapon_slot.add_child(weapon_icon)
2021-10-01 15:50:16 +02:00
if character.current_weapon == weapon:
hud_weapon_slot.get_node("selected").visible = true
else:
hud_weapon_slot.get_node("selected").visible = false
2021-10-12 23:11:23 +02:00
func update_health() -> void:
var current_health = character.health
var max_health = character.max_health
var hp_percent = current_health/max_health
# UI/hp polygon: 0 means 100% hp, 250 means 0%
var val = 250 - 250*hp_percent
HUD.find_node("hp").texture_offset = Vector2(val, 0)
2021-09-23 21:19:11 +02:00