當前位置: 首頁>>代碼示例>>C#>>正文


C# Vector2.ToCompassDirection方法代碼示例

本文整理匯總了C#中Microsoft.Xna.Framework.Vector2.ToCompassDirection方法的典型用法代碼示例。如果您正苦於以下問題:C# Vector2.ToCompassDirection方法的具體用法?C# Vector2.ToCompassDirection怎麽用?C# Vector2.ToCompassDirection使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Microsoft.Xna.Framework.Vector2的用法示例。


在下文中一共展示了Vector2.ToCompassDirection方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: HandleInput

        protected override void HandleInput(GameTime gameTime)
        {
            base.HandleInput(gameTime);

            var mouseX = (int)(MouseState.X * ClientWidth);
            var mouseY = (int)(MouseState.Y * ClientHeight);
            _ui.MouseMove(mouseX, mouseY);
            //_dummy = string.Format("MouseMove X:{0} Y:{1}", mouseX, mouseY);
            //_dummy = MouseState.X.ToString();
            if (MouseState.LeftButton == ButtonState.Pressed)
            {
                _ui.MouseDown(mouseX, mouseY);
                //_dummy = string.Format("MouseDownLeft X:{0} Y:{1}", mouseX, mouseY);
            }
            if (MouseState.LeftButton == ButtonState.Released)
            {
                _ui.MouseUp(mouseX, mouseY);
                //_dummy = string.Format("MouseUpLeft X:{0} Y:{1}", mouseX, mouseY);
            }

            var playerWantsToMove = false;

            var targetDirection = Vector2.Zero;

            if (KeyboardState.IsKeyUp(Keys.NumPad8) || (GamepadState.DPad.Up == ButtonState.Pressed && OldGamepadState.DPad.Up == ButtonState.Released))
            {
                targetDirection += new Vector2(0, -1);
                playerWantsToMove = true;
            }
            if (KeyboardState.IsKeyUp(Keys.NumPad2) || (GamepadState.DPad.Down == ButtonState.Pressed && OldGamepadState.DPad.Down == ButtonState.Released))
            {
                targetDirection += new Vector2(0, 1);
                playerWantsToMove = true;
            }
            if (KeyboardState.IsKeyUp(Keys.NumPad4) || (GamepadState.DPad.Left == ButtonState.Pressed && OldGamepadState.DPad.Left == ButtonState.Released))
            {
                targetDirection += new Vector2(-1, 0);
                playerWantsToMove = true;
            }
            if (KeyboardState.IsKeyUp(Keys.NumPad6) || (GamepadState.DPad.Right == ButtonState.Pressed && OldGamepadState.DPad.Right == ButtonState.Released))
            {
                targetDirection += new Vector2(1, 0);
                playerWantsToMove = true;
            }
            if (KeyboardState.IsKeyUp(Keys.NumPad7))
            {
                targetDirection += new Vector2(-1, -1);
                playerWantsToMove = true;
            }
            if (KeyboardState.IsKeyUp(Keys.NumPad9))
            {
                targetDirection += new Vector2(1, -1);
                playerWantsToMove = true;
            }
            if (KeyboardState.IsKeyUp(Keys.NumPad1))
            {
                targetDirection += new Vector2(-1, 1);
                playerWantsToMove = true;
            }
            if (KeyboardState.IsKeyUp(Keys.NumPad3))
            {
                targetDirection += new Vector2(1, 1);
                playerWantsToMove = true;
            }

            var leftThumb = new Vector2(GamepadState.ThumbSticks.Left.X / 32768f, -GamepadState.ThumbSticks.Left.Y / 32768f); // X[-1..0..1] Y[-1..0..1]
            Vector2 compassDir = Vector2.Zero;
            if (leftThumb.Length() >= 0.5f)
            {
                targetDirection = leftThumb.ToCompassDirection();
                playerWantsToMove = (gameTime.TotalGameTime.TotalMilliseconds > (_previousMilliseconds + 250)); //TODO(philipp): add option for speedruns ;)
            }

            if (!playerWantsToMove)
            {
                return;
            }
            else
            {
                _previousMilliseconds = gameTime.TotalGameTime.TotalMilliseconds;
            }

            var targetPosition = _world.Player.Position.ToVector2() + targetDirection;
            if (targetPosition.X >= 0 && targetPosition.Y >= 0 && targetPosition.X < _world.CurrentMap.Width && targetPosition.Y < _world.CurrentMap.Height)
            {
                var targetPositionPoint = new Point((int)targetPosition.X, (int)targetPosition.Y);
                var targetTile = _world.CurrentMap.Tiles.ElementAt(_world.CurrentMap.PointToIndex(targetPositionPoint));
                if (!targetTile.IsSolid)
                {
                    _world.Player.Position = targetPositionPoint;
                    if (targetTile.IsTeleporter)
                    {
                        targetTile.DoStepOn(targetPositionPoint, _world.CurrentMap);
                    }
                    _world.Player.Step();
                }
            }
        }
開發者ID:deccer,項目名稱:ValhallaClone,代碼行數:98,代碼來源:MainGame.cs


注:本文中的Microsoft.Xna.Framework.Vector2.ToCompassDirection方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。