// *********************************************************************** // Assembly : Unity // Author : Kimch // Created : 2018-2-29 // Description : 游戏角色逻辑类 // Last Modified By : // Last Modified On : // *********************************************************************** // // // *********************************************************************** using System.Collections.Generic; using CodeStage.AntiCheat.ObscuredTypes; using UnityEngine; namespace G { /// /// 角色类 /// public partial class EntityCha : Entity { #region Field /// /// /// private bool _isLife = true; /// /// /// protected GameObject _model; /// /// /// protected Collider _collider; private ObscuredInt _curHp; private ObscuredInt _maxHp; #endregion #region Property /// /// 是否活的 /// public bool isLife { get { return _isLife; } protected set { _isLife = value; } } /// /// 等级 /// public int level { get; protected set; } /// /// /// public int curHp { get { return _curHp; } set { _curHp = value; } } /// /// /// public int maxHp { get { return _maxHp; } set { _maxHp = value; } } /// /// /// public float hpRatio => curHp / Mathf.Max(1f, maxHp); /// /// /// public bool isSkilling { get; set; } /// /// 声音 /// public Cha_Sound scriptSound { get; private set; } #endregion #region Physic private Rigidbody _rigidbody; /// /// 质量 /// public float mass { get { return _rigidbody.mass; } set { _rigidbody.mass = value; } } /// /// 拖动 /// public float drag { get { return _rigidbody.drag; } set { _rigidbody.drag = value; } } /// /// /// /// public void SetRigidbody(Rigidbody rigidbody) { _rigidbody = rigidbody; } /// /// 冲力 /// public void AddForce(Vector3 force) { _rigidbody.AddForce(force); } /// /// /// /// public void AddForwardForce(float force) { _rigidbody.AddForce(transform.forward * force); } /// /// /// /// public void AddRightForce(float force) { _rigidbody.AddForce(transform.right * force); } /// /// /// /// public void AddUpForce(float force) { _rigidbody.AddForce(transform.up * force); } /// /// /// /// 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; /// /// /// /// protected void SetAnimator(Animator animator) { _animator = animator; if (_animator) { _animator.speed = 1f; _animator.enabled = true; _animator.cullingMode = AnimatorCullingMode.AlwaysAnimate; _animator.Update(0); } } /// /// /// /// public void SetAnimatorController(RuntimeAnimatorController animatorController) { if (_animator) { _animator.runtimeAnimatorController = animatorController; } } /// /// /// /// public void SetAnimatorSpeed(float speed) { _animator.speed = speed; } /// /// /// /// public void PlayAnimation(string name) { _animator.Play(name); _animator.Update(0); } /// /// /// /// public void PlayAnimation(int id) { _animator.Play(id); _animator.Update(0); } /// /// /// /// /// public void PlayAnimation(int id, float normalizedTime) { _animator.Play(id, -1, normalizedTime); _animator.Update(0); } /// /// /// /// /// /// public void CrossFade(int id, float normalizedTransitionDuration, float normalizedTimeOffset) { _animator.CrossFade(id, normalizedTransitionDuration, -1, normalizedTimeOffset); _animator.Update(0); } /// /// /// public void StopAnimation() { } /// /// /// /// public void SetTrigger(int id) { _animator.SetTrigger(id); _animator.Update(0); } /// /// /// /// public void ResetTrigger(int id) { _animator.ResetTrigger(id); _animator.Update(0); } /// /// /// /// /// public bool GetBool(int id) { return _animator ? _animator.GetBool(id) : false; } /// /// /// /// /// public void SetBool(int id, bool value) { if (_animator) _animator.SetBool(id, value); } /// /// /// /// /// public int GetInteger(int id) { return _animator ? _animator.GetInteger(id) : 0; } /// /// /// /// /// public void SetInteger(int id, int value) { if (_animator) _animator.SetInteger(id, value); } /// /// /// /// /// public float GetFloat(int id) { return _animator ? _animator.GetFloat(id) : 0f; } /// /// /// /// /// 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()); SetCollider(model.GetComponent()); } } 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 类型 /// /// /// /// public bool IsMainPlayer() { return entityType == EntityType.MainPlayer; } /// /// /// /// public bool IsPlayer() { return entityType >= EntityType.Player; } /// /// /// /// public bool IsGeneral() { return entityType == EntityType.General; } /// /// /// /// public bool IsBoss() { return entityType == EntityType.Boss; } /// /// /// /// public bool IsElite() { return entityType == EntityType.Elite; } /// /// 小兵 /// /// public bool IsMinion() { return entityType == EntityType.Minion; } #endregion #region Sound protected AudioSource _audioSource; public void InitSound() { _audioSource = GetComponent(); if (!_audioSource) { _audioSource = gameObject.AddComponent(); } SoundProxy.Instance.SetAudioMixer(_audioSource, SoundProxy.FX_LAYER_INDEX); } /// /// /// 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(); } } /// /// /// /// public void PlaySoundOneShot(AudioClip clip) { if (_audioSource && SoundProxy.Instance.fxEnabled) { _audioSource.PlayOneShot(clip); } } #endregion #region Method /// /// /// /// /// 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(); } protected override void OnViewInitCompleted() { } /// /// 切换场景调用接口 /// 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 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 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 } }