Public Sub Start
If X2.IsRunning Then Return
'Make sure that start was not called while the message appeared (this can happen in edge cases where the app moves to the background
'and then quickly to the foreground several times).
StartIndex = StartIndex + 1
Dim MyIndex As Int = StartIndex
Wait For (ShowMessage("Get Ready...")) Complete (Success As Boolean)
If MyIndex <> StartIndex Then Return
X2.Reset
Multitouch.ResetState
Dim ratio As Float = ivForeground.Width / ivForeground.Height
WorldWidth = 6
WorldHeight = WorldWidth / ratio
X2.ConfigureDimensions(X2.CreateVec2(WorldWidth / 2, WorldHeight / 2), WorldWidth)
'comment to disable debug drawing
' X2.EnableDebugDraw
TileMap.Initialize(X2, File.DirAssets, "space shooter.json", Null)
'square tiles. The base size is the world width.
TileMap.SetSingleTileDimensionsInMeters(WorldWidth / TileMap.TilesPerRow, WorldWidth / TileMap.TilesPerRow)
TileMap.PrepareObjectsDef(ObjectLayer)
lblMessages.Visible = False
LeftBorder = TileMap.CreateObject2ByName(ObjectLayer, "left border")
TileMap.CreateObject2ByName(ObjectLayer, "right border")
Ship = TileMap.CreateObject2ByName(ObjectLayer, "ship")
If xui.IsB4J Then
Ship.Body.SetTransform(X2.CreateVec2(Ship.Body.Position.X, 0.5), 0)
End If
Ship.Body.LinearDamping = 4 'stop quickly
ShipWidth = X2.GetShapeWidthAndHeight(Ship.Body.FirstFixture.Shape).X
ScoreLabel1.SetValueNow(0)
CreateShields
SetLives(3)
ivForeground.Visible = True
'motor is used in B4A and B4i to move the ship to the touch position.
Dim motor As B2MotorJointDef
motor.Initialize(LeftBorder.Body, Ship.Body)
motor.MaxMotorForce = 0
motor.CollideConnected = True
ShipMotor = world.CreateJoint(motor)
ShipMotor.CorrectionFactor = 0.2
X2.Start
InputsDisabled = False
End Sub
Sub World_BeginContact (Contact As B2Contact)
Dim bc As X2BodiesFromContact = X2.GetBodiesFromContact(Contact, "fire")
If bc <> Null And bc.OtherBody.Name.StartsWith("asteroid") Then
X2.AddFutureTask2(Me, "Destroy_Asteroid", 0, bc, True)
Else
Log("fire")
bc = X2.GetBodiesFromContact(Contact, "ship")
If bc <> Null Then
If bc.OtherBody.Name = "power" Then
X2.AddFutureTask(Me, "Power_Up", 0, bc.OtherBody)
' Else If bc.OtherBody.Name.StartsWith("asteroid") And IsProtected = False Then
' X2.AddFutureTask(Me, "Destroy_Ship", 0, Null)
End If
End If
End If
If bc <> Null Then
If bc.OtherBody.Name = "left edge" Then
X2.AddFutureTask(Me, "change_direction", 0, True)
Else If bc.OtherBody.Name = "right edge" Then
X2.AddFutureTask(Me, "change_direction", 0, False)
Else If bc.OtherBody.Name = "ship laser" Then
X2.AddFutureTask(Me, "EnemyHit_WithMissle", 0, bc)
Else If bc.OtherBody.Name = "ship" Then
X2.AddFutureTask(Me, "ShipHit_WithEnemy", 0, bc)
End If
End If
' bc = X2.GetBodiesFromContact(Contact, "enemy laser")
' If bc <> Null And bc.OtherBody.Name = "ship" Then
' X2.AddFutureTask(Me, "ShipHit_WithMissile", 0, bc)
' End If
bc = X2.GetBodiesFromContact(Contact, "shield")
Log("shieldcontact1")
If bc <> Null Then
X2.AddFutureTask(Me, "Shield_Hit", 0, bc)
End If
End Sub
Private Sub Shield_Hit (ft As X2FutureTask)
Log("Shield_Hit")
Dim bodies As X2BodiesFromContact = ft.Value
If bodies.OtherBody.Name <> "enemy" Then
bodies.OtherBody.Delete(X2.gs) 'delete the missile
End If
Dim shld As Shield = bodies.ThisBody.DelegateTo
shld.Hit(bodies.ThisFixture)
End Sub