InstancePlayer.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  1. using CommonAI.RTS;
  2. using CommonLang.Vector;
  3. using CommonAI.Zone.Helper;
  4. using CommonAI.Zone.Formula;
  5. using CommonLang;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using CommonLang.IO;
  11. using CommonAI.Data;
  12. using CommonAI.Zone.ZoneEditor.EventTrigger;
  13. namespace CommonAI.Zone.Instance
  14. {
  15. /// <summary>
  16. /// 可自动战斗的可操作单位
  17. /// </summary>
  18. public class InstancePlayer : InstanceUnit
  19. {
  20. private readonly string mPlayerUUID;
  21. //private HashMap<string, int> mKilledPlayersID = new HashMap<string, int>();
  22. private CommonAI.ZoneClient.SyncMode mCurrentSyncMode;
  23. private bool mIsSkillControlByServer = true;
  24. //自动拾取范围
  25. protected float mAutoPickRange = XmdsConstConfig.AUTOPICK_RANGE;
  26. protected float mAutoPickSearchRange = XmdsConstConfig.AUTOPICK_RANGE * 3;
  27. protected TimeInterval<int> mCheckGuard;
  28. protected HashMap<string, QuestData> mQuests = new HashMap<string, QuestData>();
  29. protected UnitLaunchSkillAction mWaitingSkill;
  30. protected StatePlayerControlMove mControlMove;
  31. protected StatePlayerUpdateMove mUpdateMove;
  32. protected StateFollowAndAttack mFocusTarget;
  33. protected StateFollowAndPickItem mFocusPickItem;
  34. protected StatePlayerAttackTo mAttackTo;
  35. public virtual bool isTeamMemberAndFollow()
  36. {
  37. return false;
  38. }
  39. public override string PlayerUUID
  40. {
  41. get { return mPlayerUUID; }
  42. }
  43. public string ClientID { get; set; }
  44. public bool IsGuard { get; set; }
  45. public bool IsReady { get; private set; }
  46. public override bool IsPlayer { get { return true; } }
  47. public override bool IsPlayerUnit { get { return true; } }
  48. public override bool IntersectObj { get { return (Templates.CFG.PLAYER_NONE_TOUCH) ? false : base.IntersectObj; } }
  49. public override bool IsSkillControllableByServer { get { return mIsSkillControlByServer; } }
  50. public CommonAI.ZoneClient.SyncMode ClientSyncMode { get { return mCurrentSyncMode; } }
  51. public bool IsRobot { get; set; }
  52. public InstancePlayer(InstanceZone zone, UnitInfo info, string name, int force, int level, int alliesForce)
  53. : base(zone, info, name, force, level, false, alliesForce)
  54. {
  55. this.mPlayerUUID = string.IsNullOrEmpty(name) ? "" : name;
  56. this.mSyncInfo.PlayerUUID = mPlayerUUID;
  57. this.mControlMove = new StatePlayerControlMove(this);
  58. this.mUpdateMove = new StatePlayerUpdateMove(this);
  59. this.mCheckGuard = new TimeInterval<int>(Templates.CFG.AI_VIEW_TRIGGER_CHECK_TIME_MS);
  60. }
  61. protected override void Disposing()
  62. {
  63. /*
  64. mControlMove = null;
  65. mUpdateMove = null;
  66. mFocusTarget = null;
  67. mFocusPickItem = null;
  68. mAttackTo = null;
  69. */
  70. base.Disposing();
  71. }
  72. /// <summary>
  73. /// 联网模式,断开连接。
  74. /// </summary>
  75. public virtual void OnDisconnected() { }
  76. /// <summary>
  77. /// 联网模式,重新连接。
  78. /// </summary>
  79. public virtual void OnReconnected(UnitInfo temp, int force, int level, Vector2 enterPos) { }
  80. public virtual ZoneClient.LockActorEvent GenLockActorEvent(string displayName, float inRange, float outRange, int updateInterval)
  81. {
  82. // 准备发送当前场景信息 //
  83. var loc = new ZoneClient.LockActorEvent();
  84. loc.ServerUpdateInterval = updateInterval;
  85. loc.ClientSyncObjectRange = inRange;
  86. loc.ClientSyncObjectOutRange = outRange;
  87. loc.UnitData = IOUtil.CloneIExternalizable<ZoneClient.SyncUnitInfo>(TemplateManager.MessageCodec, this.GenSyncUnitInfo(true));
  88. loc.UnitData.Name = displayName;
  89. loc.UnitData.x = this.X;
  90. loc.UnitData.y = this.Y;
  91. loc.GameServerProp = CUtils.TryClone(this.Info.Properties);
  92. loc.Skills = this.GetSkillEvent();
  93. loc.UnitData.CurrentBuffStatus = this.GetCurrentBuffStatus();
  94. loc.CurrentSkillStatus = this.GetCurrentSkillStatus();
  95. loc.CurrentItemStatus = this.GetCurrentItemStatus();
  96. loc.CurrentPlayerVars = this.GetCurrentUnitVars();
  97. loc.CurrentZoneVars = Parent.GetCurrentZoneVars();
  98. return loc;
  99. }
  100. protected override void onUpdateRecover()
  101. {
  102. base.onUpdateRecover();
  103. }
  104. override protected void onResetAI()
  105. {
  106. this.cleanFocus();
  107. this.doSomething();
  108. }
  109. protected override void onAdded(bool pointLv)
  110. {
  111. base.onAdded(pointLv);
  112. this.doSomething();
  113. }
  114. // override protected void onDead(InstanceUnit attacker)
  115. // {
  116. // if (attacker is IPlayerUnit)
  117. // {
  118. // var killer = attacker as IPlayerUnit;
  119. // killer.PutKillPlayer(this.PlayerUUID);
  120. // }
  121. // }
  122. override protected void onUpdate(bool slowRefresh)
  123. {
  124. base.onUpdate(slowRefresh);
  125. this.updateGuard();
  126. this.updateCurrentSkill();
  127. this.updateAttackTo();
  128. }
  129. protected virtual void updateCurrentSkill()
  130. {
  131. if (CurrentState is StateSkill)
  132. {
  133. var current = CurrentState as StateSkill;
  134. if (mWaitingSkill != null && current.IsCancelableBySkill)
  135. {
  136. doLaunchSkill(mWaitingSkill);
  137. }
  138. else if (IsGuard)
  139. {
  140. if ((mFocusTarget != null))
  141. {
  142. tryLaunchRandomSkillAndCancelCurrentSkill(mFocusTarget.TargetUnit, true);
  143. }
  144. }
  145. }
  146. }
  147. protected virtual void updateGuard()
  148. {
  149. if (mFocusTarget != null && !mFocusTarget.IsActive)
  150. {
  151. mFocusTarget = null;
  152. }
  153. if (mFocusPickItem != null && !mFocusPickItem.IsActive)
  154. {
  155. mFocusPickItem = null;
  156. }
  157. if (IsGuard)
  158. {
  159. // 旋风斩贴近目标 //
  160. if (mFocusTarget != null && CurrentState is StateSkill)
  161. {
  162. var state = CurrentState as StateSkill;
  163. if (state.IsControlMoveable && !state.unit.IsCannotMove)
  164. {
  165. state.setMoveTo(mFocusTarget.Target.X, mFocusTarget.Target.Y);
  166. }
  167. }
  168. // 没目标定期检测目标 //
  169. if ((mFocusTarget == null) && (mFocusPickItem == null))
  170. {
  171. if (mCheckGuard.Update(Parent.UpdateIntervalMS))
  172. {
  173. var item = findGuardPickItem();
  174. if (item != null)
  175. {
  176. doFocusPickItem(item);
  177. return;
  178. }
  179. var enemy = findGuardTarget(SkillTemplate.CastTarget.Enemy, AttackReason.Look);
  180. if (enemy != null)
  181. {
  182. doFocusAttack(enemy, SkillTemplate.CastTarget.Enemy);
  183. return;
  184. }
  185. var alias = findGuardTarget(SkillTemplate.CastTarget.AlliesIncludeSelf, AttackReason.Look);
  186. if (alias != null)
  187. {
  188. doFocusAttack(alias, SkillTemplate.CastTarget.AlliesIncludeSelf);
  189. return;
  190. }
  191. }
  192. }
  193. }
  194. }
  195. protected virtual void updateAttackTo()
  196. {
  197. if (mAttackTo != null)
  198. {
  199. if (mAttackTo.IsDone)
  200. {
  201. mAttackTo = null;
  202. }
  203. else if (!mAttackTo.IsAttack)
  204. {
  205. cleanFocus();
  206. }
  207. }
  208. }
  209. public override void doSomething()
  210. {
  211. if (mWaitingSkill != null && CurrentState is StateSkill)
  212. {
  213. if (doLaunchSkill(mWaitingSkill))
  214. {
  215. return;
  216. }
  217. }
  218. else if (IsGuard)
  219. {
  220. if (mFocusPickItem != null && mFocusPickItem.IsActive)
  221. {
  222. changeState(mFocusPickItem);
  223. return;
  224. }
  225. else
  226. {
  227. mFocusPickItem = null;
  228. }
  229. if (mFocusTarget != null && mFocusTarget.IsActive)
  230. {
  231. if (Parent.IsAttackable(this, mFocusTarget.TargetUnit, SkillTemplate.CastTarget.AlliesIncludeSelf, AttackReason.Attack, Info))
  232. {
  233. mFocusTarget = null;
  234. }
  235. else
  236. {
  237. changeState(mFocusTarget);
  238. return;
  239. }
  240. }
  241. else
  242. {
  243. mFocusTarget = null;
  244. }
  245. if (mAttackTo != null && !mAttackTo.IsDone)
  246. {
  247. changeState(mAttackTo);
  248. return;
  249. }
  250. }
  251. base.doSomething();
  252. }
  253. public virtual InstanceUnit findGuardTarget(SkillTemplate.CastTarget expect = SkillTemplate.CastTarget.Enemy, AttackReason reason = AttackReason.Look)
  254. {
  255. InstanceUnit min = null;
  256. if (getAvailableSkill(expect) != null)
  257. {
  258. float min_len = float.MaxValue;
  259. Parent.ForEachNearObjects(this.X, this.Y, this.Info.GuardRange, (InstanceUnit u, ref bool cancel) =>
  260. {
  261. if (Parent.IsAttackable(this, u, expect, reason, Info))
  262. {
  263. float len = MathVector.getDistanceSquare(u.X, u.Y, this.X, this.Y);
  264. if (min_len > len)
  265. {
  266. min_len = len;
  267. min = u;
  268. }
  269. }
  270. });
  271. }
  272. return min;
  273. }
  274. public virtual StateFollowAndAttack doFocusAttack(InstanceUnit target, SkillTemplate.CastTarget expect_target = SkillTemplate.CastTarget.Enemy)
  275. {
  276. if ((mAttackTo == null || mAttackTo.IsAttack) && Parent.IsAttackable(this, target, expect_target, AttackReason.Tracing, Info))
  277. {
  278. if (mFocusTarget != null)
  279. {
  280. if (mFocusTarget.TargetUnit != target)
  281. {
  282. mFocusTarget = new StateFollowAndAttack(this, target, expect_target, true);
  283. if (changeState(mFocusTarget))
  284. {
  285. queueEvent(new PlayerFocuseTargetEvent(this.ID, target.ID, expect_target));
  286. }
  287. }
  288. }
  289. else
  290. {
  291. mFocusTarget = new StateFollowAndAttack(this, target, expect_target, true);
  292. if (changeState(mFocusTarget))
  293. {
  294. queueEvent(new PlayerFocuseTargetEvent(this.ID, target.ID, expect_target));
  295. }
  296. }
  297. }
  298. else
  299. {
  300. mFocusTarget = null;
  301. }
  302. return mFocusTarget;
  303. }
  304. //可被自动拾取的,是那种没有进度条的
  305. public virtual InstanceItem findGuardPickItem()
  306. {
  307. InstanceItem min = null;
  308. float min_len = float.MaxValue;
  309. Parent.ForEachNearObjects(this.X, this.Y, this.mAutoPickRange, (InstanceItem u, ref bool cancel) =>
  310. {
  311. if (u.IsPickable(this))
  312. {
  313. float len = MathVector.getDistanceSquare(u.X, u.Y, this.X, this.Y);
  314. if (min_len > len)
  315. {
  316. min_len = len;
  317. min = u;
  318. }
  319. }
  320. });
  321. return min;
  322. }
  323. public virtual StateFollowAndPickItem doFocusPickItem(InstanceItem target, bool doPick = true)
  324. {
  325. if (mAttackTo == null || mAttackTo.IsAttack)
  326. {
  327. if (mFocusPickItem != null)
  328. {
  329. if (mFocusPickItem.Target != target)
  330. {
  331. StateFollowAndPickItem focusPickItem = new StateFollowAndPickItem(this, target, doPick);
  332. if (changeState(focusPickItem))
  333. {
  334. mFocusPickItem = focusPickItem;
  335. queueEvent(new PlayerFocuseTargetEvent(this.ID, target.ID, SkillTemplate.CastTarget.NA));
  336. }
  337. }
  338. }
  339. else
  340. {
  341. StateFollowAndPickItem focusPickItem = new StateFollowAndPickItem(this, target, doPick);
  342. if (changeState(focusPickItem))
  343. {
  344. mFocusPickItem = focusPickItem;
  345. queueEvent(new PlayerFocuseTargetEvent(this.ID, target.ID, SkillTemplate.CastTarget.NA));
  346. }
  347. }
  348. }
  349. else
  350. {
  351. mFocusPickItem = null;
  352. }
  353. return mFocusPickItem;
  354. }
  355. protected virtual void cleanFocus()
  356. {
  357. mFocusPickItem = null;
  358. mFocusTarget = null;
  359. }
  360. // internal void PutKillPlayer(string killed)
  361. // {
  362. // int count = 0;
  363. // if (mKilledPlayersID.TryGetValue(killed, out count))
  364. // {
  365. // mKilledPlayersID.Put(killed, count + 1);
  366. // }
  367. // else
  368. // {
  369. // mKilledPlayersID.Put(killed, 1);
  370. // }
  371. // }
  372. //--------------------------------------------------------------------------------------
  373. public virtual bool doRequest(ActorRequest req)
  374. {
  375. if (req is UnitGetStatisticRequest)
  376. {
  377. doGetStatisticRequest(req as UnitGetStatisticRequest);
  378. return true;
  379. }
  380. return false;
  381. }
  382. //设置自动拾取范围
  383. public override void SetAutoPickRange(int value)
  384. {
  385. this.mAutoPickRange = value;
  386. this.mAutoPickSearchRange = value * 3;
  387. }
  388. protected override void onAction(ObjectAction act)
  389. {
  390. //log.Info("------<onAction: " + act.ToString());
  391. if (!IsDead())
  392. {
  393. if (act is UnitMoveAction)
  394. {
  395. doMove(act as UnitMoveAction);
  396. }
  397. else if (act is UnitAxisAction)
  398. {
  399. doAxis(act as UnitAxisAction);
  400. }
  401. else if (act is UnitFaceToAction)
  402. {
  403. doFaceTo(act as UnitFaceToAction);
  404. }
  405. else if (act is UnitStopMoveAction)
  406. {
  407. doStopMove(act as UnitStopMoveAction);
  408. }
  409. else if (act is UnitLaunchSkillAction)
  410. {
  411. doLaunchSkill(act as UnitLaunchSkillAction);
  412. }
  413. else if (act is UnitUseItemAction)
  414. {
  415. doUseItem(act as UnitUseItemAction);
  416. }
  417. else if (act is UnitPickObjectAction)
  418. {
  419. doPickObject(act as UnitPickObjectAction);
  420. }
  421. else if (act is UnitUpdatePosAction)
  422. {
  423. doUnitUpdatePos(act as UnitUpdatePosAction);
  424. }
  425. else if (act is UnitCancelBuffAction)
  426. {
  427. doCancelBuff(act as UnitCancelBuffAction);
  428. }
  429. else if (act is UnitSetSubStateAction)
  430. {
  431. doUnitSetSubStateAction(act as UnitSetSubStateAction);
  432. }
  433. else if (act is PlayPetTeleportAction)
  434. {
  435. doPetTeleportAction(act as PlayPetTeleportAction);
  436. }
  437. }
  438. if (act is UnitReadAction)
  439. {
  440. doReady(act as UnitReadAction);
  441. }
  442. else if (act is UnitGuardAction)
  443. {
  444. doGuard(act as UnitGuardAction);
  445. }
  446. else if (act is UnitFollowAction)
  447. {
  448. doFollow(act as UnitFollowAction);
  449. }
  450. else if (act is UnitAttackToAction)
  451. {
  452. doAttackTo(act as UnitAttackToAction);
  453. }
  454. else if (act is UnitFocuseTargetAction)
  455. {
  456. doFocusTarget(act as UnitFocuseTargetAction);
  457. }
  458. else if (act is ChatAction)
  459. {
  460. doChat(act as ChatAction);
  461. }
  462. else if (act is UnitSetSyncModeAction)
  463. {
  464. doSetSyncMode(act as UnitSetSyncModeAction);
  465. }
  466. else if (act is UnitGetStatisticRequest)
  467. {
  468. doGetStatisticRequest(act as UnitGetStatisticRequest);
  469. }
  470. }
  471. #region PlayerActions
  472. protected virtual void doReady(UnitReadAction act)
  473. {
  474. if (IsReady == false)
  475. {
  476. IsReady = true;
  477. Parent.cb_playerReady(this);
  478. }
  479. }
  480. protected virtual void doMove(UnitMoveAction ma)
  481. {
  482. if (CurrentState is StateSkill)
  483. {
  484. StateSkill ss = CurrentState as StateSkill;
  485. ss.setMoveTo(new Vector2(ma.x, ma.y));
  486. }
  487. else
  488. {
  489. cleanFocus();
  490. startMoveTo(ma.x, ma.y);
  491. }
  492. }
  493. protected virtual void doAxis(UnitAxisAction ma)
  494. {
  495. if (CurrentState is StateSkill)
  496. {
  497. ma.dx *= 100f;
  498. ma.dy *= 100f;
  499. StateSkill ss = CurrentState as StateSkill;
  500. if (!ma.IsZero)
  501. {
  502. if (ss.IsCancelableByMove)
  503. {
  504. cleanFocus();
  505. if (this.mWaitingSkill != null)
  506. {
  507. doLaunchSkill(this.mWaitingSkill);
  508. }
  509. else if (mCurrentSyncMode != ZoneClient.SyncMode.MoveByClient_PreSkillByClient)
  510. {
  511. this.mControlMove.Action = ma;
  512. ss.block(mControlMove);
  513. }
  514. else
  515. {
  516. ss.block(mUpdateMove);
  517. }
  518. }
  519. else
  520. {
  521. this.mControlMove.Action = ma;
  522. Vector2 moveto = new Vector2(X + ma.dx, Y + ma.dy);
  523. ss.setMoveTo(moveto);
  524. }
  525. }
  526. else
  527. {
  528. ss.setMoveTo(null);
  529. }
  530. }
  531. else if (mCurrentSyncMode != ZoneClient.SyncMode.MoveByClient_PreSkillByClient)
  532. {
  533. cleanFocus();
  534. if (!ma.IsZero)
  535. {
  536. this.mControlMove.Action = ma;
  537. changeState(mControlMove);
  538. }
  539. else
  540. {
  541. doSomething();
  542. }
  543. }
  544. }
  545. protected virtual void doFaceTo(UnitFaceToAction ufa)
  546. {
  547. if (CurrentActionStatus == UnitActionStatus.Idle || CurrentActionStatus == UnitActionStatus.Move)
  548. {
  549. this.faceTo(ufa.Direction);
  550. }
  551. }
  552. protected virtual void doStopMove(UnitStopMoveAction act)
  553. {
  554. cleanFocus();
  555. if (CurrentState is StateSkill)
  556. {
  557. StateSkill ss = CurrentState as StateSkill;
  558. ss.setMoveTo(null);
  559. }
  560. else if (CurrentState is StateMove || CurrentState is StatePlayerControlMove)
  561. {
  562. doSomething();
  563. base.SetActionStatus(UnitActionStatus.Idle);
  564. }
  565. }
  566. protected virtual bool doLaunchSkill(UnitLaunchSkillAction sk)
  567. {
  568. var launched = launchSkill(sk.SkillID, new LaunchSkillParam(
  569. sk.TargetObjID,
  570. sk.SpellTargetPos,
  571. sk.IsAutoFocusNearTarget,
  572. sk.SyncDirection,
  573. sk.Direction));
  574. if (launched == null)
  575. {
  576. //Console.WriteLine("UnitLaunchSkillAction - 1");
  577. if (CurrentState is StateSkill)
  578. {
  579. var state = CurrentState as StateSkill;
  580. if (state.SkillData.ID != sk.SkillID || (state.SkillData.IsSingleAction && state.Skill.GetSkillType() != XmdsSkillType.normalAtk))
  581. {
  582. //缓存释放技能指令//
  583. mWaitingSkill = sk;
  584. }
  585. }
  586. return false;
  587. }
  588. else
  589. {
  590. //一旦释放成功,释放指令//\
  591. //Console.WriteLine("UnitLaunchSkillAction - 2");
  592. mWaitingSkill = null;
  593. return true;
  594. }
  595. }
  596. protected virtual void doGetStatisticRequest(UnitGetStatisticRequest req)
  597. {
  598. UnitGetStatisticResponse resp = new UnitGetStatisticResponse();
  599. if (req.RequestObjectsID != null)
  600. {
  601. for (int i = 0; i < req.RequestObjectsID.Length; i++)
  602. {
  603. InstanceUnit u = Parent.getUnit(req.RequestObjectsID[i]);
  604. if (u != null)
  605. {
  606. var data = u.Statistic.ToUnitStatisticData();
  607. resp.Statistics.Put(req.RequestObjectsID[i], data);
  608. }
  609. }
  610. }
  611. Parent.sendActorResponse(this, req, resp);
  612. }
  613. protected virtual void doFocusTarget(UnitFocuseTargetAction focus)
  614. {
  615. if (IsGuard)
  616. {
  617. mAttackTo = null;
  618. InstanceZoneObject src = Parent.getObject<InstanceZoneObject>(focus.targetUnitID);
  619. if ((src is InstanceUnit))
  620. {
  621. if (!IsNoneSkill && ((src as InstanceUnit).IsActive))
  622. {
  623. doFocusAttack(src as InstanceUnit);
  624. }
  625. }
  626. else if ((src is InstanceItem))
  627. {
  628. if (src.Enable)
  629. {
  630. doFocusPickItem(src as InstanceItem);
  631. }
  632. }
  633. else
  634. {
  635. cleanFocus();
  636. }
  637. }
  638. }
  639. protected virtual void doAttackTo(UnitAttackToAction act)
  640. {
  641. if (!act.attack)
  642. {
  643. cleanFocus();
  644. }
  645. mAttackTo = new StatePlayerAttackTo(this, act.targetX, act.targetY, act.attack);
  646. changeState(mAttackTo);
  647. }
  648. protected virtual void doUseItem(UnitUseItemAction use)
  649. {
  650. UseInventoryItem(use.Index, use.Count);
  651. }
  652. protected virtual void doPickObject(UnitPickObjectAction pick)
  653. {
  654. InstanceZoneObject obj = Parent.getObject<InstanceZoneObject>(pick.PickableObjectID);
  655. if (obj is InstanceItem)
  656. {
  657. InstanceItem item = obj as InstanceItem;
  658. item.PickItem(this);
  659. }
  660. else if (obj is InstanceUnit)
  661. {
  662. InstanceUnit unit = obj as InstanceUnit;
  663. this.PickUnit(unit);
  664. }
  665. }
  666. protected virtual void doChat(ChatAction chat)
  667. {
  668. ChatEvent send = new ChatEvent(chat.To);
  669. send.FromPlayerUUID = this.PlayerUUID;
  670. send.Message = chat.Message;
  671. switch (chat.To)
  672. {
  673. case ChatMessageType.PlayerToPlayer:
  674. InstancePlayer target = Parent.getPlayerByUUID(chat.TargetPlayerUUID);
  675. if (target != null)
  676. {
  677. send.ToPlayerUUID = target.PlayerUUID;
  678. Parent.queueEvent(send);
  679. }
  680. break;
  681. case ChatMessageType.PlayerToForce:
  682. send.Force = this.Force;
  683. Parent.queueEvent(send);
  684. break;
  685. case ChatMessageType.PlayerToAll:
  686. Parent.queueEvent(send);
  687. break;
  688. }
  689. }
  690. protected virtual void doUnitUpdatePos(UnitUpdatePosAction act)
  691. {
  692. //Console.WriteLine("-------------------UnitUpdatePosAction : " + TimeUtil.GetTimestampMS() + ", " + act.x + ", " + act.y + " \t \t" + act.d);
  693. if (mCurrentSyncMode == ZoneClient.SyncMode.MoveByClient_PreSkillByClient && !this.IsCannotMove)
  694. {
  695. //mWaitingSkill = null;
  696. mUpdateMove.SetPos(act);
  697. if (CurrentState is StateSkill)
  698. {
  699. var ss = CurrentState as StateSkill;
  700. ss.doUpdatePosByClient(act);
  701. //服务器判断下,蓄力阶段才同步客户端蓄力状态
  702. if (ss.CurrentActionIndex < ss.SkillData.ActionQueue.Count &&
  703. ss.SkillData.ActionQueue[ss.CurrentActionIndex].SigleActionType == ActionEnum.chargeAtk)
  704. {
  705. if (act.st == UnitActionStatus.ChargeAtkMove)
  706. {
  707. this.RemoveActionSubState(UnitActionSubStatus.ChargeAtkIdle);
  708. this.AddActionSubState(UnitActionSubStatus.ChargeAtkMove);
  709. }
  710. else if (act.st == UnitActionStatus.ChargeAtkIdle)
  711. {
  712. this.RemoveActionSubState(UnitActionSubStatus.ChargeAtkMove);
  713. this.AddActionSubState(UnitActionSubStatus.ChargeAtkIdle);
  714. }
  715. }
  716. }
  717. else if (CurrentState is StatePickObject)
  718. {
  719. if (act.st == UnitActionStatus.Move)
  720. {
  721. this.faceTo(act.d);
  722. this.setPos(act.x, act.y, this.IntersectMap);
  723. changeState(mUpdateMove);
  724. }
  725. }
  726. else if (CurrentState is StateDamage || CurrentState is StateDead || CurrentState is IStateNoneControllable)
  727. {
  728. }
  729. else if (CurrentActionStatus == UnitActionStatus.DaZuo || CurrentActionStatus == UnitActionStatus.DaZuoRecoveryAttr ||
  730. CurrentActionStatus == UnitActionStatus.Idle || CurrentActionStatus == UnitActionStatus.Move)
  731. {
  732. //changestate在帧中切换状态,这里提前判断下一个动作能否转向
  733. bool canChangeDircetion = true;
  734. StateSkill nextSkill = this.NextState as StateSkill;
  735. if (nextSkill != null && nextSkill.Skill != null)
  736. {
  737. UnitActionData action = nextSkill.Skill.GetCurAction();
  738. if(action != null && !action.IsControlFaceable)
  739. {
  740. canChangeDircetion = false;
  741. }
  742. }
  743. if (canChangeDircetion)
  744. {
  745. this.faceTo(act.d);
  746. }
  747. this.setPos(act.x, act.y, this.IntersectMap);
  748. if (act.st == UnitActionStatus.Idle)
  749. {
  750. doSomething();
  751. }
  752. else if (act.st == UnitActionStatus.Move)
  753. {
  754. changeState(mUpdateMove);
  755. }
  756. //Console.WriteLine("st=" + act.d);
  757. }
  758. }
  759. }
  760. protected virtual void doGuard(UnitGuardAction act)
  761. {
  762. if (this.IsGuard != act.guard)
  763. {
  764. var IsInCancelingGuard = this.IsGuard;
  765. this.IsGuard = act.guard;
  766. this.mIsSkillControlByServer = IsGuard || (mCurrentSyncMode != ZoneClient.SyncMode.MoveByClient_PreSkillByClient);
  767. if (IsGuard == false)
  768. {
  769. mAttackTo = null;
  770. cleanFocus();
  771. }
  772. else
  773. {
  774. mCheckGuard.Reset();
  775. }
  776. resetAI();
  777. if (NextState == null && IsInCancelingGuard && CurrentState is StateSkill)
  778. {
  779. TryEnqueueIdleState();
  780. }
  781. //通知待机状态\
  782. //if (act.forceNotify)
  783. {
  784. this.Virtual.doEvent(JSGCustomOpType.UpdateAutoBattleFlag);
  785. }
  786. }
  787. }
  788. public virtual void doEnterAutoFight()
  789. {
  790. this.IsGuard = true;
  791. }
  792. protected virtual void doFollow(UnitFollowAction act)
  793. {
  794. this.mIsSkillControlByServer = act.follow || (mCurrentSyncMode != ZoneClient.SyncMode.MoveByClient_PreSkillByClient);
  795. //this.mIsSkillControlByServer = (mCurrentSyncMode != ZoneClient.SyncMode.MoveByClient_PreSkillByClient);
  796. }
  797. protected virtual void doSetSyncMode(UnitSetSyncModeAction act)
  798. {
  799. this.mCurrentSyncMode = act.Mode;
  800. this.mIsSkillControlByServer = IsGuard || (mCurrentSyncMode != ZoneClient.SyncMode.MoveByClient_PreSkillByClient);
  801. }
  802. protected virtual void doCancelBuff(UnitCancelBuffAction act)
  803. {
  804. BuffState bs = this.GetBuffByID(act.BuffID);
  805. if (bs != null && bs.Data.IsCancelBySelf)
  806. {
  807. this.removeBuff(act.BuffID, BuffState.EndResult_ByClientRemoved);
  808. }
  809. }
  810. protected virtual void doUnitSetSubStateAction(UnitSetSubStateAction act)
  811. {
  812. base.AddActionSubState((UnitActionSubStatus)(act.UnitSubState));
  813. }
  814. protected virtual void doPetTeleportAction(PlayPetTeleportAction act)
  815. {
  816. InstanceUnit petUnit = this.Virtual.GetPetUnit();
  817. if (petUnit != null)
  818. {
  819. if (petUnit.petTeleportTime < CommonLang.CUtils.CurrentTimeMS)
  820. {
  821. petUnit.petTeleportTime = CommonLang.CUtils.CurrentTimeMS+10*1000;
  822. petUnit.lastTeleportTime = CommonLang.CUtils.CurrentTimeMS+3*1000; //3秒之后开始返回
  823. //获取玩家当前坐标
  824. float x = this.get_x();
  825. float y = this.get_y();
  826. petUnit.setPos(this.get_x(), this.get_y(), this.IntersectMap);
  827. petUnit.faceTo(this.get_direction());
  828. State s = new PetLockState(petUnit);
  829. petUnit.changeState(s);
  830. }
  831. else
  832. {
  833. if (petUnit.lastTeleportTime < CommonLang.CUtils.CurrentTimeMS)
  834. {
  835. petUnit.lastTeleportTime = CommonLang.CUtils.CurrentTimeMS+3*1000; //3秒之后开始返回
  836. this.Virtual.SendMsgToClient(CommonAI.XmdsConstConfig.TIPS_CALL_BACK_BTN);
  837. }
  838. }
  839. }
  840. }
  841. #endregion
  842. // --------------------------------------------------------------------------------------
  843. #region PlayerQuests
  844. public virtual void InitQuestData(ICollection<QuestData> datas)
  845. {
  846. foreach (QuestData q in datas)
  847. {
  848. if (q.QuestID != null)
  849. {
  850. mQuests.Put(q.QuestID, q);
  851. }
  852. }
  853. }
  854. internal QuestData doQuestAccepted(string quest)
  855. {
  856. if (!string.IsNullOrEmpty(quest))
  857. {
  858. QuestData qd = new QuestData(quest);
  859. qd.State = QuestState.Accepted;
  860. mQuests.Put(quest, qd);
  861. return qd;
  862. }
  863. return null;
  864. }
  865. internal QuestData doQuestCommitted(string quest)
  866. {
  867. QuestData qd = mQuests.RemoveByKey(quest);
  868. if (qd != null)
  869. {
  870. qd.State = QuestState.Commited;
  871. onQuestCommitted(qd);
  872. }
  873. return qd;
  874. }
  875. /// <summary>
  876. /// 当任务完成时,子类覆盖的方法被调用
  877. /// </summary>
  878. /// <param name="qd"></param>
  879. protected virtual void onQuestCommitted(QuestData qd) { }
  880. internal QuestData doQuestDropped(string quest)
  881. {
  882. QuestData qd = mQuests.RemoveByKey(quest);
  883. if (qd != null)
  884. {
  885. qd.State = QuestState.Uncharted;
  886. }
  887. return qd;
  888. }
  889. internal QuestData doQuestStatusChanged(string quest, string key, string value)
  890. {
  891. QuestData qd = mQuests.Get(quest);
  892. if (qd != null)
  893. {
  894. qd.Attributes.Put(key, value);
  895. }
  896. return qd;
  897. }
  898. public virtual bool IsQuestAccepted(string quest)
  899. {
  900. QuestData qd = mQuests.Get(quest);
  901. if (qd != null)
  902. {
  903. string ret;
  904. if (qd.Attributes.TryGetValue("state", out ret))
  905. {
  906. return ret == "0";
  907. }
  908. return qd.State == QuestState.Accepted;
  909. }
  910. return false;
  911. }
  912. public virtual string GetQuestStatus(string quest, string key)
  913. {
  914. QuestData qd = mQuests.Get(quest);
  915. if (qd != null)
  916. {
  917. string value;
  918. if (qd.Attributes.TryGetValue(key, out value))
  919. {
  920. return value;
  921. }
  922. }
  923. return null;
  924. }
  925. public virtual QuestData GetQuest(string quest)
  926. {
  927. return mQuests.Get(quest);
  928. }
  929. #endregion
  930. //--------------------------------------------------------------------------------------
  931. /// <summary>
  932. /// 更新坐标移动状态
  933. /// </summary>
  934. public class StatePlayerUpdateMove : State
  935. {
  936. private UnitUpdatePosAction act;
  937. public bool IsPosChanged
  938. {
  939. get
  940. {
  941. if (act != null)
  942. {
  943. return act.x != unit.X || act.y != unit.Y;
  944. }
  945. return false;
  946. }
  947. }
  948. public StatePlayerUpdateMove(InstancePlayer unit)
  949. : base(unit)
  950. {
  951. }
  952. public void SetPos(UnitUpdatePosAction act)
  953. {
  954. // TODO 检测作弊
  955. this.act = act;
  956. }
  957. public override bool onBlock(State new_state)
  958. {
  959. return true;
  960. }
  961. protected override void onStart()
  962. {
  963. unit.SetActionStatus(UnitActionStatus.Move);
  964. }
  965. protected override void onUpdate()
  966. {
  967. unit.SetActionStatus(UnitActionStatus.Move);
  968. }
  969. protected override void onStop()
  970. {
  971. }
  972. }
  973. //--------------------------------------------------------------------------------------
  974. /// <summary>
  975. /// 摇杆控制移动状态
  976. /// </summary>
  977. protected class StatePlayerControlMove : State
  978. {
  979. private float mDirection;
  980. private UnitAxisAction mAction;
  981. public UnitAxisAction Action
  982. {
  983. get { return mAction; }
  984. set
  985. {
  986. mAction = value;
  987. mDirection = MathVector.getDegree(value.dx, value.dy);
  988. }
  989. }
  990. public float Direction
  991. {
  992. get { return mDirection; }
  993. }
  994. public StatePlayerControlMove(InstancePlayer unit)
  995. : base(unit)
  996. {
  997. }
  998. public override bool onBlock(State new_state)
  999. {
  1000. return true;
  1001. }
  1002. protected override void onStart()
  1003. {
  1004. unit.SetActionStatus(UnitActionStatus.Move);
  1005. }
  1006. protected override void onUpdate()
  1007. {
  1008. unit.faceTo(mDirection);
  1009. unit.moveBlockTo(unit.Direction, unit.MoveSpeedSEC, zone.UpdateIntervalMS);
  1010. unit.SetActionStatus(UnitActionStatus.Move);
  1011. }
  1012. protected override void onStop()
  1013. {
  1014. }
  1015. }
  1016. //--------------------------------------------------------------------------------------
  1017. protected class StatePlayerAttackTo : State
  1018. {
  1019. private TargetPos target;
  1020. private MoveAI moveAI;
  1021. private bool is_attack;
  1022. public bool IsAttack { get { return is_attack; } }
  1023. public bool IsDone { get { return target == null; } }
  1024. public StatePlayerAttackTo(InstancePlayer unit, float tx, float ty, bool attack)
  1025. : base(unit)
  1026. {
  1027. this.target = new TargetPos(tx, ty);
  1028. this.is_attack = attack;
  1029. }
  1030. public override bool onBlock(State new_state)
  1031. {
  1032. return true;
  1033. }
  1034. override protected void onStart()
  1035. {
  1036. this.moveAI = new MoveAI(unit, true, 0);
  1037. this.moveAI.IsNoWayAutoFindNear = false; ;
  1038. if (target != null)
  1039. {
  1040. this.moveAI.FindPath(target);
  1041. unit.SetActionStatus(UnitActionStatus.Move);
  1042. }
  1043. else
  1044. {
  1045. unit.SetActionStatus(UnitActionStatus.Idle);
  1046. }
  1047. }
  1048. override protected void onUpdate()
  1049. {
  1050. if (target == null)
  1051. {
  1052. unit.doSomething();
  1053. }
  1054. else
  1055. {
  1056. var result = this.moveAI.Update();
  1057. if (result.HasFlag(MoveResult.MOVE_RESULT_ARRIVED))
  1058. {
  1059. do_stop();
  1060. }
  1061. else if (result.HasFlag(MoveResult.MOVE_RESULT_MIN_STEP) && result.HasFlag(MoveResult.MOVE_RESULT_NO_WAY))
  1062. {
  1063. do_stop();
  1064. }
  1065. else if (result.HasFlag(MoveResult.MOVE_RESULT_BLOCK_OBJ))
  1066. {
  1067. if (CMath.includeRoundPoint(unit.X, unit.Y, unit.BodyBlockSize, target.X, target.Y))
  1068. {
  1069. do_stop();
  1070. }
  1071. else if (result.obj != null && CMath.includeRoundPoint(result.obj.X, result.obj.Y, result.obj.BodyBlockSize, target.X, target.Y))
  1072. {
  1073. do_stop();
  1074. }
  1075. }
  1076. }
  1077. }
  1078. protected override void onStop()
  1079. {
  1080. }
  1081. private void do_stop()
  1082. {
  1083. target = null;
  1084. unit.doSomething();
  1085. }
  1086. class TargetPos : IPositionObject
  1087. {
  1088. public float Direction { get; set; }
  1089. public float RadiusSize { get; set; }
  1090. public float X { get; set; }
  1091. public float Y { get; set; }
  1092. public TargetPos(float tx, float ty)
  1093. {
  1094. this.X = tx;
  1095. this.Y = ty;
  1096. }
  1097. }
  1098. }
  1099. }
  1100. }