Craft.cs 15 KB

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