Пример #1
0
        public Campfire(Game game)
        {
            parentGame = game;
            // TODO: use this.Content to load your game content here
            logs = parentGame.Content.Load<Texture2D>("logs");

            // Declare a new Particle System instance and Initialize it
            mcParticleSystem = new FireParticleSystem(parentGame);
            mcParticleSystem.AutoInitialize(parentGame.GraphicsDevice, parentGame.Content, null);
        }
Пример #2
0
        public Campfire(Game game, World world, SpriteBatch batch, Vector2 position)
        {
            //Set the parent game
            _parentGame = game;

            //Initialize the logs
            _logs = _parentGame.Content.Load<Texture2D>("EnvironmentObjects/logs");
            //Initialize the scale (we're scaling down the logs when we draw instead of in the original image to show off that capability)
            _scale = new Vector2(0.5f, 0.45f);
            //No rotation
            _rotation = 0f;
            //No sprite effects
            _spriteEffects = SpriteEffects.None;
            //Layer depth to front layer
            _layerDepth = 0f;

            //Create an array to hold the data from the texture
            uint[] data = new uint[_logs.Width * _logs.Height];

            //Transfer the texture data to the array
            _logs.GetData(data);

            //Find the vertices that makes up the outline of the shape in the texture
            Vertices textureVertices = PolygonTools.CreatePolygon(data, _logs.Width, false);

            //The tool return vertices as they were found in the texture.
            //We need to find the real center (centroid) of the vertices for 2 reasons:

            //1. To translate the vertices so the polygon is centered around the centroid.
            Vector2 centroid = -textureVertices.GetCentroid();
            textureVertices.Translate(ref centroid);

            //2. To draw the texture the correct place.
            _origin = -centroid;

            //We simplify the vertices found in the texture.
            textureVertices = SimplifyTools.ReduceByDistance(textureVertices, 4f);

            //Since it is a concave polygon, we need to partition it into several smaller convex polygons
            List<Vertices> list = BayazitDecomposer.ConvexPartition(textureVertices);

            //scale the vertices from graphics space to sim space
            Vector2 vertScale = new Vector2(ConvertUnits.ToSimUnits(1)) * _scale;
            foreach (Vertices vertices in list)
            {
                vertices.Scale(ref vertScale);
            }

            //Create a single body with multiple fixtures
            _body = BodyFactory.CreateCompoundPolygon(world, list, 1f, BodyType.Dynamic);
            _body.BodyType = BodyType.Static;
            _body.CollidesWith = Category.All;
            _body.Position = position;

            // Declare a new Particle System instance and Initialize it
            _particleSystem = new FireParticleSystem(_parentGame);
            _particleSystem.AutoInitialize(_parentGame.GraphicsDevice, _parentGame.Content, batch);
        }