113 lines
3.7 KiB
C#
113 lines
3.7 KiB
C#
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using UnityEngine;
|
|||
|
using DG.Tweening;
|
|||
|
using Sirenix.OdinInspector;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 主要用于控制教程场景下,相机的阶段性移动
|
|||
|
/// </summary>
|
|||
|
public class CameraControllerInStart : UnitySingleton<CameraControllerInStart>
|
|||
|
{
|
|||
|
public enum State{None, Walk, Jump, Speed, Break, Statr}
|
|||
|
[FoldoutGroup("信息显示")][Title("此时处于状态")][EnumPaging]
|
|||
|
public State state = State.Walk;
|
|||
|
[FoldoutGroup("基本参数")][Title("相机在阶段间运动需要的时间")]
|
|||
|
public float cameraMoveDuration;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 阶段,包含本阶段的所有信息
|
|||
|
/// </summary>
|
|||
|
public struct Step{
|
|||
|
/// <summary>
|
|||
|
/// 本阶段的名字
|
|||
|
/// </summary>
|
|||
|
public State name;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 本阶段下,相机应该在哪个位置
|
|||
|
/// </summary>
|
|||
|
public Vector3 pos;
|
|||
|
}
|
|||
|
|
|||
|
private class StepListener : MonoBehaviour
|
|||
|
{
|
|||
|
public Step step;
|
|||
|
private void OnTriggerEnter2D(Collider2D other) {
|
|||
|
if(other.TryGetComponent<Person>(out var person)){
|
|||
|
//当检测到人进入,并且此时相机正处于本状态,触发相机的移动到下一个Step
|
|||
|
if(CameraControllerInStart.Instance.state == step.name && person.state == Person.PersonState.Normal){
|
|||
|
CameraControllerInStart.Instance.MoveToTheStep(step.name);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private Step[] steps;
|
|||
|
private void Start() {
|
|||
|
//初始化5个阶段的信息
|
|||
|
steps = new Step[5];
|
|||
|
//遍历子物体,根据名称初始化Step信息
|
|||
|
for (int i = 0; i < transform.childCount; i++) {
|
|||
|
Transform child = transform.GetChild(i);
|
|||
|
child.gameObject.AddComponent<StepListener>();
|
|||
|
if (child.name == "Walk") {
|
|||
|
steps[0].name = State.Walk;
|
|||
|
steps[0].pos = child.position;
|
|||
|
child.GetComponent<StepListener>().step = steps[0];
|
|||
|
}
|
|||
|
else if (child.name == "Jump") {
|
|||
|
steps[1].name = State.Jump;
|
|||
|
steps[1].pos = child.position;
|
|||
|
child.GetComponent<StepListener>().step = steps[1];
|
|||
|
}
|
|||
|
else if (child.name == "Speed") {
|
|||
|
steps[2].name = State.Speed;
|
|||
|
steps[2].pos = child.position;
|
|||
|
child.GetComponent<StepListener>().step = steps[2];
|
|||
|
}
|
|||
|
else if (child.name == "Break") {
|
|||
|
steps[3].name = State.Break;
|
|||
|
steps[3].pos = child.position;
|
|||
|
child.GetComponent<StepListener>().step = steps[3];
|
|||
|
}
|
|||
|
else if (child.name == "Start") {
|
|||
|
steps[4].name = State.Statr;
|
|||
|
steps[4].pos = child.position;
|
|||
|
child.GetComponent<StepListener>().step = steps[4];
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 相机运动到指定阶段的位置
|
|||
|
/// </summary>
|
|||
|
/// <param name="state">下一个阶段</param>
|
|||
|
public void MoveToTheStep(State state){
|
|||
|
Step step = State2Step(state);
|
|||
|
Camera.main.transform.DOMoveX(step.pos.x + 44.5f, cameraMoveDuration);
|
|||
|
this.state = ++step.name;
|
|||
|
}
|
|||
|
|
|||
|
private Step State2Step(State state){
|
|||
|
switch (state) {
|
|||
|
case State.Walk:
|
|||
|
return steps[0];
|
|||
|
case State.Jump:
|
|||
|
return steps[1];
|
|||
|
case State.Speed:
|
|||
|
return steps[2];
|
|||
|
case State.Break:
|
|||
|
return steps[3];
|
|||
|
case State.Statr:
|
|||
|
return steps[4];
|
|||
|
default:
|
|||
|
return steps[0];
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
}
|