Craft.cs 10 KB

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