Files
2026-05-26 16:15:54 +08:00

248 lines
7.2 KiB
C#
Raw Permalink 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.Threading.Tasks;
using asap.core;
using Cinemachine;
using UniRx;
using UnityEditor;
using UnityEngine;
public class ChallengeCleanStageEvent
{
public int stage;
public readonly TaskCompletionSource<bool> Tcs = new ();
}
public class ChallengeBrokeStepEvent
{
public int step;
public float dealyTime;
public readonly TaskCompletionSource<bool> Tcs = new();
}
public class ChallengeInitStepEvent
{
public int step;
}
public class ChallengeSetAnimationEvent
{
public float speed;
public float timeForUnlocking;
public float timeForUnLockingEnd;
public float timeForAttacking_1;
public float timeForAttacking_2;
public float timeForAttackStart;
public float timeForAttackEnd;
public float timeForAttackEndWait;
}
public class ChallengeMatchScene1 : MonoBehaviour
{
// private Transform _challengeStatic;
// private Transform _challengeDynamic;
private Transform _camera;
// 想法是以后绑定一个 大奖 + 台子区域的点 到 当前的场景Node或者直接坐在 场景Prefab 中,看看是否可实现
// private Transform _rewardNode;
// 管卡容器
[SerializeField] private ChallengeBox[] boxNodeContainer;
[Header("设计宽高以及分辨率")]
[SerializeField] private float designMinWidth = 1080f;
[SerializeField] private float designMinHeight = 2160f;
[SerializeField] private float designMaxWidth = 1080f;
[SerializeField] private float designMaxHeight = 1920f;
[SerializeField] private float designOrthoSize = 5f;
//
private int _lastWidth;
private int _lastHeight;
// 修改平行和投影视图
private CinemachineVirtualCamera _vCam;
// 数据管理
private FishingChallengeManager _fishingChallengeManager;
//
private CompositeDisposable _disposables = new();
// 当前 的Step
private int _curStep;
// private int _curScore;
public float CalcOrthographicSize()
{
// if (_camera == null) return;
// 计算设计宽高比和当前宽高比
float designMinAspect = designMinWidth/designMinHeight;
float designMaxAspect = designMaxWidth/designMaxHeight;
float currentAspect = Screen.width/(float)Screen.height ;
currentAspect = Mathf.Clamp(currentAspect, designMinAspect, designMaxAspect);
var size = designOrthoSize * designMaxAspect/currentAspect;
Log($"CalcOrthographicSize ->{size}");
return size;
}
private void ApplyCameraScale()
{
if (Screen.width == _lastWidth && Screen.height == _lastHeight) return;
_vCam.m_Lens.OrthographicSize = CalcOrthographicSize();
_lastWidth = Screen.width;
_lastHeight = Screen.height;
}
private void Awake()
{
// Log("Awake -> ");
// _challengeStatic = transform.Find("challenge_static");
// _challengeDynamic = transform.Find("challenge_dynamic");
_camera = transform.Find("CameraRoot/Camera");
// _rewardNode = transform.Find("RewardNode");
_vCam = _camera.GetComponent<CinemachineVirtualCamera>();
// _vCam.m_Lens.Orthographic = true;
// _vCam.m_Lens.OrthographicSize = CalcOrthographicSize(); // 设置所需大小
_fishingChallengeManager = GContext.container.Resolve<FishingChallengeManager>();
// 当前场景的阶段
_curStep = _fishingChallengeManager.fishingChallengeData.step;
// _curScore = _fishingChallengeManager.fishingChallengeData.Score;
OnAddEvents();
}
#if UNITY_EDITOR
private void Update()
{
if (Input.GetKeyDown(KeyCode.N))
{
_curStep = (_curStep + 1) % (_fishingChallengeManager.GetFullStep() + 1);
InitCurStep();
}
if (Input.GetKeyDown(KeyCode.V))
{
var e = new ChallengeCleanStageEvent
{
stage = 1,
};
OnUpdateStage(e);
}
ApplyCameraScale();
}
private void OnGMInitStep(ChallengeInitStepEvent evt)
{
_curStep = evt.step;
InitCurStep();
}
#endif
void OnChallengeTimeEvent(ChallengeSetAnimationEvent e)
{
foreach (var box in boxNodeContainer)
{
box.UpdateTimeController(e);
}
}
private void Start()
{
Log("Start");
// _vCam.Priority = 0; // 设置低优先级使其他相机接管
// #if UNITY_EDITOR
// EditorApplication.QueuePlayerLoopUpdate();
// #endif
OnSceneStart();
}
//
private void OnAddEvents()
{
GContext.OnEvent<ChallengeCleanStageEvent>().Subscribe(OnUpdateStage).AddTo(_disposables);
GContext.OnEvent<ChallengeBrokeStepEvent>().Subscribe(OnBrokeStep).AddTo(_disposables);
#if UNITY_EDITOR
GContext.OnEvent<ChallengeInitStepEvent>().Subscribe(OnGMInitStep).AddTo(_disposables);
#endif
GContext.OnEvent<ChallengeSetAnimationEvent>().Subscribe(OnChallengeTimeEvent).AddTo(_disposables);
}
private void OnSceneStart()
{
InitCurStep();
}
private void InitCurStep()
{
Log($"InitCurStep -> _curStep = {_curStep}");
var curIndex = _curStep - 1;
var maxNum = boxNodeContainer.Length;
for (var i = curIndex + 1; i < maxNum; ++i)
{
_ = boxNodeContainer[i].UpdateState(CBoxState.Locked);
}
if (curIndex < 0 || curIndex > boxNodeContainer.Length)
return;
for (var i = 0; i <= curIndex; ++i)
{
_ = boxNodeContainer[i].UpdateState(CBoxState.Broken);
}
_ = boxNodeContainer[curIndex].UpdateState(CBoxState.UnLocked);
}
private async void OnUpdateStage(ChallengeCleanStageEvent upStepEvent)
{
// Log("UpdateCurStep()");
var stageIndex = upStepEvent.stage - 1;
if (stageIndex >= 0)
{
await boxNodeContainer[stageIndex].UpdateState(CBoxState.UnLocking);
// await Awaiters.Seconds(0.5f);
}
upStepEvent.Tcs.SetResult(true);
}
private async void OnBrokeStep(ChallengeBrokeStepEvent brokeEvent)
{
var step = brokeEvent.step - 1;
if (step >= 0 && step < boxNodeContainer.Length)
{
await Awaiters.Seconds(brokeEvent.dealyTime);
await boxNodeContainer[step].UpdateState(CBoxState.Attacking);
}
brokeEvent.Tcs.SetResult(true);
}
//
private void OnEnable()
{
Log("OnEnable");
_vCam.m_Lens.Orthographic = true;
_vCam.m_Lens.OrthographicSize = CalcOrthographicSize();; // 设置所需大小
_lastWidth = Screen.width;
_lastHeight = Screen.height;
}
private void OnDisable()
{
if (_vCam)
{
_vCam.m_Lens.Orthographic = false;
if (Camera.main != null)
{
var brain = Camera.main.GetComponent<CinemachineBrain>();
brain?.ManualUpdate();
}
}
_disposables?.Dispose();
_disposables = null;
}
private void OnDestroy()
{
}
private void Log(object t)
{
Debug.Log($"<color=#00ff00> ChallengeMatchScene -> {t} </color>");
}
}