373 lines
11 KiB
C#
373 lines
11 KiB
C#
using Axibug;
|
||
using Axibug.Event;
|
||
using Axibug.Runtime;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.XR;
|
||
|
||
namespace Game
|
||
{
|
||
public abstract class RoleBase : MonoBehaviour
|
||
{
|
||
public E_NODE_TYPE RoleType => mBaseData.RoleType;
|
||
public int RoleID => mBaseData.innate.roleid;
|
||
public S_ROLE_DATA_BASE BaseData => mBaseData;
|
||
|
||
public ICharMachineBase Anime => mAnime;
|
||
|
||
public Transform ModelTransfrom { get { return mModelTransfrom; } }
|
||
public Transform BridgeTransfrom { get { return mBridgeTransfrom; } }
|
||
|
||
/// <summary>
|
||
/// ¶¯»¿ØÖÆÆ÷
|
||
/// </summary>
|
||
protected ICharMachineBase mAnime;
|
||
|
||
protected S_ROLE_DATA_BASE mBaseData;
|
||
|
||
protected Transform mBridgeTransfrom;
|
||
protected Transform mModelTransfrom;
|
||
protected Rigidbody mRigidbody;
|
||
protected Collider mselfCollider;
|
||
|
||
public Animator AttackBoxAnime;
|
||
public AttackBox AttackBoxCollider;
|
||
|
||
public Vector2 InputV2;
|
||
|
||
public E_ONCEATTACK_STEP AttackStep = E_ONCEATTACK_STEP.None;
|
||
|
||
protected virtual void Awake()
|
||
{
|
||
mBridgeTransfrom = transform.Find("Bridge");
|
||
mModelTransfrom = mBridgeTransfrom.Find("Model");
|
||
mselfCollider = transform.GetComponent<Collider>();
|
||
mRigidbody = transform.GetComponent<Rigidbody>();
|
||
}
|
||
|
||
protected virtual void OnEnable()
|
||
{
|
||
IsMove = false;
|
||
IsLeft = false;
|
||
|
||
AttackStep = E_ONCEATTACK_STEP.None;
|
||
|
||
MeshChangeDir(mIsLeft);
|
||
|
||
AppEntry.Event.Subscribe(AttackHitEventArgs.EventId, OnAttackHitEventArgs);
|
||
}
|
||
protected virtual void OnDisable()
|
||
{
|
||
AppEntry.Event.Unsubscribe(AttackHitEventArgs.EventId, OnAttackHitEventArgs);
|
||
}
|
||
|
||
|
||
private void OnAttackHitEventArgs(object sender, LogicEventArgs e)
|
||
{
|
||
AttackHitEventArgs msg = (AttackHitEventArgs)e;
|
||
if (msg == null) throw new GameException("AttackHitEventArgs is null");
|
||
|
||
if (msg.UnderAtk_RoleID != RoleID)
|
||
return;
|
||
|
||
RoleBase Attacker = GamePlayEntry.RoleMgr.FindRole(msg.Attacker_Type, msg.Attacker_RoleID);
|
||
if (Attacker == null)
|
||
{
|
||
AxibugLog.Error("Attacker Ϊ¿Õ");
|
||
return;
|
||
}
|
||
|
||
//ÊÜ»÷¡¢³¯Ïò
|
||
if (Attacker.transform.position.x < transform.position.x)
|
||
IsLeft = true;
|
||
else
|
||
IsLeft = false;
|
||
}
|
||
|
||
/// <summary>
|
||
/// ³õʼ»¯
|
||
/// </summary>
|
||
/// <param name="data"></param>
|
||
public virtual void Init(S_ROLE_DATA_BASE data)
|
||
{
|
||
mBaseData = data;
|
||
if (RoleType == E_NODE_TYPE.N_MAINPLAYER)
|
||
{
|
||
mAnime = new CharacterMachine();
|
||
mAnime.InitMachine(mModelTransfrom, data);
|
||
}
|
||
else if (RoleType == E_NODE_TYPE.N_MONSTER)
|
||
{
|
||
mAnime = new CharacterMachine();
|
||
mAnime.InitMachine(mModelTransfrom, data);
|
||
}
|
||
else if (RoleType == E_NODE_TYPE.N_NPC)
|
||
{
|
||
|
||
}
|
||
}
|
||
|
||
public virtual void SetAttackBoxAnime(Animator attackBoxAnime)
|
||
{
|
||
AttackBoxAnime = attackBoxAnime;
|
||
AttackBoxCollider = attackBoxAnime.transform.Find("Node1/Box1").GetComponent<AttackBox>();
|
||
AttackBoxCollider.InitNode(this);
|
||
}
|
||
|
||
protected virtual void Update()
|
||
{
|
||
}
|
||
protected virtual void FixedUpdate()
|
||
{
|
||
Update_Move();
|
||
Update_Jump();
|
||
FixedUpdate_Move();
|
||
}
|
||
|
||
public abstract void MeshChangeMoveState(bool ToMoveState);
|
||
public abstract void ChangeJumpState(E_JUMP_STEP jumpStep);
|
||
|
||
public void MeshChangeDir(bool ToLeftDir)
|
||
{
|
||
mBridgeTransfrom.localEulerAngles = ToLeftDir ? ConstClass.Angle_LeftDir : ConstClass.Angle_RightDir;
|
||
}
|
||
|
||
public abstract void Release();
|
||
|
||
|
||
#region ÒÆ¶¯
|
||
|
||
private bool mIsMove = false;
|
||
public float MoveTime = 0;
|
||
|
||
public bool IsMove
|
||
{
|
||
get { return mIsMove; }
|
||
set{ if (mIsMove != value)
|
||
{
|
||
mIsMove = value;
|
||
if (value)//¸Õ¿ªÊ¼Òƶ¯
|
||
{
|
||
MoveTime = 0;
|
||
MeshChangeMoveState(true);
|
||
}
|
||
else//¸ÕÍ£Ö¹ÒÆ¶¯
|
||
{
|
||
MoveTime = 0;
|
||
//Move_currentVector = Vector3.zero;//Çå¿ÕÒÆ¶¯·½Ïò
|
||
Move_currentVector.x = 0;//Çå¿ÕÒÆ¶¯·½Ïò
|
||
MeshChangeMoveState(false);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// ³¯Ïò
|
||
/// </summary>
|
||
private bool mIsLeft = false;
|
||
public bool IsLeft
|
||
{
|
||
get { return mIsLeft; }
|
||
set{ if (mIsLeft != value)
|
||
{
|
||
mIsLeft = value;
|
||
MoveTime = 0;//³¯Ïò±ä»¯Ê±£¬ÖØÖÃÒÆ¶¯Ê±¼ä
|
||
if (value)//¿ªÊ¼ÍùÓÒ
|
||
MeshChangeDir(true);
|
||
else//¿ªÊ¼Íù×ó
|
||
MeshChangeDir(false);
|
||
}
|
||
}
|
||
}
|
||
void Update_Move()
|
||
{
|
||
bool isDoMove = false;
|
||
|
||
if (AttackStep != E_ONCEATTACK_STEP.None)
|
||
{
|
||
//Èç¹ûÔÚ¹¥»÷Á÷³Ì ¾Í²»Òƶ¯
|
||
}
|
||
else
|
||
{
|
||
//if (InputV2 != Vector2.zero)
|
||
//½öºáÏòÒÆ¶¯
|
||
if (InputV2.x != 0)
|
||
{
|
||
isDoMove = true;
|
||
//Move_currentVector = new Vector3(InputV2.x, 0, InputV2.y);
|
||
//½öºáÏòÒÆ¶¯
|
||
Move_currentVector.x = InputV2.x;
|
||
}
|
||
|
||
//AxibugLog.Debug("Move_currentVector =>" + Move_currentVector);
|
||
|
||
//³¯Ïò
|
||
if (InputV2.x < 0)
|
||
IsLeft = true;
|
||
else if (InputV2.x > 0)
|
||
IsLeft = false;
|
||
}
|
||
|
||
IsMove = isDoMove;
|
||
}
|
||
|
||
/// <summary>
|
||
/// ËÙ¶È
|
||
/// </summary>
|
||
protected float Move_BaseSpeed = 3.5f;
|
||
protected float Move_SpeedMultiplier = 1f;
|
||
protected Vector3 Move_currentVector;
|
||
|
||
/// <summary>
|
||
/// ÒÆ¶¯Î»ÒÆ
|
||
/// </summary>
|
||
void FixedUpdate_Move()
|
||
{
|
||
if (IsMove) MoveTime += Time.deltaTime;
|
||
|
||
Vector3 _playerPos = mRigidbody.position;
|
||
_playerPos += Move_currentVector * Move_BaseSpeed * Move_SpeedMultiplier * Time.deltaTime;
|
||
mRigidbody.MovePosition(_playerPos);
|
||
//AxibugLog.Debug("FixedUpdate_Move =>" + _playerPos);
|
||
}
|
||
#endregion
|
||
|
||
#region
|
||
public float JumpTime = 0;
|
||
public float Jump_CurrySpeedY = 0;
|
||
|
||
|
||
private E_JUMP_STEP mJumpStep = E_JUMP_STEP.None;
|
||
public E_JUMP_STEP JumpStep
|
||
{
|
||
get { return mJumpStep; }
|
||
set
|
||
{
|
||
if (mJumpStep != value)
|
||
{
|
||
mJumpStep = value;
|
||
AxibugLog.Debug("mJumpStep =>"+ mJumpStep);
|
||
switch (value)
|
||
{
|
||
case E_JUMP_STEP.JumpUp:
|
||
SetToUpVec();
|
||
break;
|
||
case E_JUMP_STEP.JumpFall:
|
||
SetToDownVec();
|
||
break;
|
||
case E_JUMP_STEP.None:
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
void Update_Jump()
|
||
{
|
||
//if(Move_currentVector.y) ÌøÔ¾Ë¥¼õ
|
||
|
||
//ÅжÏÊÇ·ñÔÚ¿ÕÖÐ
|
||
|
||
switch (JumpStep)
|
||
{
|
||
case E_JUMP_STEP.JumpUp:
|
||
//Jump_CurrySpeedY -= Time.deltaTime * ConstClass.JumpSpeedY_Down;
|
||
//if (Jump_CurrySpeedY <= 0)
|
||
//{
|
||
// Jump_CurrySpeedY = 0;
|
||
// ChangeJumpState(E_JUMP_STEP.JumpFall);
|
||
//}
|
||
if (mRigidbody.velocity.y <= ConstClass.JumpSpeedY_MinStop && Time.time - StartJumpTime > 0.2f)
|
||
{
|
||
ChangeJumpState(E_JUMP_STEP.JumpFall);
|
||
}
|
||
break;
|
||
//ÏÂÂä¹ý³ÌÖÐ
|
||
case E_JUMP_STEP.JumpFall:
|
||
{
|
||
//TODO ÅжϵØÃæ½Ó´¥
|
||
AxibugLog.Debug("TerrainEnterNumber->" + TerrainEnterNumber);
|
||
|
||
if (TerrainEnterNumber > 0)
|
||
{
|
||
JumpStep = E_JUMP_STEP.None;
|
||
Move_currentVector.y = 0;
|
||
ChangeJumpState(E_JUMP_STEP.None);
|
||
}
|
||
}
|
||
break;
|
||
}
|
||
|
||
|
||
//if(JumpStep != E_JUMP_STEP.None)
|
||
// Move_currentVector.y = Jump_CurrySpeedY;
|
||
}
|
||
|
||
|
||
float StartJumpTime = 0;
|
||
//
|
||
void SetToUpVec()
|
||
{
|
||
mRigidbody.AddForce(Vector3.up * ConstClass.JumpSpeedY, ForceMode.VelocityChange);
|
||
AxibugLog.Debug("SetToUpVec");
|
||
StartJumpTime = Time.time;
|
||
//Jump_CurrySpeedY = ConstClass.JumpSpeedY;
|
||
}
|
||
|
||
void SetToDownVec()
|
||
{
|
||
mRigidbody.AddForce(Vector3.up * ConstClass.JumpSpeedY_Down, ForceMode.VelocityChange);
|
||
AxibugLog.Debug("SetToDownVec");
|
||
//Jump_CurrySpeedY = ConstClass.JumpSpeedY;
|
||
}
|
||
#endregion
|
||
|
||
#region ¹¥»÷ºÐ×Ó¶¯»»ú
|
||
public void DoAtkBox(string AtkBoxAnimeName)
|
||
{
|
||
AttackBoxAnime.Play(AtkBoxAnimeName);
|
||
}
|
||
#endregion
|
||
|
||
bool CheckRayTerrain()
|
||
{
|
||
Debug.DrawLine(transform.position, transform.position + Vector3.down, Color.red);
|
||
return Physics.RaycastAll(transform.position, transform.position + Vector3.down,1, LayerMask.NameToLayer("Terrain")).Length > 0;
|
||
}
|
||
|
||
#region
|
||
int TerrainEnterNumber;
|
||
|
||
|
||
private void OnCollisionEnter(Collision other)
|
||
{
|
||
if (other.gameObject.layer == LayerMask.NameToLayer("Terrain"))
|
||
TerrainEnterNumber++;
|
||
}
|
||
|
||
private void OnCollisionExit(Collision other)
|
||
{
|
||
if (other.gameObject.layer == LayerMask.NameToLayer("Terrain"))
|
||
TerrainEnterNumber--;
|
||
}
|
||
|
||
//public void OnTriggerEnter(Collider other)
|
||
//{
|
||
// if (other.gameObject.layer == LayerMask.NameToLayer("Terrain"))
|
||
// TerrainEnterNumber++;
|
||
//}
|
||
|
||
//private void OnTriggerExit(Collider other)
|
||
//{
|
||
// if (other.gameObject.layer == LayerMask.NameToLayer("Terrain"))
|
||
// TerrainEnterNumber--;
|
||
//}
|
||
#endregion
|
||
}
|
||
}
|