123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- using CommonAI.RTS;
- using CommonLang.Vector;
- using CommonAI.Zone.Helper;
- using CommonLang;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace CommonAI.Zone.Instance
- {
-
-
-
- public class InstanceManual : InstanceUnit
- {
- private StateFollowAndAttack mAttackTargget;
- private TimeExpire<WaitTimeHandler> mWaitCommand;
- public InstanceManual(InstanceZone zone, UnitInfo info, string name, int force, int level)
- : base(zone, info, name, force, level)
- {
- }
- public void queueDoAction(float timeSEC, string actionName, State.StateStopHandler over = null)
- {
- mAttackTargget = null;
- QueueStateActionTime state = new QueueStateActionTime(this, timeSEC, actionName);
- if (over != null)
- {
- state.AddStopOnce(over);
- }
- queueState(state);
- }
- public void queueIdle(float timeSEC, State.StateStopHandler over = null)
- {
- mAttackTargget = null;
- QueueStateIdleTime state = new QueueStateIdleTime(this, timeSEC);
- if (over != null)
- {
- state.AddStopOnce(over);
- }
- queueState(state);
- }
- public void queueMoveTo(float x, float y, State.StateStopHandler over = null)
- {
- mAttackTargget = null;
- QueueStateMoveTo state = new QueueStateMoveTo(this, x, y);
- if (over != null)
- {
- state.AddStopOnce(over);
- }
- queueState(state);
- }
- public void queueLaunchSkill(int skillID, bool random, State.StateStopHandler over = null)
- {
- mAttackTargget = null;
- QueueStateLaunchSkill state = new QueueStateLaunchSkill(this, skillID, random, over);
- queueState(state);
- }
- public void focuseAttack(InstanceUnit targget)
- {
- mAttackTargget = new StateFollowAndAttack(this, targget);
- changeState(mAttackTargget);
- }
- public void wait(float timeSEC, WaitTimeHandler over)
- {
-
- if (mWaitCommand != null)
- {
- mWaitCommand.Tag.Invoke();
- }
- mWaitCommand = new TimeExpire<WaitTimeHandler>(over, (int)(timeSEC * 1000));
- }
- protected override void onUpdate(bool slowRefresh)
- {
- base.onUpdate(slowRefresh);
- if (mWaitCommand != null && mWaitCommand.Update(Parent.UpdateIntervalMS))
- {
- mWaitCommand.Tag.Invoke();
- mWaitCommand = null;
- }
- }
- protected override void onAction(ObjectAction act)
- {
- if (IsDead())
- {
- return;
- }
- if (act is UnitStopMoveAction)
- {
- doSomething();
- }
- else if (act is UnitMoveAction)
- {
- UnitMoveAction move = act as UnitMoveAction;
- startMoveTo(move.x, move.y);
- }
- else if (act is UnitAxisAction)
- {
- }
- else if (act is UnitLaunchSkillAction)
- {
- UnitLaunchSkillAction sk = act as UnitLaunchSkillAction;
- launchSkill(sk.SkillID, new InstanceUnit.LaunchSkillParam(sk.TargetObjID, sk.SpellTargetPos, sk.IsAutoFocusNearTarget));
- }
- else if (act is UnitFaceToAction)
- {
- UnitFaceToAction ufa = act as UnitFaceToAction;
- this.faceTo(ufa.Direction);
- }
- else if (act is UnitSlipAction)
- {
-
- }
- else if (act is UnitGuardAction)
- {
-
- }
- else if (act is UnitFocuseTargetAction)
- {
-
- }
- }
- public delegate void WaitTimeHandler();
- class QueueStateMoveTo : State
- {
- private readonly float targetX;
- private readonly float targetY;
- private bool isEnd = false;
- private MoveAI moveAI;
- public QueueStateMoveTo(InstanceUnit unit, float x, float y)
- : base(unit)
- {
- this.targetX = x;
- this.targetY = y;
- }
- override public bool onBlock(State new_state)
- {
- if (unit.IsDead()) return true;
- return isEnd;
- }
- override protected void onStart()
- {
- unit.SetActionStatus(UnitActionStatus.Move);
- this.moveAI = new MoveAI(unit);
- this.moveAI.FindPath(targetX, targetY);
- }
- override protected void onUpdate()
- {
- if (!isEnd)
- {
- unit.faceTo(targetX, targetY);
- MoveBlockResult result = moveAI.Update();
- if ((result.result & MoveResult.MOVE_RESULT_NO_WAY) != 0)
- {
- isEnd = true;
- unit.doSomething();
- }
- else if ((result.result & MoveResult.RESULTS_MOVE_END) != 0)
- {
- float r = Math.Max(zone.MinStep, unit.BodyBlockSize);
- if (CMath.includeRoundPoint(unit.X, unit.Y, r, targetX, targetY))
- {
- isEnd = true;
- unit.doSomething();
- }
- }
- else
- {
- float r = Math.Max(zone.MinStep, unit.BodyBlockSize);
- if (CMath.includeRoundPoint(unit.X, unit.Y, r, targetX, targetY))
- {
- isEnd = true;
- unit.doSomething();
- }
- }
- }
- }
- override protected void onStop()
- {
- }
- }
- class QueueStateLaunchSkill : State
- {
- private readonly int SkillID;
- private readonly bool IsRandom;
- private readonly StateStopHandler SkillOver;
- private StateSkill mSkillState;
- public QueueStateLaunchSkill(InstanceUnit unit, int skillID, bool random, StateStopHandler over)
- : base(unit)
- {
- this.SkillID = skillID;
- this.IsRandom = random;
- this.SkillOver = over;
- }
- public override bool onBlock(State new_state)
- {
- if (unit.IsDead()) return true;
- if (new_state is StateSkill)
- {
- return true;
- }
- return mSkillState != null;
- }
- protected override void onStart()
- {
- }
- protected override void onUpdate()
- {
- if (IsRandom)
- {
- mSkillState = unit.launchRandomSkillForAll(new InstanceUnit.LaunchSkillParam());
- }
- else
- {
- mSkillState = unit.launchSkill(SkillID, new InstanceUnit.LaunchSkillParam());
- }
- if (mSkillState != null && SkillOver != null)
- {
- mSkillState.AddStopOnce(SkillOver);
- }
- }
- protected override void onStop()
- {
- if (mSkillState == null && SkillOver != null)
- {
- SkillOver.Invoke(unit, this);
- }
- }
- }
- class QueueStateIdleTime : State
- {
- private readonly TimeExpire<int> mIdleTime;
- public QueueStateIdleTime(InstanceUnit unit, float timeSEC)
- : base(unit)
- {
- mIdleTime = new TimeExpire<int>((int)(timeSEC * 1000));
- }
- override public bool onBlock(State new_state)
- {
- if (unit.IsDead()) return true;
- return mIdleTime.IsEnd;
- }
- override protected void onStart()
- {
- unit.SetActionStatus(UnitActionStatus.Idle);
- }
- override protected void onUpdate()
- {
- if (mIdleTime.Update(zone.UpdateIntervalMS))
- {
- unit.doSomething();
- }
- }
- override protected void onStop() { }
- }
- class QueueStateActionTime : QueueStateIdleTime
- {
- private readonly string ActionName;
- public QueueStateActionTime(InstanceUnit unit, float timeSEC, string actionName)
- : base(unit, timeSEC)
- {
- this.ActionName = actionName;
- }
- override protected void onStart()
- {
- unit.queueEvent(new UnitDoActionEvent(unit.ID, ActionName));
- }
- }
- }
- }
|