using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using System;
namespace YLBattle
{
///
/// 战斗中角色
///
public class Fighter : MonoBehaviour
{
#region 操作
///
/// 外部交互
///
private IBattleGlobalOper mGlobalOper = null;
///
/// 战场数据交互
///
public IBattleGlobalOper GlobalOper
{
get { return mGlobalOper; }
}
///
///
///
private DragonBones.UnityArmatureComponent pmDragonBone = null;
///
/// 状态机
///
public DragonBones.UnityArmatureComponent mDragonBone
{
get { return pmDragonBone; }
}
#endregion
#region FighterData
///
/// 是否死亡
///
public bool IsDead = false;
///
/// 编号ID
///
private string pmID = string.Empty;
///
/// 编号ID
///
public string mID
{
get
{
return pmID;
}
set
{
pmID = value;
}
}
///
/// 模版ID
///
private string pmTID = string.Empty;
///
/// 模版ID
///
public string mTID
{
get
{
return pmTID;
}
set
{
pmTID = value;
}
}
///
/// 等级
///
private int pmLevel = 0;
///
/// 等级
///
public int mLevel
{
get { return pmLevel; }
}
///
///
///
private string pmDragonName = "";
///
///
///
public string mDragonName
{
get { return pmDragonName; }
}
///
/// 所属阵营
///
private EBattleTeam pmTeam = EBattleTeam.NONE;
///
/// 所属阵营
///
public EBattleTeam mTeam
{
get
{
return pmTeam;
}
set
{
pmTeam = value;
}
}
///
///
///
private int pmSeat = 0;
///
/// 占位
///
public int mSeat
{
get { return pmSeat; }
}
private int pmHeight = 0;
///
/// 身高
///
public int mHeight
{
get { return pmHeight; }
}
///
///
///
private Vector3 pmbodyCenterPos = Vector3.zero;
///
///
///
public Vector3 mBodyCenterPos
{
get
{
if (mTeam == EBattleTeam.REDTEAM)
{
this.pmbodyCenterPos = GetScreenVector3(this.pmbodyCenterPoint.position);
this.pmbodyCenterPos.z = 0;
}
if (mTeam == EBattleTeam.BLUETEAM)
{
float posX = this.mGlobalOper.GetFighterProperty(mID, EBattleProperty.POSX);
float posY = this.mGlobalOper.GetFighterProperty(mID, EBattleProperty.POSY);
pmbodyCenterPos = new Vector3(posX, posY, 0);
}
return pmbodyCenterPos;
}
}
///
///
///
private Vector3 pmbodyRootPos = Vector3.zero;
///
///
///
public Vector3 mBodyRootPos
{
get
{
if (mTeam == EBattleTeam.REDTEAM)
{
this.pmbodyRootPos = GetScreenVector3(this.pmbodyRootPoint.position);
this.pmbodyRootPos.z = 0;
}
if (mTeam == EBattleTeam.BLUETEAM)
{
float posX = this.mGlobalOper.GetFighterProperty(mID, EBattleProperty.POSX);
float posY = this.mGlobalOper.GetFighterProperty(mID, EBattleProperty.POSY);
pmbodyRootPos = new Vector3(posX, posY, 0);
}
return pmbodyRootPos;
}
}
///
///
///
private Transform pmbodyCenterPoint = null;
///
///
///
public Transform mBodyCenterPoint
{
get
{
if (pmbodyCenterPoint == null)
{
LogHelper.LogError("没有找到发射点 TID=" + pmTID);
pmbodyCenterPoint = transform;
}
return pmbodyCenterPoint;
}
}
///
///
///
private Transform pmbodyRootPoint = null;
///
///
///
public string mNormalSound { get; set; }
///
///
///
public string mSkillSound { get; set; }
///
///
///
public string mBeattackSound { get; set; }
///
///
///
private RenderTexture pmBodyImage = null;
///
/// 红队
///
private MeshRenderer pmRedBody;
///
/// 红队
///
private MeshRenderer pmRedBodyGray;
///
/// 蓝队
///
private RawImage pmBlueBody;
///
///
///
//private GameObject pmRedSelectFlag = null;
///
/// 动画机
///
private Animator pmAnimator = null;
///
///
///
public Animator mBodyAnimator
{
get
{
if (this.pmAnimator == null)
{
pmAnimator = this.pmRedBody.GetComponent();
}
return pmAnimator;
}
}
///
/// 动画机
///
private Animator pmGrayAnimator = null;
///
///
///
public Animator mGrayAnimator
{
get
{
if (this.pmGrayAnimator == null)
{
pmGrayAnimator = this.pmRedBodyGray.GetComponent();
}
return pmGrayAnimator;
}
}
///
/// 是否外部调用龙骨动画(暴击状态时,tip调用了龙骨动画,所以不能随意修改(如被攻击恢复....)
///
private bool IsOutInvokeDragon = false;
///
/// 外部调用订阅事件
///
private Action mOutInvokeCallBack = null;
///
///
///
private GameObject mLianjiEffect = null;
///
/// 当前连击点击状态
///
private bool mLianjiState = false;
#endregion
///
/// 获取属性
///
/// 属性
/// 属性值
public float GetProperty(EBattleProperty tp)
{
return this.mGlobalOper.GetFighterProperty(mID, tp);
}
///
/// 设置实例信息
///
/// 实例ID
/// 模板ID
/// 队列
/// 占位(敌方和己方不同,注意场景中的实际占位)
/// 战场交换器
internal void SetInfo(string _id, string _strTid, EBattleTeam _eTeam, int _intSeat, IBattleGlobalOper mBFM, string _sounds, string _dragonName)
{
this.pmID = _id;
this.pmTeam = _eTeam;
this.pmTID = _strTid;
this.pmSeat = _intSeat;
this.mGlobalOper = mBFM;
this.pmLevel = (int)mBFM.GetFighterProperty(_id, EBattleProperty.LEVEL);
this.pmHeight = (int)mBFM.GetFighterProperty(_id, EBattleProperty.HEIGHT);
string[] sounds = _sounds.Split(',');
this.mNormalSound = sounds[0];
this.mSkillSound = sounds[1];
this.mBeattackSound = sounds[2];
this.pmDragonName = _dragonName;
this.gameObject.AddComponent();
///蓝队位置
if (this.mTeam == EBattleTeam.BLUETEAM)
{
this.transform.localPosition = new Vector3(this.transform.localPosition.x,
this.transform.localPosition.y + 1500 / 2 - pmHeight / 2 / 2,
this.transform.localPosition.z);
}
///被攻击位置
if (this.mTeam == EBattleTeam.BLUETEAM)
{
this.pmbodyCenterPoint = this.transform.Find("BeAttackCenter");
this.pmbodyCenterPoint.localPosition = new Vector3(0, -1500 / 2 + pmHeight * 1.0f / 7.1f, 0);
this.pmbodyRootPoint = this.transform.Find("BeAttackRoot");
this.pmbodyRootPoint.localPosition = new Vector3(0, -1500 / 2 + pmHeight * 1.0f / 7.1f, 0); ;
}
else
{
this.pmbodyCenterPoint = this.transform.Find("BeAttackCenter");
this.pmbodyCenterPoint.localPosition = new Vector3(0, -1500 / 100 / 3.5f + pmHeight / 100 / 7.1f, 0);
this.pmbodyRootPoint = this.transform.Find("BeAttackRoot");
///红队存在于3D场景中,摄像机有Y轴的倾斜,所以Y轴偏上,这里做一个修正
float offsetY = 0f;
this.pmbodyRootPoint.localPosition = new Vector3(0, -1500 / 100 / 2 - offsetY, 0);
//this.pmRedSelectFlag = this.transform.Find("SelectFlag").gameObject;
//this.pmRedSelectFlag.SetActive(false);
}
///初始化位置(蓝队以UI位置为最终点,但是出场时UI为出现,因此在这里初始第一版位置)
//if (_eTeam == EBattleTeam.BLUETEAM)
//{
// Vector3 pos = GetScreenVector3(pmbodyRootPoint.position);
// this.mGlobalOper.ModifyFighterPosition(mID, pos.x, pos.y);
// Debug.Log(this.pmID + "/" + pos.ToString());
//}
}
///
///
///
///
///
public void SetDragonBone(DragonBones.UnityArmatureComponent _dragonboneCont, RenderTexture rt)
{
this.pmDragonBone = _dragonboneCont;
this.pmBodyImage = rt;
if (this.pmTeam == EBattleTeam.BLUETEAM)
{
this.pmBlueBody = transform.Find("RawImageBody").GetComponent();
this.pmBlueBody.texture = this.pmBodyImage;
this.pmBlueBody.enabled = false;
this.pmDragonBone.gameObject.SetActive(false);
this.pmDragonBone.enabled = false;
}
if (this.pmTeam == EBattleTeam.REDTEAM)
{
this.pmRedBody = transform.Find("PlaneBody").GetComponent();
this.pmRedBody.material.mainTexture = this.pmBodyImage;
this.pmRedBodyGray = transform.Find("PlaneBodyGray").GetComponent();
this.pmRedBodyGray.material.mainTexture = this.pmBodyImage;
this.pmRedBodyGray.transform.localPosition = new Vector3(0, 0, -0.01f);
this.pmRedBodyGray.gameObject.SetActive(false);
}
if (!this.pmDragonBone.HasEventListener(DragonBones.EventObject.COMPLETE))
{
////监听动画完成事件
this.pmDragonBone.AddEventListener(DragonBones.EventObject.COMPLETE, OnDragonBoneAnimation_COMPLETE);
}
///循环事件别删除,部分动画是循环的,比如攻击,程序这强制执行修正
if (!this.pmDragonBone.HasEventListener(DragonBones.EventObject.LOOP_COMPLETE))
{
////监听动画完成事件
this.pmDragonBone.AddEventListener(DragonBones.EventObject.LOOP_COMPLETE, OnDragonBoneAnimation_LOOP_COMPLETE);
}
if (this.mTeam == EBattleTeam.REDTEAM)
{
this.pmDragonBone.animation.Play("Idle", 0);
}
}
///
/// sence坐标转屏幕坐标(red转ui)
///
///
///
private Vector3 GetScreenVector3(Vector3 orgPos)
{
Vector3 trgPos = Vector3.zero;
Vector3 sencePos = CameraManager.Instance.SenceCamara.WorldToScreenPoint(orgPos);
trgPos = CameraManager.Instance.SenceUICamera.ScreenToWorldPoint(sencePos);
return trgPos;
}
/////
///// 参考2
/////
/////
//Vector2 MonoWorldToUGUI(Vector3 orgPos)
//{
// Vector2 pos;
// Vector3 screenPos = CameraManager.Instance.SenceCamara.WorldToScreenPoint(orgPos);
// RectTransformUtility.ScreenPointToLocalPointInRectangle(mMng.MeCanvas.GetComponent(),
// screenPos, CameraManager.Instance.SenceUICamera, out pos);
// return pos;
//}
///
///
///
///
///
///
public void SetActiveBodyTexture(bool _bol, string _strAniName = "", Action ac = null)
{
this.IsOutInvokeDragon = _bol;
if (mTeam == EBattleTeam.BLUETEAM)
{
this.pmDragonBone.gameObject.SetActive(_bol);
this.pmDragonBone.enabled = _bol;
//this.pmBlueBody.enabled = _bol;
}
if (_bol && _strAniName.Length > 0)
{
this.mOutInvokeCallBack = ac;
this.PlayDragonAnimation(_strAniName, true);
}
}
///
/// 播放动画
///
/// 动画名称
/// 是否强制播放
/// 是否循环播放
private void PlayDragonAnimation(string aniName, bool isForce, int _loop = 1)
{
if (isForce)
{
this.mDragonBone.animation.Play(aniName, aniName == "Idle" ? 0 : 1);
return;
}
if (this.mDragonBone.animation.lastAnimationName == aniName)
{
return;
}
this.mDragonBone.animation.Play(aniName, aniName == "Idle" ? 0 : _loop);
}
///
///
///
///
///
public void OnDragonBoneAnimation_LOOP_COMPLETE(string type, DragonBones.EventObject eventObject)
{
switch (eventObject.animationState.name)
{
case "Attack":
if (mTeam == EBattleTeam.REDTEAM)
{
this.PlayDragonAnimation("Idle", false);
}
break;
case "Beattack":
if (mTeam == EBattleTeam.REDTEAM)
{
this.PlayDragonAnimation("Idle", false);
}
break;
}
}
///
///
///
///
///
public void OnDragonBoneAnimation_COMPLETE(string type, DragonBones.EventObject eventObject)
{
switch (eventObject.animationState.name)
{
case "Attack":
if (mTeam == EBattleTeam.REDTEAM)
{
this.PlayDragonAnimation("Idle", false);
}
if (mOutInvokeCallBack != null)
{
this.mOutInvokeCallBack.Invoke();
this.mOutInvokeCallBack = null;
this.PlayDragonAnimation("Idle", false);
}
/// SetActiveBodyTexture关闭触发会让IsOutInvokeDragon=false,使动画归位
//if (this.IsOutInvokeDragon)
//{
// ///如果是蓝队正对一下情况,会影响必杀全身像的显示
// return;
//}
//this.pmDragonBone.gameObject.SetActive(false);
//this.pmDragonBone.enabled = false;
break;
case "Beattack":
if (mTeam == EBattleTeam.REDTEAM)
{
this.PlayDragonAnimation("Idle", false);
}
break;
}
}
///
/// 攻击
///
/// 技能ID
/// 技能攻击动作 0=无动作 1=攻击动作
internal void SetAttack(string skill, int _atkactiontype)
{
/**
* 音效
*/
//Debug.Log("##################################普通攻击动作播放时加音效(跟着人属性走)#########################");
////AudioManager.Instance.PlayHeroSound(mTID, this.mAttackSound, ESoundType.HERO_ATTACK);
if (_atkactiontype != 0)
{
if (this.mTeam == EBattleTeam.BLUETEAM)
{
//this.pmDragonBone.gameObject.SetActive(true);
//this.pmDragonBone.enabled = true;
//this.PlayDragonAnimation("Attack", true);
}
else
{
this.PlayDragonAnimation("Attack", true);
}
}
this.mGlobalOper.OnCastBullet(mID, skill);
}
private bool isLianjiEffectLoad = false;
///
///
///
///
public void SetLianjiEffect(bool _bol)
{
if (isLianjiEffectLoad == false)
{
isLianjiEffectLoad = true;
SkillControl.Instance.createPosEffect("UI_SKILL_LianjiFiger", this.mBodyCenterPos, (obj) =>
{
this.mLianjiEffect = obj;
this.mLianjiEffect.SetActive(true);
this.mLianjiEffect.transform.position = this.mBodyCenterPos;
});
return;
}
if (this.mLianjiEffect == null)
{
return;
}
if (_bol)
{
this.mLianjiEffect.SetActive(true);
this.mLianjiEffect.transform.position = this.mBodyCenterPos;
}
else
{
this.mLianjiEffect.SetActive(false);
}
}
///
/// 清理特效
///
public void ClearLianjiEffect()
{
if (this.mLianjiEffect != null)
{
Destroy(this.mLianjiEffect);
this.mLianjiEffect = null;
}
}
public void SetBeSelect()
{
//this.pmRedSelectFlag.SetActive(true);
}
public void SetBeForget()
{
//this.pmRedSelectFlag.SetActive(false);
}
///
/// 受伤
///
internal void SetHit()
{
string strId = this.mTID.Substring(0, 3);
/**
* 101开头是角色,202开头是boss
* 只有这两类有被攻击动画
* 其他小怪,用闪动Animator攻击动画
*/
if (strId == "101" || strId == "202")
{
this.PlayDragonAnimation("Beattack", true, 1);
}
//else
{
this.mBodyAnimator.Play("BeAttack");
Invoke("RetsetHitColor", 0.4f);
}
}
///
///
///
void RetsetHitColor()
{
//Debug.Log(mID+ " RetsetHitColor");
if (this.IsOutInvokeDragon)
{
///如果是蓝队正对一下情况,会影响必杀全身像的显示
return;
}
this.mBodyAnimator.Play("Idle");
}
///
///
///
///
internal void SetIdle(bool _force)
{
if (this.IsOutInvokeDragon)
{
///如果是蓝队正对一下情况,会影响必杀全身像的显示
return;
}
///攻击状态,则不强制转化Idle,完整播放,待攻击动画播放完毕后,再由事件返回idle
if (_force)
{
this.PlayDragonAnimation("Idle", true);
}
else
{
if (this.mTeam == EBattleTeam.BLUETEAM)
{
this.pmDragonBone.gameObject.SetActive(false);
this.pmDragonBone.enabled = false;
}
}
}
///
/// 死亡
///
internal void SetDead()
{
this.IsDead = true;
this.ClearLianjiEffect();
this.pmDragonBone.RemoveEventListener(DragonBones.EventObject.COMPLETE, OnDragonBoneAnimation_COMPLETE);
}
///
/// 出场
///
internal void SetArrival()
{
if (this.mTeam == EBattleTeam.REDTEAM)
{
this.pmDragonBone.gameObject.SetActive(true);
this.pmDragonBone.enabled = true;
gameObject.SetActive(true);
}
else
{
this.pmDragonBone.gameObject.SetActive(false);
this.pmDragonBone.enabled = false;
}
}
///
/// 隐身~
///
internal void SetHidden()
{
this.pmDragonBone.gameObject.SetActive(false);
gameObject.SetActive(false);
}
///
/// (红队)隐身
///
internal void SetDisapper()
{
this.pmRedBody.gameObject.SetActive(false);
this.pmRedBodyGray.gameObject.SetActive(true);
this.mGrayAnimator.Play("Die");
Invoke("LateDisapper", 0.5f);
}
///
/// 延迟删除
///
void LateDisapper()
{
FightingManager.Instance.DeleteOneFighter(mID);
}
///
/// 变亮
///
internal void SetLight()
{
//this.pmRedBody.gameObject.SetActive(true);
this.mGrayAnimator.Play("Show");
this.pmRedBodyGray.gameObject.SetActive(false);
}
///
/// 变暗
///
internal void SetDark()
{
//this.pmRedBody.gameObject.SetActive(false);
this.pmRedBodyGray.gameObject.SetActive(true);
this.mGrayAnimator.Play("Dark");
}
///
/// 查找Transform
///
/// 名称
/// Transform
public Transform FindTransform(string name)
{
Transform[] trans = this.transform.GetComponentsInChildren();
for (int i = 0; i < trans.Length; i++)
{
if (trans[i].name == name)
{
return trans[i];
}
}
return null;
}
}
}