BattleObject.cs 7.6 KB

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