private void InitializeInput(SampleGameObject game) { Point startPoint = new Point(); touch.TouchStarted += HandleTouchStart; touch.TouchEnded += HandleTouchEnd; void HandleTouchStart(object sender, TouchEventArgs args) { startPoint = args.Position; } void HandleTouchEnd(object sender, TouchEventArgs args) { var endPoint = args.Position; HandleSwipe(endPoint.X - startPoint.X, endPoint.Y - startPoint.Y); } void HandleSwipe(int dx, int dy) { if (Math.Abs(dx) + Math.Abs(dy) < 50) { game.Bus.Notify(Events.SPACE_PRESS); } else if (-dy > Math.Abs(dx)) { // NORTH ResetInputState(game.Input); game.Input.IsUp = true; } else if (dx >= Math.Abs(dy)) { // EAST ResetInputState(game.Input); game.Input.IsRight = true; } else if (dy > Math.Abs(dx)) { // SOUTH ResetInputState(game.Input); game.Input.IsDown = true; } else if (-dx >= Math.Abs(dy)) { // WEST ResetInputState(game.Input); game.Input.IsLeft = true; } } void ResetInputState(InputState state) { game.Input.IsUp = false; game.Input.IsRight = false; game.Input.IsDown = false; game.Input.IsLeft = false; } }
private void InitializeInput(SampleGameObject game) { keyboard.KeyPressed += (sender, args) => { var key = args.Key; if (key == Keys.Up) { game.Input.IsUp = true; } else if (key == Keys.Right) { game.Input.IsRight = true; } else if (key == Keys.Down) { game.Input.IsDown = true; } else if (key == Keys.Left) { game.Input.IsLeft = true; } else if (key == Keys.Space) { game.Bus.Notify(Events.SPACE_PRESS); } }; keyboard.KeyReleased += (sender, args) => { var key = args.Key; if (key == Keys.Up) { game.Input.IsUp = false; } else if (key == Keys.Right) { game.Input.IsRight = false; } else if (key == Keys.Down) { game.Input.IsDown = false; } else if (key == Keys.Left) { game.Input.IsLeft = false; } }; }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { GraphicsDevice.Viewport = new Viewport(0, 0, 500, 500); viewport = new BoxingViewportAdapter(Window, GraphicsDevice, 500, 500, 0, 0); spriteBatch = new SpriteBatch(GraphicsDevice); var gameObject = new SampleGameObject(Content, spriteBatch, GraphicsDevice, viewport); InitializeInput(gameObject); world = new SampleWorld(gameObject); world.AddScene("start", new StartScene()); world.AddScene("main", new MainScene()); base.Initialize(); world.Initialize(); world.Switch("start"); }