'Class module
Sub Class_Globals
Private Maps As lgMapTiledMap
Private MapRenderer As lgMapOrthogonalTiledMapRenderer
Dim Box2Drenderer As lgBox2DDebugRenderer
Dim CurrentLayer As lgMapTiledMapLayer
Dim MapWidth, MapHeight As Int 'in tiles
Dim TileSize As Float 'in pixels
Dim World As lgBox2DWorld
'Dim WorldGround As lgMapPolylineMapObject
Dim MapObject As lgMapObject
Dim bdyGround As lgBox2DBody
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(Filename As String, Width As Int, Height As Int, GutterSize As Float)
'Create the Box2D physics world
Log("Begin map loading")
Dim vGravity As lgMathVector2
World.Initialize(vGravity.set(0, -10), True, "Box2D")
World.SetContinuousPhysics(True)
World.SetWarmStarting(True)
loadmap(Filename)
MapWidth = Maps.Properties.get("width")
MapHeight = Maps.Properties.get("height")
Log ("MapWidth:" & MapWidth)
Log ("MapHeight:" & MapHeight)
Dim TileWidth As Int = 20
Dim TileHeight As Int = 20
Dim RatioW As Float = Width / MapWidth
Dim RatioH As Float = Height / MapHeight
'Log("ratioW : "& RatioW )
Log("ratioh : "& RatioH )
If RatioW < RatioH Then
MapRenderer.Initialize3(Maps, RatioH )
TileSize = MapRenderer.UnitScale * TileWidth
Else
MapRenderer.Initialize3(Maps, RatioW)
TileSize = MapRenderer.UnitScale * TileHeight
End If
Log("map loaded... now creating ground")
Create_ground
End Sub
'Each layer is a different maze
Public Sub SelectLayer(Layer As Int)
CurrentLayer = Maps.Layers.get(Layer)
End Sub
Public Sub FromTileToPixel_X(TileX As Int) As Int
Return Round((TileX - 0.5) * TileSize)
End Sub
Public Sub FromTileToPixel_Y(TileY As Int) As Int
Return Round((TileY - 0.5) * TileSize)
End Sub
Public Sub FromTileToPixel_InvertedY(TileY As Int) As Int
Return Round((MapHeight - 1.5 - TileY) * TileSize)
End Sub
Public Sub FromPixelToTile_X(PixelX As Int) As Int
Return Round((PixelX / TileSize) + 0.5)
End Sub
Public Sub FromPixelToTile_Y(PixelY As Int) As Int
Return Round((PixelY / TileSize) + 0.5)
End Sub
'Gets the tile at the given coordinates
Public Sub GetTile(TileX As Int, TileY As Int) As lgMapStaticTiledMapTile
Dim Cell As lgMapTiledMapLayerCell = CurrentLayer.GetCell(TileX, TileY)
If Cell = Null Then
Return Null
Else
Return Cell.Tile
End If
End Sub
Private Sub loadmap(Filename As String)
'Loads the mazes (TMX format)
Dim TMXLoader As lgMapTmxMapLoader
Maps = TMXLoader.Initialize2("maps/"& Filename )
End Sub
Public Sub Reload
Maps.Dispose
loadmap("naamloos.tmx")
If MapRenderer.IsInitialized Then
MapRenderer.Map= Maps
Else
MapRenderer.Initialize(Maps)
End If
End Sub
'Draws the current layer
Public Sub Draw(Camera As lgOrthographicCamera)
World.Step(1/60, 8, 3)
MapRenderer.SetCameraView(Camera)
MapRenderer.render()
Box2Drenderer.render(World.InternalObject,Camera.Combined)
End Sub
Public Sub Dispose
Maps.Dispose
MapRenderer.Dispose
End Sub
Public Sub Create_ground
'get the mapground polyline from the maps and transpose to a fixture definition
Dim WorldGround As lgMapPolylineMapObject = Maps.Layers.Get2("ObjTest").Objects.Get2("MapGround")
Dim eshape As lgBox2DPolygonShape
eshape.Set2(WorldGround.Polyline.TransformedVertices)
Dim fd As lgBox2DFixtureDef
'now we have the fixture
fd.shape = eshape
Dim bd As lgBox2DBodyDef
bdyGround = World.CreateBody(bd)
bdyGround.createFixture(fd)
eshape.dispose
bdyGround.Position.Set(WorldGround.Polyline.OriginX,WorldGround.Polyline.OriginY)
End Sub
Sub Box2D_BeginContact(Contact As lgBox2DContact)
'The ground is discarded
If Contact.FixtureA.Body <> bdyGround AND Contact.FixtureB.Body <> bdyGround Then
'The Contact instance is going to be reused, so its data have to be copied
Log("BeginContact")
End If
End Sub