BattleObject.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public class BattleObject : MapObject, IBattleObject , ITarget
  5. {
  6. public Animation animation;
  7. public Texture[] textureArr;
  8. public Material material;
  9. protected int _userId;
  10. protected AI.AIType _aiType;
  11. protected float m_OriginMoveSpeed = 3f;
  12. public virtual float originMoveSpeed
  13. {
  14. get{
  15. return m_OriginMoveSpeed;
  16. }
  17. set{
  18. m_OriginMoveSpeed = value;
  19. }
  20. }
  21. protected float m_ModifyMoveSpeed = 0f;
  22. public virtual float modifyMoveSpeed
  23. {
  24. get{
  25. return m_ModifyMoveSpeed;
  26. }
  27. set{
  28. m_ModifyMoveSpeed = value;
  29. }
  30. }
  31. protected bool _enableMove = true;
  32. protected bool _enablePower = true;
  33. protected bool _holdAction = false;
  34. public float headBarHeight = 1.5f;
  35. protected HeadBar headBar;
  36. public bool enabledHeadBar = true;
  37. protected float _hp;
  38. protected float _maxHp;
  39. protected float _originMaxHp;
  40. public int lastShooterId = int.MinValue;
  41. public int lastShooterCraftId = int.MinValue;
  42. protected float lastDamageTime;
  43. protected float hpRecoverInterval = 8f;
  44. protected float hpRecoverSpeed = 0.025f;
  45. public float viewRange = 8f;
  46. public bool overwhelming;
  47. protected bool _isDead;
  48. protected float deadTime;
  49. protected Vector3 deadPos;
  50. protected List<Buff> buffList = new List<Buff>();
  51. protected ThreatManager threatManager;
  52. public event EventUtil.SimpleEventDelegate HpChanged;
  53. public event EventUtil.SimpleEventDelegate Killed;
  54. private ITarget _target;
  55. public ITarget target{
  56. get{
  57. return _target;
  58. }
  59. set{
  60. _target = value;
  61. }
  62. }
  63. public override void Init (Map map)
  64. {
  65. base.Init (map);
  66. threatManager = new ThreatManager();
  67. if(enabledHeadBar)
  68. {
  69. InitHeadBar();
  70. }
  71. Renderer renderer = GetComponentInChildren<Renderer> ();
  72. if (renderer != null)
  73. material = renderer.material;
  74. else
  75. material = null;
  76. }
  77. public virtual void EnterFrame(){}
  78. public int userId
  79. {
  80. get{
  81. return _userId;
  82. }
  83. set{
  84. _userId = value;
  85. }
  86. }
  87. public AI.AIType aiType
  88. {
  89. get{
  90. return _aiType;
  91. }
  92. set{
  93. _aiType = value;
  94. }
  95. }
  96. public override TeamUtil.Team team {
  97. get {
  98. return base.team;
  99. }
  100. set {
  101. base.team = value;
  102. if(textureArr.Length > 0)
  103. {
  104. int index = value.GetHashCode() - 1;
  105. Renderer[] renderers = this.GetComponentsInChildren<Renderer>();
  106. for(int i=0; i<renderers.Length; i++)
  107. {
  108. renderers[i].material.mainTexture = textureArr[index];
  109. }
  110. }
  111. }
  112. }
  113. public HeadBar GetHeadBar()
  114. {
  115. return headBar;
  116. }
  117. public virtual Transform GetBaseTransform()
  118. {
  119. return transform;
  120. }
  121. public override Vector3 position {
  122. get {
  123. if(IsDead())
  124. return deadPos;
  125. return base.position;
  126. }
  127. set {
  128. if(!IsDead())
  129. base.position = value;
  130. }
  131. }
  132. public virtual Vector3 heartPos
  133. {
  134. get{
  135. Vector3 pos = position;
  136. pos.y = 1.5f;
  137. return pos;
  138. }
  139. }
  140. public virtual bool isMoving
  141. {
  142. get{
  143. return false;
  144. }
  145. }
  146. public virtual bool enableMove
  147. {
  148. set{
  149. _enableMove = value;
  150. if(!value)
  151. {
  152. for(int k=buffList.Count-1; k>=0; k--)
  153. {
  154. if(k >= buffList.Count)
  155. continue;
  156. buffList[k].OnDismove();
  157. }
  158. }
  159. }
  160. get{
  161. return _enableMove;
  162. }
  163. }
  164. public bool enablePower
  165. {
  166. set{
  167. _enablePower = value;
  168. }
  169. get{
  170. return _enablePower;
  171. }
  172. }
  173. public virtual bool holdAction
  174. {
  175. set{
  176. _holdAction = value;
  177. }
  178. get{
  179. return _holdAction;
  180. }
  181. }
  182. //hp & mp
  183. public void InitHeadBar()
  184. {
  185. GameObject headBarPrefab = Resources.Load(Config.HEAD_BAR_PREFAB) as GameObject;
  186. GameObject headBarObj = Instantiate(headBarPrefab) as GameObject;
  187. PopUpManager.AddToMainCanvas (headBarObj);
  188. headBar = headBarObj.GetComponent<HeadBar>();
  189. headBar.Init(this);
  190. }
  191. public float hp
  192. {
  193. set{
  194. if(_hp != value)
  195. {
  196. _hp = NumberUtil.forceBetween(value, 0, maxHp);
  197. if(HpChanged != null)
  198. HpChanged();
  199. if(headBar != null)
  200. {
  201. headBar.UpdateHpBar();
  202. }
  203. }
  204. }
  205. get{
  206. return _hp;
  207. }
  208. }
  209. public float maxHp
  210. {
  211. set{
  212. _maxHp = value;
  213. }
  214. get{
  215. return _maxHp;
  216. }
  217. }
  218. public float originMaxHp
  219. {
  220. get{
  221. return _originMaxHp;
  222. }
  223. }
  224. public virtual void MakeDamage(Bullet bullet)
  225. {
  226. lastShooterId = bullet.GetOwner ().userId;
  227. lastShooterCraftId = (bullet.GetOwner() is Craft ? (bullet.GetOwner() as Craft).GetCraftId() : 0);
  228. lastDamageTime = GameTime.time;
  229. if(!overwhelming)
  230. hp -= bullet.damage;
  231. if(bullet.GetOwner().team != team)
  232. threatManager.AddThreat(bullet.GetOwner().id, bullet.damage);
  233. DamageStat (lastShooterId, bullet.damage);
  234. OnDamage();
  235. }
  236. public virtual void MakeDamage(float damage, BattleObject launcher)
  237. {
  238. lastShooterId = launcher.userId;
  239. lastDamageTime = GameTime.time;
  240. if(!overwhelming)
  241. hp -= damage;
  242. if(launcher.team != team)
  243. threatManager.AddThreat(launcher.id, damage);
  244. DamageStat (lastShooterId, damage);
  245. OnDamage();
  246. }
  247. public void MakeDamage(float damage)
  248. {
  249. lastDamageTime = GameTime.time;
  250. if(!overwhelming)
  251. hp -= damage;
  252. }
  253. private void DamageStat(int userId, float damage)
  254. {
  255. Player player = Session.GetInstance ().GetBattleSession ().GetPlayer (userId);
  256. if (player != null)
  257. player.damage += damage;
  258. }
  259. public virtual void MakeHeal(Bullet bullet)
  260. {
  261. hp += bullet.damage;
  262. HealStat (bullet.GetOwner().userId, bullet.damage);
  263. }
  264. private void HealStat(int userId, float heal)
  265. {
  266. Player player = Session.GetInstance ().GetBattleSession ().GetPlayer (userId);
  267. if (player != null)
  268. player.heal += heal;
  269. }
  270. protected void CheckHpRecover()
  271. {
  272. if(hp < maxHp && IsLeaveDamage())
  273. {
  274. float hpRecover = maxHp*hpRecoverSpeed*GameTime.deltaTime;
  275. hp += hpRecover;
  276. }
  277. }
  278. public bool IsLeaveDamage()
  279. {
  280. return GameTime.time - lastDamageTime > hpRecoverInterval;
  281. }
  282. public void AddBuff(Buff buff)
  283. {
  284. if(buff == null)
  285. return;
  286. buffList.Add(buff);
  287. }
  288. public void RemoveBuff(Buff buff)
  289. {
  290. buffList.Remove(buff);
  291. }
  292. public List<Buff> GetBuffList()
  293. {
  294. return buffList;
  295. }
  296. public void DealBuff()
  297. {
  298. for(int i=buffList.Count-1; i>=0; i--)
  299. {
  300. buffList[i].Update();
  301. }
  302. }
  303. public void OnMove()
  304. {
  305. for(int i=buffList.Count-1; i>=0; i--)
  306. {
  307. buffList[i].OnMove();
  308. }
  309. }
  310. public void OnDamage()
  311. {
  312. for(int i=buffList.Count-1; i>=0; i--)
  313. {
  314. buffList[i].OnDamage();
  315. }
  316. }
  317. public void OnUserPower()
  318. {
  319. for(int i=buffList.Count-1; i>=0; i--)
  320. {
  321. buffList[i].OnUserPower();
  322. }
  323. }
  324. protected bool invisible;
  325. public virtual void SetInvisible(bool invisible, bool isAlly)
  326. {
  327. if(invisible == this.invisible)
  328. {
  329. return;
  330. }
  331. this.invisible = invisible;
  332. Collider collider = GetComponent<Collider>();
  333. if(invisible)
  334. {
  335. float alpha = isAlly ? 0.5f : 0;
  336. Invisibler.Start(this, alpha, false);
  337. if(collider != null)
  338. collider.enabled = isAlly ? true : false;
  339. headBar.gameObject.SetActive(isAlly);
  340. }
  341. else
  342. {
  343. float alpha = 1f;
  344. Invisibler.Start(this, alpha, true);
  345. if(collider != null)
  346. collider.enabled = true;
  347. headBar.gameObject.SetActive(true);
  348. }
  349. }
  350. public bool IsInvisible()
  351. {
  352. return invisible;
  353. }
  354. public bool CanShoot()
  355. {
  356. return !IsDead() && !IsInvisible();
  357. }
  358. public bool IsDead()
  359. {
  360. return _isDead;
  361. }
  362. public virtual void Dead()
  363. {
  364. if(_isDead)
  365. return;
  366. deadPos = position;
  367. _isDead = true;
  368. deadTime = GameTime.time;
  369. if(SoundUtils.IsSoundNear(position))
  370. {
  371. SoundManager.GetInstatnce().effectSound.Play(SoundManager.GetInstatnce().effectSound.explode);
  372. }
  373. BattleController.Shake(position);
  374. GameObject explosionObj = EffectUtil.CreateEffect("Explosion");
  375. Vector3 pos = position;
  376. pos.y = 1f;
  377. explosionObj.transform.position = pos;
  378. if(map != null)
  379. map.RemoveBattleObject(this);
  380. Collider coll = GetComponent<Collider>();
  381. if(coll != null)
  382. coll.enabled = false;
  383. Renderer[] renderList = GetComponentsInChildren<Renderer>();
  384. for(int i=0; i<renderList.Length; i++)
  385. {
  386. renderList[i].enabled = false;
  387. }
  388. if(Killed != null)
  389. Killed();
  390. }
  391. public ThreatManager GetThreadManager()
  392. {
  393. return threatManager;
  394. }
  395. }