BattleObject.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. if(headBar == null)
  186. {
  187. GameObject headBarPrefab = Resources.Load(Config.HEAD_BAR_PREFAB) as GameObject;
  188. GameObject headBarObj = Instantiate(headBarPrefab) as GameObject;
  189. PopUpManager.AddToMainCanvas (headBarObj);
  190. headBar = headBarObj.GetComponent<HeadBar>();
  191. }
  192. headBar.Init(this);
  193. }
  194. public float hp
  195. {
  196. set{
  197. if(_hp != value)
  198. {
  199. _hp = NumberUtil.forceBetween(value, 0, maxHp);
  200. if(HpChanged != null)
  201. HpChanged();
  202. if(headBar != null)
  203. {
  204. headBar.UpdateHpBar();
  205. }
  206. }
  207. }
  208. get{
  209. return _hp;
  210. }
  211. }
  212. public float maxHp
  213. {
  214. set{
  215. _maxHp = value;
  216. }
  217. get{
  218. return _maxHp;
  219. }
  220. }
  221. public float originMaxHp
  222. {
  223. get{
  224. return _originMaxHp;
  225. }
  226. }
  227. public virtual void MakeDamage(Bullet bullet)
  228. {
  229. lastShooterId = bullet.GetOwner ().userId;
  230. lastShooterCraftId = (bullet.GetOwner() is Craft ? (bullet.GetOwner() as Craft).GetCraftId() : 0);
  231. lastDamageTime = GameTime.time;
  232. if(!overwhelming)
  233. hp -= bullet.damage;
  234. if(bullet.GetOwner().team != team)
  235. threatManager.AddThreat(bullet.GetOwner().id, bullet.damage);
  236. DamageStat (lastShooterId, bullet.damage);
  237. OnDamage();
  238. }
  239. public virtual void MakeDamage(float damage, BattleObject launcher)
  240. {
  241. lastShooterId = launcher.userId;
  242. lastDamageTime = GameTime.time;
  243. if(!overwhelming)
  244. hp -= damage;
  245. if(launcher.team != team)
  246. threatManager.AddThreat(launcher.id, damage);
  247. DamageStat (lastShooterId, damage);
  248. OnDamage();
  249. }
  250. public void MakeDamage(float damage)
  251. {
  252. lastDamageTime = GameTime.time;
  253. if(!overwhelming)
  254. hp -= damage;
  255. }
  256. private void DamageStat(int userId, float damage)
  257. {
  258. Player player = Session.GetInstance ().GetBattleSession ().GetPlayer (userId);
  259. if (player != null)
  260. player.damage += damage;
  261. }
  262. public virtual void MakeHeal(Bullet bullet)
  263. {
  264. hp += bullet.damage;
  265. HealStat (bullet.GetOwner().userId, bullet.damage);
  266. }
  267. private void HealStat(int userId, float heal)
  268. {
  269. Player player = Session.GetInstance ().GetBattleSession ().GetPlayer (userId);
  270. if (player != null)
  271. player.heal += heal;
  272. }
  273. protected void CheckHpRecover()
  274. {
  275. if(hp < maxHp && IsLeaveDamage())
  276. {
  277. float hpRecover = maxHp*hpRecoverSpeed*GameTime.deltaTime;
  278. hp += hpRecover;
  279. }
  280. }
  281. public bool IsLeaveDamage()
  282. {
  283. return GameTime.time - lastDamageTime > hpRecoverInterval;
  284. }
  285. public void AddBuff(Buff buff)
  286. {
  287. if(buff == null)
  288. return;
  289. buffList.Add(buff);
  290. }
  291. public void RemoveBuff(Buff buff)
  292. {
  293. buffList.Remove(buff);
  294. }
  295. public List<Buff> GetBuffList()
  296. {
  297. return buffList;
  298. }
  299. public void DealBuff()
  300. {
  301. for(int i=buffList.Count-1; i>=0; i--)
  302. {
  303. buffList[i].Update();
  304. }
  305. }
  306. public void OnMove()
  307. {
  308. for(int i=buffList.Count-1; i>=0; i--)
  309. {
  310. buffList[i].OnMove();
  311. }
  312. }
  313. public void OnDamage()
  314. {
  315. for(int i=buffList.Count-1; i>=0; i--)
  316. {
  317. buffList[i].OnDamage();
  318. }
  319. }
  320. public void OnUserPower()
  321. {
  322. for(int i=buffList.Count-1; i>=0; i--)
  323. {
  324. buffList[i].OnUserPower();
  325. }
  326. }
  327. protected bool invisible;
  328. public virtual void SetInvisible(bool invisible, bool isAlly)
  329. {
  330. if(invisible == this.invisible)
  331. {
  332. return;
  333. }
  334. this.invisible = invisible;
  335. Collider collider = GetComponent<Collider>();
  336. if(invisible)
  337. {
  338. float alpha = isAlly ? 0.5f : 0;
  339. Invisibler.Start(this, alpha, false);
  340. if(collider != null)
  341. collider.enabled = isAlly ? true : false;
  342. headBar.gameObject.SetActive(isAlly);
  343. }
  344. else
  345. {
  346. float alpha = 1f;
  347. Invisibler.Start(this, alpha, true);
  348. if(collider != null)
  349. collider.enabled = true;
  350. headBar.gameObject.SetActive(true);
  351. }
  352. }
  353. public bool IsInvisible()
  354. {
  355. return invisible;
  356. }
  357. public bool CanShoot()
  358. {
  359. return !IsDead() && !IsInvisible();
  360. }
  361. public bool IsDead()
  362. {
  363. return _isDead;
  364. }
  365. public virtual void Dead()
  366. {
  367. if(_isDead)
  368. return;
  369. deadPos = position;
  370. _isDead = true;
  371. deadTime = GameTime.time;
  372. if(SoundUtils.IsSoundNear(position))
  373. {
  374. SoundManager.GetInstatnce().effectSound.Play(SoundManager.GetInstatnce().effectSound.explode);
  375. }
  376. BattleController.Shake(position);
  377. GameObject explosionObj = EffectUtil.CreateEffect("Explosion");
  378. Vector3 pos = position;
  379. pos.y = 1f;
  380. explosionObj.transform.position = pos;
  381. if(map != null)
  382. map.RemoveBattleObject(this);
  383. Collider coll = GetComponent<Collider>();
  384. if(coll != null)
  385. coll.enabled = false;
  386. Renderer[] renderList = GetComponentsInChildren<Renderer>();
  387. for(int i=0; i<renderList.Length; i++)
  388. {
  389. renderList[i].enabled = false;
  390. }
  391. if(Killed != null)
  392. Killed();
  393. }
  394. public ThreatManager GetThreadManager()
  395. {
  396. return threatManager;
  397. }
  398. }