Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
8.10 Projectile creation code
#FantomX_UsePhysics = True
Import fantomX
Import character
' Import projectile
Import projectile
Import custom_engine
Class Game Extends App
Field engine:ftEngine
Field default_layer:ftLayer
Field default_scene:ftScene
Field player:Character
Field enemies:List<Character>
Field last_enemy_time:Float
Field next_enemy_interval:Float
' add collection to hold the games projectiles
Field projectiles:List<Projectile>
Const PLAYER_GROUP:Int = 1
Const ENEMY_GROUP:Int = 2
' Add projectile collision group
Const PROJECTILES:Int = 3
Method OnCreate()
Self.engine = New CustomEngine
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)
box.SetColGroup(PLAYER_GROUP)
box.SetColType(Self.engine.ctBox)
box.SetText("PLAYER")
Local projectile_type:ProjectileType = New ProjectileType()
Self.player = New Character(box, 3, True, projectile_type)
Self.enemies = New List<Character>()
Self.last_enemy_time = Millisecs()
Self.next_enemy_interval = 3000
' initialize projectile list
Self.projectiles = New List<Projectile>()
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 (KeyDown(KEY_SPACE))
CreateProjectile(player)
End
For Local enemy:Character = Eachin Self.enemies
enemy.Update(engine.GetCanvasWidth(), engine.GetCanvasHeight())
If (enemy.box.GetText() = "DESTROYED")
enemy.box.Remove()
Self.enemies.RemoveFirst(enemy)
End
End
If engine.GetPaused() = False
engine.Update(time_delta)
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 projectile_type:ProjectileType = New ProjectileType()
Local rand_color:Float = Rnd(0, 3)
Local health = 1
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]
health = 2
Else
colors = [0, 0, 255]
health = 3
End
box.SetColor(colors[0], colors[1], colors[2])
box.SetSpeedX(Rnd(10, 30) * -1)
box.SetColGroup(ENEMY_GROUP)
box.SetColWith(PLAYER_GROUP, True)
box.SetText("ENEMY")
enemies.AddLast(New Character(box, health, False, projectile_type))
Self.next_enemy_interval = Rnd(0, 3) * 1000
End
' Add a method to create new projectiles
Method CreateProjectile(character:Character)
Local projectile_box:ftObject = Self.engine.CreateBox(10, 20, character.box.GetPosX(), character.box.GetPosY())
projectile_box.SetColGroup(PROJECTILES)
If (character.is_player)
projectile_box.SetSpeedX(10)
projectile_box.SetColor(0, 255, 255)
projectile_box.SetColWith(ENEMY_GROUP, True)
Else
projectile_box.SetSpeedX(-10)
projectile_box.SetColor(255, 255, 0)
projectile_box.SetColWith(PLAYER_GROUP, True)
End
projectile_box.SetText("PROJECTILE")
Self.projectiles.AddLast(New Projectile(projectile_box, character.projectile_type))
End
Method OnRender()
engine.Clear(255, 255, 255)
engine.Render()
engine.SetColor(0, 70, 70)
engine.RenderFlush()
End
End
Function Main()
New Game()
End
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment