2022-08-19 14:23:35 +08:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using DG.Tweening;
|
|
|
|
|
using Sirenix.OdinInspector;
|
2022-08-27 21:30:16 +08:00
|
|
|
|
using UnityEngine.SceneManagement;
|
2022-08-19 14:23:35 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 马背上人的控制类
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class Person : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
public enum PersonState{
|
|
|
|
|
Normal,//骑在马背
|
|
|
|
|
Shoot,//从发射到着地平台
|
|
|
|
|
Walk,//在平台行走
|
|
|
|
|
FallingOff,//下落,从脱离平台到判定有结果前
|
|
|
|
|
Recover,//判定成功到恢复到Normal的过程态
|
|
|
|
|
Dead//判定失败后均为此态
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 人此时的状态
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Header("信息显示")]
|
|
|
|
|
[Title("人此时的状态")]
|
|
|
|
|
[EnumPaging()]
|
|
|
|
|
public PersonState state;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 人在人马分离障碍的平台上时的行走速度
|
|
|
|
|
/// </summary>
|
|
|
|
|
[FoldoutGroup("主要参数",true)]
|
|
|
|
|
[Title("人在人马分离障碍的平台上时的行走速率")]
|
|
|
|
|
public float walkSpeed = 1f;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 人在人马分离障碍前,被抛出的速度
|
|
|
|
|
/// </summary>
|
|
|
|
|
[FoldoutGroup("附加参数",false)]
|
|
|
|
|
[Title("人在人马分离障碍前,被抛出的速度")]
|
|
|
|
|
public Vector2 throwingSpeed;
|
|
|
|
|
|
|
|
|
|
[FoldoutGroup("附加参数",false)]
|
|
|
|
|
[Title("人在离开平台时起跳的速度")]
|
|
|
|
|
public Vector2 jumpSpeed;
|
|
|
|
|
|
|
|
|
|
[FoldoutGroup("附加参数")][Title("人死后的飞出速度")]
|
|
|
|
|
public Vector2 deadFlyVeloctiy;
|
|
|
|
|
|
|
|
|
|
[FoldoutGroup("附加参数")][Title("人死后的飞出的旋转的角速度")]
|
|
|
|
|
public float deadFlyRotateSpeed;
|
2022-08-27 21:30:16 +08:00
|
|
|
|
[FoldoutGroup("音效剪辑")][Title("人死音效剪辑")]
|
|
|
|
|
public AudioClip deadSE;
|
|
|
|
|
[FoldoutGroup("音效剪辑")][Title("人走路音效剪辑")]
|
|
|
|
|
public AudioClip walkSE;
|
|
|
|
|
[FoldoutGroup("音效剪辑")][Title("人起跳音效剪辑")]
|
|
|
|
|
public AudioClip shootSE;
|
|
|
|
|
|
2022-08-19 14:23:35 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 默认状态下的追踪点
|
|
|
|
|
/// </summary>
|
|
|
|
|
private Transform defaultTrackingPoint;
|
|
|
|
|
|
|
|
|
|
private Rigidbody2D m_rigidbody2D;
|
|
|
|
|
|
2022-08-25 05:33:40 +08:00
|
|
|
|
private Animator m_animator;
|
2022-08-27 21:30:16 +08:00
|
|
|
|
private AudioSource m_audioSource;
|
2022-08-25 05:33:40 +08:00
|
|
|
|
|
2022-08-19 14:23:35 +08:00
|
|
|
|
|
|
|
|
|
void Start(){
|
|
|
|
|
//Init
|
|
|
|
|
//Init
|
|
|
|
|
//Find
|
|
|
|
|
defaultTrackingPoint = GameObject.Find("人的默认位置(不可改名)").transform;
|
|
|
|
|
m_rigidbody2D = GetComponent<Rigidbody2D>();
|
2022-08-25 05:33:40 +08:00
|
|
|
|
m_animator = GetComponentInChildren<Animator>();
|
2022-08-27 21:30:16 +08:00
|
|
|
|
m_audioSource = GetComponent<AudioSource>();
|
2022-08-19 14:23:35 +08:00
|
|
|
|
//Find
|
|
|
|
|
}
|
|
|
|
|
void Update(){
|
|
|
|
|
//默认状态下的Update
|
|
|
|
|
if(state == PersonState.Normal){
|
|
|
|
|
//同步人和马的位置和旋转
|
|
|
|
|
transform.position = defaultTrackingPoint.position;
|
|
|
|
|
transform.rotation = defaultTrackingPoint.rotation;
|
|
|
|
|
}
|
|
|
|
|
//行走状态下的Update
|
|
|
|
|
else if(state == PersonState.Walk){
|
|
|
|
|
//让人向右移动
|
|
|
|
|
transform.position += (Vector3.right * walkSpeed * Time.deltaTime);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-08-25 05:33:40 +08:00
|
|
|
|
/// 人发射的瞬间触发一次,由人马分离障碍物触发
|
2022-08-19 14:23:35 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
public void Shoot(){
|
2022-08-27 21:30:16 +08:00
|
|
|
|
//恢复刚体重力到1
|
|
|
|
|
m_rigidbody2D.gravityScale = 1;
|
2022-08-19 14:23:35 +08:00
|
|
|
|
Debug.Log("人发射");
|
|
|
|
|
state = PersonState.Shoot;
|
|
|
|
|
//给与刚体一个右上角的速度,使其刚好能抛到平台上
|
|
|
|
|
m_rigidbody2D.velocity = throwingSpeed;
|
|
|
|
|
//1秒内修正人的旋转
|
|
|
|
|
transform.DORotate(new Vector3(0,0,0),1).OnComplete(
|
|
|
|
|
//旋转修正结束后,锁定刚体Z轴旋转
|
|
|
|
|
() => m_rigidbody2D.constraints = RigidbodyConstraints2D.FreezeRotation
|
|
|
|
|
);
|
2022-08-25 05:33:40 +08:00
|
|
|
|
//动画
|
|
|
|
|
m_animator.SetTrigger("跳上");
|
2022-08-27 21:30:16 +08:00
|
|
|
|
//
|
|
|
|
|
m_audioSource.clip = shootSE;
|
|
|
|
|
m_audioSource.Play();
|
|
|
|
|
m_audioSource.loop = false;
|
2022-08-19 14:23:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 人接触到平台的瞬间触发,由人马分离障碍物触发
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Walk(){
|
|
|
|
|
Debug.Log("人行走");
|
|
|
|
|
state = PersonState.Walk;
|
2022-08-25 05:33:40 +08:00
|
|
|
|
//动画
|
|
|
|
|
m_animator.SetTrigger("跑步");
|
2022-08-27 21:30:16 +08:00
|
|
|
|
//
|
|
|
|
|
m_audioSource.clip = walkSE;
|
|
|
|
|
m_audioSource.Play();
|
|
|
|
|
m_audioSource.loop = true;
|
2022-08-19 14:23:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 人离开平台的瞬间触发,由人马分离障碍物触发
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void FallingOff(){
|
|
|
|
|
Debug.Log("人降落");
|
|
|
|
|
state = PersonState.FallingOff;
|
|
|
|
|
//给刚体一个右上的速度,模拟一下起跳
|
|
|
|
|
m_rigidbody2D.velocity = jumpSpeed;
|
2022-08-25 05:33:40 +08:00
|
|
|
|
//动画
|
|
|
|
|
m_animator.SetTrigger("跳下");
|
2022-08-27 21:30:16 +08:00
|
|
|
|
//
|
|
|
|
|
m_audioSource.clip = shootSE;
|
|
|
|
|
m_audioSource.Play();
|
|
|
|
|
m_audioSource.loop = false;
|
2022-08-19 14:23:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Recover(){
|
|
|
|
|
Debug.Log("人成功降落到马背");
|
2022-08-27 21:30:16 +08:00
|
|
|
|
//清除重力
|
|
|
|
|
m_rigidbody2D.gravityScale = 0;
|
2022-08-19 14:23:35 +08:00
|
|
|
|
state = PersonState.Recover;
|
|
|
|
|
//1秒内修正人的位置和旋转,与defaultTrackingPoint同步
|
|
|
|
|
//后发现,由于追踪的动画终点是定好了的,但是马在这一秒还是会运动,会导致问题,所以这里把时间改短
|
|
|
|
|
transform.DOMove(defaultTrackingPoint.position,0.2f);
|
|
|
|
|
transform.DORotate(defaultTrackingPoint.rotation.eulerAngles,0.2f).OnComplete(
|
|
|
|
|
() => state = PersonState.Normal
|
|
|
|
|
);
|
2022-08-25 05:33:40 +08:00
|
|
|
|
//动画
|
|
|
|
|
m_animator.SetTrigger("恢复");
|
2022-08-27 21:30:16 +08:00
|
|
|
|
//
|
|
|
|
|
m_audioSource.clip = shootSE;
|
|
|
|
|
m_audioSource.Play();
|
|
|
|
|
m_audioSource.loop = false;
|
2022-08-19 14:23:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 由GameController触发,用于控制人死后的动作
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Death(GameController.Death deathInfo){
|
|
|
|
|
state = PersonState.Dead;
|
2022-08-27 21:30:16 +08:00
|
|
|
|
//触发状态机动画
|
|
|
|
|
m_animator.SetTrigger("摔落");
|
2022-08-19 14:23:35 +08:00
|
|
|
|
//让人和马一样,旋转着飞出去,同时关闭自身的碰撞体
|
|
|
|
|
GetComponent<CapsuleCollider2D>().enabled = false;
|
|
|
|
|
Rigidbody2D rigidbody2D = GetComponent<Rigidbody2D>();
|
|
|
|
|
rigidbody2D.velocity = deadFlyVeloctiy;
|
|
|
|
|
rigidbody2D.angularVelocity = deadFlyRotateSpeed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnCollisionEnter2D(Collision2D other) {
|
|
|
|
|
//如果创到地面,且是在降落状态,则判定为游戏失败
|
|
|
|
|
if(other.gameObject.tag == "Ground" && state == PersonState.FallingOff){
|
2022-08-27 21:30:16 +08:00
|
|
|
|
//如果此时是GamePlay场景
|
|
|
|
|
if(SceneManager.GetActiveScene().name == "0813中期提交"){
|
|
|
|
|
GameController.Death death = new GameController.Death();
|
|
|
|
|
death.deadReason = GameController.Death.DeadReason.PersonFall;
|
|
|
|
|
GameController.Instance.GameOver(death);
|
|
|
|
|
Debug.Log("人掉落到地面");
|
|
|
|
|
}
|
|
|
|
|
//如果此时是教程场景试做
|
|
|
|
|
else if(SceneManager.GetActiveScene().name == "教程场景试做"){
|
|
|
|
|
StartCoroutine(DeadInStartCoroutine());
|
|
|
|
|
}
|
|
|
|
|
m_audioSource.clip = deadSE;
|
|
|
|
|
m_audioSource.Play();
|
|
|
|
|
//触发状态机动画
|
|
|
|
|
m_animator.SetTrigger("摔落");
|
2022-08-19 14:23:35 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-27 21:30:16 +08:00
|
|
|
|
private IEnumerator DeadInStartCoroutine(){
|
|
|
|
|
//切断输入
|
|
|
|
|
Player.Instance.ToNullMap();
|
|
|
|
|
//稍作等待
|
|
|
|
|
yield return new WaitForSeconds(0.5f);
|
|
|
|
|
//呼出黑幕
|
|
|
|
|
BlackController.Instance.Trans();
|
|
|
|
|
//等待屏幕完全变黑
|
|
|
|
|
yield return new WaitUntil(() => {return BlackController.Instance.AllBlack;});
|
|
|
|
|
//复位人和马
|
|
|
|
|
Transform horse = FindObjectOfType<Horse>().transform;
|
|
|
|
|
horse.position = new Vector3(31.2f,-11.4f,0);
|
|
|
|
|
horse.eulerAngles = Vector3.zero;
|
|
|
|
|
state = PersonState.Normal;
|
|
|
|
|
//修改状态机动画
|
|
|
|
|
m_animator.SetTrigger("恢复骑马");
|
|
|
|
|
//恢复障碍触发
|
|
|
|
|
FindObjectOfType<HumanHorseBreakObstacle>().hasShoot = false;
|
|
|
|
|
//等待上面几步完成
|
|
|
|
|
yield return new WaitForSeconds(0.5f);
|
|
|
|
|
//恢复输入
|
|
|
|
|
Player.Instance.ToMap();
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-19 14:23:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|