コード例 #1
0
        public FDCreature GetPreferredAttackTargetInRange(int creatureId)
        {
            FDCreature creature = this.GetCreature(creatureId);

            AttackItemDefinition attackItem = creature.Data.GetAttackItem();

            if (attackItem == null)
            {
                return(null);
            }

            FDSpan span = attackItem.AttackScope;

            DirectRangeFinder finder = new DirectRangeFinder(this.gameField, creature.Position, span.Max, span.Min);
            FDRange           range  = finder.CalculateRange();

            // Get a preferred target in range
            foreach (FDPosition position in range.Positions)
            {
                FDCreature target = this.GetCreatureAt(position);
                if (target != null && target.Faction == CreatureFaction.Enemy)
                {
                    return(target);
                }
            }

            return(null);
        }
コード例 #2
0
        public override void TakeAction()
        {
            if (this.NeedAndCanRecover())
            {
                this.GameAction.DoCreatureRest(this.Creature.CreatureId);
                return;
            }

            // Get target
            FDCreature target = this.LookForAggressiveTarget();

            // According to the target, find the nearest position within the Move scope, and get the path to that position
            FDMovePath movePath = this.DecidePositionAndPath(target.Position);

            // Do the walk
            this.GameAction.CreatureWalk(new SingleWalkAction(this.Creature.CreatureId, movePath));

            FDPosition destination = movePath.Desitination ?? this.Creature.Position;

            AttackItemDefinition item = this.Creature.Data.GetAttackItem();

            if (item != null)
            {
                FDSpan            span   = item.AttackScope;
                DirectRangeFinder finder = new DirectRangeFinder(this.GameAction.GetField(), destination, span.Max, span.Min);
                FDRange           range  = finder.CalculateRange();
                if (range.Contains(target.Position))
                {
                    // If in attack range, attack the target
                    this.GameAction.DoCreatureAttack(this.Creature.CreatureId, target.Position);
                }
            }

            this.GameAction.DoCreatureRest(this.Creature.CreatureId);
        }
コード例 #3
0
        protected FDMovePath DecidePositionAndPath(FDPosition targetPos)
        {
            DistanceResolver disResolver = new DistanceResolver(this.GameAction.GetField());

            disResolver.ResolveDistanceFrom(targetPos, this.Creature.Position);

            FDPosition originalPos = this.Creature.Position;

            float bestDistance       = 999;
            int   bestDistanceInUnit = -1;
            bool  inAttackScope      = false;

            MoveRangeFinder finder    = new MoveRangeFinder(this.GameAction, this.Creature);
            FDMoveRange     moveRange = finder.CalculateMoveRange();

            FDSpan     span     = null;
            FDPosition finalPos = originalPos;

            if (this.GameAction.GetCreatureAt(targetPos) != null)
            {
                span = Creature.Data.GetAttackItem()?.AttackScope;
            }
            else
            {
                span = new FDSpan(0, 0);
            }

            foreach (FDPosition movePos in moveRange.Positions)
            {
                int distanceToTarget = GetDirectDistance(targetPos, movePos);
                if (span.ContainsValue(distanceToTarget))
                {
                    inAttackScope = true;
                    if (distanceToTarget > bestDistanceInUnit)
                    {
                        bestDistanceInUnit = distanceToTarget;
                        finalPos           = movePos;
                    }
                }

                if (!inAttackScope)
                {
                    float distance = disResolver.GetDistanceTo(movePos);
                    if (distance < bestDistance)
                    {
                        bestDistance = distance;
                        finalPos     = movePos;
                    }
                }
            }

            return(moveRange.GetPath(finalPos));
        }
コード例 #4
0
        public override void OnEnter()
        {
            base.OnEnter();

            if (this.AttackRange == null)
            {
                AttackItemDefinition attackItem = this.Creature.Data.GetAttackItem();
                if (attackItem == null)
                {
                    return;
                }

                FDSpan            span   = attackItem.AttackScope;
                DirectRangeFinder finder = new DirectRangeFinder(gameAction.GetField(), this.Creature.Position, span.Max, span.Min);
                this.AttackRange = finder.CalculateRange();
            }

            // Display the attack range on the UI.
            ShowRangePack pack = new ShowRangePack(this.AttackRange);

            SendPack(pack);
        }