'Code module
Sub Process_Globals
End Sub
Sub CreateLevel(world As lgBox2DWorld, tiledmap As lgMapTiledMap)
Private scalex As Float = Main.scaleX
Private scaley As Float = Main.scaleY
Dim layerint As Int = -1
For i = 0 To tiledmap.Layers.GetAllLayers.Size-1
Dim layer As lgMapLayer = tiledmap.Layers.GetAllLayers.Get(i)
If layer.Name = "objectlayer1" Then
layerint = i
Exit
End If
Next
If layerint < 0 Then Return
For Each obj As lgMapObject In tiledmap.Layers.Get(layerint).Objects.GetAllObjects
If obj Is lgMapPolygonMapObject Then
Dim polygonobj As lgMapPolygonMapObject = obj
Dim polygon As lgBox2DPolygonShape
Dim newVertices(polygonobj.Polygon.Vertices.Length) As Float
For i = 0 To polygonobj.Polygon.Vertices.Length - 1
Dim ver As Float = polygonobj.Polygon.Vertices(i)
newVertices(i) = ver / scalex
Next
polygon.Set2(newVertices)
Dim polygonShapeDef As lgBox2DFixtureDef
polygonShapeDef.shape = polygon
polygonShapeDef.restitution=0.2
polygonShapeDef.Density = 0.5
Dim polygonBodyDef As lgBox2DBodyDef
polygonBodyDef.Type = world.BODYTYPE_Static
polygonBodyDef.position.set(PosCon.world2box2d(polygonobj.Polygon.X,polygonobj.Polygon.Y,0,0).x,PosCon.world2box2d(polygonobj.Polygon.X,polygonobj.Polygon.Y,0,0).y)
Dim polygonbody As lgBox2DBody
polygonbody = world.createBody(polygonBodyDef)
polygonbody.createFixture(polygonShapeDef)
polygonbody.UserData = polygonobj.Name
polygon.dispose
Log("polygon created")
else if obj Is lgMapRectangleMapObject Then
Dim rectobj As lgMapRectangleMapObject = obj
Dim Box As lgBox2DPolygonShape
Box.SetAsBox2((rectobj.Rectangle.width/2)/scalex,(rectobj.Rectangle.height/2)/scaley,PosCon.world2box2d(rectobj.Rectangle.x,rectobj.Rectangle.y,rectobj.Rectangle.width,rectobj.Rectangle.height),0)
Dim fd As lgBox2DFixtureDef
fd.shape = Box
fd.restitution = 0.2
fd.Density = 1
fd.friction = 1
Dim bd_def As lgBox2DBodyDef
bd_def.type = world.BODYTYPE_Static
bd_def.position.set(0,0)
Dim box2dbody As lgBox2DBody
box2dbody = world.createBody(bd_def)
box2dbody.createFixture(fd)
box2dbody.UserData = obj.Name
Box.dispose
Log("box created")
else if obj Is lgMapEllipseMapObject Then
Dim circleobj As lgMapEllipseMapObject = obj
Dim Circle As lgBox2DCircleShape
Circle.Radius = (circleobj.Ellipse.width/scalex)/2
Dim circlefd As lgBox2DFixtureDef
circlefd.shape = Circle
circlefd.density = 1
circlefd.restitution = 0.2
circlefd.friction = 1
circlefd.isSensor = True
Dim circledef As lgBox2DBodyDef
circledef.type = world.BODYTYPE_Static
circledef.position.Set(PosCon.world2box2d(circleobj.Ellipse.x,circleobj.Ellipse.Y,0,0).x+Circle.Radius,PosCon.world2box2d(circleobj.Ellipse.x,circleobj.Ellipse.Y,0,0).y+Circle.Radius)
Dim circlebody As lgBox2DBody
circlebody = world.CreateBody(circledef)
circlebody.createFixture(circlefd)
circlebody.UserData = obj.Name
Circle.dispose
else if obj Is lgMapPolylineMapObject Then
Dim polylineobj As lgMapPolylineMapObject = obj
Dim chain As lgBox2DChainShape
Dim newVertices(polylineobj.Polyline.Vertices.Length) As Float
For i = 0 To polylineobj.Polyline.Vertices.Length - 1
Dim ver As Float = polylineobj.Polyline.Vertices(i)
newVertices(i) = ver / scalex
Next
chain.createChain(newVertices)
Dim chainShapeDef As lgBox2DFixtureDef
chainShapeDef.shape = chain
chainShapeDef.restitution=0.2
chainShapeDef.Density = 0.5
Dim chainBodyDef As lgBox2DBodyDef
chainBodyDef.Type = world.BODYTYPE_Static
chainBodyDef.position.set(PosCon.world2box2d(polylineobj.Polyline.X,polylineobj.Polyline.Y,0,0).x,PosCon.world2box2d(polylineobj.Polyline.X,polylineobj.Polyline.Y,0,0).y)
Dim chainbody As lgBox2DBody
chainbody = world.createBody(chainBodyDef)
chainbody.createFixture(chainShapeDef)
chainbody.UserData = polylineobj.Name
chain.dispose
Log("polyline created")
End If
Next
End Sub