Exemple #1
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
                    );
            }
        }
Exemple #2
0
        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);
        }
Exemple #3
0
        protected override void Update(GameTime gameTime)
        {
            var keyboardState  = KeyboardExtended.GetState();
            var mouseState     = MouseExtended.GetState();
            var elapsedSeconds = gameTime.GetElapsedSeconds();

            if (keyboardState.IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            if (keyboardState.WasKeyJustDown(Keys.Space))
            {
                _tweener.CancelAll();
            }

            if (keyboardState.WasKeyJustDown(Keys.Tab))
            {
                _tweener.CancelAndCompleteAll();
            }

            if (mouseState.IsButtonDown(MouseButton.Left))
            {
                _tweener.TweenTo(this, a => a.Linear, mouseState.Position.ToVector2(), 1.0f)
                .Easing(EasingFunctions.QuadraticOut);
            }

            _tweener.Update(elapsedSeconds);

            base.Update(gameTime);
        }
        protected override void Update(GameTime gameTime, ReadOnlySpan <Entity> _)
        {
            if (GameContext.Hud.State != HUDState.Default || GameContext.Hud.IsMouseOnHud || !GameContext.Player.Has <AllowedToAct>())
            {
                return;
            }
            var map      = GameContext.Map;
            var state    = MouseExtended.GetState();
            var worldPos = GameContext.Camera.ScreenToWorld(state.Position.X, state.Position.Y);
            var point    = (worldPos / 32).ToPoint();

            position = point.ToVector2() * 32 + new Vector2(16);
            GameContext.PointedEntity = null;
            if (GameContext.GameState == GameState.Peace)
            {
                if (!map.MovementGrid.Contains(point))
                {
                    return;
                }


                if (!HandlePoint(worldPos))
                {
                    if (map.MovementGrid.IsWalkableAt(point.X, point.Y))
                    {
                        mark.Play("idle");
                    }
                    else
                    {
                        mark.Play("no");
                    }
                }
            }
            else
            {
                var possibleMoves = GameContext.Player.Get <PossibleMoves>();
                if (possibleMoves.Contains(point))
                {
                    if (!HandlePoint(worldPos))
                    {
                        mark.Play("idle");
                    }
                }
                else
                {
                    mark.Play("no");
                }
            }
            Draw();
            mark.Update(gameTime.ElapsedGameTime.Milliseconds);
        }
Exemple #5
0
        public override void Update(GameTime gameTime)
        {
            var mouseState    = MouseExtended.GetState();
            var keyboardState = KeyboardExtended.GetState();

            if (keyboardState.WasKeyJustDown(Keys.Escape))
            {
                Game.Exit();
            }

            if (mouseState.LeftButton == ButtonState.Pressed || keyboardState.WasAnyKeyJustDown())
            {
                ScreenManager.LoadScreen(new HippaGameScreen(Game), new FadeTransition(GraphicsDevice, Color.Black, 0.5f));
            }
        }
Exemple #6
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());
            }
        }
Exemple #7
0
        public override void Update(GameTime gameTime)
        {
            var elapsedSeconds = gameTime.GetElapsedSeconds();
            var mouseState     = MouseExtended.GetState();
            var keyboardState  = KeyboardExtended.GetState();

            if (keyboardState.WasKeyJustDown(Keys.Escape))
            {
                ScreenManager.LoadScreen(new TitleScreen(Game), new ExpandTransition(GraphicsDevice, Color.Black));
            }

            MovePaddlePlayer(mouseState);

            MovePaddleAi(_redPaddle, elapsedSeconds);

            ConstrainPaddle(_bluePaddle);
            ConstrainPaddle(_redPaddle);

            MoveBall(elapsedSeconds);

            if (BallHitPaddle(_ball, _bluePaddle))
            {
                // TODO: Change the angle of the bounce
                //_tweener.TweenTo(_bluePaddle, p => p.Rotation, MathHelper.Pi / 16f, 0.2f)
                //    //.OnSet(v => new Vector2(v.X, _bluePaddle.Position.Y))
                //    .RepeatReverse()
                //    .Easing(EasingFunctions.ExponentialIn);

                _plopSoundEffect.Play(1.0f, _random.NextSingle(0.5f, 1.0f), -1f);
            }

            if (BallHitPaddle(_ball, _redPaddle))
            {
                // TODO: Change the angle of the bounce

                //_tweener.TweenTo(_redPaddle, p => p.Position, _redPaddle.Position + new Vector2(15, 0), 0.2f)
                //    .RepeatReverse()
                //    .Easing(EasingFunctions.ExponentialIn);

                _plopSoundEffect.Play(1f, _random.NextSingle(-1f, 1f), 1f);
            }

            _tweener.Update(elapsedSeconds);
        }
        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);
                    }
                }
            }
        }
Exemple #9
0
        public override void Process(GameTime gameTime, int entityId)
        {
            var keyboardState = KeyboardExtended.GetState();
            var mouseState    = MouseExtended.GetState();

            var moveable = _moveableMapper.Get(entityId);

            if (moveable != null)
            {
                ProcessMovement(gameTime, moveable, keyboardState);
            }

            var zoomable = _zoomableMapper.Get(entityId);

            if (zoomable != null)
            {
                ProcessZoom(gameTime, zoomable, mouseState);
            }
        }
Exemple #10
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;
        }
Exemple #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;
        }