Skip to content

Instantly share code, notes, and snippets.

@codetravis
Last active December 9, 2016 14:16
Show Gist options
  • Save codetravis/9146fb2b2959b5bd5681a936fb2b4bb5 to your computer and use it in GitHub Desktop.
Save codetravis/9146fb2b2959b5bd5681a936fb2b4bb5 to your computer and use it in GitHub Desktop.
7.6 Add collision groups and check for collision
Field last_enemy_time:Float
Field next_enemy_interval:Float
' create named collision groups
Const PLAYER_GROUP:Int = 1
Const ENEMY_GROUP:Int = 2
Method OnCreate()
Self.engine = New ftEngine
default_scene = engine.GetDefaultScene()
default_layer = engine.GetDefaultLayer()
Local box:ftObject = Self.engine.CreateBox(120, 20, engine.GetCanvasWidth()/2, engine.GetCanvasHeight()/2)
box.SetColor(0, 70, 70)
box.SetMaxSpeed(20.0)
box.SetMinSpeed(-20.0)
' Set the collision group the player character is part of
' and the collsion type
box.SetColGroup(PLAYER_GROUP)
box.SetColType(Self.engine.ctBox)
Self.player = New Character(box, True)
Self.enemies = New List<Character>()
Self.last_enemy_time = Millisecs()
Self.next_enemy_interval = 3000
Seed = Millisecs()
End
Method OnUpdate()
Local time_delta:Float = Float(engine.CalcDeltaTime())/60.0
If ((Millisecs() - Self.last_enemy_time) > Self.next_enemy_interval)
CreateEnemy()
Self.last_enemy_time = Millisecs()
End
player.Update(engine.GetCanvasWidth(), engine.GetCanvasHeight())
If engine.GetPaused() = False
engine.Update(time_delta)
' Tell the engine to check for collisions
engine.CollisionCheck()
End
End
Method CreateEnemy()
Local rand_width:Float = Rnd(3, 7) * 10
Local rand_height:Float = Rnd(3, 7) * 10
Local rand_y:Float = Rnd(rand_height, Self.engine.GetCanvasHeight())
Local box:ftObject = Self.engine.CreateBox(rand_width, rand_height, Self.engine.GetCanvasWidth(), rand_y)
Local rand_color:Float = Rnd(0, 3)
Local colors:Int[] = [0, 0, 0]
If (rand_color <= 1.0)
colors = [255, 0, 0]
Else If (rand_color > 1.0 And rand_color <= 2.0)
colors = [0, 255, 0]
Else
colors = [0, 0, 255]
End
box.SetColor(colors[0], colors[1], colors[2])
box.SetSpeedX(Rnd(10, 30) * -1)
' We add the enemy box to the enemy collision group
' and make it able to collide with the player
box.SetColGroup(ENEMY_GROUP)
box.SetColWith(PLAYER_GROUP, True)
enemies.AddLast(New Character(box))
Self.next_enemy_interval = Rnd(0, 3) * 1000
End
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment