Пример #1
0
        /// <summary>
        /// Handle selecting units and buildings by the user
        /// </summary>
        /// <param name="input"></param>
        /// <param name="testGameController"></param>
        /// <param name="gameView"></param>
        /// <param name="gamePlayMenu"></param>

        private void handleSelecting(MouseState input, Controller testGameController, ZRTS.View.ViewScreenConvert gameView, ViewGamePlayMenu gamePlayMenu)
        {
            /* Left click to select units */

            // Store the first corner of the "drag box"
            if (input.LeftButton == ButtonState.Pressed && prevInput.LeftButton == ButtonState.Released)
            {
                if (testGameController.isWithinGameBound(selectX, selectY))
                {
                    selectX = gameView.convertScreenLocToGameLoc(input.X, input.Y).X;
                    selectY = gameView.convertScreenLocToGameLoc(input.X, input.Y).Y;

                    //gameView.setFirstCornerOfDragBox(input.X, input.Y);
                    //gameView.IsDragging = true;
                }
            }

            // While dragging, update the view to draw the box
            if (input.LeftButton == ButtonState.Pressed)
            {
                //gameView.setDragBox(input.X, input.Y);
            }

            // "Drag box" is created, select all units within box
            if (input.LeftButton == ButtonState.Released && prevInput.LeftButton == ButtonState.Pressed)
            {
                float releaseX = gameView.convertScreenLocToGameLoc(input.X, input.Y).X; // coords of release location
                float releaseY = gameView.convertScreenLocToGameLoc(input.X, input.Y).Y;
                float pressX   = selectX;                                                // coords of press location
                float pressY   = selectY;

                if (testGameController.isWithinGameBound(releaseX, releaseY) && testGameController.isWithinGameBound(pressX, pressY))
                {
                    /*
                     * Retrieve all units within the drag box - Use Min and Max to find the topleft and
                     * bottomright corner
                     */
                    testGameController.scenario.selectUnits(
                        (int)Math.Min(pressX, releaseX),
                        (int)Math.Min(pressY, releaseY),
                        (int)(Math.Max(pressX, releaseX) - Math.Min(pressX, releaseX)),
                        (int)(Math.Max(pressY, releaseY) - Math.Min(pressY, releaseY))
                        );
                }

                //gameView.IsDragging = false;
                //gameView.resetDragBox();
            }
        }
Пример #2
0
        /// <summary>
        /// Give a command when click at icons
        /// </summary>
        /// <param name="input"></param>
        /// <param name="gameView"></param>
        /// <param name="testGameController"></param>
        private void giveCommand(MouseState input, ZRTS.View.ViewScreenConvert gameView, Controller testGameController)
        {
            commandX = gameView.convertScreenLocToGameLoc(input.X, input.Y).X;
            commandY = gameView.convertScreenLocToGameLoc(input.X, input.Y).Y;


            if (testGameController.isWithinGameBound(commandX, commandY))
            {
                foreach (ZRTSModel.Entities.Entity entity in testGameController.scenario.getPlayer().SelectedEntities)
                {
                    switch (currentPlayerCommand)
                    {
                    // Move command
                    case PlayerCommand.MOVE:
                        testGameController.giveActionCommand(entity,
                                                             new ZRTSLogic.Action.MoveAction(commandX, commandY, testGameController.gameWorld, entity));
                        break;


                    // Cancel command
                    case PlayerCommand.CANCEL:
                        // TODO: Cancel current action
                        break;

                    // Attack command
                    // TODO: Animation of the attack
                    case PlayerCommand.ATTACK:
                        if (entity.entityType == ZRTSModel.Entities.Entity.EntityType.Unit)
                        {
                            // Select Unit at the clicked Cell.
                            ZRTSModel.Entities.Entity temp = testGameController.scenario.getUnit((int)commandX, (int)commandY);

                            // If there is no Unit, select the StaticEntity on the Cell.
                            if (temp == null)
                            {
                                temp = testGameController.scenario.getGameWorld().map.getCell((int)commandX, (int)commandY).entity;
                            }

                            // If there is an Entity on the Cell.
                            if (temp != null)
                            {
                                System.Console.Out.WriteLine("Selected Attack at " + commandX + ":" + commandY);
                                testGameController.giveActionCommand(entity,
                                                                     new ZRTSLogic.Action.SimpleAttackAction((ZRTSModel.Entities.Unit)entity, temp));
                            }
                        }
                        break;

                    // Build command
                    case PlayerCommand.BUILD:
                        if (testGameController.makeUnitBuild(entity,
                                                             new ZRTSModel.Entities.Building(testGameController.scenario.getPlayer(), new ZRTSModel.Entities.BuildingStats()),
                                                             testGameController.gameWorld.map.getCell((int)commandX, (int)commandY)))
                        {
                            System.Console.Out.WriteLine("Building at " + commandX + ":" + commandY);
                        }
                        else
                        {
                            System.Console.Out.WriteLine("Can't place a building at " + commandX + ":" + commandY);
                        }
                        break;
                    }
                }
            }
        }