bazar_alea/client/player/CameraComponent.gd

132 lines
4.8 KiB
GDScript3
Raw Permalink Normal View History

2023-11-14 21:41:08 +00:00
extends Node
class_name CameraComponent
## Script used to control the camera for the player
@export var networking : PlayerNetworkingComponent
#####################################
#Refrences
@export var SpringArm : SpringArm3D
@export var Camera : Camera3D
@export var PlayerRef : CharacterMovementComponent
@onready var HObject = SpringArm
@onready var VObject = SpringArm
#####################################
var CameraHOffset := 0.0
2023-11-24 23:00:05 +00:00
@export var view_angle : Global.VIEW_ANGLE = Global.VIEW_ANGLE.RIGHT_SHOULDER:
2023-11-14 21:41:08 +00:00
get: return view_angle
set(Newview_angle):
2023-11-19 19:41:38 +00:00
# if view_mode == Global.VIEW_MODE.first_person:
2023-11-14 21:41:08 +00:00
# return
view_angle = Newview_angle
if Camera:
match Newview_angle:
2023-11-24 23:00:05 +00:00
Global.VIEW_ANGLE.RIGHT_SHOULDER:
2023-11-14 21:41:08 +00:00
CameraHOffset = 0.45
update_camera_offset()
2023-11-24 23:00:05 +00:00
Global.VIEW_ANGLE.LEFT_SHOULDER:
2023-11-14 21:41:08 +00:00
CameraHOffset = -0.45
update_camera_offset()
2023-11-24 23:00:05 +00:00
Global.VIEW_ANGLE.HEAD:
2023-11-14 21:41:08 +00:00
CameraHOffset = 0.0
update_camera_offset()
2023-11-24 23:00:05 +00:00
@export var view_mode : Global.VIEW_MODE = Global.VIEW_MODE.THIRD_PERSON :
2023-11-14 21:41:08 +00:00
get: return view_mode
set(Newview_mode):
view_mode = Newview_mode
if VObject:
VObject.rotation.x = 0.0
if SpringArm:
match view_mode:
2023-11-24 23:00:05 +00:00
Global.VIEW_MODE.FIRST_PERSON:
view_angle = Global.VIEW_ANGLE.HEAD
PlayerRef.rotation_mode = Global.ROTATION_MODE.LOOKING_DIRECTION
2023-11-14 21:41:08 +00:00
SpringArm.spring_length = -0.4
VObject = Camera
2023-11-24 23:00:05 +00:00
Global.VIEW_MODE.THIRD_PERSON:
2023-11-14 21:41:08 +00:00
SpringArm.spring_length = 1.75
VObject = SpringArm
var camera_h : float = 0
var camera_v : float = 0
@export var camera_vertical_min : float = -90
@export var camera_vertical_max : float = 90
## Assign a [camera_values] resource to it and change its values to tweak camera settings
@export var camera_settings : CameraValues = CameraValues.new()
@export var first_person_camera_bone : BoneAttachment3D
var current_fov : float = 90.0
var acceleration_h = 10
var acceleration_v = 10
var spring_arm_position_relative_to_player : Vector3
func _ready():
spring_arm_position_relative_to_player = SpringArm.position
SpringArm.top_level = true
func _physics_process(delta):
if camera_settings.camera_change_fov_on_speed and PlayerRef.actual_velocity.length() > camera_settings.camera_fov_change_starting_speed:
smooth_fov(current_fov + clampf((PlayerRef.actual_velocity.length()-camera_settings.camera_fov_change_starting_speed)*(camera_settings.camera_max_fov_change/10.0),0,camera_settings.camera_max_fov_change))
2023-11-24 23:00:05 +00:00
SpringArm.position = SpringArm.position.lerp((get_parent().global_position + spring_arm_position_relative_to_player) if view_mode == Global.VIEW_MODE.THIRD_PERSON else first_person_camera_bone.global_position,(1/camera_settings.camera_inertia) if view_mode == Global.VIEW_MODE.THIRD_PERSON else 1.0)
2023-11-14 21:41:08 +00:00
camera_v = clamp(camera_v,deg_to_rad(camera_vertical_min),deg_to_rad(camera_vertical_max))
HObject.rotation.y = lerp(HObject.rotation.y,camera_h,delta * acceleration_h)
VObject.rotation.x = lerp(VObject.rotation.x,camera_v,delta * acceleration_v)
match PlayerRef.rotation_mode:
2023-11-24 23:00:05 +00:00
Global.ROTATION_MODE.AIMING:
if PlayerRef.gait == Global.GAIT.SPRINTING: # character can't sprint while aiming
PlayerRef.gait = Global.GAIT.RUNNING
2023-11-14 21:41:08 +00:00
smooth_fov(60.0)
2023-11-24 23:00:05 +00:00
Global.ROTATION_MODE.VELOCITY_DIRECTION:
2023-11-14 21:41:08 +00:00
smooth_fov(90.0)
2023-11-24 23:00:05 +00:00
Global.ROTATION_MODE.LOOKING_DIRECTION:
2023-11-14 21:41:08 +00:00
smooth_fov(90.0)
func update_camera_offset():
var tween := create_tween()
tween.tween_property(Camera,"h_offset",CameraHOffset,0.5).set_ease(Tween.EASE_IN_OUT).set_trans(Tween.TRANS_EXPO)
var changing_view := false
func smooth_fov(_current_fov:float):
current_fov = _current_fov
if changing_view:
return
changing_view=true
var tween := create_tween()
tween.tween_property(Camera,"fov",current_fov,0.1)
tween.tween_callback(func(): changing_view=false)
2023-11-19 19:41:38 +00:00
func smooth_camera_transition(pos:Vector3, look_at:Vector3, duration:float = 1.0 ,ease_:Tween.EaseType = Tween.EASE_IN_OUT, trans:Tween.TransitionType = Tween.TRANS_LINEAR):
2023-11-14 21:41:08 +00:00
# Camera.global_position = Camera.to_global(Camera.global_position)
Camera.top_level = true
var tween := create_tween()
tween.set_parallel()
2023-11-19 19:41:38 +00:00
tween.tween_property(Camera,"position",pos,duration).set_ease(ease_).set_trans(trans)
tween.tween_method(func(arr:Array): Camera.look_at_from_position(arr[0],arr[1]),[Camera.position,look_at],[pos,look_at],duration).set_ease(ease_).set_trans(trans)
2023-11-14 21:41:08 +00:00
var reseting : bool = false
func reset_camera_transition(smooth_transition: bool = true):
if Camera.top_level == false:
return
if smooth_transition:
if reseting == true:
return
reseting = true
Camera.top_level = false
var tween := create_tween()
tween.set_parallel()
tween.tween_property(Camera,"position",Vector3(0,0,SpringArm.spring_length),1.0)
tween.tween_property(Camera,"rotation",Vector3.ZERO,1.0)
tween.tween_callback(func(): reseting=false)
else:
Camera.rotation = Vector3.ZERO
Camera.top_level = false