Craft.cs 16 KB

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