Files
5.24BuYu/Assets/MyGame/Scripts/Characters/Monster/MonsterContral.cs
T

368 lines
12 KiB
C#
Raw Normal View History

2026-03-31 18:30:49 +08:00
#region AI优化
#endregion
#region zijixie
using Assets.MyGame.Scripts.Net.API.GameConfig_SubClass;
2026-02-03 11:35:19 +08:00
using Assets.MyGame.Scripts.Net.APIResponses.Enum;
using Spine;
using Spine.Unity;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using static MonsterMoveLine;
2026-03-12 10:48:53 +08:00
//public enum type
2026-02-03 11:35:19 +08:00
public class MonsterContral : MonoBehaviour
{
2026-03-09 09:25:01 +08:00
public string guid;
2026-02-03 11:35:19 +08:00
public MonsterDataBase monsterData = new MonsterDataBase(new MonsterHealthBase());
public SkeletonAnimation skeleton;
public bool isSpine = true;
2026-03-18 17:26:14 +08:00
public string fishTypeID;
2026-02-03 11:35:19 +08:00
public Dictionary<CharacterAnimationType, string> dic_AnimKeyAnimName = new Dictionary<CharacterAnimationType, string>();
public CharacterStateType currState;
public int HP = 5;
public GameObject effect_Die;//删除
2026-04-23 17:24:31 +08:00
public GameObject effect_DieAward;
2026-02-03 11:35:19 +08:00
GameObject hurt;
2026-02-09 10:44:38 +08:00
public bool isMove = true;
2026-03-12 10:48:53 +08:00
public bool is3D = false;
2026-03-18 17:26:14 +08:00
public string forwordXYZ;
2026-04-03 18:32:19 +08:00
protected RectTransform uirootRect;
protected Camera mainCamera;
2026-04-03 09:28:13 +08:00
public virtual void DieFish(object obj=null)
{
Destroy(gameObject);
}
2026-02-03 11:35:19 +08:00
//递归查找子物体
Transform FindDeepChild(Transform parent, string name)
{
2026-02-09 10:44:38 +08:00
2026-02-03 11:35:19 +08:00
foreach (Transform child in parent)
{
if (child.name == name)
return child;
var result = FindDeepChild(child, name);
if (result != null)
return result;
}
return null;
}
2026-04-03 18:32:19 +08:00
const string YZMoveLineName="鱼阵";
const string PuTongFishMoveLineName="曲线";
2026-03-18 17:26:14 +08:00
public virtual void Init(string pathId, float Time, bool isyuzhen = false, string yzID = "")
2026-02-03 11:35:19 +08:00
{
2026-03-18 17:26:14 +08:00
BezierCurveData bezierCurveData;
if (isyuzhen)
{
2026-04-03 18:32:19 +08:00
bezierCurveData = MonsterMoveLine.dic_BezierCurveDatas[YZMoveLineName + pathId];
2026-03-18 17:26:14 +08:00
}
else
{
2026-04-03 18:32:19 +08:00
bezierCurveData = MonsterMoveLine.dic_BezierCurveDatas[PuTongFishMoveLineName + pathId];
2026-03-18 17:26:14 +08:00
}
2026-02-09 10:44:38 +08:00
monsterData.moveLine.Init(bezierCurveData, Time);
2026-02-03 11:35:19 +08:00
isyz = isyuzhen;
2026-02-09 10:44:38 +08:00
this.yzID = yzID;
if (yzID != "")
switch (yzID.Substring(0, 3))
{
case "700":
if (bezierCurveData.start.y < 0)
{
//float z = transform.localEulerAngles.z; transform.localEulerAngles = new Vector3(0, 0, z + 180);
//transform.rotation= Quaternion.Euler(Vector3.forward*180+transform.eulerAngles);
var v = transform.localScale;
v.y *= -1;
transform.localScale = v;
}
break;
case "701":
break;
default:
break;
}
switch ((FishTypeEnum)monsterData.fishTypeConfig.fishType)
2026-02-03 11:35:19 +08:00
{
2026-02-09 10:44:38 +08:00
case FishTypeEnum.普通鱼:
Destroy(gameObject, Time);
break;
case FishTypeEnum.Boss鱼:
Destroy(gameObject, Time);
break;
case FishTypeEnum.黄金鱼:
Destroy(gameObject, Time);
break;
case FishTypeEnum.特殊鱼:
Destroy(gameObject, Time);
break;
default:
break;
2026-02-03 11:35:19 +08:00
}
}
2026-03-10 15:36:10 +08:00
public void OnDestroy()
{
CreateFishManager.Instance.fishDatas_hashCodekey.Remove(guid);
}
2026-04-03 09:28:13 +08:00
public virtual void Awake()
2026-02-03 11:35:19 +08:00
{
2026-02-09 10:44:38 +08:00
var t = FindDeepChild(transform, "HitEffect");
if (t != null)
2026-02-03 11:35:19 +08:00
{
hurt = t.gameObject;
hurt.SetActive(false);
}
2026-02-09 10:44:38 +08:00
2026-02-03 11:35:19 +08:00
}
public void OnHurt()
{
2026-02-09 10:44:38 +08:00
if (hurt != null)
2026-02-03 11:35:19 +08:00
{
StartCoroutine(Hurt());
}
}
//受击闪烁
IEnumerator Hurt()
{
hurt.SetActive(true);
yield return new WaitForSeconds(0.2f);
hurt.SetActive(false);
}
public void InitAnimList()
{
dic_AnimKeyAnimName.Add(CharacterAnimationType.move, "stand2");
}
//这里捕鱼的结构很简单可以穿模直接写移动
void Update()
{
OnUpdate(currState);
}
public void Start()
{
InitAnimList();
ChangeState(CharacterStateType.move);
2026-04-03 18:32:19 +08:00
uirootRect = UIManager.Instance.uiRoot.GetComponent<RectTransform>();
mainCamera = Camera.main;
2026-02-03 11:35:19 +08:00
}
public virtual void OnMoveEnter()
{
PlayAnim(CharacterAnimationType.move);
//播放动画
//skeleton.AnimationState.SetAnimation(0, dic_AnimKeyAnimName[CharacterAnimationType.move], true);
}
public virtual void OnMoveExit()
{
}
2026-03-18 17:26:14 +08:00
public float moveTime = 0;
2026-02-03 11:35:19 +08:00
public bool isyz = false;
2026-02-09 10:44:38 +08:00
public string yzID = "";
2026-02-03 11:35:19 +08:00
public virtual void OnMoveUpdata()
{
if (isyz)
{
2026-02-09 10:44:38 +08:00
switch (yzID.Substring(0, 3))
2026-02-03 11:35:19 +08:00
{
2026-02-09 10:44:38 +08:00
//向正前方移动
case "700":
2026-02-03 11:35:19 +08:00
moveTime += Time.deltaTime;
2026-02-09 10:44:38 +08:00
//可优化
var movestart = monsterData.moveLine.bezierCurveData.start;
2026-04-03 18:32:19 +08:00
movestart = CoordinateTransformation.CanvasToWorld(movestart, uirootRect, mainCamera);
2026-02-09 10:44:38 +08:00
//Debug.Log("movestart:"+movestart);
if (monsterData.moveLine.bezierCurveData.start.y > 0)
{
Vector2 v2 = movestart + (Vector2)(-1 * transform.up * moveTime * monsterData.moveLine.moveTime);
transform.position = v2;
}
else
{
Vector2 v2 = movestart + (Vector2)(transform.up * moveTime * monsterData.moveLine.moveTime);
transform.position = v2;
}
//Vector2 v2 = movestart+(Vector2)(-1*transform.up*moveTime* monsterData.moveLine.moveTime);
//transform.position = v2;
break;
case "701":
moveTime += Time.deltaTime;
//可优化
movestart = monsterData.moveLine.bezierCurveData.start;
2026-04-03 18:32:19 +08:00
movestart = CoordinateTransformation.CanvasToWorld(movestart, uirootRect,mainCamera);
2026-02-09 10:44:38 +08:00
//Debug.Log("movestart:" + movestart);
{
Vector2 v2 = movestart + (Vector2)(-1 * transform.up * moveTime * monsterData.moveLine.moveTime);
transform.position = v2;
}
break;
case "702":
2026-03-27 15:18:35 +08:00
case "710":
2026-02-09 10:44:38 +08:00
moveTime += Time.deltaTime;
{
2026-03-31 18:30:49 +08:00
//Vector2 v2 = monsterData.moveLine.GetBezierPoint(moveTime);
Vector2 v2 = monsterData.moveLine.GetBezierPointJob(moveTime);
2026-03-12 10:48:53 +08:00
if ((FishTypeEnum)monsterData.fishTypeConfig.fishType != FishTypeEnum.Boss鱼)
2026-02-09 10:44:38 +08:00
{
2026-03-27 15:18:35 +08:00
switch (forwordXYZ)
{
case "X":
transform.right = (v2 - (Vector2)transform.position).normalized;
break;
case "Y":
transform.up = (v2 - (Vector2)transform.position).normalized;
break;
case "Z":
2026-03-31 17:00:05 +08:00
2026-03-27 15:18:35 +08:00
transform.forward = (v2 - (Vector2)transform.position).normalized;
break;
case "-X":
transform.right = ((Vector2)transform.position - v2).normalized;
break;
case "-Y":
transform.up = ((Vector2)transform.position - v2).normalized;
break;
case "-Z":
transform.forward = ((Vector2)transform.position - v2).normalized;
break;
default:
transform.up = ((Vector2)transform.position - v2).normalized;
break;
}
//transform.up = ((Vector2)transform.position - v2).normalized;
2026-02-09 10:44:38 +08:00
}
transform.position = v2;
}
2026-02-03 11:35:19 +08:00
break;
default:
break;
}
}
else
{
2026-03-12 10:48:53 +08:00
2026-02-03 11:35:19 +08:00
moveTime += Time.deltaTime;
2026-03-31 18:30:49 +08:00
Vector2 v2 = monsterData.moveLine.GetBezierPointJob(moveTime);
2026-03-12 10:48:53 +08:00
2026-02-09 10:44:38 +08:00
if ((FishTypeEnum)monsterData.fishTypeConfig.fishType != FishTypeEnum.Boss鱼)
2026-02-03 11:35:19 +08:00
{
2026-03-18 17:26:14 +08:00
if (forwordXYZ == "" || forwordXYZ == null)
2026-03-12 10:48:53 +08:00
{
2026-03-18 17:26:14 +08:00
if (!is3D)
{
2026-03-31 17:00:05 +08:00
transform.up = ((Vector2)transform.position - v2).normalized;
2026-03-18 17:26:14 +08:00
}
else
{
transform.forward = (v2 - (Vector2)transform.position).normalized;
}
2026-03-12 10:48:53 +08:00
}
else
{
2026-03-18 17:26:14 +08:00
switch (forwordXYZ)
{
case "X":
transform.right = (v2 - (Vector2)transform.position).normalized;
break;
case "Y":
transform.up = (v2 - (Vector2)transform.position).normalized;
break;
case "Z":
transform.forward = (v2 - (Vector2)transform.position).normalized;
break;
case "-X":
2026-03-31 17:00:05 +08:00
transform.right = ((Vector2)transform.position - v2).normalized;
2026-03-18 17:26:14 +08:00
break;
case "-Y":
2026-03-31 17:00:05 +08:00
transform.up = ((Vector2)transform.position - v2).normalized;
2026-03-18 17:26:14 +08:00
break;
case "-Z":
2026-03-31 17:00:05 +08:00
transform.forward = ((Vector2)transform.position - v2).normalized;
2026-03-18 17:26:14 +08:00
break;
default:
break;
}
2026-03-12 10:48:53 +08:00
}
2026-02-03 11:35:19 +08:00
}
2026-03-12 10:48:53 +08:00
2026-02-03 11:35:19 +08:00
transform.position = v2;
}
2026-02-09 10:44:38 +08:00
2026-02-03 11:35:19 +08:00
}
public void PlayAnim(CharacterAnimationType animType, bool loop = true)
{
2026-02-09 10:44:38 +08:00
if (skeleton != null)
skeleton.AnimationState.SetAnimation(0, dic_AnimKeyAnimName[animType], loop);
2026-02-03 11:35:19 +08:00
}
public void OnEnter(CharacterStateType stateType)
{
switch (stateType)
{
case CharacterStateType.move:
OnMoveEnter();
break;
case CharacterStateType.idle:
break;
default:
break;
}
}
public void OnUpdate(CharacterStateType stateType)
{
2026-02-09 10:44:38 +08:00
if (!isMove)
{
return;
}
2026-02-03 11:35:19 +08:00
switch (stateType)
{
case CharacterStateType.move:
OnMoveUpdata();
break;
case CharacterStateType.idle:
break;
default:
break;
}
}
public void OnExit(CharacterStateType stateType)
{
switch (stateType)
{
case CharacterStateType.move:
break;
case CharacterStateType.idle:
break;
default:
break;
}
}
public void ChangeState(CharacterStateType stateType)
{
OnExit(currState);
currState = stateType;
OnEnter(currState);
}
}
2026-03-31 18:30:49 +08:00
#endregion