2025-05-18 01:04:31 +08:00

940 lines
22 KiB
C#

// ***********************************************************************
// Assembly : Unity
// Author : Kimch
// Created : 2018-2-29
// Description : 游戏角色逻辑类
// Last Modified By :
// Last Modified On :
// ***********************************************************************
// <copyright file= "EntityCha" company=""></copyright>
// <summary></summary>
// ***********************************************************************
using System.Collections.Generic;
using CodeStage.AntiCheat.ObscuredTypes;
using UnityEngine;
namespace G
{
/// <summary>
/// 角色类
/// </summary>
public partial class EntityCha : Entity
{
#region Field
/// <summary>
///
/// </summary>
private bool _isLife = true;
/// <summary>
///
/// </summary>
protected GameObject _model;
/// <summary>
///
/// </summary>
protected Collider _collider;
private ObscuredInt _curHp;
private ObscuredInt _maxHp;
#endregion
#region Property
/// <summary>
/// 是否活的
/// </summary>
public bool isLife
{
get { return _isLife; }
protected set { _isLife = value; }
}
/// <summary>
/// 等级
/// </summary>
public int level
{
get;
protected set;
}
/// <summary>
///
/// </summary>
public int curHp
{
get { return _curHp; }
set { _curHp = value; }
}
/// <summary>
///
/// </summary>
public int maxHp
{
get { return _maxHp; }
set { _maxHp = value; }
}
/// <summary>
///
/// </summary>
public float hpRatio => curHp / Mathf.Max(1f, maxHp);
/// <summary>
///
/// </summary>
public bool isSkilling
{
get;
set;
}
/// <summary>
/// 声音
/// </summary>
public Cha_Sound scriptSound
{
get;
private set;
}
#endregion
#region Physic
private Rigidbody _rigidbody;
/// <summary>
/// 质量
/// </summary>
public float mass
{
get { return _rigidbody.mass; }
set { _rigidbody.mass = value; }
}
/// <summary>
/// 拖动
/// </summary>
public float drag
{
get { return _rigidbody.drag; }
set { _rigidbody.drag = value; }
}
/// <summary>
///
/// </summary>
/// <param name="rigidbody"></param>
public void SetRigidbody(Rigidbody rigidbody)
{
_rigidbody = rigidbody;
}
/// <summary>
/// 冲力
/// </summary>
public void AddForce(Vector3 force)
{
_rigidbody.AddForce(force);
}
/// <summary>
///
/// </summary>
/// <param name="force"></param>
public void AddForwardForce(float force)
{
_rigidbody.AddForce(transform.forward * force);
}
/// <summary>
///
/// </summary>
/// <param name="force"></param>
public void AddRightForce(float force)
{
_rigidbody.AddForce(transform.right * force);
}
/// <summary>
///
/// </summary>
/// <param name="force"></param>
public void AddUpForce(float force)
{
_rigidbody.AddForce(transform.up * force);
}
/// <summary>
///
/// </summary>
/// <param name="collider"></param>
public void SetCollider(Collider collider)
{
_collider = collider;
if (_collider)
{
_collider.enabled = true;
}
}
//
public void SetColliderEnabled(bool enabled)
{
if (_collider)
{
_collider.enabled = enabled;
}
}
#endregion
#region Animator
protected Animator _animator;
/// <summary>
///
/// </summary>
/// <param name="animator"></param>
protected void SetAnimator(Animator animator)
{
_animator = animator;
if (_animator)
{
_animator.speed = 1f;
_animator.enabled = true;
_animator.cullingMode = AnimatorCullingMode.AlwaysAnimate;
_animator.Update(0);
}
}
/// <summary>
///
/// </summary>
/// <param name="animatorController"></param>
public void SetAnimatorController(RuntimeAnimatorController animatorController)
{
if (_animator)
{
_animator.runtimeAnimatorController = animatorController;
}
}
/// <summary>
///
/// </summary>
/// <param name="speed"></param>
public void SetAnimatorSpeed(float speed)
{
_animator.speed = speed;
}
/// <summary>
///
/// </summary>
/// <param name="name"></param>
public void PlayAnimation(string name)
{
_animator.Play(name);
_animator.Update(0);
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
public void PlayAnimation(int id)
{
_animator.Play(id);
_animator.Update(0);
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="normalizedTime"></param>
public void PlayAnimation(int id, float normalizedTime)
{
_animator.Play(id, -1, normalizedTime);
_animator.Update(0);
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="normalizedTransitionDuration"></param>
/// <param name="normalizedTimeOffset"></param>
public void CrossFade(int id, float normalizedTransitionDuration, float normalizedTimeOffset)
{
_animator.CrossFade(id, normalizedTransitionDuration, -1, normalizedTimeOffset);
_animator.Update(0);
}
/// <summary>
///
/// </summary>
public void StopAnimation()
{
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
public void SetTrigger(int id)
{
_animator.SetTrigger(id);
_animator.Update(0);
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
public void ResetTrigger(int id)
{
_animator.ResetTrigger(id);
_animator.Update(0);
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public bool GetBool(int id)
{
return _animator ? _animator.GetBool(id) : false;
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="value"></param>
public void SetBool(int id, bool value)
{
if (_animator)
_animator.SetBool(id, value);
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public int GetInteger(int id)
{
return _animator ? _animator.GetInteger(id) : 0;
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="value"></param>
public void SetInteger(int id, int value)
{
if (_animator)
_animator.SetInteger(id, value);
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public float GetFloat(int id)
{
return _animator ? _animator.GetFloat(id) : 0f;
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="value"></param>
public void SetFloat(int id, float value)
{
if (_animator)
_animator.SetFloat(id, value);
}
#endregion
#region Model
public virtual void SetModel(GameObject model)
{
_model = model;
if (model)
{
SetAnimator(model.GetComponent<Animator>());
SetCollider(model.GetComponent<Collider>());
}
}
public virtual void SetModelVisible(bool visible)
{
if (_model)
_model.SetActive(visible);
}
public virtual void SetShadowVisible(bool visible)
{
}
protected BloodBoard _blood;
public virtual void ShowHpBar()
{
if (_blood)
{
_blood.gameObject.SetActive(true);
}
}
public virtual void HideHpBar()
{
if (_blood)
{
_blood.gameObject.SetActive(false);
}
}
public virtual void CreateHpBar()
{
}
public virtual void DestroyHpBar()
{
if (_blood)
{
_blood.Release();
_blood = null;
}
}
public virtual void SetHpBar()
{
SetHpBar(curHp, maxHp, 0f, 0);
}
public virtual void SetHpBar(int index)
{
SetHpBar(curHp, maxHp, 0f, index);
}
public virtual void SetHpBar(int cur, int max)
{
SetHpBar(cur, max, 0f, 0);
}
public virtual void SetHpBar(int cur, int max, int index)
{
SetHpBar(cur, max, 0f, index);
}
public virtual void SetHpBar(int cur, int max, float height, int index)
{
if (_blood)
{
_blood.SetHpValue(cur, max, height, index);
}
}
public virtual void UpdateHpBar()
{
}
#endregion
#region
/// <summary>
///
/// </summary>
/// <returns></returns>
public bool IsMainPlayer()
{
return entityType == EntityType.MainPlayer;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public bool IsPlayer()
{
return entityType >= EntityType.Player;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public bool IsGeneral()
{
return entityType == EntityType.General;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public bool IsBoss()
{
return entityType == EntityType.Boss;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public bool IsElite()
{
return entityType == EntityType.Elite;
}
/// <summary>
/// 小兵
/// </summary>
/// <returns></returns>
public bool IsMinion()
{
return entityType == EntityType.Minion;
}
#endregion
#region Sound
protected AudioSource _audioSource;
public void InitSound()
{
_audioSource = GetComponent<AudioSource>();
if (!_audioSource)
{
_audioSource = gameObject.AddComponent<AudioSource>();
}
SoundProxy.Instance.SetAudioMixer(_audioSource, SoundProxy.FX_LAYER_INDEX);
}
/// <summary>
///
/// </summary>
public void PlaySound()
{
if (_audioSource && SoundProxy.Instance.fxEnabled)
{
_audioSource.Play();
}
}
public void PlaySound(AudioClip clip)
{
if (_audioSource && SoundProxy.Instance.fxEnabled)
{
_audioSource.clip = clip;
_audioSource.Play();
}
}
/// <summary>
///
/// </summary>
/// <param name="clip"></param>
public void PlaySoundOneShot(AudioClip clip)
{
if (_audioSource && SoundProxy.Instance.fxEnabled)
{
_audioSource.PlayOneShot(clip);
}
}
#endregion
#region Method
/// <summary>
///
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
public virtual bool Init(EntityInfo info)
{
if (info != null)
{
this.id = info.guid;
this.itemId = info.itemId;
this.position = info.position;
this.level = info.level;
}
this.InitView();
this.InitSound();
return true;
}
public virtual void InitComponent()
{
scriptSound = GetComponent<Cha_Sound>();
}
protected override void OnViewInitCompleted()
{
}
/// <summary>
/// 切换场景调用接口
/// </summary>
public virtual void OnEnterScene()
{
}
public bool m_handMovement;
public void SetCurDir(Vector3 dir)
{
}
public virtual float GetAtkSpeed()
{
return 0f;
//return m_data.getMaxPro(25);
}
public virtual float GetMoveSpeed()
{
return 0f;
}
public virtual float GetWalkSpeed()
{
return 0f;
}
public virtual void OnStateChange(enStat state, Dictionary<string, object> dict)
{
//if (eventOnState != null)
//{
// eventOnState(state, dict);
//}
}
public void InitAI()
{
realRunAI = false;
StartAI();
EndAI();
realRunAI = true;
}
public virtual void StartAI()
{
//if (m_aiTree != null)
//{
// m_aiTree.enabled = true;
// m_aiTree.EnableBehavior();
// bActiveAI = true;
//}
//m_bEnableShowHint = true;
}
public virtual void EndAI(bool bPause = false)
{
//if (m_aiTree != null)
//{
// m_aiTree.enabled = false;
// m_aiTree.DisableBehavior(bPause);
// bActiveAI = false;
//}
//m_bEnableShowHint = false;
}
public virtual void OnCancelSkill()
{
}
public virtual void ResetTargetAfterAtk()
{
}
public bool EnableHurt
{
get;
set;
//get
//{
// return m_bEnableHurt;
//}
//set
//{
// m_bEnableHurt = value;
//}
}
public bool normalAtkEnableMove
{
get;
set;
//get
//{
// return bEnableMoveWithNormalAtk;
//}
//set
//{
// bEnableMoveWithNormalAtk = value;
//}
}
public void MoveWithNormalAtk(Vector3 nForce)
{
//if (bEnableMoveWithNormalAtk && !LevelData.IsPVPMode)
//{
// AddRigidBodyForce(nForce);
//}
}
public virtual void SetAtkCollision(bool enable)
{
}
public virtual void ResetRigidBody()
{
}
public virtual void UpdateAttackSpeed()
{
float speed = 0.719999969f + GetAtkSpeed();
//m_animation["attack1"].speed = speed;
//m_animation["attack2"].speed = speed;
//m_animation["attack3"].speed = speed;
//m_animation["attack4"].speed = speed;
//m_animation["attack5"].speed = speed;
}
public int myWeaponType => 0;
public Stat_StateManager m_stateMgr;
public EntityCha target
{
get
{
return m_target;
}
set
{
m_target = value;
}
}
public Transform targetTransform => target.transform;
protected EntityCha m_target;
public bool realRunAI
{
get;
set;
}
public virtual bool CanBeAtk()
{
//bool flag = !IsDead() && !invincible && myGameObject.activeSelf;
//if (flag && IsMonster() && GetCurActionState() == enStat.STAT_ACTION_GRABED)
//{
// flag = false;
//}
//return flag;
return false;
}
public void ChangeTarget(EntityCha newTarget, bool bForce = false)
{
if (bForce)
{
target = newTarget;
}
//else if (m_data != null && !m_data.HasBuffState(127))
//{
// target = newTarget;
//}
}
public Vector3 GetTargetPos()
{
if ((bool)target)
{
return target.position;
}
return Vector3.zero;
}
public bool SetActionStatDelegates(enStat stat, string key, StatDelegate del = null)
{
bool result = false;
if (m_stateMgr)
{
result = m_stateMgr.SetActionStatDelegates(stat, key, del);
}
return result;
}
public virtual bool SetCurActionState(enStat stat, Dictionary<string, object> param = null, bool bForce = false, bool clearDelegates = false)
{
bool result = false;
//if ((bool)m_stateMgr)
//{
// result = m_stateMgr.SetCurActionState(stat, param, bForce, clearDelegates);
//}
return result;
}
public bool IsDead()
{
return false;
}
public virtual float GetMovDisAfterAtk()
{
return 0f;
//return m_data.GetMovDisAfterAtk();
}
public virtual float GetWalkDis()
{
return 0f;
//return m_data.GetWalkDis();
}
public virtual float GetAtkDis()
{
//SkillItem canUseSkill = GetCanUseSkill();
//if (canUseSkill != null)
//{
// if (canUseSkill.GetRange() == 0f)
// {
// Debuger.LogError("获取攻击距离 Error ID = " + canUseSkill.templateID);
// }
// return canUseSkill.GetRange() / 100f;
//}
return 0.15f;
}
public float Move(Vector3 dir, float speed)
{
Quaternion to = Quaternion.LookRotation(dir);
float num = Time.deltaTime * speed;
//AddCurPos(dir * num);
//SetCurRotation(Quaternion.Lerp(GetCurRotation(), to, Time.deltaTime * 8f));
return num;
}
public virtual void SetTargetTouchPos()
{
if (target != null)
{
//Vector3 targetPos = GetTargetPos();
//Vector3 curPos = GetCurPos();
//float num = Vector3.Distance(targetPos, curPos);
//float atkDis = GetAtkDis();
//if (num > atkDis)
//{
// Vector3 b = Vector3.Normalize(targetPos - curPos) * atkDis;
// touchPos = curPos + b;
//}
//else
//{
// touchPos = targetPos;
//}
}
}
public virtual bool invincible
{
get; set;
//get
//{
// return m_invincible;
//}
//set
//{
// m_invincible = value;
//}
}
public Vector3 aiAddHpTargetPos
{
get;
set;
}
public virtual bool IsCurAtkSelfRound()
{
return false;
}
public enStat GetCurActionState()
{
return enStat.STAT_ACTION;// m_stateMgr.GetCurActionState();
}
public void LookToTarget(Transform target)
{
transform.LookAt(target);
}
public virtual float GetIdleTimeBeforeAtk()
{
return 0;
//return m_data.GetIdleTimeBeforeAtk();
}
public float GetRunAwayDis()
{
float atkDis = GetAtkDis();
var m_runAwayDis = atkDis * 0.6f;
return m_runAwayDis;
}
public bool HasBorn()
{
return false;
//return m_hasBorn;
}
public bool IsInjured()
{
float curPro = 0;// m_data.getCurPro(2);
float maxPro = 0;// m_data.getMaxPro(2);
return curPro < maxPro;
}
#endregion
#region Unity
protected virtual void Awake()
{
InitComponent();
}
private void OnDestroy()
{
DestroyHpBar();
SetShadowVisible(false);
}
#endregion
}
}