Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

LOIC - Low Orbit Ion Cannon (game)

edited January 2012 in Code Sharing Posts: 622

Touch the screen anywhere to fire the LOIC.

Shoot near the bases to destroy them and speed up.

--LOIC (low orbit ion cannon)
function setup()
    displayMode(FULLSCREEN)
    loic = LOIC(-25,HEIGHT/2)
    ct = {} -- orbit table
    nc = 0 -- num crater
    ctg = setTarget(loic.cp) -- current target
    ctg.rs = 0
    ntg = setTarget(loic.np) -- next target
end

function draw()
    drawMap()
    for i = 1, nc do 
        tint(127, 127, 127, 255)
        sprite("Tyrian Remastered:Firestroid",ct[i].x,ct[i].y)
        noTint()
    end
    if ctg.a then sprite("Tyrian Remastered:Button Glow",ctg.x,ctg.y) end
    if ntg.a then sprite("Tyrian Remastered:Button Glow",ntg.x,ntg.y) end
    strokeWidth(1)
    noFill()
    stroke(255, 0, 0, 255)
    loic:draw()
    if loic.rs ~= ctg.rs then
        if ctg.a and loic.s > 1 then loic.s = loic.s - 1 end
        ctg = ntg 
        ntg = setTarget(loic.np) -- next target
    end
end

function touched(touch)
    --displayMode(STANDARD)
    if touch.state == BEGAN then
        if loic.x-100 > 0 and loic.x-100 < WIDTH then
            loic.fire = true
            local lv = vec2(loic.x-100,loic.y-100)
            nc = nc + 1
            if lv:dist(vec2(ctg.x,ctg.y)) < 50 then
                ctg.a = false
                loic.s = loic.s + 1
                ct[nc] = {x=ctg.x,y=ctg.y}
            elseif lv:dist(vec2(ntg.x,ntg.y)) < 50 then
                ntg.a = false
                loic.s = loic.s + 1
                ct[nc] = {x=ntg.x,y=ntg.y}
            else    
                ct[nc] = {x=loic.x-100,y=loic.y-100}
            end
        end
    end
end

function setTarget(path)
    local r = math.random(90) + 6
    local rx = math.random(67) - 34
    local ry = math.random(67) - 34
    while (path[r].x+rx) < 100 or (path[r].x+rx) > WIDTH - 100 or
        (path[r].y+ry) < 100 or (path[r].y+ry) > HEIGHT - 100 do
        r = math.random(90) + 6
        rx = math.random(67) - 34
        ry = math.random(67) - 34
    end
    return {a=true,x=path[r].x+rx,y=path[r].y+ry,rs=loic.rs+1}    
end

function drawMap()
    background(0, 32, 255, 255) --ocean
    fill(25, 156, 36, 255) --contient
    stroke(25,156,36,255)
    rectMode(CORNER)
    rect(100,500,150,100) -- north america
    rect(200,200,150,150) -- south america
    rect(400,400,150,110) -- africa
    rect(400,500,75,25) -- north africa
    rect(470,300,75,105) -- southern africa
    rect(400,525,25,25) --spain
    rect(425,540,130,50) -- central europe
    rect(550,450,305,250) -- asia
    rect(850,600,150,75) -- northern asia
    rect(700,150,100,75) --australia
    --rect()
    strokeWidth(5) -- gradients
    stroke(0, 0, 0, 255)
    line(0,HEIGHT/2,WIDTH,HEIGHT/2)
    line(0,HEIGHT/4,WIDTH,HEIGHT/4)
    line(0,HEIGHT/4*3,WIDTH,HEIGHT/4*3)
    line(0,HEIGHT/8,WIDTH,HEIGHT/8)
    line(0,HEIGHT/8*7,WIDTH,HEIGHT/8*7)
    line(0,HEIGHT/8*5,WIDTH,HEIGHT/8*5)
    line(0,HEIGHT/8*3,WIDTH,HEIGHT/8*3)
    line(WIDTH/2,0,WIDTH/2,HEIGHT)
    line(WIDTH/4,0,WIDTH/4,HEIGHT)
    line(WIDTH/4*3,0,WIDTH/4*3,HEIGHT)
    line(WIDTH/8,0,WIDTH/8,HEIGHT)
    line(WIDTH/8*7,0,WIDTH/8*7,HEIGHT)
    line(WIDTH/8*5,0,WIDTH/8*5,HEIGHT)
    line(WIDTH/8*3,0,WIDTH/8*3,HEIGHT)
end

LOIC = class()

function LOIC:init(x,y)
    self.x = x
    self.y = y
    self.rs = 0 -- rotational shift
    self.cp = {} -- current path
    self.np = {} -- next path
    self.nnp = {} -- next next path
    self:makePath()
    self.fire = false
    self.firing = false
    self.firecount = 0
    self.s = 1 -- speed
end

function LOIC:draw()
    self.x = self.x + self.s
    if self.x > WIDTH + 150 then 
        self.x = -25
        self.rs = self.rs + 1
        self:makePath()
    end
    self:drawPath()
    self.y = (math.sin((self.x/100)+self.rs)*HEIGHT/2) + (HEIGHT/2) + 100
    if self.fire then
        self.fire = false
        self.firing = true
        self.firecount=10
    end
    if self.firing then
        self.firecount = self.firecount - 1
        if self.firecount == 0 then self.firing = false 
        elseif self.firecount < 6 then 
            fill(255, 0, 0, 255)
            stroke(127, 127, 127, 255)
        else fill(127, 127, 127, 127) 
            stroke(255, 111, 0, 255) 
        end
    else
        fill(127, 127, 127, 127)
        stroke(127, 127, 127, 255)        
    end
    strokeWidth(0)
    ellipse(self.x-100,self.y-100,100,100)
    strokeWidth(10)
    line(self.x,self.y,self.x-100,self.y-100)
    pushMatrix()
    spriteMode(CENTER)
    translate(self.x,self.y)
    rotate(-45)
    translate(-self.x,-self.y)
    sprite("Tyrian Remastered:Part I",self.x,self.y)
    popMatrix()   
end

function LOIC:makePath()
    local i
    local c = 0
    local y0, y1, y2
    for i = 100, WIDTH + 100, 10 do
        c = c + 1
        y0 = (math.sin((i/100)+self.rs)*HEIGHT/2) + (HEIGHT/2) 
        y1 = (math.sin((i/100)+self.rs+1)*HEIGHT/2) + (HEIGHT/2) 
        y2 = (math.sin((i/100)+self.rs+2)*HEIGHT/2) + (HEIGHT/2) 
        self.cp[c] = {x=i-100,y=y0}
        self.np[c] = {x=i-100,y=y1}
        self.nnp[c] = {x=i-100,y=y2}
    end
end

function LOIC:drawPath()
    local i
    --strokeWidth(2)
    noFill()
    
    for i = 1, 103 do
        strokeWidth(3)
        stroke(255,0,0,255)
        if (i * 10) > self.x - 90 then ellipse(self.cp[i].x,self.cp[i].y,10,10) end
        stroke(0, 255, 0, 255)
        if (i * 10) > self.x + 10 then ellipse(self.cp[i].x,self.cp[i].y,10,10) end
        if (i * 10) < self.x - 90 then ellipse(self.np[i].x,self.np[i].y,10,10) end
        --ellipse(self.np[i].x,self.np[i].y,10,10)
        strokeWidth(1)
        stroke(0, 255, 0, 255)
        if (i * 10) > self.x - 90 then ellipse(self.np[i].x,self.np[i].y,10,10) end
        if (i * 10) < self.x - 90 then ellipse(self.nnp[i].x,self.nnp[i].y,10,10) end
    end
end

I got bored trying to draw a good map so I'm calling it done.

The pathing is just a sine wave. I didn't have the ambition to implement the tan/sin of a satellite orbiting a sphere projected onto to 2D. A real path maybe just as fun.

Comments

  • edited January 2012 Posts: 622
  • Posts: 2,820

    Looks awsome. I'll check it out when I get a chance. If only other people would enter also.

  • Posts: 622

    I think people are working towards it, several days left, I just ran out of steam first.

  • SimeonSimeon Admin Mod
    Posts: 5,412

    Odd. I can't get this to draw anything (pasted all the code into Main, replacing what was there). I'm wondering if it's a bug in my version of Codea, does anyone else have issues?

  • Posts: 1,255

    It works for me (either 1.2.5 on iPad or 1.2.7 on iPad 2). Nifty game. I love the orbital paths.

  • Posts: 622

    Try commenting out the FULLSCREEN and see if something snuck in?

  • SimeonSimeon Admin Mod
    Posts: 5,412

    Yeah it's getting a runtime error: [string "--LOIC (low orbit ion cannon)..."]:180: attempt to index field '?' (a nil value). It's probably something on my end, glad I tested this.

Sign In or Register to comment.