/// <summary> /// Creates a wall /// </summary> /// <param name="x">x position in pixels</param> /// <param name="y">y position in pixels</param> /// <param name="w">width of wall in pixels</param> /// <param name="h">height of wall in pixels</param> /// <returns></returns> private GameObject Wall(Vec2 topLeft, Vec2 bottomRight) { // Define the ground body. var wallBodyDef = new BodyDef(); wallBodyDef.Position.Set(topLeft.X, topLeft.Y); // Call the body factory which creates the wall box shape. // The body is also added to the world. var wallBody = PhysicsWorld.CreateBody(wallBodyDef); // Define the wall box shape. var wallShapeDef = new PolygonDef(); wallShapeDef.Friction = 0.3f; wallShapeDef.Density = 1.0f; // The extents are the half-widths of the box. var wallPhysicsSize = new Vec2(Math.Abs(bottomRight.X - topLeft.X), Math.Abs(bottomRight.Y - topLeft.Y)); if (wallPhysicsSize.X <= 0) { wallPhysicsSize.X = 1 * GameData.MetersPerPixel; } if (wallPhysicsSize.Y <= 0) { wallPhysicsSize.Y = 1 * GameData.MetersPerPixel; } wallShapeDef.Filter.CategoryBits = (ushort)CollisionCategory.Wall; wallShapeDef.Filter.MaskBits = (ushort)(CollisionCategory.Player | CollisionCategory.Alien | CollisionCategory.PlayerProjectile | CollisionCategory.AlienProjectile); wallShapeDef.SetAsBox(wallPhysicsSize.X, wallPhysicsSize.Y); // Add the ground shape to the ground body. var shape = wallBody.CreateShape(wallShapeDef); var vTex = GameUtils.GraphicsVec(wallPhysicsSize); if (vTex.X <= 0) { vTex.X = 1; } if (vTex.Y <= 0) { vTex.Y = 1; } Logger.Info($"Wall created at ({wallBody.GetPosition().X},{wallBody.GetPosition().Y}) " + $"extends to ({wallBody.GetPosition().X + wallPhysicsSize.X},{wallBody.GetPosition().Y + wallPhysicsSize.Y})"); return(new GameObject(PhysicsWorld, null, shape, wallBody, 0, GameData, GameUtils)); }
/// <summary> /// Draws this background object using the given SpriteBatch /// </summary> /// <param name="spriteBatch"></param> /// <param name="cameraOrigin"></param> /// <param name="viewport"></param> public override void OnDraw(SpriteBatch spriteBatch, Vec2 cameraOrigin, Vector2 viewport) { //parallax calculation //we can use Scale.X and Scale.Y here because they are derived from distance-from-camera /*var diffx = (WorldPosition.X + ((1 - Scale.X) * cameraOrigin.X)) - cameraOrigin.X; * var diffy = (WorldPosition.Y + ((1 - Scale.Y) * cameraOrigin.Y)) - cameraOrigin.Y; * * var location = new Vector2(diffx * GameData.PixelsPerMeter, diffy * GameData.Instance.PixelsPerMeter); * spriteBatch.Draw(Texture, location, null, null, null, 0, Scale); */ var location = GameUtils.GraphicsVec(WorldPosition - cameraOrigin); spriteBatch.Draw(Texture, location); }