Exemplo n.º 1
0
        internal override void Update(GameTime gameTime)
        {
            moveTimer -= gameTime.ElapsedGameTime.TotalSeconds;
            if (moveTimer > 0)
            {
                return;
            }

            moveTimer = 1.0f;

            this.Owner.UpdateHateList();

            HateListEntry entry = this.Owner.HateList.GetHighestHateListEntry();

            if (entry.Target == null)
            {
                Log.AI(this.Owner?.ToString(), "No enemy ... moving!");

                if (bUpdatePath)
                {
                    Path = Owner.CurrentMap.CalcPath(Owner.TileX, Owner.TileY, destX, destY);
                    if (Path != null)
                    {
                        curNodeIdx  = 0;
                        bUpdatePath = false;
                    }
                }

                if (curNodeIdx == -1 || Path == null)
                {
                    return;
                }

                if (curNodeIdx >= Path.Count)
                {
                    this.Owner.Idle();
                    return;
                }

                IMapPathNode node = Path[curNodeIdx++];
                // TODO!!! Move, not Set
                Owner.SetPosition(node.X, node.Y);
                return;
            }

            this.Owner.CurrentTarget = entry.Target;

            Log.AI(this.Owner?.ToString(), "Enemy spotted! Attacking " + entry.Target.Name + entry.Target.UniqueID);

            bUpdatePath = true;
            Path        = null;

            MoveOrAttack(entry.Target);
        }
Exemplo n.º 2
0
        private void MoveOrAttack(Entity ent)
        {
            float offx = ent.X - this.Owner.X;
            float offy = ent.Y - this.Owner.Y;

            double sqr_dist = (offx * offx + offy * offy);

            double sqr_meleerange = ent.AttackRange * ent.AttackRange;

            if (sqr_dist < sqr_meleerange)
            {
                // Target is in range -> Perform an attack
                if (this.Owner.PerformAttack(ent))
                {
                    if (this.Owner is Unit)
                    {
                        Unit unit = (Unit)this.Owner;
                        unit.Orientation = Unit.OrientationFromDiff(offx, offy);
                        unit.Sprite.CurrentAnimation.Reset();
                    }
                }
            }
            else
            {
                // Target is out of range -> Move towards it

                // If no path has been calculated yet or if the target has moved, calculate new path
                if (Path == null || ent.TileX != targetX || ent.TileY != targetY)
                {
                    targetX = ent.TileX;
                    targetY = ent.TileY;
                    Path    = Owner.CurrentMap.CalcPath(Owner.TileX, Owner.TileY, targetX, targetY);
                    if (Path == null)
                    {
                        return;
                    }
                    curNodeIdx = 0;
                }

                if (curNodeIdx < Path.Count)
                {
                    IMapPathNode node = Path[curNodeIdx++];
                    // TODO!!! Move, not Set
                    Owner.SetPosition(node.X, node.Y);
                }
                else
                {
                    Log.Error("Error calculating path");
                }
            }
        }
Exemplo n.º 3
0
        internal override void Update(GameTime gameTime)
        {
            moveTimer -= gameTime.ElapsedGameTime.TotalSeconds;
            if (moveTimer > 0)
            {
                float scale = (float)(1.0f - (moveTimer / walkDistance));

                movePosX = scale * (targetPosX - startPosX);
                movePosY = scale * (targetPosY - startPosY);

                Owner.SetPosition(startPosX + movePosX, startPosY + movePosY);
                return;
            }

            double remainingDiff = -moveTimer;

            Owner.SetPosition(targetPosX, targetPosY);

            walkDistance = Owner.WalkSpeed + remainingDiff;
            moveTimer    = walkDistance;

            startPosX = Owner.X;
            startPosY = Owner.Y;

            movePosX = startPosX;
            movePosY = startPosY;

            if (curNodeIdx == -1 || Path == null)
            {
                return;
            }

            if (curNodeIdx >= Path.Count - 1)
            {
                Owner.SetPosition(targetPosX, targetPosY);
                this.Owner.Idle();
                return;
            }

            IMapPathNode node = Path[curNodeIdx++];

            targetPosX = node.X;
            targetPosY = node.Y;

            if (Owner is Unit)
            {
                Unit unit = (Unit)Owner;
                unit.Orientation = Unit.OrientationFromDiff((targetPosX - startPosX), (targetPosY - startPosY));
            }
        }
Exemplo n.º 4
0
        private void MoveOrAttack(Entity ent)
        {
            float offx = this.Owner.X - ent.X;
            float offy = this.Owner.Y - ent.Y;

            double sqr_dist = (offx * offx + offy * offy);

            double sqr_meleerange = ent.AttackRange * ent.AttackRange;

            if (sqr_dist < sqr_meleerange)
            {
                if (this.Owner.CanAttack && ent.ShouldBeAttacked)
                {
                    // Target is in range -> Perform an attack
                    this.Owner.PerformAttack(ent);
                }
            }
            else
            {
                // Target is out of range -> Move towards it

                Log.AI(this.Owner?.ToString(), "Target is out of range ... moving towards it!");

                // If no path has been calculated yet or if the target has moved, calculate new path
                if (TargetPath == null || ent.X != targetX || ent.Y != targetY)
                {
                    targetX    = ent.TileX;
                    targetY    = ent.TileY;
                    TargetPath = Owner.CurrentMap.CalcPath(Owner.TileX, Owner.TileY, targetX, targetY);
                    if (TargetPath == null)
                    {
                        return;
                    }
                    curNodeIdx = 0;
                }

                IMapPathNode node = TargetPath[curNodeIdx++];
                // TODO!!! Move, not Set
                Owner.SetPosition(node.X, node.Y);
            }
        }
Exemplo n.º 5
0
        internal override bool Enter()
        {
            moveTimer    = Owner.WalkSpeed;
            walkDistance = Owner.WalkSpeed;
            curNodeIdx   = -1;
            Path         = Owner.CurrentMap.CalcPath(Owner.TileX, Owner.TileY, targetTileX, targetTileY);

            startPosX = Owner.X;
            startPosY = Owner.Y;

            if (Path != null)
            {
                curNodeIdx = 0;

                IMapPathNode initialNode = Path[curNodeIdx++];
                if (initialNode == null)
                {
                    return(false);
                }

                targetPosX = initialNode.X;
                targetPosY = initialNode.Y;

                if (Owner is Unit)
                {
                    Unit unit = (Unit)Owner;
                    unit.Sprite.SetCurrentAnimationByName("Walk");
                    unit.Orientation = Unit.OrientationFromDiff((targetPosX - startPosX), (targetPosY - startPosY));
                }
            }
            else
            {
                return(false);
            }

            return(true);
        }