Craft.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public class Craft : BattleObject, IPowerOwner
  5. {
  6. public enum State
  7. {
  8. None,
  9. Idle,
  10. Run,
  11. Attack,
  12. Power,
  13. }
  14. public enum Part
  15. {
  16. All,
  17. Top,
  18. Bottom,
  19. }
  20. public string nick;
  21. private LinkedList<AStarNode> path;
  22. private Vector3 nextMovePosition;
  23. private bool _isMoving;
  24. public override bool isMoving
  25. {
  26. get{return _isMoving;}
  27. }
  28. public AStarPoint targetMovePosition = new AStarPoint();
  29. public bool forceMove;
  30. private float rotateSpeed = 200f;
  31. public float rotateTargetAngle;
  32. private bool _isTargetRotating;
  33. public float rotateMoveTargetAngle;
  34. private bool _isMoveRotating;
  35. public Transform bodyTrans;
  36. public Transform topPartTrans;
  37. public Transform bottomPartTrans;
  38. private Animation animationTop;
  39. private Animation animationBottom;
  40. public Material materialTop;
  41. public Material materialBottom;
  42. private PowerManager powerManager;
  43. private SwapManager swapManager;
  44. private ShotManager shotManager;
  45. public State state;
  46. private CraftData craftData;
  47. public MiniCraft miniCraft;
  48. private GameObject haloObj;
  49. private int haloId = -1;
  50. private int skinId;
  51. public event EventUtil.SimpleEventDelegate MoveStepComplete;
  52. public event EventUtil.SimpleEventDelegate OnSwapped;
  53. public event EventUtil.SimpleEventDelegate OnCtrlChanged;
  54. void Awake()
  55. {
  56. swapManager = new SwapManager(this);
  57. }
  58. public virtual void Init(Map map, CraftData craftData)
  59. {
  60. base.Init(map);
  61. this.craftData = craftData;
  62. id = craftData.id;
  63. userId = craftData.userId;
  64. _originMaxHp = hp = maxHp = craftData.GetMaxHp();
  65. nick = craftData.nick;
  66. PositionTo(craftData.position);
  67. Init();
  68. headBar.UpdateColor();
  69. headBar.text.text = nick;
  70. }
  71. public void Init()
  72. {
  73. bodyTrans = transform.Find("Body");
  74. topPartTrans = bodyTrans.Find("Top");
  75. if(topPartTrans != null)
  76. {
  77. animationTop = topPartTrans.GetComponent<Animation>();
  78. materialTop = topPartTrans.GetComponentInChildren<Renderer>().material;
  79. material = materialTop;
  80. }
  81. bottomPartTrans = bodyTrans.Find("Bottom");
  82. if(bottomPartTrans != null)
  83. {
  84. animationBottom = bottomPartTrans.GetComponent<Animation>();
  85. materialBottom = bottomPartTrans.GetComponentInChildren<Renderer>().material;
  86. }
  87. if(topPartTrans == null)
  88. animation = bodyTrans.GetComponentInChildren<Animation>();
  89. if(craftData != null)
  90. {
  91. if(powerManager == null)
  92. powerManager = new PowerManager(this, craftData.GetAttackId(), craftData.GetPowerIds(), new int[]{2001, 2002});
  93. else
  94. powerManager.UpdatePower(craftData.GetAttackId(), craftData.GetPowerIds());
  95. team = craftData.team;
  96. }
  97. shotManager = GetComponentInChildren<ShotManager>();
  98. SetState (State.Idle);
  99. }
  100. public float GetDamage()
  101. {
  102. return craftData.GetDamage ();
  103. }
  104. public override TeamUtil.Team team {
  105. get {
  106. return _team;
  107. }
  108. set {
  109. _team = value;
  110. if(textureArr.Length > 0)
  111. {
  112. int index = value.GetHashCode() - 1;
  113. if(topPartTrans == null)
  114. {
  115. SkinnedMeshRenderer[] renderers = bodyTrans.GetComponentsInChildren<SkinnedMeshRenderer>();
  116. for(int i=0; i<renderers.Length; i++)
  117. {
  118. renderers[i].material.mainTexture = textureArr[index];
  119. }
  120. }
  121. else
  122. {
  123. SkinnedMeshRenderer[] renderers = topPartTrans.GetComponentsInChildren<SkinnedMeshRenderer>();
  124. for(int i=0; i<renderers.Length; i++)
  125. {
  126. renderers[i].material.mainTexture = textureArr[index];
  127. }
  128. renderers = bottomPartTrans.GetComponentsInChildren<SkinnedMeshRenderer>();
  129. for(int i=0; i<renderers.Length; i++)
  130. {
  131. renderers[i].material.mainTexture = textureArr[index];
  132. }
  133. }
  134. }
  135. }
  136. }
  137. public SwapManager GetSwapManager()
  138. {
  139. return swapManager;
  140. }
  141. public ShotManager GetShotManager()
  142. {
  143. return shotManager;
  144. }
  145. private bool isCtrl = false;
  146. public void SetCtrl(bool value)
  147. {
  148. isCtrl = value;
  149. UpdateHaloColor ();
  150. headBar.UpdateColor ();
  151. if(OnCtrlChanged != null)
  152. OnCtrlChanged();
  153. }
  154. public bool IsCtrl()
  155. {
  156. return isCtrl;
  157. }
  158. public void SetHalo(int id)
  159. {
  160. if(haloId != id)
  161. {
  162. haloId = id;
  163. if(haloObj != null)
  164. Destroy(haloObj);
  165. haloObj = HaloManager.GetInstance().CreateHaloObjById(id);
  166. haloObj.transform.SetParent(transform);
  167. haloObj.transform.localPosition = new Vector3(0, 0.01f, 0);
  168. haloObj.transform.localScale = new Vector3(1f, 1f, 1f);
  169. haloObj.transform.localEulerAngles = new Vector3(0, 0, 0);
  170. UpdateHaloColor ();
  171. }
  172. }
  173. private void UpdateHaloColor()
  174. {
  175. if (haloObj == null || craftData == null)
  176. return;
  177. if(isCtrl)
  178. {
  179. Color color = TeamUtil.GetTeamColor(TeamUtil.Team.Yellow.GetHashCode());
  180. haloObj.GetComponent<HaloObj>().SetColor(color);
  181. }
  182. else
  183. {
  184. Color color = TeamUtil.GetTeamColor(team.GetHashCode());
  185. haloObj.GetComponent<HaloObj>().SetColor(color);
  186. }
  187. }
  188. public int GetHalo()
  189. {
  190. return haloId;
  191. }
  192. public void SetSkin(int id, Texture texture)
  193. {
  194. this.skinId = id;
  195. if(texture != null)
  196. {
  197. SkinnedMeshRenderer[] rendererArr = bodyTrans.GetComponentsInChildren<SkinnedMeshRenderer>();
  198. for(int i=0; i<rendererArr.Length; i++)
  199. {
  200. rendererArr[i].material.mainTexture = texture;
  201. }
  202. }
  203. }
  204. public int GetSkin()
  205. {
  206. return skinId;
  207. }
  208. public bool IsHero()
  209. {
  210. if (craftData != null)
  211. return craftData.isHero;
  212. return false;
  213. }
  214. public void SetCraftId(int id)
  215. {
  216. craftData.SetCraftId(id);
  217. }
  218. public int GetCraftId()
  219. {
  220. return craftData.GetCraftId();
  221. }
  222. public string GetIcon()
  223. {
  224. return craftData.GetIcon();
  225. }
  226. public override Transform GetBaseTransform()
  227. {
  228. if(topPartTrans != null)
  229. return topPartTrans;
  230. return transform;
  231. }
  232. public void SetState(State state)
  233. {
  234. if(this.state != state)
  235. {
  236. this.state = state;
  237. string animName = state.ToString();
  238. if(animation != null)
  239. {
  240. if(animation.GetClip(animName) != null)
  241. animation.Play(animName);
  242. }
  243. if(animationTop == null)
  244. return;
  245. if(state == State.Idle)
  246. {
  247. if(animationTop.GetClip(animName) != null)
  248. {
  249. animationTop.Play(animName);
  250. animationBottom.Play(animName);
  251. }
  252. }
  253. else if(state == State.Run)
  254. {
  255. if(animationTop.GetClip(animName) != null)
  256. {
  257. animationTop.Play(animName);
  258. animationBottom.Play(animName);
  259. }
  260. }
  261. else if(state == State.Attack)
  262. {
  263. if(animationTop.GetClip(animName) != null)
  264. animationTop.Play(animName);
  265. if(isMoving)
  266. {
  267. if(animationBottom.GetClip(State.Run.ToString()) != null)
  268. animationBottom.Play(State.Run.ToString());
  269. }
  270. else
  271. {
  272. if(animationBottom.GetClip(State.Idle.ToString()) != null)
  273. animationBottom.Play(State.Idle.ToString());
  274. }
  275. }
  276. }
  277. }
  278. public bool IsPlayingState(State state, Part part = Part.All)
  279. {
  280. string animName = state.ToString();
  281. if(animation != null)
  282. {
  283. return animation.IsPlaying(animName);
  284. }
  285. if(animationTop != null && (part == Part.Top || part == Part.All))
  286. {
  287. return animationTop.IsPlaying(animName);
  288. }
  289. if(animationBottom != null && (part == Part.Bottom || part == Part.All))
  290. {
  291. return animationBottom.IsPlaying(animName);
  292. }
  293. return false;
  294. }
  295. public void PositionTo(Vector3 position)
  296. {
  297. StopMove();
  298. transform.position = position;
  299. }
  300. public override bool enableMove
  301. {
  302. set{
  303. base.enableMove = value;
  304. if(!value && isMoving)
  305. {
  306. StopMove();
  307. }
  308. }
  309. get{
  310. return _enableMove;
  311. }
  312. }
  313. public float moveSpeed
  314. {
  315. get{
  316. return originMoveSpeed + modifyMoveSpeed;
  317. }
  318. }
  319. public void MoveTo(int col, int row)
  320. {
  321. if(!enableMove || holdAction)
  322. return;
  323. StartMove(col, row);
  324. }
  325. public void MoveTo(float x, float y)
  326. {
  327. int col = Map.XToColumn(x);
  328. int row = Map.ZToRow(y);
  329. MoveTo(col, row);
  330. }
  331. protected void StartMove(int col, int row)
  332. {
  333. targetMovePosition.x = col;
  334. targetMovePosition.y = row;
  335. Vector3 position = transform.position;
  336. int startCol = (int)(position.x/Map.TILE_WIDTH);
  337. int startRow = (int)(position.z/Map.TILE_LENGTH);
  338. map.PathSearchEnqueue(new AStarPoint(startCol, startRow), new AStarPoint(col, row), this);
  339. }
  340. public void SetPath(LinkedList<AStarNode> path)
  341. {
  342. //long startTime = System.DateTime.Now.ToFileTime();
  343. //path = map.GetPath(new AStarPoint(startCol, startRow), new AStarPoint(col, row), this);
  344. //long endTime = System.DateTime.Now.ToFileTime();
  345. //Debuger.Log("search path spend time : "+(endTime-startTime));
  346. this.path = path;
  347. if(!IsDead() && path != null)
  348. {
  349. path.RemoveFirst();
  350. _isMoving = true;
  351. if(state == State.Idle)
  352. SetState(State.Run);
  353. if(_teleportStep > 0)
  354. Teleport(_teleportStep);
  355. else
  356. setNextMovePosition();
  357. OnMove();
  358. swapManager.AbortSwap();
  359. }
  360. }
  361. public void StopMove()
  362. {
  363. if(holdAction)
  364. {
  365. if(target != null)
  366. {
  367. for(int i=0; i<buffList.Count; i++)
  368. {
  369. if(buffList[i].hasCloseTarget)
  370. {
  371. int col = Map.XToColumn(target.position.x);
  372. int row = Map.ZToRow(target.position.z);
  373. StartMove(col, row);
  374. break;
  375. }
  376. }
  377. }
  378. return;
  379. }
  380. path = null;
  381. _isMoving = false;
  382. forceMove = false;
  383. if(state == State.Run)
  384. SetState(State.Idle);
  385. }
  386. // Update is called once per frame
  387. void Update () {
  388. if(IsDead())
  389. {
  390. if(GameTime.time-deadTime>5f)//do not remove immediately
  391. {
  392. Destroy(this.miniCraft.gameObject);
  393. Destroy(this.gameObject);
  394. }
  395. return;
  396. }
  397. if(_isMoving)
  398. {
  399. moving();
  400. }
  401. if(target != null && target.CanShoot())
  402. {
  403. Vector3 targetPosition = target.position;
  404. float theta = NumberUtil.getRadianByATan(targetPosition.x, targetPosition.z, position.x, position.z);
  405. float rotateAngle = 90-NumberUtil.radianToAngle(theta);
  406. if(!rotateTargetAngle.Equals(rotateAngle))
  407. {
  408. rotateTargetAngle = rotateAngle;
  409. _isTargetRotating = true;
  410. }
  411. }
  412. rotating();
  413. CheckHpRecover();
  414. DealBuff();
  415. if((state == State.Attack || state == State.Power) && !IsPlayingState(state, Part.Top))
  416. {
  417. if(isMoving)
  418. {
  419. SetState(State.Run);
  420. }
  421. else
  422. {
  423. SetState(State.Idle);
  424. }
  425. }
  426. if(swapManager.isPreparingSwap && !swapManager.requestedSwap)
  427. {
  428. headBar.SetSpellProgress(swapManager.GetPreparingSwapTime(), SwapManager.PREPARING_SWAP_DURATION);
  429. }
  430. }
  431. private void moving()
  432. {
  433. float timeLeft = updateMovePosition(GameTime.deltaTime);
  434. if(_isMoving && timeLeft > 0)
  435. {
  436. updateMovePosition(timeLeft);
  437. }
  438. }
  439. private float updateMovePosition(float deltaTime)
  440. {
  441. Vector3 position = transform.position;
  442. float deltaX = nextMovePosition.x-position.x;
  443. float deltaY = nextMovePosition.z-position.z;
  444. float theta = Mathf.Atan2(deltaY, deltaX);
  445. float distance = Mathf.Sqrt(deltaX*deltaX+deltaY*deltaY);
  446. float moveDistance = moveSpeed*deltaTime;
  447. float timeLeft = 0;
  448. if(moveDistance >= distance)
  449. {
  450. timeLeft = deltaTime*(1f-distance/moveDistance);
  451. DealMoveStepComplete();
  452. setNextMovePosition();
  453. }
  454. else
  455. {
  456. float movX = moveDistance*Mathf.Cos(theta);
  457. float movZ = moveDistance*Mathf.Sin(theta);
  458. position.x += movX;
  459. position.z += movZ;
  460. transform.position = position;
  461. }
  462. return timeLeft;
  463. }
  464. private void DealMoveStepComplete()
  465. {
  466. Vector3 position = transform.position;
  467. position.x = nextMovePosition.x;
  468. position.z = nextMovePosition.z;
  469. transform.position = position;
  470. if(MoveStepComplete != null)
  471. MoveStepComplete();
  472. }
  473. private void setNextMovePosition()
  474. {
  475. if(path != null)
  476. {
  477. if(path.Count > 0)
  478. {
  479. AStarNode astarNode = path.First.Value;
  480. path.RemoveFirst();
  481. nextMovePosition = AstarNodeToPosition(astarNode);
  482. Vector3 position = transform.position;
  483. float theta = NumberUtil.getRadianByATan(nextMovePosition.x, nextMovePosition.z, position.x, position.z);
  484. float rotateAngle = 90-NumberUtil.radianToAngle(theta);
  485. Rotate (rotateAngle);
  486. }
  487. else
  488. {
  489. StopMove();
  490. }
  491. if(miniCraft != null)
  492. miniCraft.UpdatePos();
  493. }
  494. }
  495. public void Rotate(float rotateAngle)
  496. {
  497. rotateMoveTargetAngle = rotateAngle;
  498. _isMoveRotating = true;
  499. if(target == null)
  500. {
  501. if(!rotateTargetAngle.Equals(rotateMoveTargetAngle))
  502. {
  503. rotateTargetAngle = rotateMoveTargetAngle;
  504. _isTargetRotating = true;
  505. }
  506. }
  507. }
  508. private Vector3 AstarNodeToPosition(AStarNode astarNode)
  509. {
  510. return new Vector3((astarNode.X+0.5f)*Map.TILE_WIDTH, 0, (astarNode.Y+0.5f)*Map.TILE_LENGTH);
  511. }
  512. public void setRotateTargetAngle(float value)
  513. {
  514. rotateTargetAngle = value;
  515. _isTargetRotating = true;
  516. }
  517. private void rotating()
  518. {
  519. if(!enableMove || swapManager.isPreparingSwap)
  520. return;
  521. if(bottomPartTrans == null)
  522. {
  523. if(_isMoveRotating || _isTargetRotating)
  524. {
  525. float targetRotation = 0;
  526. if(_isMoving)
  527. {
  528. targetRotation = rotateMoveTargetAngle;
  529. }
  530. else
  531. {
  532. targetRotation = rotateTargetAngle;
  533. }
  534. Vector3 eulerAngles = bodyTrans.eulerAngles;
  535. float currentRotation = eulerAngles.y;
  536. float rotateStep = rotateSpeed*GameTime.deltaTime;
  537. currentRotation = NumberUtil.getCloseToTargetAngle(currentRotation, targetRotation, rotateStep);
  538. eulerAngles.y = currentRotation;
  539. bodyTrans.eulerAngles = eulerAngles;
  540. if(_isMoving)
  541. {
  542. if(rotateMoveTargetAngle.Equals(currentRotation))
  543. {
  544. _isMoveRotating = false;
  545. }
  546. }
  547. else
  548. {
  549. if(rotateTargetAngle.Equals(currentRotation))
  550. {
  551. _isTargetRotating = false;
  552. }
  553. }
  554. }
  555. }
  556. else
  557. {
  558. Vector3 eulerAngles;
  559. float rotateStep = rotateSpeed*GameTime.deltaTime;
  560. if(_isTargetRotating)
  561. {
  562. eulerAngles = topPartTrans.localEulerAngles;
  563. float currentRotation = eulerAngles.y;
  564. currentRotation = NumberUtil.getCloseToTargetAngle(currentRotation, rotateTargetAngle, rotateStep);
  565. eulerAngles.y = currentRotation;
  566. topPartTrans.localEulerAngles = eulerAngles;
  567. if(shotManager != null)
  568. shotManager.transform.localEulerAngles = eulerAngles;
  569. if(rotateTargetAngle.Equals(currentRotation))
  570. {
  571. _isTargetRotating = false;
  572. }
  573. }
  574. if(_isMoveRotating)
  575. {
  576. eulerAngles = bottomPartTrans.localEulerAngles;
  577. float currentBottomRotation = eulerAngles.y;
  578. currentBottomRotation = NumberUtil.getCloseToTargetAngle(currentBottomRotation, rotateMoveTargetAngle, rotateStep);
  579. eulerAngles.y = currentBottomRotation;
  580. bottomPartTrans.localEulerAngles = eulerAngles;
  581. if(rotateMoveTargetAngle.Equals(currentBottomRotation))
  582. {
  583. _isMoveRotating = false;
  584. }
  585. }
  586. }
  587. }
  588. private int _teleportStep = 0;
  589. public void Teleport(int step)
  590. {
  591. _teleportStep = Mathf.Max(_teleportStep, step);
  592. if(!isMoving)
  593. {
  594. return;
  595. }
  596. GameObject disappearObj = EffectUtil.CreateDisappear();
  597. disappearObj.transform.position = position;
  598. for(int i=0; i<step; i++)
  599. {
  600. setNextMovePosition();
  601. if(!isMoving)
  602. {
  603. break;
  604. }
  605. }
  606. DealMoveStepComplete();
  607. _teleportStep = 0;
  608. GameObject landingObj = EffectUtil.CreateLanding();
  609. landingObj.transform.position = position;
  610. }
  611. public override void MakeDamage (Bullet bullet)
  612. {
  613. base.MakeDamage (bullet);
  614. swapManager.AbortSwap();
  615. }
  616. override public void SetInvisible(bool invisible, bool isAlly)
  617. {
  618. if(invisible == this.invisible)
  619. {
  620. return;
  621. }
  622. base.SetInvisible(invisible, isAlly);
  623. if(invisible)
  624. {
  625. haloObj.gameObject.SetActive(isAlly);
  626. }
  627. else
  628. {
  629. haloObj.gameObject.SetActive(true);
  630. }
  631. }
  632. public PowerManager GetPowerManager()
  633. {
  634. return powerManager;
  635. }
  636. public void RadioAlert()
  637. {
  638. headBar.RadioAlert();
  639. if(miniCraft != null)
  640. miniCraft.RadioAlert();
  641. }
  642. public void SwapComplete()
  643. {
  644. if(OnSwapped != null)
  645. OnSwapped();
  646. }
  647. override public void Remove()
  648. {
  649. map.RemoveBattleObject(this);
  650. }
  651. public override string ToString()
  652. {
  653. return "User["+userId+"] Id["+id+"] Type["+typeId+"]";
  654. }
  655. }