using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using Sirenix.OdinInspector;
///
/// 主要用于控制教程场景下,相机的阶段性移动
///
public class CameraControllerInStart : UnitySingleton
{
public enum State{None, Walk, Jump, Speed, Break, Statr}
[FoldoutGroup("信息显示")][Title("此时处于状态")][EnumPaging]
public State state = State.Walk;
[FoldoutGroup("基本参数")][Title("相机在阶段间运动需要的时间")]
public float cameraMoveDuration;
///
/// 阶段,包含本阶段的所有信息
///
public struct Step{
///
/// 本阶段的名字
///
public State name;
///
/// 本阶段下,相机应该在哪个位置
///
public Vector3 pos;
}
private class StepListener : MonoBehaviour
{
public Step step;
private void OnTriggerEnter2D(Collider2D other) {
if(other.TryGetComponent(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();
if (child.name == "Walk") {
steps[0].name = State.Walk;
steps[0].pos = child.position;
child.GetComponent().step = steps[0];
}
else if (child.name == "Jump") {
steps[1].name = State.Jump;
steps[1].pos = child.position;
child.GetComponent().step = steps[1];
}
else if (child.name == "Speed") {
steps[2].name = State.Speed;
steps[2].pos = child.position;
child.GetComponent().step = steps[2];
}
else if (child.name == "Break") {
steps[3].name = State.Break;
steps[3].pos = child.position;
child.GetComponent().step = steps[3];
}
else if (child.name == "Start") {
steps[4].name = State.Statr;
steps[4].pos = child.position;
child.GetComponent().step = steps[4];
}
}
}
///
/// 相机运动到指定阶段的位置
///
/// 下一个阶段
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];
}
}
}