Example #1
0
        void completeBuildQueueItem(BuildQueueItem item)
        {
            BuildUnitButtonType buttonType = item.Type as BuildUnitButtonType;
            if (buttonType != null)
            {
                Unit unit;
                if (buttonType.UnitType == UnitType.MeleeNublet)
                    unit = new MeleeNublet(new Vector2(), Team, item.ID);
                else if (buttonType.UnitType == UnitType.RangedNublet)
                    unit = new RangedNublet(new Vector2(), Team, item.ID);
                else
                    unit = new WorkerNublet(new Vector2(), Team, item.ID);

                float angle = 0;

                Vector2 spawnLocation;
                if (rallyPoints.Count > 0)
                {
                    angle = (float)Math.Atan2(rallyPoints[0].Point.Y - CenterPoint.Y, rallyPoints[0].Point.X - CenterPoint.X);
                    angle = Util.ConvertToPositiveRadians(angle);

                    /*if (angle < MathHelper.TwoPi / 4)
                        spawnLocation = new Vector2(Rectangle.X + Rectangle.Width - map.TileSize, Rectangle.Y + Rectangle.Height - map.TileSize / 2);
                    else if (angle < MathHelper.TwoPi / 2)
                        spawnLocation = new Vector2(Rectangle.X + map.TileSize, Rectangle.Y + Rectangle.Height - map.TileSize / 2);
                    else if (angle < MathHelper.TwoPi * .75f)
                        spawnLocation = new Vector2(Rectangle.X + map.TileSize, Rectangle.Y + map.TileSize / 2);
                    else
                        spawnLocation = new Vector2(Rectangle.X + Rectangle.Width - map.TileSize, Rectangle.Y + map.TileSize / 2);*/

                    PathNode closestPathNode = null;
                    float closest = float.MaxValue;

                    foreach (PathNode pathNode in exitPathNodes)
                    {
                        float distance = Vector2.Distance(pathNode.Tile.CenterPoint, RallyPoints[0].Point);
                        if (distance < closest)
                        {
                            closestPathNode = pathNode;
                            closest = distance;
                        }
                    }

                    if (closestPathNode != null)
                        spawnLocation = closestPathNode.Tile.CenterPoint;
                    else if (exitPathNodes.Count > 0)
                        spawnLocation = exitPathNodes[0].Tile.CenterPoint;
                    else
                        spawnLocation = new Vector2(Rectangle.X + Rts.map.TileSize, Rectangle.Y + Rectangle.Height - Rts.map.TileSize / 2);
                }
                else
                {
                    spawnLocation = new Vector2(Rectangle.X + Rts.map.TileSize, Rectangle.Y + Rectangle.Height - Rts.map.TileSize / 2);
                }

                unit.CenterPoint = new Vector2(spawnLocation.X, spawnLocation.Y);
                unit.Rotation = angle;
                unit.InitializeCurrentPathNode();

                if (rallyPoints.Count == 0)
                {
                    unit.CheckForWallHit();
                    unit.CheckForPush();
                }
                else
                {
                    MoveCommand command = null;

                    if (rallyPoints[0].Resource != null && unit is WorkerNublet)
                        command = new HarvestCommand(unit, rallyPoints[0].Resource);
                    else
                        command = new MoveCommand(unit, RallyPoints[0].Point);

                    if (command != null)
                    {
                        unit.GiveCommand(command);
                        Rts.pathFinder.AddPathFindRequest(command, false, false, false);
                    }

                    for (int i = 1; i < RallyPoints.Count; i++)
                    {
                        if (rallyPoints[i].Resource != null && unit is WorkerNublet)
                            unit.QueueCommand(new HarvestCommand(unit, rallyPoints[i].Resource));
                        else
                            unit.QueueCommand(new MoveCommand(unit, RallyPoints[i].Point));
                    }

                    unit.CheckForWallHit();
                }
            }
        }
Example #2
0
        void completeBuildQueueItem(BuildQueueItem item)
        {
            BuildUnitButtonType buttonType = item.Type as BuildUnitButtonType;

            if (buttonType != null)
            {
                Unit unit;
                if (buttonType.UnitType == UnitType.MeleeNublet)
                {
                    unit = new MeleeNublet(new Vector2(), Team, item.ID);
                }
                else if (buttonType.UnitType == UnitType.RangedNublet)
                {
                    unit = new RangedNublet(new Vector2(), Team, item.ID);
                }
                else
                {
                    unit = new WorkerNublet(new Vector2(), Team, item.ID);
                }

                float angle = 0;

                Vector2 spawnLocation;
                if (rallyPoints.Count > 0)
                {
                    angle = (float)Math.Atan2(rallyPoints[0].Point.Y - CenterPoint.Y, rallyPoints[0].Point.X - CenterPoint.X);
                    angle = Util.ConvertToPositiveRadians(angle);

                    /*if (angle < MathHelper.TwoPi / 4)
                     *  spawnLocation = new Vector2(Rectangle.X + Rectangle.Width - map.TileSize, Rectangle.Y + Rectangle.Height - map.TileSize / 2);
                     * else if (angle < MathHelper.TwoPi / 2)
                     *  spawnLocation = new Vector2(Rectangle.X + map.TileSize, Rectangle.Y + Rectangle.Height - map.TileSize / 2);
                     * else if (angle < MathHelper.TwoPi * .75f)
                     *  spawnLocation = new Vector2(Rectangle.X + map.TileSize, Rectangle.Y + map.TileSize / 2);
                     * else
                     *  spawnLocation = new Vector2(Rectangle.X + Rectangle.Width - map.TileSize, Rectangle.Y + map.TileSize / 2);*/

                    PathNode closestPathNode = null;
                    float    closest         = float.MaxValue;

                    foreach (PathNode pathNode in exitPathNodes)
                    {
                        float distance = Vector2.Distance(pathNode.Tile.CenterPoint, RallyPoints[0].Point);
                        if (distance < closest)
                        {
                            closestPathNode = pathNode;
                            closest         = distance;
                        }
                    }

                    if (closestPathNode != null)
                    {
                        spawnLocation = closestPathNode.Tile.CenterPoint;
                    }
                    else if (exitPathNodes.Count > 0)
                    {
                        spawnLocation = exitPathNodes[0].Tile.CenterPoint;
                    }
                    else
                    {
                        spawnLocation = new Vector2(Rectangle.X + Rts.map.TileSize, Rectangle.Y + Rectangle.Height - Rts.map.TileSize / 2);
                    }
                }
                else
                {
                    spawnLocation = new Vector2(Rectangle.X + Rts.map.TileSize, Rectangle.Y + Rectangle.Height - Rts.map.TileSize / 2);
                }

                unit.CenterPoint = new Vector2(spawnLocation.X, spawnLocation.Y);
                unit.Rotation    = angle;
                unit.InitializeCurrentPathNode();

                if (rallyPoints.Count == 0)
                {
                    unit.CheckForWallHit();
                    unit.CheckForPush();
                }
                else
                {
                    MoveCommand command = null;

                    if (rallyPoints[0].Resource != null && unit is WorkerNublet)
                    {
                        command = new HarvestCommand(unit, rallyPoints[0].Resource);
                    }
                    else
                    {
                        command = new MoveCommand(unit, RallyPoints[0].Point);
                    }

                    if (command != null)
                    {
                        unit.GiveCommand(command);
                        Rts.pathFinder.AddPathFindRequest(command, false, false, false);
                    }

                    for (int i = 1; i < RallyPoints.Count; i++)
                    {
                        if (rallyPoints[i].Resource != null && unit is WorkerNublet)
                        {
                            unit.QueueCommand(new HarvestCommand(unit, rallyPoints[i].Resource));
                        }
                        else
                        {
                            unit.QueueCommand(new MoveCommand(unit, RallyPoints[i].Point));
                        }
                    }

                    unit.CheckForWallHit();
                }
            }
        }
Example #3
0
File: Rts.cs Project: nubington/rts
        public override void Update(GameTime gameTime)
        {
            // check for exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                Game1.Game.Exit();
            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape))
            {
                //Graphics.ToggleFullScreen();
                cleanup();
                returnControl("exit");
                return;
            }

            // mute check
            checkForMute();

            // pause check
            if (Keyboard.GetState(PlayerIndex.One).IsKeyUp(Keys.P))
                allowPause = true;
            if (allowPause && Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.P))
            {
                paused ^= true;
                allowPause = false;
                if (paused)
                {
                    MediaPlayer.Volume /= 4;
                    GameTimer.Stop();
                }
                else
                {
                    MediaPlayer.Volume *= 4;
                    GameTimer.Start();
                }
            }

            // tell pathfinder to start calculating
            Unit.PathFinder.Go = true;

            // update mouse and keyboard state
            mouseState = Mouse.GetState();
            keyboardState = Keyboard.GetState();

            // pathfinding performance info
            timeForPathFindingProfiling += gameTime.ElapsedGameTime.TotalMilliseconds;
            if (timeForPathFindingProfiling >= 500)
            {
                double pathFindingTime;
                lock (Unit.PathFinder.TimeSpentPathFindingLock)
                {
                    pathFindingTime = Unit.PathFinder.TimeSpentPathFinding.TotalMilliseconds;
                    Unit.PathFinder.TimeSpentPathFinding = TimeSpan.Zero;
                }
                pathFindingPercentage = pathFindingTime / timeForPathFindingProfiling * 100;
                timeForPathFindingProfiling = 0;

                lock (PathFindRequest.HighPriorityPathFindRequests)
                {
                    pathFindQueueSize = PathFindRequest.HighPriorityPathFindRequests.Count;
                }
            }

            // do nothing else if paused
            if (paused)
                return;

            //update fps
            fpsElapsedTime += gameTime.ElapsedGameTime;
            if (fpsElapsedTime > TimeSpan.FromSeconds(1))
            {
                //Game1.Game.Window.Title = "FPS: " + (frameCounter > 2 ? frameCounter.ToString() : "COOL");
                fpsMessage = "FPS: " + (frameCounter > 2 ? frameCounter.ToString() : "COOL");
                fpsMessage += " - Unit count: " + Unit.Units.Count;
                fpsElapsedTime -= TimeSpan.FromSeconds(1);
                frameCounter = 0;
            }

            // update buttons
            SimpleButton.UpdateAll(mouseState, keyboardState);

            if (button1.Rectangle.Contains(mouseState.X, mouseState.Y) && mouseState.LeftButton == ButtonState.Pressed)
            {
                Unit brownGuy;
                brownGuy = new RangedNublet(new Vector2(worldViewport.Width * .25f, worldViewport.Height / 2), 1);
                //brownGuy.Texture = brownGuyTexture;
                //brownGuy.AddWayPoint(new Vector2(Graphics.GraphicsDevice.Viewport.Width * .75f, Graphics.GraphicsDevice.Viewport.Height / 2));
                brownGuy.GiveCommand(new MoveCommand(new Vector2(worldViewport.Width * .75f, worldViewport.Height / 2), 1));
            }
            else if (button2.Rectangle.Contains(mouseState.X, mouseState.Y) && mouseState.LeftButton == ButtonState.Pressed)
            {
                for (int i = 0; i < 10; i++)
                {
                    Unit brownGuy;
                    brownGuy = new RangedNublet(new Vector2(worldViewport.Width * .25f, worldViewport.Height / 2), 1);
                    //brownGuy.Texture = brownGuyTexture;
                    //brownGuy.AddWayPoint(new Vector2(Graphics.GraphicsDevice.Viewport.Width * .75f, Graphics.GraphicsDevice.Viewport.Height / 2));
                    brownGuy.GiveCommand(new MoveCommand(new Vector2(worldViewport.Width * .75f, worldViewport.Height / 2), 1));
                }
            }
            else if (button3.Rectangle.Contains(mouseState.X, mouseState.Y) && mouseState.LeftButton == ButtonState.Pressed)
            {
                Graphics.ToggleFullScreen();
                uiViewport = GraphicsDevice.Viewport;
                worldViewport = GraphicsDevice.Viewport;
                worldViewport.Height -= (minimapSize + minimapBorderSize * 2);
                GraphicsDevice.Viewport = worldViewport;
                initializeMapTexture();
                initializeCommandCardArea();
                initializeSelectionInfoArea();
            }
            else if (button4.Rectangle.Contains(mouseState.X, mouseState.Y) && mouseState.LeftButton == ButtonState.Pressed)
            {
                Unit meleeNublet;
                meleeNublet = new MeleeNublet(new Vector2(worldViewport.Width * .25f, worldViewport.Height / 2), 1);
                //brownGuy.Texture = brownGuyTexture;
                //brownGuy.AddWayPoint(new Vector2(Graphics.GraphicsDevice.Viewport.Width * .75f, Graphics.GraphicsDevice.Viewport.Height / 2));
                meleeNublet.GiveCommand(new MoveCommand(new Vector2(worldViewport.Width * .75f, worldViewport.Height / 2), 1));
            }
            else if (button5.Rectangle.Contains(mouseState.X, mouseState.Y) && mouseState.LeftButton == ButtonState.Pressed)
            {
                foreach (Unit unit in Unit.Units)
                {
                    unit.Die();
                }
            }

            /*if (SelectedUnits.Count == 1)
            {
                Game1.Game.Window.Title = "idle: " + SelectedUnits[0].IsIdle + " hp: " + SelectedUnits[0].Hp + "/" + SelectedUnits[0].MaxHp + ". position " + SelectedUnits[0].CenterPoint;
            }*/

            checkForShift(gameTime);

            checkForCommands();

            Shrinker.UpdateShrinkers(gameTime);

            checkHotKeyGroups(gameTime);

            SelectBox.Update(worldViewport, camera);

            checkForLeftClick(gameTime);

            checkForRightClick();

            checkForStop();
            checkForTab();

            RtsBullet.UpdateAll(gameTime);

            Unit.UpdateUnits(gameTime);
            UnitAnimation.UpdateAll();

            removeDeadUnitsFromSelections();

            checkForMouseCameraScroll(gameTime);
            checkForCameraZoom(gameTime);
            checkForCameraRotate(gameTime);
            if (keyboardState.IsKeyDown(Keys.Space))
                centerCameraOnSelectedUnits();
            clampCameraToMap();
        }