BattleObject.cs 7.7 KB

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