public Player CreatePlayer(Texture2D crateTexture) { var crateShapeDef = new PolygonDef(); var cratePhysicsSize = GameUtils.PhysicsVec(new Vector2(crateTexture.Width, crateTexture.Height)); crateShapeDef.Vertices = new Vec2[4]; crateShapeDef.Vertices[0] = new Vec2(-(cratePhysicsSize.X / 2), -(cratePhysicsSize.Y / 2)); crateShapeDef.Vertices[1] = new Vec2((cratePhysicsSize.X / 2), -(cratePhysicsSize.Y / 2)); crateShapeDef.Vertices[2] = new Vec2((cratePhysicsSize.X / 2), (cratePhysicsSize.Y / 2)); crateShapeDef.Vertices[3] = new Vec2(-(cratePhysicsSize.X / 2), (cratePhysicsSize.Y / 2)); crateShapeDef.VertexCount = 4; Logger.Info($"crate size = ({cratePhysicsSize.X},{cratePhysicsSize.Y})"); crateShapeDef.Density = GameData.PlayerDensity; crateShapeDef.Friction = GameData.PlayerFriction; crateShapeDef.Filter.CategoryBits = CollisionCategory.Player; crateShapeDef.Filter.MaskBits = (ushort)(CollisionCategory.Wall | CollisionCategory.Alien | CollisionCategory.AlienProjectile | CollisionCategory.Pickup); var crateBodyDef = new BodyDef(); crateBodyDef.IsBullet = true; var playerPosition = new Vec2(GameData.PlayerStartX, GameData.PlayerStartY); crateBodyDef.Position.Set(playerPosition.X, playerPosition.Y); var crateBody = PhysicsWorld.CreateBody(crateBodyDef); var crateShape = crateBody.CreateShape(crateShapeDef); crateBody.SetMassFromShapes(); var player = new Player(Content, PhysicsWorld, crateTexture, GameWorld, crateShape, crateBody, AnimationFactory, WeaponInventory, FilteredInputListener, GameData, GameUtils); GameWorld.AddGameObject(player); return(player); }
/// <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)); }
private PickupTemplate CreatePickupTemplate(PickupDefinition definition) { var kvp = definition.Values; var texture = ContentManager.Load <Texture2D>(kvp["TextureName"]); var scale = float.Parse(kvp["Scale"]); var width = texture.Width * scale; var height = texture.Height * scale; var shapeDef = new PolygonDef(); var physicsSize = GameUtils.PhysicsVec(new Vector2(width, height)); shapeDef.Vertices = new Vec2[4]; shapeDef.Vertices[0] = new Vec2(-(physicsSize.X / 2), -(physicsSize.Y / 2)); shapeDef.Vertices[1] = new Vec2((physicsSize.X / 2), -(physicsSize.Y / 2)); shapeDef.Vertices[2] = new Vec2((physicsSize.X / 2), (physicsSize.Y / 2)); shapeDef.Vertices[3] = new Vec2(-(physicsSize.X / 2), (physicsSize.Y / 2)); shapeDef.VertexCount = 4; shapeDef.Density = float.Parse(kvp["Density"]); shapeDef.Friction = float.Parse(kvp["Friction"]); shapeDef.Filter.CategoryBits = CollisionCategory.Pickup; shapeDef.Filter.MaskBits = CollisionCategory.Player; var rand = new Random((int)(DateTime.UtcNow - DateTime.MinValue).Ticks); var minX = GameData.MaxXDimension * .1; var minY = GameData.MaxYDimension * .1; var maxX = GameData.MaxXDimension - minX; var maxY = GameData.MaxYDimension - minY; var origin = new Vec2(rand.Next((int)minX, (int)maxX), rand.Next((int)minY, (int)maxY)); var bodyDef = new BodyDef(); bodyDef.Position.Set(origin.X, origin.Y); var body = PhysicsWorld.CreateBody(bodyDef); var shape = body.CreateShape(shapeDef); body.SetMass(new MassData { Mass = 0 }); return(new PickupTemplate { Texture = texture, RigidBody = body, Shape = shape, Scale = scale }); }