示例#1
0
        public List<Body> generateLevel()
        {
            String pathToCollisionFile = level.levelConfig.GlobalSection["collision"];

            var vertices = new Vector2[4];

            var startPointsStatic = new List<Vector2>();

            var grays = new List<Vector2>();

            var map = new Bitmap(pathToCollisionFile);

            for (int x = 0; x < map.Size.Width; ++x)
            {
                for (int y = 0; y < map.Size.Height; ++y)
                {
                    var pixel = map.GetPixel(x, y);
                    if (isRed(pixel))
                    {
                        startPointsStatic.Add(new Vector2(x, y));
                    }
                }
            }

            foreach (var startPoint in startPointsStatic)
            {
                vertices = GetVertices(map, startPoint);
                var bodyDef = new BodyDef();
                bodyDef.type = BodyType.Static;

                bodyDef.angle = 0;
                bodyDef.position = level.ConvertToBox2D(startPoint);

                var body = level.World.CreateBody(bodyDef);

                var edges = new List<EdgeInfo>();

                if (vertices.Length < 2)
                {
                    throw new Exception();
                }

                for (int i = 1; i < vertices.Length; ++i)
                {
                    var vertexA = vertices[i - 1];
                    var vertexB = vertices[i];

                    var edgeInfo = new EdgeInfo();
                    edgeInfo.shape = new EdgeShape();
                    edgeInfo.isVertical = vertexA.X == vertexB.X;
                    edgeInfo.shape.Set(vertexA, vertexB);
                    edges.Add(edgeInfo);

                }

                foreach (var edge in edges)
                {
                    var fixture = new FixtureDef();
                    var elementInfo = new LevelElementInfo();
                    fixture.shape = edge.shape;
                    if (edge.isVertical)
                    {
                        fixture.friction = 0.0f;
                        elementInfo.type = LevelElementType.Wall;
                    }
                    else
                    {
                        fixture.friction = level.GroundFriction;
                        elementInfo.type = LevelElementType.Ground;
                    }
                    fixture.userData = elementInfo;
                    body.CreateFixture(fixture);
                }

                bodyList.Add(body);
            }

            return bodyList;
        }
示例#2
0
        public List<scBody> generateLevel()
        {
            String pathToCollisionFile = level.levelConfig.GlobalSection["collision"];

            var vertices = new Vector2[4];

            var startPointsStatic = new List<Vector2>();

            var grays = new List<Vector2>();

            var map = new Bitmap(pathToCollisionFile);

            for (int x = 0; x < map.Size.Width; ++x)
            {
                for (int y = 0; y < map.Size.Height; ++y)
                {
                    var pixel = map.GetPixel(x, y);
                    if (isRed(pixel))
                    {
                        startPointsStatic.Add(new Vector2(x, y));
                    }
                }
            }

            foreach (var startPoint in startPointsStatic)
            {
                vertices = GetVertices(map, startPoint);

                var edges = new List<EdgeInfo>();

                if (vertices.Length < 2)
                {
                    throw new Exception();
                }

                for (int i = 1; i < vertices.Length; ++i)
                {
                    var vertexA = vertices[i - 1];
                    var vertexB = vertices[i];

                    var edgeInfo = new EdgeInfo();
                    edgeInfo.shape = new scEdgeShape();
                    edgeInfo.isVertical = vertexA.X == vertexB.X;
                    edgeInfo.shape.start = vertexA;
                    edgeInfo.shape.end = vertexB;
                    edges.Add(edgeInfo);

                }

                IList<scBodyPartDescription> bodyPartDescriptions = new List<scBodyPartDescription>();

                foreach (var edge in edges)
                {
                    var bodyPartDescription = new scBodyPartDescription();
                    var elementInfo = new LevelElementInfo();
                    bodyPartDescription.shape = edge.shape;
                    if (edge.isVertical)
                    {
                        bodyPartDescription.friction = 0.0f;
                        elementInfo.type = LevelElementType.Wall;
                    }
                    else
                    {
                        bodyPartDescription.friction = level.GroundFriction;
                        elementInfo.type = LevelElementType.Ground;
                    }
                    bodyPartDescription.userData = elementInfo;
                    bodyPartDescriptions.Add(bodyPartDescription);
                }

                var bodyDescription = new scBodyDescription();
                bodyDescription.bodyType = scBodyType.Static;

                bodyDescription.transform.rotation.radians = 0;
                bodyDescription.transform.position = startPoint;

                var body = level.World.createBody(bodyDescription, bodyPartDescriptions);

                bodyList.Add(body);
            }

            return bodyList;
        }