#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
Sub Class_Globals
Public X2 As X2Utils
Private xui As XUI 'ignore
Public world As B2World
Public Ground, resorte, canon As X2BodyWrapper
Private ivForeground As B4XView
Private ivBackground As B4XView
Public lblStats As B4XView
Public TileMap As X2TileMap
Public Const ObjectLayer As String = "Object Layer 1"
Private Label1 As Label
Dim check_characters As Int
Dim number_of_characters As Int =15
Public mCharacter(number_of_characters) As Character
End Sub
Public Sub Initialize (Parent As B4XView)
Parent.LoadLayout("GameLayout")
world.Initialize("world", world.CreateVec2(0, -10))
X2.Initialize(Me, ivForeground, world)
Dim WorldWidth As Float = 6 'meters
Dim WorldHeight As Float = WorldWidth / 1.333 'same ratio as in the designer script
X2.ConfigureDimensions(world.CreateVec2(WorldWidth / 2, WorldHeight / 2), WorldWidth)
'Load the graphics and add them to the cache.
'comment to disable debug drawing
'X2.EnableDebugDraw
CreateStaticBackground
'Passing Null for the target view parameter because we are not creating the background with a tile layer.
TileMap.Initialize(X2, File.DirAssets, "tiled.json", Null)
TileMap.SetSingleTileDimensionsInMeters(WorldWidth / TileMap.TilesPerRow, WorldHeight / TileMap.TilesPerColumn)
TileMap.PrepareObjectsDef(ObjectLayer)
Create_elements
load_images
End Sub
Private Sub CreateStaticBackground
Dim bc As BitmapCreator
bc.Initialize(ivBackground.Width / xui.Scale / 2, ivBackground.Height / xui.Scale / 2)
bc.FillGradient(Array As Int(0xFF006EFF, 0xFF00DAAD), bc.TargetRect, "TOP_BOTTOM")
X2.SetBitmapWithFitOrFill(ivBackground, bc.Bitmap)
End Sub
Public Sub Resize
X2.ImageViewResized
End Sub
Private Sub load_images
Dim width As Float = 0.22
Dim all As List = X2.ReadSprites(xui.LoadBitmap(File.DirAssets, "walking_sprite.png"), 1, 7, width, width*(1.3))
X2.GraphicCache.PutGraphic("walking", Array(all.Get(0), all.Get(1), all.Get(2), all.Get(3), all.Get(4), all.Get(5), all.Get(6)))
Dim falling As List = X2.ReadSprites(xui.LoadBitmap(File.DirAssets, "falling.png"), 1, 4, width, width*(1.3))
X2.GraphicCache.PutGraphic("falling", Array(falling.Get(0), falling.Get(1), falling.Get(2), falling.Get(3)))
Dim rock As List = X2.ReadSprites(xui.LoadBitmap(File.DirAssets, "rock.png"), 1, 4, 0.25, 0.25)
X2.GraphicCache.PutGraphic("rock", Array(rock.Get(0), rock.Get(1), rock.Get(2), rock.Get(3)))
End Sub
Public Sub Tick (GS As X2GameStep)
End Sub
Private Sub Create_elements
Dim ol As X2ObjectsLayer = TileMap.Layers.Get(ObjectLayer)
For Each template As X2TileObjectTemplate In ol.ObjectsById.Values
Select template.Name
Case "character"
For check_characters=0 To number_of_characters - 1
template.BodyDef.Position.X = X2.RndFloat(X2.ScreenAABB.BottomLeft.X, X2.ScreenAABB.TopRight.X)
Dim bw As X2BodyWrapper = TileMap.CreateObject(template)
mCharacter(check_characters).Initialize(bw)
Next
Case "ground"
TileMap.CreateObject(template)
Case "wall"
TileMap.CreateObject(template)
Case "rock"
TileMap.CreateObject(template)
End Select
Next
End Sub
Public Sub DrawingComplete
End Sub
'Return True to stop the game loop
Public Sub BeforeTimeStep (GS As X2GameStep) As Boolean
Return False
End Sub
Private Sub World_BeginContact (Contact As B2Contact)
Dim bodies As X2BodiesFromContact = X2.GetBodiesFromContact(Contact, "character")
If bodies = Null Then
For check_characters=0 To number_of_characters - 1
mCharacter(check_characters).touch=""
Next
Return
End If
Select bodies.OtherBody.Name
Case "ground"
For check_characters=0 To number_of_characters - 1
If bodies.ThisBody= mCharacter(check_characters).bw Then
mCharacter(check_characters).touch="ground"
mCharacter(check_characters).Tick(bodies.OtherBody)
End If
Next
Case "wall"
For check_characters=0 To number_of_characters - 1
If bodies.ThisBody= mCharacter(check_characters).bw Then
mCharacter(check_characters).touch="wall"
mCharacter(check_characters).Tick(bodies.OtherBody)
End If
Next
Case "rock"
For check_characters=0 To number_of_characters - 1
If bodies.ThisBody= mCharacter(check_characters).bw Then
mCharacter(check_characters).touch="rock"
mCharacter(check_characters).Tick(bodies.OtherBody)
End If
Next
End Select
End Sub
Private Sub World_EndContact(Contact As B2Contact)
Dim bodies As X2BodiesFromContact = X2.GetBodiesFromContact(Contact, "character")
If bodies <> Null Then
For check_characters=0 To number_of_characters - 1
mCharacter(check_characters).touch=""
Next
End If
End Sub