From e8f87cdf96027ab690ee55848580fb1e2f6c2fd6 Mon Sep 17 00:00:00 2001 From: Nathan Lovato Date: Thu, 28 Nov 2019 13:43:48 +0100 Subject: [PATCH] Add a parent CameraState class to get autocompletion and type checks --- project.godot | 6 ++++++ src/Player/Camera/CameraRig.gd | 1 + src/Player/Camera/CameraState.gd | 10 +++++++++ src/Player/Camera/States/Aim.gd | 10 ++++----- src/Player/Camera/States/Camera.gd | 33 +++++++++++++++-------------- src/Player/Camera/States/Default.gd | 2 +- 6 files changed, 40 insertions(+), 22 deletions(-) create mode 100644 src/Player/Camera/CameraState.gd diff --git a/project.godot b/project.godot index 9d35588..0210d5f 100644 --- a/project.godot +++ b/project.godot @@ -14,6 +14,11 @@ _global_script_classes=[ { "language": "GDScript", "path": "res://src/Player/Camera/CameraRig.gd" }, { +"base": "State", +"class": "CameraState", +"language": "GDScript", +"path": "res://src/Player/Camera/CameraState.gd" +}, { "base": "Spatial", "class": "Mannequiny", "language": "GDScript", @@ -41,6 +46,7 @@ _global_script_classes=[ { } ] _global_script_class_icons={ "CameraRig": "", +"CameraState": "", "Mannequiny": "", "Player": "", "PlayerState": "", diff --git a/src/Player/Camera/CameraRig.gd b/src/Player/Camera/CameraRig.gd index 1e82680..52dd053 100644 --- a/src/Player/Camera/CameraRig.gd +++ b/src/Player/Camera/CameraRig.gd @@ -12,6 +12,7 @@ signal aim_fired(target_vector) onready var camera: InterpolatedCamera = $InterpolatedCamera onready var spring_arm: SpringArm = $SpringArm onready var aim_ray: RayCast = $AimRay +onready var aim_target: Sprite3D = $AimTarget var player: KinematicBody diff --git a/src/Player/Camera/CameraState.gd b/src/Player/Camera/CameraState.gd new file mode 100644 index 0000000..2709bd4 --- /dev/null +++ b/src/Player/Camera/CameraState.gd @@ -0,0 +1,10 @@ +extends State +class_name CameraState + + +var camera_rig: CameraRig + + +func _ready() -> void: + yield(owner, "ready") + camera_rig = owner diff --git a/src/Player/Camera/States/Aim.gd b/src/Player/Camera/States/Aim.gd index f827b10..576f445 100644 --- a/src/Player/Camera/States/Aim.gd +++ b/src/Player/Camera/States/Aim.gd @@ -1,4 +1,4 @@ -extends State +extends CameraState """ Logic state for the camera where configuration are made to the camera to set the field of view, position offset (over the shoulder), etc. @@ -15,16 +15,16 @@ func unhandled_input(event: InputEvent) -> void: _state_machine.transition_to("Camera/Default") elif event.is_action_pressed("action"): _state_machine.transition_to("Camera/Default") - owner.emit_signal("aim_fired", - owner.aim_ray.get_collision_point() if owner.aim_ray.is_colliding() - else owner.get_global_transform().origin) + camera_rig.emit_signal("aim_fired", + camera_rig.aim_ray.get_collision_point() if camera_rig.aim_ray.is_colliding() + else camera_rig.get_global_transform().origin) else: _parent.unhandled_input(event) func physics_process(delta: float) -> void: _parent.physics_process(delta) - _parent.aim_target.update(owner.aim_ray) + _parent.aim_target.update(camera_rig.aim_ray) func enter(msg: Dictionary = {}) -> void: diff --git a/src/Player/Camera/States/Camera.gd b/src/Player/Camera/States/Camera.gd index d4a1ccd..5b8e9d8 100644 --- a/src/Player/Camera/States/Camera.gd +++ b/src/Player/Camera/States/Camera.gd @@ -1,4 +1,4 @@ -extends State +extends CameraState """ Parent state for all camera based states for the Camera. Handles input based on mouse/gamepad and movement and configuration based on which state we're using. @@ -9,7 +9,7 @@ logic contained in a central location while being easily modifiable. """ -onready var aim_target: Sprite3D = owner.get_node("AimTarget") +onready var aim_target: Sprite3D export var is_y_inverted: = true export var fov_default: = 70.0 @@ -26,10 +26,11 @@ var _is_aiming: = false func _ready(): Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) yield(owner, "ready") + aim_target = camera_rig.aim_target func physics_process(delta: float) -> void: - owner.global_transform.origin = owner.player.global_transform.origin + owner._position_start + camera_rig.global_transform.origin = camera_rig.player.global_transform.origin + camera_rig._position_start var look_direction: = get_look_direction() var move_direction: = get_move_direction() @@ -44,12 +45,12 @@ func physics_process(delta: float) -> void: if not is_moving_towards_camera and not _is_aiming: auto_rotate(move_direction) - owner.rotation.y = wrapf(owner.rotation.y, -PI, PI) + camera_rig.rotation.y = wrapf(camera_rig.rotation.y, -PI, PI) - if owner.camera.fov != _fov_current: - owner.camera.fov = lerp(owner.camera.fov, _fov_current, 0.05) + if camera_rig.camera.fov != _fov_current: + camera_rig.camera.fov = lerp(camera_rig.camera.fov, _fov_current, 0.05) - owner.spring_arm.translation = lerp(owner.spring_arm.translation, owner._position_start + _offset, 0.05) + camera_rig.spring_arm.translation = lerp(camera_rig.spring_arm.translation, camera_rig._position_start + _offset, 0.05) func enter(msg: Dictionary = {}) ->void: @@ -59,13 +60,13 @@ func enter(msg: Dictionary = {}) ->void: func auto_rotate(move_direction: Vector3) -> void: - var offset: float = owner.player.rotation.y - owner.rotation.y + var offset: float = camera_rig.player.rotation.y - camera_rig.rotation.y var target_angle: float = ( - owner.player.rotation.y - 2 * PI if offset > PI - else owner.player.rotation.y + 2 * PI if offset < -PI - else owner.player.rotation.y + camera_rig.player.rotation.y - 2 * PI if offset > PI + else camera_rig.player.rotation.y + 2 * PI if offset < -PI + else camera_rig.player.rotation.y ) - owner.rotation.y = lerp(owner.rotation.y, target_angle, 0.015) + camera_rig.rotation.y = lerp(camera_rig.rotation.y, target_angle, 0.015) func unhandled_input(event: InputEvent) -> void: @@ -83,12 +84,12 @@ func unhandled_input(event: InputEvent) -> void: func process_camera_input(input: Vector2) -> void: if input.x != 0: - owner.rotation.y -= input.x + camera_rig.rotation.y -= input.x if input.y != 0: var angle: = input.y - owner.rotation.x -= angle * -1.0 if is_y_inverted else angle - owner.rotation.x = clamp(owner.rotation.x, -0.75, 1.25) - owner.rotation.z = 0 + camera_rig.rotation.x -= angle * -1.0 if is_y_inverted else angle + camera_rig.rotation.x = clamp(camera_rig.rotation.x, -0.75, 1.25) + camera_rig.rotation.z = 0 """ diff --git a/src/Player/Camera/States/Default.gd b/src/Player/Camera/States/Default.gd index 3b8aaab..17c79fb 100644 --- a/src/Player/Camera/States/Default.gd +++ b/src/Player/Camera/States/Default.gd @@ -1,4 +1,4 @@ -extends State +extends CameraState """ Logic state for the camera - implies no modification to the camera settings. """