private void DoGamepadMovement() { const double deadzone = 0.09; var bounds = GraphicsDevice.Viewport.Bounds; // Left stick snapshot lsX = Ps4Input.Ps4RawAxis(0, Axis.LeftX); lsY = Ps4Input.Ps4RawAxis(0, Axis.LeftY); // Left stick deadzone if (Math.Abs(lsX) < deadzone) { lsX = 0; } if (Math.Abs(lsY) < deadzone) { lsY = 0; } var newPosX = playerPos.X + lsX * maxMoveDist; if (newPosX + playerRectangle.Width > bounds.Width) { playerPos.X = bounds.Width - playerRectangle.Width; } else if (newPosX < 0) { playerPos.X = 0; } else { playerPos.X = Convert.ToSingle(Math.Ceiling(Convert.ToDouble(newPosX))); } var newPosY = playerPos.Y + lsY * maxMoveDist; if (newPosY + playerRectangle.Height > bounds.Height) { playerPos.Y = bounds.Height - playerRectangle.Height; } else if (newPosY < 0) { playerPos.Y = 0; } else { playerPos.Y = Convert.ToSingle(Math.Ceiling(Convert.ToDouble(newPosY))); } }
/// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.Black); spriteBatch.Begin(); // Draw world geometry - Rectangles foreach (var rect in worldRects) { var geo = rect.Geometry; spriteBatch.FillRectangle( new Rectangle((int)geo.Position.X, (int)geo.Position.Y, geo.Width, geo.Height), geo.DrawColor, geo.RotAngle); } // Draw world geometry - Ellipses foreach (var ellipse in worldEllipses) { var geo = ellipse.Geometry; spriteBatch.DrawCircle(new Vector2(geo.Position.X, geo.Position.Y), geo.Height / 2, 40, geo.DrawColor); } // Character spriteBatch.Draw(playerRectangle, playerPos, Color.White); // Diagnostic info if (Ps4Input.Ps4IsConnected(0)) { spriteBatch.DrawString(arial, $"{lsX}, {lsY}", diagInfoPos, Color.White); } spriteBatch.DrawString(arial, $"{playerPos.X}, {playerPos.Y}", new Vector2(playerPos.X, playerPos.Y + 30), Color.White); spriteBatch.End(); base.Draw(gameTime); }
/// <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() { // Controller Ps4Input.Initialize(this); // Player initialization playerPos = new Vector2(20, 20); playerRectangle = new Texture2D(graphics.GraphicsDevice, 30, 30); Color[] playerColorData = new Color[30 * 30]; for (int i = 0; i < playerColorData.Length; ++i) { playerColorData[i] = Color.White; } playerRectangle.SetData(playerColorData); // World geometry const int hsWallWidth = 15; Vector2 hsOrig = new Vector2(400, 300); Vector2 leftShrubCoord = new Vector2(430, 275); var geometries = new List <WorldShapeGeometry> { #region Hard-coded geometry // House exterior walls new WorldShapeGeometry(hsOrig, 150, hsWallWidth, Shapes.Rectangle, 0, Color.DarkGray), new WorldShapeGeometry(new Vector2(hsOrig.X + 200, hsOrig.Y), 300, hsWallWidth, Shapes.Rectangle, 0, Color.DarkGray), new WorldShapeGeometry(new Vector2(hsOrig.X + 500, hsOrig.Y), hsWallWidth, 600, Shapes.Rectangle, 0, Color.DarkGray), new WorldShapeGeometry(new Vector2(hsOrig.X + 200 + hsWallWidth, hsOrig.Y + 600), 300, hsWallWidth, Shapes.Rectangle, 0, Color.DarkGray), new WorldShapeGeometry(new Vector2(hsOrig.X, hsOrig.Y + hsWallWidth), hsWallWidth, 450, Shapes.Rectangle, 0, Color.DarkGray), new WorldShapeGeometry(new Vector2(hsOrig.X, hsOrig.Y + hsWallWidth + 450), 200, hsWallWidth, Shapes.Rectangle, 0, Color.DarkGray), new WorldShapeGeometry(new Vector2(hsOrig.X + 200, hsOrig.Y + hsWallWidth + 450), hsWallWidth, 150, Shapes.Rectangle, 0, Color.DarkGray), // House floor new WorldShapeGeometry(new Vector2(hsOrig.X + hsWallWidth, hsOrig.Y + hsWallWidth), 485, 450, Shapes.Rectangle, 0, Color.SaddleBrown, passable: true), new WorldShapeGeometry(new Vector2(hsOrig.X + 215, hsOrig.Y + 465), 285, 135, Shapes.Rectangle, 0, Color.SaddleBrown, passable: true), // Shrubs, front left new WorldShapeGeometry(leftShrubCoord, 30, 30, Shapes.Ellipse, 0, Color.DarkGreen), new WorldShapeGeometry(new Vector2(leftShrubCoord.X + 35, leftShrubCoord.Y), 30, 30, Shapes.Ellipse, 0, Color.DarkGreen), new WorldShapeGeometry(new Vector2(leftShrubCoord.X + 70, leftShrubCoord.Y), 30, 30, Shapes.Ellipse, 0, Color.DarkGreen), // Shrubs, front right new WorldShapeGeometry(new Vector2(leftShrubCoord.X + 220, leftShrubCoord.Y), 30, 30, Shapes.Ellipse, 0, Color.DarkGreen), new WorldShapeGeometry(new Vector2(leftShrubCoord.X + 255, leftShrubCoord.Y), 30, 30, Shapes.Ellipse, 0, Color.DarkGreen), new WorldShapeGeometry(new Vector2(leftShrubCoord.X + 290, leftShrubCoord.Y), 30, 30, Shapes.Ellipse, 0, Color.DarkGreen), // River new WorldShapeGeometry(new Vector2(1600, -20), 65, 450, Shapes.Rectangle, MathHelper.ToRadians(20), Color.DarkBlue), new WorldShapeGeometry(new Vector2(1450, 400), 65, 500, Shapes.Rectangle, MathHelper.ToRadians(5), Color.DarkBlue), new WorldShapeGeometry(new Vector2(1407, 898), 65, 700, Shapes.Rectangle, MathHelper.ToRadians(2), Color.DarkBlue), // Bridge new WorldShapeGeometry(new Vector2(1400, 500), 150, 75, Shapes.Rectangle, 0, Color.Brown, passable: true) #endregion }; foreach (var geometry in geometries) { world.Add(new WorldShape(geometry, graphics.GraphicsDevice)); } // Filtered collections of world geometry based on shape type worldRects = world.Where(x => x.Geometry.ShapeType == Shapes.Rectangle).ToList(); worldEllipses = world.Where(x => x.Geometry.ShapeType == Shapes.Ellipse).ToList(); base.Initialize(); }
// Start is called before the first frame update void Start() { input = new Ps4Input(inputSource); }