Craft.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. using UnityEngine;
  2. using UnityEngine.AI;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using Sfs2X.Entities.Data;
  6. public class Craft : BattleObject, IPowerOwner
  7. {
  8. public enum State
  9. {
  10. None,
  11. Idle,
  12. Run,
  13. Attack,
  14. Power,
  15. }
  16. public enum Part
  17. {
  18. All,
  19. Top,
  20. Bottom,
  21. }
  22. public string nick;
  23. private bool m_Moving;
  24. public override bool isMoving
  25. {
  26. get{return m_Moving;}
  27. }
  28. public bool forceMove;
  29. private NavMeshAgent meshAgent;
  30. public Vector3 moveDestination = Vector3.zero;
  31. private PowerManager powerManager;
  32. private SwapManager swapManager;
  33. private CraftModel craftModel;
  34. public State state;
  35. private CraftData craftData;
  36. private CraftEquipModify equipModify;
  37. public MiniCraft miniCraft;
  38. private GameObject haloObj;
  39. private int haloId = -1;
  40. private int skinId;
  41. public event EventUtil.SimpleEventDelegate MoveStepComplete;
  42. public event EventUtil.SimpleEventDelegate OnSwapped;
  43. public event EventUtil.SimpleEventDelegate OnCtrlChanged;
  44. void Awake()
  45. {
  46. swapManager = new SwapManager(this);
  47. meshAgent = GetComponent<NavMeshAgent> ();
  48. }
  49. public virtual void Init(Map map, CraftData craftData, CraftEquipModify equipModify)
  50. {
  51. base.Init(map);
  52. this.craftData = craftData;
  53. SetEquipModify (equipModify);
  54. id = craftData.id;
  55. userId = craftData.userId;
  56. nick = craftData.nick;
  57. PositionTo(craftData.position);
  58. Init();
  59. hp = maxHp;
  60. headBar.UpdateColor();
  61. headBar.text.text = nick;
  62. }
  63. public void Init()
  64. {
  65. _originMaxHp = maxHp = craftData.GetMaxHp() + equipModify.hp;
  66. if(craftData != null)
  67. {
  68. if(powerManager == null)
  69. powerManager = new PowerManager(this, craftData.GetAttackId(), craftData.GetPowerIds(), craftData.extraPowerIds);
  70. else
  71. powerManager.UpdatePower(craftData.GetAttackId(), craftData.GetPowerIds());
  72. team = craftData.team;
  73. }
  74. SetState (State.Idle);
  75. Debuger.LogError (this.name+" maxhp "+maxHp+" damage "+GetDamage()+" mov "+moveSpeed);
  76. }
  77. public void SetEquipModify(CraftEquipModify equipModify)
  78. {
  79. if (equipModify == null)
  80. this.equipModify = new CraftEquipModify ();
  81. else
  82. this.equipModify = equipModify;
  83. }
  84. public ISFSObject GetEquipModifyData()
  85. {
  86. return equipModify.GetData ();
  87. }
  88. public float GetDamage()
  89. {
  90. return craftData.GetDamage () + equipModify.damage;
  91. }
  92. public override TeamUtil.Team team {
  93. get {
  94. return _team;
  95. }
  96. set {
  97. _team = value;
  98. if(textureArr.Length > 0)
  99. {
  100. int index = value.GetHashCode() - 1;
  101. craftModel.SetSkin(textureArr[index]);
  102. }
  103. }
  104. }
  105. public SwapManager GetSwapManager()
  106. {
  107. return swapManager;
  108. }
  109. public ShotManager GetShotManager()
  110. {
  111. return craftModel.shotManager;
  112. }
  113. public CraftModel GetCraftModel()
  114. {
  115. return craftModel;
  116. }
  117. public void SetCraftModel(CraftModel craftModel)
  118. {
  119. if (this.craftModel != null) {
  120. Destroy (this.craftModel.gameObject);
  121. }
  122. this.craftModel = craftModel;
  123. this.craftModel.transform.SetParent (this.transform);
  124. this.craftModel.transform.localEulerAngles = Vector3.zero;
  125. this.craftModel.transform.localPosition = Vector3.zero;
  126. this.craftModel.transform.localScale = Vector3.one;
  127. }
  128. private bool isCtrl = false;
  129. public void SetCtrl(bool value)
  130. {
  131. isCtrl = value;
  132. UpdateHaloColor ();
  133. headBar.UpdateColor ();
  134. if(OnCtrlChanged != null)
  135. OnCtrlChanged();
  136. }
  137. public bool IsCtrl()
  138. {
  139. return isCtrl;
  140. }
  141. public void SetHalo(int id)
  142. {
  143. if(haloId != id)
  144. {
  145. haloId = id;
  146. if(haloObj != null)
  147. Destroy(haloObj);
  148. haloObj = HaloManager.GetInstance().CreateHaloObjById(id);
  149. haloObj.transform.SetParent(transform);
  150. haloObj.transform.localPosition = new Vector3(0, 0.01f, 0);
  151. haloObj.transform.localScale = new Vector3(1f, 1f, 1f);
  152. haloObj.transform.localEulerAngles = new Vector3(0, 0, 0);
  153. UpdateHaloColor ();
  154. }
  155. }
  156. private void UpdateHaloColor()
  157. {
  158. if (haloObj == null || craftData == null)
  159. return;
  160. if(isCtrl)
  161. {
  162. Color color = TeamUtil.GetTeamColor(TeamUtil.Team.Yellow.GetHashCode());
  163. haloObj.GetComponent<HaloObj>().SetColor(color);
  164. }
  165. else
  166. {
  167. Color color = TeamUtil.GetTeamColor(team.GetHashCode());
  168. haloObj.GetComponent<HaloObj>().SetColor(color);
  169. }
  170. }
  171. public int GetHalo()
  172. {
  173. return haloId;
  174. }
  175. public void SetSkin(int id, Texture texture)
  176. {
  177. this.skinId = id;
  178. if(texture != null)
  179. craftModel.SetSkin (texture);
  180. }
  181. public int GetSkin()
  182. {
  183. return skinId;
  184. }
  185. public bool IsHero()
  186. {
  187. if (craftData != null)
  188. return craftData.isHero;
  189. return false;
  190. }
  191. public void SetCraftId(int id)
  192. {
  193. craftData.SetCraftId(id);
  194. }
  195. public int GetCraftId()
  196. {
  197. return craftData.GetCraftId();
  198. }
  199. public string GetIcon()
  200. {
  201. return craftData.GetIcon();
  202. }
  203. public override Transform GetBaseTransform()
  204. {
  205. if(craftModel.topAnim != null)
  206. return craftModel.topAnim.transform;
  207. return transform;
  208. }
  209. public void SetState(State state)
  210. {
  211. this.state = state;
  212. if (state == State.Attack)
  213. craftModel.Attack ();
  214. }
  215. public void PositionTo(Vector3 position)
  216. {
  217. StopMove();
  218. transform.position = position;
  219. }
  220. public override bool enableMove
  221. {
  222. set{
  223. base.enableMove = value;
  224. if(!value && isMoving)
  225. {
  226. StopMove();
  227. }
  228. }
  229. get{
  230. return _enableMove;
  231. }
  232. }
  233. public float moveSpeed
  234. {
  235. get{
  236. float speed = originMoveSpeed + modifyMoveSpeed;
  237. return speed > 0 ? speed + originMoveSpeed * equipModify.move / 100f : speed;
  238. }
  239. }
  240. public void MoveTo(int col, int row)
  241. {
  242. if(!enableMove || holdAction)
  243. return;
  244. StartMove(Map.GetCenterX(col), Map.GetCenterZ(row));
  245. }
  246. public void MoveTo(float x, float y)
  247. {
  248. int col = Map.XToColumn(x);
  249. int row = Map.ZToRow(y);
  250. MoveTo(col, row);
  251. }
  252. protected void StartMove(float x, float y)
  253. {
  254. moveDestination.Set (x, 0, y);
  255. SetIsMoving(true);
  256. OnMove();
  257. swapManager.AbortSwap();
  258. }
  259. private void SetIsMoving(bool value)
  260. {
  261. m_Moving = value;
  262. craftModel.moving = value;
  263. }
  264. public void StopMove()
  265. {
  266. if(holdAction)
  267. {
  268. if(target != null)
  269. {
  270. for(int i=0; i<buffList.Count; i++)
  271. {
  272. if(buffList[i].hasCloseTarget)
  273. {
  274. StartMove(target.position.x, target.position.z);
  275. break;
  276. }
  277. }
  278. }
  279. return;
  280. }
  281. SetIsMoving (false);
  282. forceMove = false;
  283. }
  284. // Update is called once per frame
  285. // public override void EnterFrame ()
  286. void FixedUpdate () {
  287. if(IsDead())
  288. {
  289. if(GameTime.time-deadTime>5f)//do not remove immediately
  290. {
  291. Destroy(this.miniCraft.gameObject);
  292. Destroy(this.gameObject);
  293. }
  294. return;
  295. }
  296. if(isMoving)
  297. {
  298. moving();
  299. }
  300. if (target != null && target.CanShoot ()) {
  301. craftModel.LookTo (target.position);
  302. } else if(craftModel != null){
  303. craftModel.LookToForward ();
  304. }
  305. CheckHpRecover();
  306. DealBuff();
  307. if(swapManager.isPreparingSwap && !swapManager.requestedSwap)
  308. {
  309. headBar.SetSpellProgress(swapManager.GetPreparingSwapTime(), SwapManager.PREPARING_SWAP_DURATION);
  310. }
  311. }
  312. private void moving()
  313. {
  314. meshAgent.SetDestination (moveDestination);
  315. if (meshAgent.remainingDistance > 0) {
  316. if(miniCraft != null)
  317. miniCraft.UpdatePos();
  318. } else {
  319. if (MoveStepComplete != null)
  320. MoveStepComplete ();
  321. StopMove ();
  322. }
  323. }
  324. public void SetRotateTargetAngle(float angle)
  325. {
  326. Vector3 targetPos = transform.position;
  327. float radian = angle * Mathf.Deg2Rad;
  328. float x = Mathf.Cos (radian);
  329. float y = Mathf.Sin (radian);
  330. targetPos.x += x;
  331. targetPos.z += y;
  332. craftModel.LookAt (targetPos);
  333. }
  334. private int _teleportStep = 0;
  335. public void Teleport(int step)
  336. {
  337. _teleportStep = Mathf.Max(_teleportStep, step);
  338. if(!isMoving)
  339. {
  340. return;
  341. }
  342. GameObject disappearObj = EffectUtil.CreateDisappear();
  343. disappearObj.transform.position = position;
  344. for(int i=0; i<step; i++)
  345. {
  346. // setNextMovePosition();
  347. if(!isMoving)
  348. {
  349. break;
  350. }
  351. }
  352. // DealMoveStepComplete();
  353. _teleportStep = 0;
  354. GameObject landingObj = EffectUtil.CreateLanding();
  355. landingObj.transform.position = position;
  356. }
  357. public override void MakeDamage (Bullet bullet)
  358. {
  359. base.MakeDamage (bullet);
  360. swapManager.AbortSwap();
  361. }
  362. override public void SetInvisible(bool invisible, bool isAlly)
  363. {
  364. if(invisible == this.invisible)
  365. {
  366. return;
  367. }
  368. base.SetInvisible(invisible, isAlly);
  369. if(invisible)
  370. {
  371. haloObj.gameObject.SetActive(isAlly);
  372. }
  373. else
  374. {
  375. haloObj.gameObject.SetActive(true);
  376. }
  377. }
  378. public PowerManager GetPowerManager()
  379. {
  380. return powerManager;
  381. }
  382. public void RadioAlert()
  383. {
  384. headBar.RadioAlert();
  385. if(miniCraft != null)
  386. miniCraft.RadioAlert();
  387. }
  388. public void SwapComplete()
  389. {
  390. if(OnSwapped != null)
  391. OnSwapped();
  392. }
  393. public UAVehicle GetUAV()
  394. {
  395. return map.GetUAVehicle (userId);
  396. }
  397. override public void Remove()
  398. {
  399. map.RemoveBattleObject(this);
  400. }
  401. public override string ToString()
  402. {
  403. return "User["+userId+"] Id["+id+"] Type["+typeId+"]";
  404. }
  405. }