示例#1
0
文件: Game1.cs 项目: hckrieger/Simon
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            mouse = MouseExtended.GetState();

            for (int i = 0; i < squareSet.Count; i++)
            {
                squareSet[i].Update(gameTime);
            }

            //Switch between gamestates
            switch (state)
            {
            case GameState.Start:
                Start();
                break;

            case GameState.Watch:
                WatchStateLogic(gameTime);
                break;

            case GameState.Repeat:
                RepeatStateLogic();
                break;

            case GameState.GameOver:
                GameOver();
                break;
            }
            base.Update(gameTime);
        }
示例#2
0
        /// <summary>
        /// Update(GameTime)
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">(GameTime) - Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime); // Call built-in MonoGame Update method.

            if (!form.ContainsFocus)
            {
                return;
            }

            HandleViewportSizeChange();                               // Check for window size change and handle it.

            MouseStateExtended mouseState = MouseExtended.GetState(); // Get the current state of the mouse
            Point mousePos = mouseState.Position;                     // Get current position of the mouse
            // Get the position of the mouse in the world relative to the absolute screen position.
            Vector2 worldPos = camera.ScreenToWorld(mousePos.ToVector2());

            // Get the Tile and positional data associated with it (if any) at the mouse position in the world.
            TileLayer.TilePositionDetail tilePositionDetail = worldMap.GetTileAtPosition(worldPos, ActiveLayer);
            Tile tile = tilePositionDetail.Tile;

            // Handle User input
            #region Handle input
            // if right mouse button is clicked allow the camera to be dragged along with the mouse.
            if (mouseState.IsButtonDown(MouseButton.Right))
            {
                // move camera with respect to mouse postion and zoom level.
                camera.Move(mouseState.DeltaPosition.ToVector2() / camera.Zoom);
            }
            else if (mouseState.DeltaScrollWheelValue != 0) // if mouse wheel is scrolled, change zoom level
            {
                // change the camera zoom level based on scroll wheel direciton
                // clamped between min and max zoom levels.
                camera.Zoom = MathHelper.Clamp(
                    camera.Zoom - mouseState.DeltaScrollWheelValue * 0.001f,
                    camera.MinimumZoom,
                    camera.MaximumZoom
                    );
            }
            else if (mouseState.IsButtonDown(MouseButton.Left)) // if left mouse button clicked
            {
                if (tile != null && BrushTile != null)          // check for tile at the current position and if brush tile selected.
                {
                    // Set the Tile at the clicked position to the Brush Tile.
                    tile.TilesetIndex = BrushTile.TilesetIndex;
                    tile.TileIndex    = BrushTile.TileIndex;
                }
            }
            #endregion

            // If a brush Tile is selected and is a valid position. Show brush Tile at map position.
            if (BrushTile != null && tilePositionDetail.IsValidPosition)
            {
                // Add the Brush Tile to the Map's selected Tile for display at mouse position.
                worldMap.AddSelectedTile(
                    tilePositionDetail.Coordinates.X,
                    tilePositionDetail.Coordinates.Y,
                    BrushTile
                    );
            }
        }
示例#3
0
        public void Update(WpfKeyboard keyboard, WpfMouse mouse)
        {
            PreviousKeyboardState = CurrentKeyboardState;
            CurrentKeyboardState  = keyboard.GetState();
            KeyboardStateExtended = new KeyboardStateExtended(CurrentKeyboardState, PreviousKeyboardState);

            PreviousMouseState = CurrentMouseState;
            CurrentMouseState  = mouse.GetState();
            MouseStateExtended = new MouseStateExtended(CurrentMouseState, PreviousMouseState);
        }
示例#4
0
        public override void Update(GameTime gameTime)
        {
            MouseStateExtended mouseState = MouseExtended.GetState();
            float elapsedSeconds          = gameTime.GetElapsedSeconds();

            foreach (Player player in _map.Players)
            {
                player.Position = Vector2.Lerp(player.Position, player.TargetPosition, 0.05f);
            }

            if (mouseState.WasButtonJustDown(MouseButton.Left))
            {
                Click?.Invoke(this, mouseState.Position.ToVector2());
            }
        }
        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            if (!form.ContainsFocus)
            {
                return;
            }

            HandleViewportSizeChange();

            MouseStateExtended    mouseState    = MouseExtended.GetState();
            KeyboardStateExtended keyboardState = KeyboardExtended.GetState();
            Point   mousePosition = mouseState.Position;
            Vector2 worldPosition = camera.ScreenToWorld(mousePosition.ToVector2());

            TileLayer.TilePositionDetail      tilePositionDetail = myMap.GetTileAtPosition(worldPosition, ActiveLayer);
            CollisionLayer.CellPositionDetail cellPositionDetail = myMap.GetCellAtPosition(worldPosition);
            Tile tile = tilePositionDetail.Tile;

            if (mouseState.IsButtonDown(MouseButton.Right))
            {
                camera.Move(mouseState.DeltaPosition.ToVector2() / camera.Zoom);
            }
            else if (mouseState.DeltaScrollWheelValue != 0)
            {
                camera.Zoom = MathHelper.Clamp(camera.Zoom - mouseState.DeltaScrollWheelValue * 0.001f, camera.MinimumZoom, camera.MaximumZoom);
            }

            if (ActivePaintingTool != null)
            {
                if (ActivePaintingTool.IsValidPosition(myMap, keyboardState, tilePositionDetail, cellPositionDetail))
                {
                    if (mouseState.IsButtonDown(MouseButton.Left))
                    {
                        ActivePaintingTool.Paint(myMap, keyboardState, tilePositionDetail, cellPositionDetail);
                    }
                    else
                    {
                        ActivePaintingTool.Hover(myMap, keyboardState, tilePositionDetail, cellPositionDetail);
                    }
                }
            }
        }
示例#6
0
        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            if (!form.ContainsFocus)
            {
                return;
            }

            HandleViewportSizeChange();

            MouseStateExtended mouseState = MouseExtended.GetState();
            Point   mousePosition         = mouseState.Position;
            Vector2 worldPosition         = camera.ScreenToWorld(mousePosition.ToVector2());

            TileLayer.TilePositionDetail tilePositionDetail = myMap.GetTileAtPosition(worldPosition, ActiveLayer);
            Tile tile = tilePositionDetail.Tile;

            if (mouseState.IsButtonDown(MouseButton.Right))
            {
                camera.Move(mouseState.DeltaPosition.ToVector2() / camera.Zoom);
            }
            else if (mouseState.DeltaScrollWheelValue != 0)
            {
                camera.Zoom = MathHelper.Clamp(camera.Zoom - mouseState.DeltaScrollWheelValue * 0.001f, camera.MinimumZoom, camera.MaximumZoom);
            }
            else if (mouseState.IsButtonDown(MouseButton.Left))
            {
                if (tile != null && BrushTile != null)
                {
                    tile.TilesetIndex = BrushTile.TilesetIndex;
                    tile.TileIndex    = BrushTile.TileIndex;
                }
            }

            if (BrushTile != null && tilePositionDetail.IsValidPosition)
            {
                myMap.AddImmediateTile(tilePositionDetail.Coordinates.X, tilePositionDetail.Coordinates.Y, BrushTile);
            }
        }
        public void Update(GameTime time, Entity entity)
        {
            var newState = MouseExtended.GetState();

            if (!CanHandleInput(newState))
            {
                mouseState = newState;
                return;
            }

            var position = entity.Get <Position>();
            var pointed  = GameContext.PointedEntity != null?GameContext.PointedEntity.Value.Get <Position>() : null;

            BaseAction after       = null;
            var        mapPosition = mouseState.MapPosition(GameContext.Camera);

            if (pointed != null)
            {
                after = GetAfterAction(entity, GameContext.PointedEntity.Value);
            }
            else if (!GameContext.Map.MovementGrid.IsWalkableAt(mapPosition.X, mapPosition.Y))
            {
                return;
            }

            if (GameContext.Map.PathFinder.TryGetPath(position, mapPosition, out var first, out var last, 2f))
            {
                if (after != null)
                {
                    var action = last ?? first;
                    action.Alternative = after;
                    action.Abort();
                }

                entity.Set <BaseAction>(first);
            }

            mouseState = newState;
        }
示例#8
0
 private void MovePaddlePlayer(MouseStateExtended mouseState)
 {
     _bluePaddle.Position.Y = mouseState.Position.Y;
 }
示例#9
0
 private void ProcessZoom(GameTime gameTime, Zoomable zoomable, MouseStateExtended mouseState)
 {
     zoomable.Zoom -= (float)gameTime.ElapsedGameTime.TotalSeconds * 0.015f * mouseState.DeltaScrollWheelValue;
 }
示例#10
0
 public void Update(WpfMouse mouse)
 {
     PreviousMouseState = CurrentMouseState;
     CurrentMouseState  = mouse.GetState();
     MouseStateExtended = new MouseStateExtended(CurrentMouseState, PreviousMouseState);
 }
示例#11
0
 private bool CanHandleInput(MouseStateExtended newState)
 {
     return(mouseState.LeftButton == ButtonState.Pressed && newState.LeftButton == ButtonState.Released &&
            GameContext.Hud.State == HUDState.Default &&
            !GameContext.Hud.IsMouseOnHud);
 }
示例#12
0
        public override void Update(GameTime gameTime)
        {
            var mouseState    = MouseExtended.GetState();
            var keyboardState = KeyboardExtended.GetState();

            var mousePosition = _camera.ScreenToWorld(mouseState.Position.ToVector2()).FromIsometric();

            (int, int)currentTile = mousePosition.MapFromCenterTilePointToTopRightTileIndex(_currentPlaceableUnitConfig?.TileSpan ?? (1, 1));

            // TODO: Introduce state machine :D
            if (keyboardState.IsKeyDown(Keys.D1))
            {
                MakeNewPlacementGuide(new MinerUnitConfig(), currentTile);
            }

            if (keyboardState.IsKeyDown(Keys.D2))
            {
                MakeNewPlacementGuide(new ConveyorBeltUnitConfig(), currentTile);
            }

            if (keyboardState.IsKeyDown(Keys.Escape) && _currentPlaceableEntity != null)
            {
                ClearPlacementGuide();
            }

            if (keyboardState.IsKeyDown(Keys.R) && _currentPlaceableEntity != null && !isRotating)
            {
                isRotating = true;
                var alignable = _currentPlaceableEntity.Get <Alignable>();
                if (alignable != null)
                {
                    alignable.Rotate();
                }
            }
            else if (keyboardState.IsKeyUp(Keys.R))
            {
                isRotating = false;
            }

            var currentTileOccupied = IsCurrentTileOccupied(currentTile);

            if (mouseState.LeftButton == ButtonState.Released && _previousMouseState.LeftButton == ButtonState.Pressed &&
                _currentPlaceableEntity != null && !currentTileOccupied)
            {
                var alignable = _currentPlaceableEntity.Get <Alignable>();
                _entityBuilder.BuildUnit(CreateEntity(), currentTile, _currentPlaceableUnitConfig, alignable.Direction);
            }

            foreach (var entityId in ActiveEntities)
            {
                var placeable    = _placeableMapper.Get(entityId);
                var tilePosition = _tilePositionMapper.Get(entityId);
                var tileBorder   = _tileBorderMapper.Get(entityId);

                if (placeable == null)
                {
                    continue;
                }

                if (tilePosition != null)
                {
                    tilePosition.ChangeTile(currentTile);
                }

                tileBorder.Color = currentTileOccupied ? Color.Red : Color.LimeGreen;
            }

            _previousMouseState = mouseState;
        }
示例#13
0
    public static Point MapPosition(this MouseStateExtended state, OrthographicCamera camera)
    {
        var worldPos = camera.ScreenToWorld(state.Position.X, state.Position.Y);

        return((worldPos / 32).ToPoint());
    }