Skip to content

Instantly share code, notes, and snippets.

@mostalive
Forked from anonymous/Main.lua
Created March 7, 2012 18:34

Revisions

  1. @invalid-email-address Anonymous created this gist Mar 7, 2012.
    86 changes: 86 additions & 0 deletions Main.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,86 @@
    hero = nil
    enemies = nil
    bgLines = nil
    explosion = nil
    killCount=0


    GAME_PLAYING = 0
    GAME_DEAD = 1
    GAME_WON = 2
    state = GAME_PLAYING
    heroSize=100

    -- Use this function to perform your initial setup
    function setup()
        state = GAME_PLAYING

        iparameter("BackgroundSpawnRate",0,3,2)

        hero = Invader()
        hero.position = vec2(WIDTH/2, 150)

        enemies = EnemyHorde()
        enemies.heroBullets = hero.bullets

        bgLines = StreamingLines()
    end

    function touchingAtPos()
        if CurrentTouch.state == BEGAN or 
           CurrentTouch.state == MOVING then
            return vec2( CurrentTouch.x, CurrentTouch.y )
        end 

        return nil
    end

    function showScore()
        watch("killCount")
    end
    -- This function gets called once every frame
    function draw()

        background(0, 0, 0, 255)

        bgLines.spawnRate = BackgroundSpawnRate

        bgLines:update()
        bgLines:draw()
        showScore()

        if state == GAME_PLAYING then 
            -- Process touch
            touch = touchingAtPos()
            if touch then
                if touch.x < (hero.position.x - 10) then
                    hero.position.x = hero.position.x - 3
                elseif touch.x > (hero.position.x + 10) then
                    hero.position.x = hero.position.x + 3
                end
            end

            if killCount == 20 then
                enemies.spawnPattern = ENEMY_SPAWN_HARD
            end

            enemies:draw()

            hero:draw()

            -- Check if hero is hit
            for i,v in ipairs(enemies.units) do
                if v:dist(hero.position) < heroSize then
                    state = GAME_DEAD
                    explosion = Explosion(hero.position)
                end
            end
        elseif state == GAME_DEAD then
            if explosion then
                explosion:draw()
                if explosion:isDone() then
                    explosion = nil
                end
            end
        end
    end