示例#1
0
 // updates blasts and removes them from list when size reached
 public static void UpdateShrinkers(GameTime gameTime)
 {
     for (int i = 0; i < Shrinkers.Count; i++)
     {
         Shrinker s = Shrinkers[i];
         if (s.Update(gameTime))
         {
             Shrinkers.Remove(s);
             i--;
         }
     }
 }
示例#2
0
文件: Rts.cs 项目: nubington/bill
        void giveMoveCommand(Vector2 mousePosition)
        {
            // create move command shrinker thing
            Shrinker moveCommandThing;
            if (Unit.PathFinder.IsPointWalkable(mousePosition))
                moveCommandThing = new Shrinker(mousePosition - new Vector2(moveCommandShrinkerSize / 2f, moveCommandShrinkerSize / 2f), moveCommandShrinkerSize, 10);
            else
                moveCommandThing = new Shrinker(map.FindNearestWalkableTile(mousePosition) - new Vector2(moveCommandShrinkerSize / 2f, moveCommandShrinkerSize / 2f), moveCommandShrinkerSize, 10);
            moveCommandThing.Texture = moveCommandTexture;

            // create magic box
            Rectangle magicBox = SelectedUnits[0];
            foreach (Unit unit in SelectedUnits)
                magicBox = Rectangle.Union(magicBox, unit.Rectangle);

            // box is too big or clicked inside magic box
            if (magicBox.Width > magicBoxMaxSize || magicBox.Height > magicBoxMaxSize ||
                magicBox.Contains((int)mousePosition.X, (int)mousePosition.Y))
            {
                bool isPointWalkable = Unit.PathFinder.IsPointWalkable(mousePosition);
                // assign move targets to mouse position
                foreach (Unit unit in SelectedUnits)
                {
                    // if mouse position is not in a walkable tile, find nearest walkable tile
                    Vector2 destinationPoint;
                    if (isPointWalkable)
                        destinationPoint = mousePosition;
                    else
                        destinationPoint = map.FindNearestWalkableTile(mousePosition);

                    // not holding shift
                    if (keyboardState.IsKeyUp(Keys.LeftShift))
                    {
                        //float distanceToDestination = Vector2.Distance(unit.CurrentPathNode.Tile.CenterPoint, destinationPoint);
                        //if (distanceToDestination <= unit.Diameter)
                        //unit.GiveCommand(new MoveCommand(destinationPoint, 1));
                        //else
                        MoveCommand command = new MoveCommand(destinationPoint, 1);
                        unit.GiveCommand(command);
                        Unit.PathFinder.AddPathFindRequest(unit, command, unit.CurrentPathNode, false);
                    }
                    // holding shift
                    else
                    {
                        //float distanceBetweenCurrentAndNewMoveTarget = Vector2.Distance(unit.FinalMoveDestination, destinationPoint);

                        //if (distanceBetweenCurrentAndNewMoveTarget <= unit.Diameter)
                        //    unit.QueueCommand(new MoveCommand(destinationPoint, 1));
                        //else
                        MoveCommand command = new MoveCommand(destinationPoint, 1);
                        unit.QueueCommand(command);
                        //Unit.PathFinder.AddPathFindRequest(unit, command, unit.CurrentPathNode);
                    }
                }
            }
            // clicked outside magic box
            else
            {
                // make destination box and keep in screen
                Rectangle destBox = magicBox;
                destBox.X = (int)mousePosition.X - destBox.Width / 2;
                destBox.Y = (int)mousePosition.Y - destBox.Height / 2;

                // calculate angle from magic box to destination box
                float angle = (float)Math.Atan2(destBox.Center.Y - magicBox.Center.Y, destBox.Center.X - magicBox.Center.X);
                float angleX = (float)Math.Cos(angle);
                float angleY = (float)Math.Sin(angle);
                float distance = Vector2.Distance(new Vector2(magicBox.Center.X, magicBox.Center.Y), new Vector2(destBox.Center.X, destBox.Center.Y));

                // assign move targets based on angle
                foreach (Unit unit in SelectedUnits)
                {
                    // if mouse position is not in a walkable tile, find nearest walkable tile
                    Vector2 destinationPoint = unit.CenterPoint + new Vector2(distance * angleX, distance * angleY);
                    if (!Unit.PathFinder.IsPointWalkable(destinationPoint))
                        destinationPoint = map.FindNearestWalkableTile(destinationPoint);

                    // not holding shift
                    if (keyboardState.IsKeyUp(Keys.LeftShift))
                    {
                        //float distanceToDestination = Vector2.Distance(unit.CurrentPathNode.Tile.CenterPoint, destinationPoint);
                        //if (distanceToDestination <= unit.Diameter)
                        //    unit.GiveCommand(new MoveCommand(destinationPoint, 1));
                        //else

                        MoveCommand command = new MoveCommand(destinationPoint, 1);
                        unit.GiveCommand(command);
                        Unit.PathFinder.AddPathFindRequest(unit, command, unit.CurrentPathNode, false);
                    }
                    // holding shift
                    else
                    {
                        //float distanceBetweenCurrentAndNewMoveTarget = Vector2.Distance(unit.FinalMoveDestination, destinationPoint);

                        //if (distanceBetweenCurrentAndNewMoveTarget <= unit.Diameter)
                        //    unit.QueueCommand(new MoveCommand(destinationPoint, 1));
                        //else
                        unit.QueueCommand(new MoveCommand(destinationPoint, 1));
                    }
                }
            }
        }