Esempio n. 1
0
        void performAttackIfStarted(AttackCommand command)
        {
            if (attackStarted)
            {
                if (timeSinceLastAttack >= initialAttackDelay)
                {
                    attackStarted = false;

                    RtsBullet b = new RtsBullet(this, command.Target, centerPoint, 5, 150);
                    b.Texture = BulletTexture;
                }
            }
        }
Esempio n. 2
0
        // when attacking
        void checkForPush(AttackCommand command)
        {
            lock (PotentialCollisionsLock)
            {
                foreach (Unit unit in PotentialCollisions)
                {
                    if (Intersects(unit))
                    {
                        float angle = (float)Math.Atan2(unit.centerPoint.Y - centerPoint.Y, unit.centerPoint.X - centerPoint.X);

                        float distance = Radius + unit.Radius;
                        float force    = distance - Vector2.Distance(unit.centerPoint, centerPoint);

                        if (unit == command.Target)
                        {
                            PushSimple(angle + (float)Math.PI, force);
                        }
                        //else if (unit.IsAttacking && ((AttackCommand)unit.Commands[0]).Target == command.Target)
                        //{
                        //    PushSimple(angle + (float)Math.PI, force);
                        //}
                        else if (unit.IsAttacking && unit.isWithinRangeOfTarget)
                        {
                            PushSimple(angle + (float)Math.PI, force);

                            avoidingUnits         = true;
                            isWithinRangeOfTarget = true;
                        }
                        else
                        {
                            //pushCount++;
                            unit.Push(this, angle, force * .1f);
                            PushSimple(angle + (float)Math.PI, force * .9f);
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        // when attacking
        void checkForPush(AttackCommand command)
        {
            lock (PotentialCollisionsLock)
            {
                foreach (Unit unit in PotentialCollisions)
                {
                    if (Intersects(unit))
                    {
                        float angle = (float)Math.Atan2(unit.centerPoint.Y - centerPoint.Y, unit.centerPoint.X - centerPoint.X);

                        float distance = Radius + unit.Radius;
                        float force = distance - Vector2.Distance(unit.centerPoint, centerPoint);

                        if (unit == command.Target)
                        {
                            PushSimple(angle + (float)Math.PI, force);
                        }
                        //else if (unit.IsAttacking && ((AttackCommand)unit.Commands[0]).Target == command.Target)
                        //{
                        //    PushSimple(angle + (float)Math.PI, force);
                        //}
                        else if (unit.IsAttacking && unit.isWithinRangeOfTarget)
                        {
                            PushSimple(angle + (float)Math.PI, force);

                            avoidingUnits = true;
                            isWithinRangeOfTarget = true;
                        }
                        else
                        {
                            //pushCount++;
                            unit.Push(this, angle, force * .1f);
                            PushSimple(angle + (float)Math.PI, force * .9f);
                        }
                    }
                }
            }
        }
Esempio n. 4
0
        void Attack(AttackCommand command, GameTime gameTime)
        {
            avoidingUnits = false;
            isWithinRangeOfTarget = false;

            if (command.Target.IsDead)
            {
                nextCommand();
                return;
            }

            clearPushStatus();
            clearHitWallStatus();

            //float angle = (float)Math.Atan2(command.Target.CenterPoint.Y - centerPoint.Y, command.Target.CenterPoint.X - centerPoint.X);

            float distanceToTarget = Vector2.Distance(centerPoint, command.Target.CenterPoint) - (Radius + command.Target.Radius);

            if (distanceToTarget <= attackRange)
            {
                isWithinRangeOfTarget = true;

                // begin attack animation
                if (timeSinceLastAttack >= attackDelay)
                {
                    timeSinceLastAttack = 0;
                    attackStarted = true;
                }

                turnTowards(command.Target.CenterPoint, 120 / Radius, gameTime);
            }
            else if (!attackStarted)
            {
                float moveX = Util.ScaleWithGameTime(speed.X, gameTime);
                float moveY = Util.ScaleWithGameTime(speed.Y, gameTime);

                Vector2 wayPoint = command.WayPoints[0];

                Vector2 difference = wayPoint - centerPoint;
                if (Math.Abs(difference.X) < moveX && Math.Abs(difference.Y) < moveY)
                {
                    this.CenterPoint = wayPoint;

                    lastWayPoint = wayPoint;
                    if (command.WayPoints.Count > 1)
                        command.NextWayPoint(this, PathFinder);
                    return;
                }

                float angle = (float)Math.Atan2(wayPoint.Y - centerPoint.Y, wayPoint.X - centerPoint.X);
                moveX *= (float)Math.Cos(angle);
                moveY *= (float)Math.Sin(angle);

                lastMove.X = moveX;
                lastMove.Y = moveY;
                PrecisePosition += lastMove;

                checkForWallHit();
                // checkForPush sets avoidingUnits
                checkForPush(command);

                command.Destination = command.Target.CenterPoint;

                turnTowards(wayPoint, 120 / Radius, gameTime);
            }

            if (avoidingUnits)
            {
                if (instanceFrameCount % (reCalculatePathFrameDelay * 2) == 0)
                    //command.WayPoints = PathFinder.FindPath(CurrentPathNode, command.Target.CenterPoint, true);
                    PathFinder.AddPathFindRequest(this, command, CurrentPathNode, true);
            }
            else if (instanceFrameCount % reCalculatePathFrameDelay == 0)
                //command.WayPoints = PathFinder.FindPath(CurrentPathNode, command.Target.CenterPoint, false);
                PathFinder.AddPathFindRequest(this, command, CurrentPathNode, false);
        }
Esempio n. 5
0
        void performAttackIfStarted(AttackCommand command)
        {
            if (attackStarted)
            {
                if (timeSinceLastAttack >= initialAttackDelay)
                {
                    attackStarted = false;

                    RtsBullet b = new RtsBullet(this, command.Target, centerPoint, 5, 150);
                    b.Texture = BulletTexture;
                }
            }
        }
Esempio n. 6
0
        void giveAttackCommand(Vector2 mousePosition)
        {
            foreach (Unit unit in Unit.Units)
            {
                if (unit.Contains(mousePosition))
                {
                    UnitAnimation a = new UnitAnimation(unit, unit.Width, .75f, 8, false, redCircleTexture, transparentTexture);
                    a.Start();

                    foreach (Unit u in SelectedUnits)
                    {
                        if (u != unit)
                        {
                            if (keyboardState.IsKeyUp(Keys.LeftShift))
                            {
                                AttackCommand command = new AttackCommand(unit);
                                u.GiveCommand(command);
                                Unit.PathFinder.AddPathFindRequest(u, command, u.CurrentPathNode, false);
                                //u.GiveCommand(new AttackCommand(unit));
                            }
                            else
                                u.QueueCommand(new AttackCommand(unit));
                        }
                    }
                    return;
                }
            }

            giveMoveCommand(mousePosition);
        }
Esempio n. 7
0
        void Attack(AttackCommand command, GameTime gameTime)
        {
            avoidingUnits         = false;
            isWithinRangeOfTarget = false;

            if (command.Target.IsDead)
            {
                nextCommand();
                return;
            }

            clearPushStatus();
            clearHitWallStatus();

            //float angle = (float)Math.Atan2(command.Target.CenterPoint.Y - centerPoint.Y, command.Target.CenterPoint.X - centerPoint.X);

            float distanceToTarget = Vector2.Distance(centerPoint, command.Target.CenterPoint) - (Radius + command.Target.Radius);

            if (distanceToTarget <= attackRange)
            {
                isWithinRangeOfTarget = true;

                // begin attack animation
                if (timeSinceLastAttack >= attackDelay)
                {
                    timeSinceLastAttack = 0;
                    attackStarted       = true;
                }

                turnTowards(command.Target.CenterPoint, 120 / Radius, gameTime);
            }
            else if (!attackStarted)
            {
                float moveX = Util.ScaleWithGameTime(speed.X, gameTime);
                float moveY = Util.ScaleWithGameTime(speed.Y, gameTime);

                Vector2 wayPoint = command.WayPoints[0];

                Vector2 difference = wayPoint - centerPoint;
                if (Math.Abs(difference.X) < moveX && Math.Abs(difference.Y) < moveY)
                {
                    this.CenterPoint = wayPoint;

                    lastWayPoint = wayPoint;
                    if (command.WayPoints.Count > 1)
                    {
                        command.NextWayPoint(this, PathFinder);
                    }
                    return;
                }

                float angle = (float)Math.Atan2(wayPoint.Y - centerPoint.Y, wayPoint.X - centerPoint.X);
                moveX *= (float)Math.Cos(angle);
                moveY *= (float)Math.Sin(angle);

                lastMove.X       = moveX;
                lastMove.Y       = moveY;
                PrecisePosition += lastMove;

                checkForWallHit();
                // checkForPush sets avoidingUnits
                checkForPush(command);

                command.Destination = command.Target.CenterPoint;

                turnTowards(wayPoint, 120 / Radius, gameTime);
            }

            if (avoidingUnits)
            {
                if (instanceFrameCount % (reCalculatePathFrameDelay * 2) == 0)
                {
                    //command.WayPoints = PathFinder.FindPath(CurrentPathNode, command.Target.CenterPoint, true);
                    PathFinder.AddPathFindRequest(this, command, CurrentPathNode, true);
                }
            }
            else if (instanceFrameCount % reCalculatePathFrameDelay == 0)
            {
                //command.WayPoints = PathFinder.FindPath(CurrentPathNode, command.Target.CenterPoint, false);
                PathFinder.AddPathFindRequest(this, command, CurrentPathNode, false);
            }
        }
Esempio n. 8
0
        public void Update(GameTime gameTime)
        {
            instanceFrameCount++;
            updateCurrentPathNode();
            timeSinceLastAttack += (int)gameTime.ElapsedGameTime.TotalMilliseconds;

            if (Commands.Count == 0)
            {
                return;
            }

            UnitCommand command = Commands[0];

            if (command is AttackCommand)
            {
                AttackCommand attackCommand = (AttackCommand)command;

                if (attackCommand.Target.IsDead)
                {
                    nextCommand();
                }
                else
                {
                    Attack(attackCommand, gameTime);
                    performAttackIfStarted(attackCommand);
                }
            }
            else if (command is MoveCommand)
            {
                MoveCommand moveCommand = (MoveCommand)command;
                if (instanceFrameCount % reCalculatePathFrameDelay == 0)
                {
                    //moveCommand.WayPoints = PathFinder.FindPath(CurrentPathNode, moveCommand.Destination, false);
                    //PathFinder.SmoothPath(moveCommand.WayPoints, this);
                    PathFinder.AddPathFindRequest(this, moveCommand, CurrentPathNode, false);
                }
                //if (instanceFrameCount % reCalculatePathFrameDelay == 1)
                //    PathFinder.SmoothPath(moveCommand.WayPoints, this);
                Move(moveCommand, gameTime);
            }

            // update attack command destinations
            for (int i = 0; i < Commands.Count; i++)
            {
                AttackCommand c = Commands[i] as AttackCommand;
                if (c != null)
                {
                    if (c.Target.IsDead)
                    {
                        Commands.Remove(c);
                        i--;
                    }
                    else
                    {
                        c.Destination = c.Target.CenterPoint;
                    }
                }
            }

            // update queued command starting points
            for (int i = 1; i < Commands.Count; i++)
            {
                MoveCommand c        = Commands[i] as MoveCommand;
                MoveCommand previous = Commands[i - 1] as MoveCommand;
                if (c != null && previous != null)
                {
                    c.WayPoints[0] = previous.Destination;
                }
            }

            // recalculate queued paths

            /*if (command is MoveCommand && instanceFrameCount % (reCalculatePathFrameDelay) == 0)
             * {
             *  MoveCommand moveCommand = (MoveCommand)command;
             *  for (int i = 1; i < Commands.Count; i++)
             *  {
             *      MoveCommand c = Commands[i] as MoveCommand;
             *      MoveCommand previousCommand = Commands[i - 1] as MoveCommand;
             *      if (c != null && previousCommand != null)
             *      {
             *          int y = (int)MathHelper.Clamp(previousCommand.Destination.Y / Map.TileSize, 0, Map.Height - 1);
             *          int x = (int)MathHelper.Clamp(previousCommand.Destination.X / Map.TileSize, 0, Map.Width - 1);
             *
             *          PathNode node = PathFinder.PathNodes[y, x];
             *          if (!node.Tile.Walkable)
             *              node = PathFinder.FindNearestPathNode(y, x);
             *
             *          c.WayPoints = PathFinder.FindPath(node, c.Destination, false);
             *      }
             *  }
             * }*/
        }