2022-03-12 15:56:25 +08:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.InputSystem;
|
2022-03-12 21:05:15 +08:00
|
|
|
|
using Sirenix.OdinInspector;
|
|
|
|
|
using DG.Tweening;
|
2022-03-12 15:56:25 +08:00
|
|
|
|
|
|
|
|
|
public class Player : PlayerControl
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2022-03-15 20:26:46 +08:00
|
|
|
|
/// 玩家刚体组件喵
|
2022-03-12 21:05:15 +08:00
|
|
|
|
/// </summary>
|
2022-03-12 15:56:25 +08:00
|
|
|
|
Rigidbody2D m_rigidbody;
|
|
|
|
|
|
2022-04-01 20:10:20 +08:00
|
|
|
|
[Header("骨骼")]
|
|
|
|
|
[FoldoutGroup("子物体")]
|
|
|
|
|
public GameObject bone;
|
|
|
|
|
Animator boneAnim;
|
|
|
|
|
|
2022-03-12 21:05:15 +08:00
|
|
|
|
/// <summary>
|
2022-03-15 20:26:46 +08:00
|
|
|
|
/// 脚底位置子物体
|
2022-03-12 21:05:15 +08:00
|
|
|
|
/// </summary>
|
2022-03-15 20:26:46 +08:00
|
|
|
|
[Header("脚底位置")] [FoldoutGroup("子物体")]
|
|
|
|
|
public Transform groundCheck;
|
2022-03-12 21:05:15 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-03-15 20:26:46 +08:00
|
|
|
|
/// 手持武器
|
2022-03-12 21:05:15 +08:00
|
|
|
|
/// </summary>
|
2022-04-06 10:05:29 +08:00
|
|
|
|
[ReadOnly]
|
2022-03-15 20:26:46 +08:00
|
|
|
|
[Header("手持武器")] [FoldoutGroup("子物体")]
|
|
|
|
|
public Item weapon;
|
|
|
|
|
|
2022-03-19 23:02:32 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 武器生成位置(需要绑在骨骼上)
|
|
|
|
|
/// </summary>
|
2022-04-01 20:10:20 +08:00
|
|
|
|
|
|
|
|
|
[Header("武器生成位置(需要绑在骨骼上)")]
|
|
|
|
|
[FoldoutGroup("子物体")]
|
|
|
|
|
public Transform weaponTrans;
|
|
|
|
|
[Header("武器生成位置偏移")]
|
|
|
|
|
[FoldoutGroup("子物体")]
|
2022-03-19 23:02:32 +08:00
|
|
|
|
public Vector3 weaponPos;
|
2022-03-15 20:26:46 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 角色朝向(向右为1)
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ReadOnly] [Header("朝向监测")]
|
2022-03-12 21:05:15 +08:00
|
|
|
|
public int isRight = 1;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-03-15 20:26:46 +08:00
|
|
|
|
/// 角色是否落地
|
2022-03-12 21:05:15 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
[ReadOnly]
|
2022-03-15 20:26:46 +08:00
|
|
|
|
[Header("落地监测")]
|
2022-03-12 21:05:15 +08:00
|
|
|
|
public bool isGround = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-03-15 20:26:46 +08:00
|
|
|
|
[Header("行走速度")][FoldoutGroup("角色操作数据")]
|
2022-03-12 21:05:15 +08:00
|
|
|
|
public float speed;
|
|
|
|
|
|
2022-03-15 20:26:46 +08:00
|
|
|
|
[Header("跳跃力量")][FoldoutGroup("角色操作数据")]
|
2022-03-12 21:05:15 +08:00
|
|
|
|
public float jumpForce;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-03-15 20:26:46 +08:00
|
|
|
|
/// 角色翻面时长
|
2022-03-12 21:05:15 +08:00
|
|
|
|
/// </summary>
|
2022-03-15 20:26:46 +08:00
|
|
|
|
[Header("角色翻面所需时长")]
|
2022-03-12 21:05:15 +08:00
|
|
|
|
public float flipDuration = 0.1f;
|
|
|
|
|
|
|
|
|
|
|
2022-03-25 19:50:39 +08:00
|
|
|
|
|
|
|
|
|
//布尔-------------------------
|
|
|
|
|
bool forceFlip = false;
|
|
|
|
|
bool isHurting = false;
|
|
|
|
|
|
|
|
|
|
|
2022-03-12 15:56:25 +08:00
|
|
|
|
/// <summary>
|
2022-03-15 20:26:46 +08:00
|
|
|
|
/// 玩家初始化喵
|
2022-03-12 15:56:25 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
override protected void Start()
|
|
|
|
|
{
|
|
|
|
|
base.Start();
|
|
|
|
|
m_rigidbody = gameObject.GetComponent<Rigidbody2D>();
|
2022-04-01 20:10:20 +08:00
|
|
|
|
boneAnim = bone.gameObject.GetComponent<Animator>();
|
2022-04-06 10:05:29 +08:00
|
|
|
|
//WeaponInit();
|
2022-03-12 15:56:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-12 21:05:15 +08:00
|
|
|
|
//UPDATE
|
2022-03-12 15:56:25 +08:00
|
|
|
|
void Update()
|
|
|
|
|
{
|
2022-04-06 10:05:29 +08:00
|
|
|
|
//Debug.Log(weapon.name + " " + weapon.GetIsAtk());
|
|
|
|
|
|
2022-04-01 20:10:20 +08:00
|
|
|
|
//OnInputDetect();
|
2022-03-12 21:05:15 +08:00
|
|
|
|
Flip();
|
2022-04-01 20:10:20 +08:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
if (inputDir!=0 && boneAnim.GetBool("Move")==false)
|
|
|
|
|
boneAnim.SetBool("Move", true);
|
|
|
|
|
*/
|
2022-03-12 15:56:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-14 12:50:06 +08:00
|
|
|
|
void FixedUpdate() {
|
|
|
|
|
Moving();
|
|
|
|
|
}
|
2022-03-12 15:56:25 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-03-15 20:26:46 +08:00
|
|
|
|
/// 翻面函数
|
2022-03-12 15:56:25 +08:00
|
|
|
|
/// </summary>
|
2022-03-12 21:05:15 +08:00
|
|
|
|
void Flip()
|
|
|
|
|
{
|
|
|
|
|
int lastFrameDir = isRight;
|
2022-03-25 19:50:39 +08:00
|
|
|
|
if (inputDir * lastFrameDir < 0 || forceFlip == true)
|
2022-03-12 21:05:15 +08:00
|
|
|
|
{ isRight *= -1;
|
|
|
|
|
//transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
|
|
|
|
|
//transform.DOScale(new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z),0.1f);
|
|
|
|
|
|
2022-03-15 20:26:46 +08:00
|
|
|
|
//分情况定值翻转 可以规避在旋转动画未结束时再次翻转的造成的起始角度不同从而影响目标角度误差的问题
|
2022-03-12 21:05:15 +08:00
|
|
|
|
switch (isRight) {
|
|
|
|
|
case 1:
|
|
|
|
|
transform.DORotate(new Vector3(0, 0, 0), flipDuration);
|
|
|
|
|
break;
|
|
|
|
|
case -1:
|
|
|
|
|
transform.DORotate(new Vector3(0,180,0), flipDuration);
|
|
|
|
|
break;
|
2022-03-15 20:26:46 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-12 21:05:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:10:20 +08:00
|
|
|
|
/*
|
2022-03-12 21:05:15 +08:00
|
|
|
|
/// <summary>
|
2022-03-15 20:26:46 +08:00
|
|
|
|
/// 检测是否停止摇杆输入
|
2022-03-12 21:05:15 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
void OnInputDetect() {
|
2022-03-25 19:50:39 +08:00
|
|
|
|
if (inputDir == 0&&isHurting==false)
|
2022-03-12 21:05:15 +08:00
|
|
|
|
Freeze();
|
2022-04-01 20:10:20 +08:00
|
|
|
|
}*/
|
|
|
|
|
|
2022-03-12 21:05:15 +08:00
|
|
|
|
/// <summary>
|
2022-03-15 20:26:46 +08:00
|
|
|
|
/// 强制静止
|
2022-03-12 21:05:15 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
void Freeze() {
|
|
|
|
|
m_rigidbody.velocity = new Vector2(0,m_rigidbody.velocity.y);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 20:26:46 +08:00
|
|
|
|
void Moving() {
|
2022-03-25 19:50:39 +08:00
|
|
|
|
if (isHurting == false)
|
2022-04-01 20:10:20 +08:00
|
|
|
|
{
|
|
|
|
|
switch (inputDir) {
|
|
|
|
|
case 0:
|
|
|
|
|
boneAnim.SetBool("Move",false);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
boneAnim.SetBool("Move",true);
|
|
|
|
|
break;
|
2022-04-06 10:05:29 +08:00
|
|
|
|
}
|
2022-03-25 19:50:39 +08:00
|
|
|
|
|
2022-04-06 10:05:29 +08:00
|
|
|
|
m_rigidbody.velocity = new Vector2(inputDir * speed, m_rigidbody.velocity.y); }
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
boneAnim.SetBool("Move", false);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 20:26:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-03-12 15:56:25 +08:00
|
|
|
|
/// <param name="ctx"></param>
|
|
|
|
|
override protected void OnMove(InputAction.CallbackContext ctx)
|
|
|
|
|
{
|
2022-03-12 21:05:15 +08:00
|
|
|
|
base.OnMove(ctx);
|
2022-03-14 12:50:06 +08:00
|
|
|
|
|
2022-03-12 15:56:25 +08:00
|
|
|
|
}
|
2022-03-13 23:27:31 +08:00
|
|
|
|
override protected void OnAtk() {
|
|
|
|
|
base.OnAtk();
|
2022-04-06 10:05:29 +08:00
|
|
|
|
AtkAnim();
|
|
|
|
|
weapon.SetIsAtk(true);
|
|
|
|
|
StopInput(1.2f);
|
|
|
|
|
|
2022-03-15 20:26:46 +08:00
|
|
|
|
|
2022-03-12 15:56:25 +08:00
|
|
|
|
}
|
|
|
|
|
override protected void OnJump() {
|
2022-03-13 23:27:31 +08:00
|
|
|
|
base.OnJump();
|
2022-03-12 21:05:15 +08:00
|
|
|
|
if (isGround == true) {
|
|
|
|
|
isGround = false;
|
|
|
|
|
m_rigidbody.velocity = new Vector2(m_rigidbody.velocity.x, jumpForce);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-12 15:56:25 +08:00
|
|
|
|
}
|
|
|
|
|
override protected void OnInteract() {
|
2022-03-13 23:27:31 +08:00
|
|
|
|
base.OnInteract();
|
2022-03-14 12:50:06 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 20:26:46 +08:00
|
|
|
|
protected override void OnLeftChange()
|
2022-03-25 19:50:39 +08:00
|
|
|
|
{
|
|
|
|
|
ItemController.Instance.LeftChangeItem();
|
2022-03-15 20:26:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnRightChange()
|
|
|
|
|
{
|
2022-03-25 19:50:39 +08:00
|
|
|
|
//WeaponReturn();
|
2022-03-15 20:26:46 +08:00
|
|
|
|
ItemController.Instance.RightChangeItem();
|
2022-03-25 19:50:39 +08:00
|
|
|
|
//WeaponInPosition();
|
2022-03-15 20:26:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-25 19:50:39 +08:00
|
|
|
|
//武器更换相关函数--------------------------------------------------
|
2022-03-19 23:02:32 +08:00
|
|
|
|
public void WeaponInPosition() {
|
2022-03-15 20:26:46 +08:00
|
|
|
|
GameObject chosenOne;
|
2022-04-06 10:05:29 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
chosenOne = GameObject.Find(weapon.name);
|
|
|
|
|
if (chosenOne.TryGetComponent<Item>(out Item chosenWeapen))
|
|
|
|
|
chosenWeapen.InPosition(weaponTrans, weaponPos);
|
|
|
|
|
}
|
|
|
|
|
catch (System.Exception e)
|
|
|
|
|
{
|
|
|
|
|
e.ToString();
|
|
|
|
|
Debug.LogError("初始武器未绑定,若本关卡需要武器,请绑定,否则无视此信息" );
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-15 20:26:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-19 23:02:32 +08:00
|
|
|
|
public void WeaponReturn()
|
2022-03-15 20:26:46 +08:00
|
|
|
|
{
|
|
|
|
|
GameObject chosenOne;
|
|
|
|
|
chosenOne = GameObject.Find(weapon.name);
|
2022-03-19 23:02:32 +08:00
|
|
|
|
if (chosenOne.TryGetComponent<Item>(out Item chosenWeapon))
|
|
|
|
|
{
|
|
|
|
|
//Debug.Log(chosenOne.name + chosenWeapon.GetOriposition());
|
|
|
|
|
chosenWeapon.ReturnToOri(); }
|
|
|
|
|
|
|
|
|
|
}
|
2022-03-25 19:50:39 +08:00
|
|
|
|
//武器生成---------------------------------------------------------
|
2022-03-19 23:02:32 +08:00
|
|
|
|
void WeaponInit() {
|
|
|
|
|
WeaponInPosition();
|
2022-03-12 15:56:25 +08:00
|
|
|
|
}
|
2022-03-12 21:05:15 +08:00
|
|
|
|
|
2022-03-25 19:50:39 +08:00
|
|
|
|
//受到攻击
|
|
|
|
|
public void GetHurt(int count,float time,float hitter_x) {
|
|
|
|
|
isHurting = true;
|
2022-04-01 20:10:20 +08:00
|
|
|
|
StopInput(time);
|
|
|
|
|
//播动画
|
|
|
|
|
HurtInteractAnim();
|
2022-03-25 19:50:39 +08:00
|
|
|
|
if ((hitter_x - transform.position.x)*isRight< 0) {
|
|
|
|
|
forceFlip = true;
|
|
|
|
|
Flip();
|
|
|
|
|
forceFlip = false;
|
|
|
|
|
}
|
|
|
|
|
m_rigidbody.velocity = new Vector2(-isRight * 3, 3);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void StopInput(float time) {
|
|
|
|
|
ToMap("Null");
|
|
|
|
|
Invoke(nameof(ResetInput), time);
|
2022-04-06 10:05:29 +08:00
|
|
|
|
|
2022-03-25 19:50:39 +08:00
|
|
|
|
}
|
2022-03-15 20:26:46 +08:00
|
|
|
|
|
2022-03-25 19:50:39 +08:00
|
|
|
|
void ResetInput() {
|
|
|
|
|
ToMap("Normal");
|
|
|
|
|
isHurting = false;
|
2022-04-06 10:05:29 +08:00
|
|
|
|
weapon.SetIsAtk(false);
|
2022-03-25 19:50:39 +08:00
|
|
|
|
}
|
2022-03-15 20:26:46 +08:00
|
|
|
|
|
2022-03-25 19:50:39 +08:00
|
|
|
|
//跳跃判定(有点怪 后面可能会改)
|
2022-03-15 20:26:46 +08:00
|
|
|
|
|
|
|
|
|
private void OnCollisionEnter2D(Collision2D collision)//当有物体碰上
|
2022-03-12 21:05:15 +08:00
|
|
|
|
{
|
2022-03-15 20:26:46 +08:00
|
|
|
|
//创到地面时触发一次
|
2022-03-12 21:05:15 +08:00
|
|
|
|
if (collision.transform.CompareTag("Ground"))
|
|
|
|
|
{
|
2022-03-15 20:26:46 +08:00
|
|
|
|
//向脚底发射一条短射线
|
2022-03-12 21:05:15 +08:00
|
|
|
|
Ray2D ray = new Ray2D(
|
2022-03-15 20:26:46 +08:00
|
|
|
|
groundCheck.position,
|
2022-03-12 21:05:15 +08:00
|
|
|
|
Vector2.down
|
|
|
|
|
);
|
|
|
|
|
Debug.DrawRay(ray.origin, ray.direction, Color.red, 10f);
|
2022-03-15 20:26:46 +08:00
|
|
|
|
//获取射线的碰撞结果
|
2022-03-12 21:05:15 +08:00
|
|
|
|
RaycastHit2D hit2D;
|
|
|
|
|
hit2D = Physics2D.Raycast(ray.origin, ray.direction, 0.001f);
|
2022-03-15 20:26:46 +08:00
|
|
|
|
//如果射线有结果并且射线创到的是地面,才表示着地了
|
2022-03-12 21:05:15 +08:00
|
|
|
|
if (hit2D && hit2D.collider.transform.CompareTag("Ground"))
|
|
|
|
|
{
|
|
|
|
|
isGround = true;
|
|
|
|
|
}
|
|
|
|
|
else Debug.Log("Not Ground");
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-01 20:10:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------动画控制-----------------------------------
|
|
|
|
|
|
|
|
|
|
public void TreeInteractAnim() {
|
|
|
|
|
boneAnim.SetTrigger("与树交互");
|
|
|
|
|
}
|
|
|
|
|
public void FireInteractAnim()
|
|
|
|
|
{
|
|
|
|
|
boneAnim.SetTrigger("与火交互");
|
|
|
|
|
}
|
|
|
|
|
public void HurtInteractAnim() {
|
|
|
|
|
boneAnim.SetTrigger("受伤");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void StoneInteractAnim()
|
|
|
|
|
{
|
|
|
|
|
boneAnim.SetTrigger("与火交互");
|
2022-03-12 21:05:15 +08:00
|
|
|
|
}
|
2022-04-06 10:05:29 +08:00
|
|
|
|
|
|
|
|
|
public void AtkAnim() {
|
|
|
|
|
boneAnim.SetTrigger("攻击");
|
|
|
|
|
}
|
2022-04-01 20:10:20 +08:00
|
|
|
|
}
|