Files
Cielonos/Assets/Scripts/MainGame/Characters/Player/View/PlayerViewSubcontroller.cs
SoulliesOfficial d15957c719 更新
2025-12-17 04:19:38 -05:00

114 lines
3.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using DG.Tweening;
using SLSFramework.General;
using Unity.Cinemachine;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Serialization;
namespace Cielonos.MainGame.Characters
{
public class PlayerViewSubcontroller : SubcontrollerBase<Player>, IPlayerSubcontroller
{
public Player player => owner;
public Camera playerCamera;
public Transform cameraRoot;
public CinemachineStateDrivenCamera stateDrivenCamera;
public CinemachineCamera freeLookCamera;
public CinemachineCamera lockOnCamera;
public bool isLockedOn = false;
public bool isLockedSetRoot;
public CharacterBase testEnemy;
public Transform testEnemyTarget;
public CameraRotationSubmodule cameraRotationSm;
public OcclusionFadeSubmodule occlusionFadeSm;
public LerpFloat cameraDistance;
public override void Initialize()
{
base.Initialize();
cameraRotationSm = new CameraRotationSubmodule(this, player.transform.eulerAngles.y);
occlusionFadeSm = new OcclusionFadeSubmodule(this);
cameraDistance = new LerpFloat(10f, 1f);
}
private void Start()
{
testEnemyTarget = testEnemy.bodyPartsSc.staticCenterPoint;
//SwitchToLockTarget( testEnemyTarget );
}
private void Update()
{
if (Keyboard.current.tabKey.wasPressedThisFrame)
{
if (!isLockedOn)
{
SwitchToLockTarget( testEnemyTarget );
}
else
{
SwitchToFreeLook();
}
}
cameraDistance.Update(player.selfTimeSm.DeltaTime);
freeLookCamera.GetComponent<CinemachineThirdPersonFollow>().CameraDistance = cameraDistance.currentValue;
lockOnCamera.GetComponent<CinemachinePositionComposer>().CameraDistance = cameraDistance.currentValue;
}
private void LateUpdate()
{
if (!isLockedOn)
{
cameraRotationSm.Update();
}
else
{
if(isLockedSetRoot) cameraRoot.LookAt(testEnemyTarget);
float distance = (testEnemyTarget.position - cameraRoot.transform.position).Flatten().magnitude;
if(distance < 1f) SwitchToFreeLook();
}
occlusionFadeSm.Update();
}
void SwitchToLockTarget(Transform target)
{
testEnemyTarget = target;
isLockedOn = true;
isLockedSetRoot = false;
// --- CM3 核心操作 ---
// 1. 设置 LookAt 目标。在 CM3 中LookingAt 是 Target 结构体的一部分,或者直接赋值给 LookAt 属性
lockOnCamera.Target.LookAtTarget = testEnemyTarget;
// 2. 提高优先级,激活锁定相机
stateDrivenCamera.GetComponent<Animator>().SetBool("isLockTarget", true);
cameraRoot.DOLookAt(testEnemyTarget.position, 0.5f).SetEase(Ease.InOutSine).OnComplete(()=>isLockedSetRoot = true).Play();
// (可选) 你可以在这里播放锁定音效或显示 UI 准星
Debug.Log($"Locked on: {target.name}");
}
void SwitchToFreeLook()
{
cameraRotationSm.SyncRotationWithCamera(playerCamera);
isLockedOn = false;
// --- CM3 核心操作 ---
// 1. 重置 LookAt 目标
//lockOnCamera.Target.LookAtTarget = null;
// 2. 降低优先级,切换回自由视角相机
stateDrivenCamera.GetComponent<Animator>().SetBool("isLockTarget", false);
// (可选) 你可以在这里播放解锁音效或隐藏 UI 准星
Debug.Log("Switched to Free Look");
}
}
}