Skip to content

Instantly share code, notes, and snippets.

@devilstower
Created January 8, 2012 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devilstower/1579471 to your computer and use it in GitHub Desktop.
Save devilstower/1579471 to your computer and use it in GitHub Desktop.
Orb-Bits
-- Orb-Bits
-- =======================
-- Version 1.0
-- 8 Jan 2012
-- Mark Sumner
-- devilstower@gmail.com
-- =======================
function setup()
    displayMode(FULLSCREEN)
    x = 100
    y = 100
    system = 1
    scale = 1
    deg = 270
    ship = Ship(111,111)
    newSystem()
    soundtimer = 0
    gameon = true
end
function newSystem()
    -- create systems
    if system == 1 then
        planets = {}
        rocks = {}
        planets[1] = Orbiter(75, 0, 20)
        planets[1].clr = color(222, 222, 222, 255)
        planets[2] = Orbiter(150, 0, 30)
        planets[2].clr = color(231, 230, 84, 255)
        planets[3] = Orbiter(220, 0, 30)
        planets[3].clr = color(134, 196, 224, 255)
        for i = 1, 36 do
            rocks[i] = Orbiter(320, i * 10, 6)
            rocks[i].clr = color(255, 0, 14, 255)
            rocks[i].tagged = true
        end
        ship = Ship(planets[1].radius, 0)
        ship.fuel = 600
    elseif system == 2 then
        planets = {}
        rocks = {}
        planets[1] = Orbiter(80, 0, 22)
        planets[1].clr = color(166, 255, 0, 255)
        planets[2] = Orbiter(150, 0, 35)
        planets[2].clr = color(0, 237, 255, 255)
        planets[3] = Orbiter(300, 0, 40)
        planets[3].clr = color(255, 0, 222, 255)
        for i = 1, 36 do
            rocks[i] = Orbiter(200 + i * 10, i * 10, 6)
            rocks[i].clr = color(255, 0, 14, 255)
            rocks[i].tagged = true
        end
        ship = Ship(planets[2].radius, 0)
        ship.fuel = 500
    elseif system == 3 then
        planets = {}
        rocks = {}
        planets[1] = Orbiter(100, 0, 22)
        planets[1].clr = color(227, 229, 223, 255)
        planets[2] = Orbiter(150, 0, 35)
        planets[2].clr = color(0, 237, 255, 255)
        planets[3] = Orbiter(290, 0, 20)
        planets[3].clr = color(255, 0, 222, 255)
        planets[4] = Orbiter(340, 0, 20)
        planets[4].clr = color(0, 255, 2, 255)
        for i = 1, 36 do
            rocks[i] = Orbiter(200 + math.random(50), i * 10, 6)
            rocks[i].clr = color(255, 0, 14, 255)
            rocks[i].tagged = true
        end
        ship = Ship(planets[4].radius, 0)
        ship.fuel = 400
    elseif system == 4 then
        planets = {}
        rocks = {}
        planets[1] = Orbiter(35, 0, 15)
        planets[1].clr = color(224, 226, 221, 255)
        planets[2] = Orbiter(75, 0, 25)
        planets[2].clr = color(180, 225, 90, 255)
        planets[3] = Orbiter(125, 0, 25)
        planets[3].clr = color(0, 236, 255, 255)
        planets[4] = Orbiter(200, 0, 20)
        planets[4].clr = color(255, 145, 0, 255)
        for i = 1, 36 do
            rocks[i] = Orbiter(260 + math.random(35), i * 10, 6)
            rocks[i].clr = color(255, 0, 14, 255)
            rocks[i].tagged = true
        end
        planets[5] = Orbiter(340, 0, 50)
        planets[5].clr = color(176, 0, 255, 255)
        
        
        ship = Ship(planets[4].radius, 0)
        ship.fuel = 600
    else
        -- random system
        numplanets = math.random(5) + 1
        planets = {}
        for i = 1, numplanets do
            planets[i] = Orbiter(i *  75, math.random(360), 
            math.random(25) + 10)
            planets[i].clr = color(155 + math.random(100), 
            155 + math.random(100), 155 + math.random(100), 255)
        end
        rocks = {}
        --ring 1
        numrocks = math.random(30) + 1
        for i = 1, numrocks do
            rocks[i] = Orbiter( 190 + math.random(30), 
            math.random(360), 6)
            rocks[i].clr = color(255, 0, 14, 255)
            rocks[i].tagged = true
        end
        ship = Ship(planets[numplanets].radius, 
        planets[numplanets].deg)
        ship.fuel = 600
    end
end
function draw()
    local i
    background(0, 0, 0)
    noSmooth()
    stroke(246, 255, 0, 255)
    pushMatrix()
    translate(WIDTH/2, HEIGHT/2)
    i = math.random(10) + 15
    line(-i, 0, i, 0)
    line(0, -i, 0, i)
    line(-i, -i, i, i)
    line(-i, i, i, -i)
    popMatrix()
    
    alltagged = true
    
    for i, p in ipairs(planets) do
        p:draw()
        if ship.fire == false and p:land(ship.x, ship.y) then
            ship:land(p)
            p.tagged = true
            if ElapsedTime > soundtimer + 1 then
                sound(SOUND_JUMP, p.radius)
                soundtimer = ElapsedTime
            end
        end
        if not p.tagged then
            alltagged = false
        end
    end
    
    for i, r in ipairs(rocks) do
        r:draw()
        if r:land(ship.x, ship.y) and not ship.exploded then
            ship.exploded = true
            ship.exptimer = 1
        end
    end
    
    if CurrentTouch.state == BEGAN or CurrentTouch.state == MOVING then
        
        if gameon then
            if not ship.exploded then
                if ship.fuel > 0 then
                    ship.fire = true
                    ship.fuel = ship.fuel - 1
                end
            end
        else
            gameon = true
            ship = Ship(1000,1)
            scale = 1
            system = 1
            newSystem()
        end
    else
        ship.fire = false
    end
    
    ship:draw()
    
    if alltagged then
        scale = scale + 0.1
        if scale > 8 then
            system = system + 1
            newSystem()
        end
    else
        if scale > 1 then
            scale = scale - 0.1
        end
    end
    
    if ship.radius < 10 then
        if scale > 0 then
            scale = scale - 0.1
        else
            gameon = false
        end
    end
    
    if ship.exploded and ship.exptimer > 20 then
        gameon = false
    end
    
    if not gameon then
        line(300, 300, 300, 330)
        line(300, 300, 315, 300)
        line(300, 330, 315, 330)
        line(315, 300, 315, 315)
        line(315, 315, 310, 315)
        
        line(320, 300, 330, 330)
        line(330, 330, 340, 300)
        line(325, 315, 335, 315)
        
        line(350, 300, 360, 330)
        line(360, 330, 370, 300)
        line(370, 300, 380, 330)
        line(380, 330, 390, 300)
        
        line(400, 300, 400, 330)
        line(400, 300, 420, 300)
        line(400, 330, 420, 330)
        line(400, 315, 410, 315)
        
        line(600, 300, 600, 330)
        line(600, 300, 620, 300)
        line(600, 330, 620, 330)
        line(620, 330, 620, 300)
        
        line(630, 330, 640, 300)
        line(640, 300, 650, 330)
        
        line(660, 300, 660, 330)
        line(660, 300, 680, 300)
        line(660, 330, 680, 330)
        line(660, 315, 670, 315)
        
        line(690, 300, 690, 330)
        line(690, 330, 710, 330)
        line(710, 330, 690, 315)
        line(690, 315, 710, 300)
        
    end
    
end
Orbiter = class()
function Orbiter:init(r, d, s)
    -- set initial location
    self.radius = r
    self.deg = d
    self.size = s
    self.x = 0
    self.y = 0
    self.dd = 100 / r * -1
    self.clr = color(255, 255, 255, 255)
    self.tagged = false
    self.scale = 1
end
function Orbiter:draw()
    local r, x, y
    noFill()
    stroke(self.clr)
    strokeWidth(2)
    -- the further from the center, the slower it goes
    self.deg = self.deg + self.dd
    if self.deg > 360 then
        self.deg = self.deg - 360
    end
    r = math.rad(self.deg)
    self.x = (self.radius * scale) * math.cos(r) + WIDTH / 2
    self.y = (self.radius * scale) * math.sin(r) + HEIGHT / 2
    pushMatrix()
    translate(self.x, self.y)
    rotate(self.deg - 90)
    ellipse(0, 0, self.size)
    x = self.size / 2
    if self.tagged then
        line(-x, 0, x, 0)
        line(0, -x, 0, x)
    end
    popMatrix()
end
    
 function Orbiter:land(x, y)
    if math.abs(self.x - x) < self.size / 2 + 10 and
    math.abs(self.y - y) < self.size / 2 + 10 then
        return true
    else
        return false
    end
    
end
Ship = class()
function Ship:init(r, d)
    self.radius = r
    self.deg = d
    self.planet = 0
    self.landed = false
    self.x = 0
    self.y = 0
    self.dd = 100 / r * -1
    self.fire = false
    self.exploded = false
    self.exptimer = 1
    self.fuel = 1000
end
function Ship:land(p)
    self.radius = p.radius + p.size / 2
    self.deg = p.deg + 2
    self.landed = true
end
    
function Ship:draw()
    local r, tx, ty, i
    -- the further from the center, the slower it goes
    self.deg = self.deg + self.dd
    if self.deg > 360 then
        self.deg = self.deg - 360
    end
    r = math.rad(self.deg)
    tx = (self.radius * scale) * math.cos(r) + WIDTH / 2
    ty = (self.radius * scale) * math.sin(r) + HEIGHT / 2
    self.x = tx
    self.y = ty
    stroke(235, 235, 234, 255)
    pushMatrix()
    translate(self.x, self.y)
    rotate(self.deg - 90)
    if not self.exploded then
        line(0, 0, 5, 15)
        line(5, 15, 10, 0)
        line(10, 0, 0, 0)
        if self.fire then
            stroke(223, 148, 161, 255)
            line(2, 0, 5, -5)
            line(8, 0, 5, -5)
            line(5, 0, 5, -11)
            self.radius = self.radius + 1
            self.dd = 100 / self.radius * -1
            self.landed = false
        else
            if self.landed == false then
                self.radius = self.radius - 100 / self.radius
                if self.radius < 0 then
                    self.radius = 0
                end
                self.dd = 100 / self.radius * -1
            end
        end
    else
        if self.exptimer < 100 then
            self.exptimer = self.exptimer + 1
            line(self.exptimer, 0, self.exptimer, 0)
            line(0, self.exptimer, 0, self.exptimer)
            line(-self.exptimer, -self.exptimer, 
            self.exptimer, self.exptimer)
            line(-self.exptimer, self.exptimer, 
            self.exptimer, -self.exptimer)
        end
    end
    popMatrix()
    pushMatrix()
    translate(WIDTH - 50, 0)
    local y
    y = (HEIGHT - 100) / 1000
    stroke(0, 216, 255, 255)
    if self.fuel < 200 then
        stroke(255, 26, 0, 255)
    end
    strokeWidth(2)
    line(20, 50, 20, 50 + y * self.fuel)
    line(1, 20, 1, 30)
    line(1, 30, 5, 30)
    line(1, 25, 5, 25)
    line(10, 20, 10, 30)
    line(10, 20, 15, 20)
    line(15, 20, 15, 30)
    line(20, 20, 20, 30)
    line(20, 20, 25, 20)
    line(20, 25, 25, 25)
    line(20, 30, 25, 30)
    line(30, 20, 30, 30)
    line(30, 20, 35, 20)
    popMatrix()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment