/// <summary> /// Initializes a new instance of the BodyPartInfo class. /// </summary> /// <param name="entity">The body part entity.</param> /// <param name="bodyPartComponent">The body part component.</param> /// <param name="physicsComponent">The physics component.</param> public BodyPartInfo(Entity entity, BodyPartComponent bodyPartComponent, PhysicsComponent physicsComponent) { this.Entity = entity; this.BodyPartComponent = bodyPartComponent; this.PhysicsComponent = physicsComponent; }
/// <summary> /// Create the body part entity. /// </summary> /// <param name="bodyEntity">The body entity that the torso belongs to.</param> /// <param name="bodyPart">The type of body part.</param> /// <param name="spriteType">The sprite type.</param> /// <param name="spriteFamily">The sprite family.</param> /// <param name="position">The world position of the body part.</param> /// <param name="collisionGroup">The collision group of the body part.</param> /// <param name="spriteVariation">The sprite variation.</param> /// <param name="isPhysical">Indicates whether the body part can collide with physics objects.</param> /// <returns>The body part entity and the component.</returns> private BodyPartInfo AssembleBodyPart( Entity bodyEntity, BodyPart bodyPart, string spriteType, string spriteFamily, Vector2 position, short collisionGroup, int spriteVariation = -1, bool isPhysical = true) { Entity entity = this.world.EntityManager.CreateEntity(); // Add the body part var bodyPartComponent = new BodyPartComponent(bodyEntity, bodyPart); this.world.EntityManager.AddComponent(entity, bodyPartComponent); // Add the sprite string spriteName = this.world.Resources.GetSpriteName("body", spriteType, spriteFamily, spriteVariation); this.world.EntityManager.AddComponent(entity, new SpriteComponent(spriteName)); Body body; if (isPhysical) { // Get the texture data for the sprite Rectangle rectangle = this.world.Resources.GetSpriteRectangle(spriteName); uint[] spriteData = new uint[rectangle.Width * rectangle.Height]; this.world.Resources.SpriteSheet.GetData(0, rectangle, spriteData, 0, spriteData.Length); // Create a polygon for the sprite texture Vertices vertices = PolygonTools.CreatePolygon(spriteData, rectangle.Width, true); // Scale the vertices from pixels to physics-world units var scale = new Vector2(Const.PixelsToMeters, -Const.PixelsToMeters); vertices.Scale(ref scale); // Partition into smaller polygons to split concave segments List<Vertices> convexVertices = BayazitDecomposer.ConvexPartition(vertices); // Create the body body = BodyFactory.CreateCompoundPolygon(this.world.Physics, convexVertices, 1.0f); } else { // Create a tiny rectangle since this isn't a physical body. A body is just created so that the body // part can be welded onto other parts body = BodyFactory.CreateRectangle(this.world.Physics, 0.001f, 0.001f, 1.0f); } body.IsStatic = false; body.CollisionGroup = collisionGroup; body.IsSensor = !isPhysical; // Add the physics component var physicsComponent = new PhysicsComponent(body); this.world.EntityManager.AddComponent(entity, physicsComponent); // Add the position component this.world.EntityManager.AddComponent(entity, new PositionComponent(body, position)); // Add the scale component this.world.EntityManager.AddComponent(entity, new ScaleComponent(Const.PixelsToMeters)); return new BodyPartInfo(entity, bodyPartComponent, physicsComponent); }