Files
Cielonos/Assets/Scripts/MainGame/Characters/Player/View/PlayerViewSubcontroller.cs

108 lines
3.6 KiB
C#
Raw Normal View History

2025-11-25 08:19:33 -05:00
using System;
2025-12-17 04:19:38 -05:00
using DG.Tweening;
2025-11-25 08:19:33 -05:00
using SLSFramework.General;
using Unity.Cinemachine;
using UnityEngine;
2025-12-17 04:19:38 -05:00
using UnityEngine.InputSystem;
2025-11-25 08:19:33 -05:00
using UnityEngine.Serialization;
2025-12-22 18:36:29 -05:00
using Ease = DG.Tweening.Ease;
2025-11-25 08:19:33 -05:00
namespace Cielonos.MainGame.Characters
{
public class PlayerViewSubcontroller : SubcontrollerBase<Player>, IPlayerSubcontroller
{
public Player player => owner;
public Camera playerCamera;
2025-12-17 04:19:38 -05:00
public Transform cameraRoot;
public CinemachineStateDrivenCamera stateDrivenCamera;
2025-11-25 08:19:33 -05:00
public CinemachineCamera freeLookCamera;
2025-12-17 04:19:38 -05:00
public CinemachineCamera lockOnCamera;
public bool isLockedOn = false;
public bool isLockedSetRoot;
public CharacterBase testEnemy;
public Transform testEnemyTarget;
2025-11-25 08:19:33 -05:00
public CameraRotationSubmodule cameraRotationSm;
public OcclusionFadeSubmodule occlusionFadeSm;
public override void Initialize()
{
base.Initialize();
cameraRotationSm = new CameraRotationSubmodule(this, player.transform.eulerAngles.y);
occlusionFadeSm = new OcclusionFadeSubmodule(this);
2025-12-17 04:19:38 -05:00
}
private void Start()
{
testEnemyTarget = testEnemy.bodyPartsSc.staticCenterPoint;
//SwitchToLockTarget( testEnemyTarget );
2025-11-25 08:19:33 -05:00
}
private void Update()
{
2025-12-17 04:19:38 -05:00
if (Keyboard.current.tabKey.wasPressedThisFrame)
{
if (!isLockedOn)
{
SwitchToLockTarget( testEnemyTarget );
}
else
{
SwitchToFreeLook();
}
}
2025-11-25 08:19:33 -05:00
}
private void LateUpdate()
{
2025-12-17 04:19:38 -05:00
if (!isLockedOn)
{
cameraRotationSm.Update();
}
else
{
if(isLockedSetRoot) cameraRoot.LookAt(testEnemyTarget);
float distance = (testEnemyTarget.position - cameraRoot.transform.position).Flatten().magnitude;
if(distance < 1f) SwitchToFreeLook();
}
2025-11-25 08:19:33 -05:00
occlusionFadeSm.Update();
}
2025-12-17 04:19:38 -05:00
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");
}
2025-11-25 08:19:33 -05:00
}
}