Skip to content

Instantly share code, notes, and snippets.

@monkeyman32123
Created March 16, 2014 21:32
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 monkeyman32123/4ae7273c8b09759aba95 to your computer and use it in GitHub Desktop.
Save monkeyman32123/4ae7273c8b09759aba95 to your computer and use it in GitHub Desktop.
--# Main
displayMode(FULLSCREEN_NO_BUTTONS)
supportedOrientations(LANDSCAPE_ANY)
function setup()
shiploc = vec2(768/2, HEIGHT/2)
highscore = readLocalData("highscore1")
if highscore == nil then
saveLocalData("highscore1", 0)
highscore = 0
else
saveLocalData("highscore1", highscore)
end
----
img = image(50,50)
setContext(img)
fill(255, 0, 0, 255)
fontSize(50)
sprite("Documents:ExampleCircle",15,15,50)
setContext()
explosion = Explosion({
image = img,
centre = vec2(0,0),
trails = true,
rows = 15,
cols = 15,
factor = 10,
friction = .005
})
---
score = 0
youdead = false
elapsed = 0
stars = true
MENU = 1
GAME = 2
GAME_OVER = 3
PAUSE = 4
screen = MENU
stroke(255, 0, 0, 139)
smooth()
lineCapMode(ROUND)
starangle = math.random()*359
lasso, originid = nil, nil
lassocol = color(0, 255, 255, 255)
simpveloc=4
shipVelocity = vec2(simpveloc,0)
anglerad = math.random(1,4)*(math.pi/2)
dots = {}
numdots = math.deg(math.pi*.5)
shipsize = 30
dotang=0
lowlim = 30
upperlim = 120
b=0
dotfx = true
--768 width
--1024 height
--256 difference
drawguides = false
guidevec= vec2(0,0)
strokecolour = color(0, 255, 0, 255)
colour = color(0, 0, 0, 255)
buttons = {}
buttons[0] = Button(false,256,768/5,768,0,"Turn Around",strokecolour,colour, strokecolour, 3, true, 2, false, false, false, false,35)
buttons[1] = Button(false,256,768/5,768,768/5-1,"Break Lasso",strokecolour,colour, strokecolour, 3, false, 1, false, false, false, false,35)
buttons[2] = Button(true,256/5,768/5,768,(768/5)*2-3,"2",strokecolour,colour, strokecolour, 3, false, 1, true, false, false, false,35)
buttons[3] = Button(true,256/5,768/5,768+((256/5)*1),(768/5)*2-3,"3",strokecolour,colour, strokecolour, 3, false, 1, true, false, false, false,35)
buttons[4] = Button(true,256/5,768/5,768+((256/5)*2),(768/5)*2-3,"4",strokecolour,colour, strokecolour, 3, false, 1, true, false, false, false,35)
buttons[5] = Button(true,256/5,768/5,768+((256/5)*3),(768/5)*2-3,"5",strokecolour,colour, strokecolour, 3, false, 1, true, false, false, false,35)
buttons[6] = Button(true,256/5,768/5,768+((256/5)*4),(768/5)*2-3,"6",strokecolour,colour, strokecolour, 3, false, 1, true, false, false, false,35)
buttons[7] =Button(false,256,256/5,768,(768/5)*2.5-30,"Velocity",strokecolour,colour, strokecolour, 3, false, 1, false, false, false, true,35)
buttons[8] =Button(false,256,256,768,(768/5)*2.5+20,"[Hidden]",strokecolour,colour, strokecolour, 3, false, 1, false, false, false, false,35)
minimap = Minimap(768,(768/5)*2.5+20,256)
--Button:init(circ, width, height, xloc, yloc, namestring, namecolour, colour, strokecolour, strokewidth, timedbool, timelen, togglebool, defaultinvis, defaultoff, istag, fontsize)
menubuttons = {}
menubuttons[0] = Button(false,512,768/5,(WIDTH/2)-512/2,HEIGHT/2,"BEGIN",strokecolour,colour, strokecolour, 3, false, 1, false, false, false, false,150)
buttons[4].on = false
gameoverbuttons = {}
gameoverbuttons[0] = Button(false,512,768/5,(WIDTH/2)-512/2,HEIGHT/2-80, "MENU",strokecolour,colour, strokecolour, 3, false, 1, false, false, false, false,150)
gameoverbuttons[1] = Button(false,512,768/5,(WIDTH/2)-512/2,HEIGHT/2-(768/5)*1.5,"RETRY",strokecolour,colour, strokecolour, 3, false, 1, false, false, false, false,150)
lasers = {}
blips = {}
lastelapsed=0
lastelapsed2=0
menfont= 0
alphagameover = 0
invincible = false
newhighscore = false
end
function draw()
background(0,0,0,255)
if screen == MENU then
fill(0,255,0,255)
textMode(CENTER)
fontSize(180)
font("Futura-Medium")
text("GYROSHIP", WIDTH/2-math.cos(ElapsedTime*5), HEIGHT-90+math.sin(ElapsedTime*5))
menubuttons[0]:draw()
--fill(0,0,255,255)
--rect(WIDTH/2, HEIGHT-200, 100,100)
fontSize(11)
textMode(CORNER)
fill(0,255,0,255)
text("Gyroship -- V1.0 -- monkeyman32123",0,0)
elseif screen == GAME then
if stars then
pushMatrix()
translate(768/2,768/2)
spriteMode(CENTER)
rotate(starangle)
sprite("Documents:stars4", 0,0, 1300)
--sprite("Documents:earth", 0,0, 768)
rectMode(CENTER)
fill(0,0,0,90)
rect(768,768,0,0)
popMatrix()
end
--font("Copperplate-Bold")
font("Copperplate-Bold")
rectMode(CORNER)
fontSize(20)
ellipseMode(CENTER)
calcshiploc()
processLasers()
processBlips()
fxdraw()
drawship()
for i=0, #buttons do
buttons[i]:draw()
end
minimap:draw()
elseif screen == GAME_OVER then
alphagameover = alphagameover + 2
if alphagameover > 255 then
alphagameover = 255
end
if stars then
pushMatrix()
translate(768/2,768/2)
spriteMode(CENTER)
rotate(starangle)
sprite("Documents:stars4", 0,0, 1300)
--sprite("Documents:earth", 0,0, 768)
rectMode(CENTER)
fill(0,0,0,90)
rect(768,768,0,0)
popMatrix()
end
--font("Copperplate-Bold")
font("Futura-Medium")
rectMode(CORNER)
fontSize(20)
ellipseMode(CENTER)
processLasers()
processBlips()
fxdraw()
drawship()
for i=0, #buttons do
buttons[i]:draw()
end
minimap:draw()
font("Futura-Medium")
fill(0,0,0,alphagameover)
stroke(0,0,0,0)
strokeWidth(0)
rect(0-1,0-2,WIDTH+2,HEIGHT+4)
fontSize(160)
fill(0,255,0,alphagameover)
text("GAME OVER", WIDTH/2, HEIGHT-90)
fontSize(70)
if newhighscore then
fill(0,145+(110*math.sin(ElapsedTime*5)),0,255)
text("NEW HIGH SCORE!!!", WIDTH/2, HEIGHT-200)
fontSize(70.5)
fill(0,145-(110*math.sin(ElapsedTime*5)),0,255)
text("NEW HIGH SCORE!!!", WIDTH/2, HEIGHT-200)
fontSize(71)
fill(0,145+(110*math.sin(ElapsedTime*5)),0,255)
text("NEW HIGH SCORE!!!", WIDTH/2, HEIGHT-200)
fontSize(71.5)
fill(0,145-(110*math.sin(ElapsedTime*5)),0,255)
text("NEW HIGH SCORE!!!", WIDTH/2, HEIGHT-200)
fontSize(70)
fill(0,255,0,255)
text("HIGH SCORE: "..highscore,WIDTH/2, HEIGHT-275)
else
text("SCORE: "..score, WIDTH/2, HEIGHT-200)
text("HIGH SCORE: "..highscore,WIDTH/2, HEIGHT-275)
end
gameoverbuttons[0].x = 0-512 + (((WIDTH/2)+256)*(alphagameover/255))
gameoverbuttons[1].x = WIDTH - (((WIDTH/2)+256)*(alphagameover/255))
for i=0, #gameoverbuttons do
gameoverbuttons[i]:draw()
end
elseif screen == PAUSE then
fill(0,255,0,255)
textMode(CENTER)
fontSize(180)
font("Futura-Medium")
text("PAUSED", WIDTH/2, HEIGHT-90)
fontSize(70)
text("SCORE: "..score, WIDTH/2, HEIGHT-200)
end
end
function drawship()
pushMatrix()
translate(shiploc.x,shiploc.y)
rotate(math.deg(anglerad)-90)
if youdead then
explosion:draw()
end
if not youdead then
sprite("Space Art:Red Ship", 0,0, shipsize)
end
popMatrix()
strokeWidth(3)
stroke(0,255,0,255)
fill(0, 0, 0, 255)
rect(768,0-1,256,HEIGHT+1.5)
end
function fxdraw()
if drawguides then
dist = guidevec:dist(shiploc)
if dist >lowlim and dist <upperlim then
strokeWidth(1)
stroke(0, 255, 0, 255)
line(guidevec.x-40, guidevec.y, guidevec.x+40, guidevec.y)
line(guidevec.x, guidevec.y-40, guidevec.x, guidevec.y+40)
fill(0,0,0,0)
ellipse(guidevec.x, guidevec.y, 60)
ellipse(guidevec.x, guidevec.y, dist*2)
end
end
if lasso ~= nil then
stroke(lassocol)
strokeWidth(3)
line(lasso.x,lasso.y, shiploc.x,shiploc.y)
if b<lassolength then
calcdots()
end
strokeWidth(10)
pushMatrix()
translate(lasso.x, lasso.y)
for i=1, #dots do
if i%2 == 0 then
rotate(dotang)
ellipse(dots[i].x,dots[i].y, 5)
rotate(-dotang)
else
rotate(-dotang)
ellipse(dots[i].x,dots[i].y, 5)
rotate(dotang)
end
end
dotang=dotang+.05
popMatrix()
end
if (drawguides==true or lasso == nil) and youdead == false then
strokeWidth(1)
stroke(0, 255, 0, 255)
fill(0, 0, 0, 0)
ellipse(shiploc.x,shiploc.y,lowlim*2)
ellipse(shiploc.x,shiploc.y,upperlim*2)
end
fill(0, 255, 0, 255)
textMode(CORNER)
font("Copperplate-Bold")
if score <= highscore then
text("HIGH SCORE: "..highscore, 10, HEIGHT-30)
text("SCORE: "..score, 768/2, HEIGHT-30)
else
text("NEW HIGH SCORE: "..score, 10, HEIGHT-30)
end
text("TIME: "..math.floor(ElapsedTime-elapsed), 768-100, HEIGHT-30)
end
function touched(touch)
if screen == MENU then
if menubuttons[0]:ispressed(touch) then
menubuttons[0]:callafterpressed()
screen = GAME
elapsed = ElapsedTime
lastelapsed = 0
lastelapsed2 = 0
end
end
if screen == GAME then
if touch.state == BEGAN and originid == nil and touch.x < 768 then
drawguides = true
originid = touch.id
elseif touch.state == ENDED and touch.id == originid and (vec2(touch.x,touch.y):dist(shiploc) < upperlim and vec2(touch.x,touch.y):dist(shiploc) >lowlim) then
b=0
local t = (vec2(touch.x,touch.y))
dots = nil
dots = {}
drawguides = false
initlasso(t)
originid = nil
elseif touch.state == ENDED and touch.id == originid then
drawguides = false
originid = nil
end
for i=0,#buttons do
if buttons[i]:ispressed(touch) then
print(i)
buttons[i]:callafterpressed()
processbuttonpress(i)
end
end
if touch ~= nil and touch.id == originid then
guidevec= vec2(touch.x,touch.y)
end
end
if screen == GAME_OVER then
for i=0, #gameoverbuttons do
if gameoverbuttons[i]:ispressed(touch) then
gameoverbuttons[i]:callafterpressed()
if i == 0 then
screen = MENU
resetdastuff()
elseif i == 1 then
resetdastuff()
elapsed = ElapsedTime
screen = GAME
lastelapsed = 0
lastelapsed2 = 0
end
end
end
end
end
function initlasso(t)
lassolength = vec2(t.x,t.y):dist(shiploc)
if lassolength < lowlim or lassolength > upperlim then
lasso = nil
originid = nil
return
end
lasso = vec2(t.x,t.y)
local circumf = 2*math.pi*lassolength
angleaddrad = 2*math.pi*simpveloc/circumf
local offset = lasso - shiploc
local anglebet = math.atan2(offset.y,offset.x)
local tangent = anglebet - (math.pi/2)
if (anglerad - anglebet)%(2*math.pi) > math.pi then
anglerad = tangent
else
anglerad = tangent + math.pi
angleaddrad = -angleaddrad
end
end
function calcshiploc()
if lasso ~= nil then
anglerad = anglerad+ angleaddrad
end
if lasso ~= nil then
shiploc = lasso + (shiploc - lasso):rotate(angleaddrad)
else
shiploc = shipVelocity:rotate(anglerad) + shiploc
end
if shiploc.x < 0+10 or shiploc.x > 768-10 or shiploc.y < 0+10 or shiploc.y > 768-10 then
turnaround()
end
end
function calcdots()
if dotfx == true then
b=b+lassolength/math.sqrt(lassolength*4)
if b>lassolength then
b= lassolength
end
else
b=lassolength
end
for i=0, numdots do
if i%2 == 0 then
b=b+3
dots[i] = vec2((b*math.cos(math.rad(i*(360/numdots)))),(b*math.sin(math.rad(i*(360/numdots)))))
b=b-3
else
dots[i] = vec2((b*math.cos(math.rad(i*(360/numdots)))),(b*math.sin(math.rad(i*(360/numdots)))))
end
end
end
function turnaround()
anglerad = anglerad + math.pi
if anglerad > 2*math.pi then
anglerad = anglerad-(2*math.pi)
end
if lasso~=nil then
angleaddrad = -angleaddrad
end
end
function breaklasso()
lasso = nil
originid = nil
dots = nil
dots = {}
end
function changeveloc(newVeloc)
simpveloc = newVeloc
shipVelocity = vec2(simpveloc, 0)
if lasso ~= nil then
initlasso(lasso)
end
for b = 2, 6 do
if b ~= simpveloc then
buttons[b].on = true
elseif b == simpveloc then
buttons[b].on = false
end
end
end
function altlasso(indivoffsets,fromlasso,xoffset,yoffset,dist,angle)
if indivoffsets then
if fromlasso then
t = vec2(lasso.x+xoffset,lasso.y+yoffset)
else
t = vec2(shiploc.x+xoffset, shiploc.y+yoffset)
end
b=0
dots = nil
dots = {}
initlasso(t)
else
t= vec2(0,0)
if fromlasso then
t.x = lasso.x + (math.cos(math.rad(angle))*dist)
t.y = lasso.y + (math.sin(math.rad(angle))*dist)
else
t.x = shiploc.x + (math.cos(math.rad(angle))*dist)
t.y = shiploc.y + (math.sin(math.rad(angle))*dist)
end
b=0
dots = nil
dots = {}
initlasso(t)
end
end
function processbuttonpress(i)
if i == 0 then
turnaround()
elseif i==1 then
breaklasso()
end
if i>=2 and i <= 6 then
changeveloc(tonumber(buttons[i].name))
for b = 2, 6 do
if b ~= i then
buttons[b].on = true
elseif b == i then
buttons[b].on = false
end
end
end
if i == 8 then
screen = PAUSE
end
end
function processLasers()
local gorp = (ElapsedTime-elapsed)/200
if gorp > .5 then
gorp= .5
end
if ElapsedTime-elapsed > lastelapsed+1.25-gorp and youdead == false then
--if math.random(0,100) > 99-ElapsedTime/20 then
local z = 3-(gorp*3)
local g = .4+gorp
if math.random(0,1) == 1 then
if lasers[0] == nil then
lasers[0] = Laser(0,0,0,0, true, true, false,z,g, ElapsedTime-elapsed)
else
lasers[#lasers+1] = Laser(0,0,0,0, true, true, false, z,g, ElapsedTime-elapsed)
end
--Laser:init(x1,y1,x2,y2, rand, vert, horiz, t, t2, e)
else
if lasers[0] == nil then
lasers[0] = Laser(0,0,0,0, true, false, true, z,g, ElapsedTime-elapsed)
else
lasers[#lasers+1] = Laser(0,0,0,0, true, false, true,z,g, ElapsedTime-elapsed)
end
end
if lasers[#lasers].vert == true then
if lasers[#lasers].x1>768 or lasers[#lasers].x1<0 then
lasers[#lasers] = nil
end
else
if lasers[#lasers].y1>768 or lasers[#lasers].y1<0 then
lasers[#lasers] = nil
end
end
lastelapsed = ElapsedTime-elapsed
end
if lasers[0] ~= nil then
for i=0, #lasers do
lasers[i]:draw()
end
end
if lasers[0] ~= nil and youdead == false then
for i=0, #lasers do
if lasers[i].alive == true and lasers[i]:getDistTo(shiploc) <15 and youdead == false and invincible == false then
--screen = GAME_OVER
youdead = true
explosion:activate(0)
sound(SOUND_EXPLODE, 10342)
lasso = nil
drawguides = false
if lasers[i].scored == true then
score = score-1
end
if score < 0 then
score = 0
end
if score > highscore then
saveLocalData("highscore1", score)
highscore = score
newhighscore = true
end
return
end
if lasers[i].alive == true and lasers[i]:getDistTo(shiploc) > 15 and lasers[i]:getDistTo(shiploc) < 50 and lasers[i].scored == false and youdead == false then
score = score + 1
lasers[i].scored = true
end
end
end
if youdead then
screen = GAME_OVER
end
for i= #lasers,0,-1 do
if lasers[i] ~= nil and lasers[i].alive == false then table.remove(lasers, i) end
end
end
function processBlips()
local glorp = (math.floor(ElapsedTime-elapsed))/50
if glorp > 2 then glorp = 2 end
if ElapsedTime-elapsed > lastelapsed2+10+glorp then
if blips[0] == nil then
blips[0] = Blips(true,false,0,0,math.random(0,6),7-glorp,50-(10*glorp))
--Blips:init(randloc, randother, xloc, yloc, type, time, size)
else
blips[#blips+1] = Blips(true,false,0,0,math.random(0,6),7-glorp,50-(10*glorp))
end
lastelapsed2 = ElapsedTime-elapsed
end
if blips[0]~=nil then for i=0, #blips do blips[i]:draw() end end
if blips[0]~= nil then
for i=#blips,0,-1 do if blips[i].fxdone == true then table.remove(blips,i) end end
end
end
function resetdastuff()
score = 0
youdead = false
elapsed = 0
stars = true
MENU = 1
GAME = 2
GAME_OVER = 3
screen = MENU
stroke(255, 0, 0, 139)
smooth()
lineCapMode(ROUND)
displayMode(FULLSCREEN_NO_BUTTONS)
supportedOrientations(LANDSCAPE_RIGHT)
starangle = math.random()*359
lasso, originid = nil, nil
lassocol = color(0, 255, 255, 255)
simpveloc=4
shipVelocity = vec2(simpveloc,0)
shiploc = vec2(768/2, HEIGHT/2)
anglerad = math.random(1,4)*(math.pi/2)
dots = nil
dots = {}
numdots = math.deg(math.pi*.5)
shipsize = 30
dotang=0
lowlim = 30
upperlim = 120
b=0
dotfx = true
--768 width
--1024 height
--256 difference
drawguides = false
guidevec= vec2(0,0)
buttons[0].on = true
buttons[0].timer = false
buttons[0].halfpressed = false
buttons[0].originid = nil
buttons[1].on = true
buttons[1].halfpressed = false
buttons[1].originid = nil
buttons[2].on = true
buttons[2].halfpressed = false
buttons[2].originid = nil
buttons[3].on = true
buttons[3].halfpressed = false
buttons[3].originid = nil
buttons[4].on = false
buttons[4].halfpressed = false
buttons[4].originid = nil
buttons[5].on = true
buttons[5].halfpressed = false
buttons[5].originid = nil
buttons[6].on = true
buttons[6].halfpressed = false
buttons[6].originid = nil
buttons[8].on = true
buttons[8].halfpressed = false
buttons[8].originid = nil
gameoverbuttons[0].on = true
gameoverbuttons[0].halfpressed = false
gameoverbuttons[0].originid = nil
gameoverbuttons[1].on = true
gameoverbuttons[1].halfpressed = false
gameoverbuttons[1].originid = nil
lasers = nil
lasers = {}
menfont= 0
alphagameover = 0
newhighscore = false
blips = nil
blips = {}
end
function setHighScore(a)
saveLocalData("highscore1",a)
end
-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
--# Button
Button = class()
function Button:init(circ, width, height, xloc, yloc, namestring, namecolour, colour, strokecolour, strokewidth, timedbool, timelen, togglebool, defaultinvis, defaultoff, istag, fontsize)
self.circle = circ
if circ == true then
self.dia = width
self.rad = width/2
else
self.wid = width
self.hei = height
end
self.x = xloc
self.y = yloc
self.loc = vec2(self.x,self.y)
self.name = namestring
self.namecol = namecolour
self.col = colour
self.strokewid = strokewidth
self.strokecol = strokecolour
self.timed = timedbool
if self.timed then
self.time = timelen
end
self.toggle = togglebool
if defaultoff then
self.on = false
else
self.on = true
end
if defaultinvis then
self.visible = false
else
self.visible = true
end
self.timer = false
self.elapsed = 0
self.pressed = false
self.originid = nil
self.halfpressed = false
self.pressable = true
self.tag = istag
self.fsize = fontsize
end
function Button:draw()
fontSize(self.fsize)
textMode(CENTER)
ellipseMode(CORNER)
if self.visible then
fill(self.col)
strokeWidth(self.strokewid)
stroke(self.strokecol)
if self.circle then
ellipse(self.x,self.y,self.dia)
fill(self.namecol)
text(self.name, self.x+self.rad,self.y+self.rad)
else
rect(self.x,self.y,self.wid,self.hei)
fill(self.namecol)
text(self.name, self.x+(self.wid/2), self.y+(self.hei/2))
end
end
if self.halfpressed and self.on and self.visible then
fill(0, 0, 0, 120)
strokeWidth(0)
if self.circle then
ellipse(self.x,self.y,self.dia)
else
rect(self.x,self.y,self.wid,self.hei)
end
end
if not self.on or not self.pressable and self.visible then
fill(0, 0, 0, 120)
strokeWidth(0)
if self.circle then
ellipse(self.x,self.y,self.dia)
else
rect(self.x,self.y,self.wid,self.hei)
end
end
if self.timer then
self:processTimer()
lineCapMode(PROJECT)
local vlorp = WIDTH-(((ElapsedTime-self.elapsed)/self.time)*self.wid)
stroke(255,0,0,255)
strokeWidth(5)
if vlorp > self.x+self.wid-5 then
vlorp= self.x+self.wid-5
elseif vlorp < self.x+5 then
vlorp = self.x+5
end
if self.timer then
line(self.x+5, self.y+5,vlorp,self.y+5)
end
lineCapMode(ROUND)
end
end
function Button:ispressed(touch)
if not self.tag then
if self.visible and self.pressable then
t = vec2(touch.x, touch.y)
if touch.state == BEGAN and self.originid == nil then
if self.circle then
if (self.loc + vec2(self.rad,self.rad)):dist(t) < self.rad then
self.originid = touch.id
self.halfpressed = true
end
else
if t.x > self.x and t.x < self.x+self.wid and t.y > self.y and t.y < self.y+self.hei then
self.originid = touch.id
self.halfpressed = true
end
end
elseif touch.state == ENDED and touch.id == self.originid then
if self.circle then
if (self.loc + vec2(self.rad,self.rad)):dist(t) < self.rad then
self.pressed = true
self.halfpressed = false
end
else
if t.x > self.x and t.x < self.x+self.wid and t.y > self.y and t.y < self.y+self.hei then
self.pressed = true
self.halfpressed = false
end
end
self.halfpressed = false
self.originid = nil
else return end
if self.pressed then
self:processBehaviour()
end
end
return self.pressed
end
end
function Button:processTimer()
if ElapsedTime - self.elapsed > self.time then
self.timer = false
if not self.toggle then
self.on = true
elseif self.toggle then
self.pressable = true
end
end
end
function Button:togglevisible()
if self.visible then self.visible = false else self.visible = true end
end
function Button:processBehaviour()
if self.toggle and self.pressable then
if not self.on then
self.on = true
elseif self.on then
self.on = false
end
end
if self.timed and self.toggle and self.pressable then
self.elapsed = ElapsedTime
self.pressable = false
self.timer = true
end
if self.timed and not self.toggle and self.on then
self.elapsed = ElapsedTime
self.on = false
self.timer = true
elseif not self.toggle and not self.on then
self.pressed = false
end
end
function Button:callafterpressed()
self.pressed = false
end
--# Laser
Laser = class()
function Laser:init(x1,y1,x2,y2, rand, vert, horiz, t, t2, e)
self.scored = false
self.alive = true
self.ON = 1
self.OFF = 2
self.time = t
self.lifespan = t2+t
self.timer = self.ON
self.elapsed = e
self.horiz = horiz
self.vert = vert
if not rand and not vert and not horiz then
self.x1=x1
self.x2=x2
self.y1=y1
self.y2=y2
elseif rand and not vert and not horiz then
elseif rand and vert and not horiz then
self.x1= math.random(shiploc.x-250,shiploc.x+250)
--self.x1= math.random(0,768)
self.x2= self.x1
self.y1= -60
self.y2= 768+60
elseif rand and not vert and horiz then
self.x1= -60
self.x2= 768+60
self.y1=math.random(shiploc.y-250,shiploc.y+250)
--self.y1=math.random(0,768)
self.y2=self.y1
elseif not rand and vert and not horiz then
self.x1=x1
self.x2=x1
self.y1=-60
self.y2=769+60
elseif not rand and not vert and horiz then
self.x1=-60
self.x2=768+60
self.y1=y1
self.y2=y1
elseif not rand and vert and horiz then
self.x1=x1
self.x2=x2
self.y1=y1
self.y2=y2
elseif rand and vert and horiz then
self.x1=x1
self.x2=x2
self.y1=y1
self.y2=y2
end
end
function Laser:draw()
strokeWidth(3* (ElapsedTime-elapsed-self.elapsed))
stroke(255, 0, 0, 255)
if ElapsedTime-elapsed-self.elapsed < self.lifespan then
if self.timer == self.ON then
local mult = (1/(ElapsedTime-elapsed-self.elapsed))-.1
if mult >1 then
mult = 1
end
if self.vert then
local h = 50*math.sin(math.rad(60))*mult-10
line(self.x1-25,768-5,self.x1+25, 768-5)
line(self.x1-25,768-5,self.x1, 768-h-5)
line(self.x1+25,768-5,self.x1, 768-h-5)
elseif self.horiz then
local h = 50*math.sin(math.rad(60))*mult-10
line(0+5, self.y1+25, 0+5, self.y1-25)
line(0+5, self.y1+25, 0+5+h, self.y1)
line(0+5, self.y1-25, 0+5+h, self.y1)
end
self:processtimer()
elseif self.timer == self.OFF then
strokeWidth(3)
stroke(255,0,0,255)
line(self.x1,self.y1,self.x2,self.y2)
end
self.alive= true
else
self.alive= false
end
end
function Laser:getDistTo(coords, override)
if self.timer == self.OFF or override then
local m= (self.y2-self.y1)/(self.x2-self.x1)
local b= (self.y2-(self.x2*m))
local dist = (math.abs(((-m)*coords.x)+coords.y+(-b)))/(math.sqrt((m^2)+1))
local len = math.sqrt(((self.x2-self.x1)^2)+((self.y2-self.y1)^2))
local p1dist = vec2(self.x1,self.y1):dist(coords)
local p2dist = vec2(self.x2,self.y2):dist(coords)
if self.vert then dist = math.abs(coords.x-self.x1) end
if p1dist > len or p2dist > len then
if p1dist < p2dist then
return p1dist
else
return p2dist
end
else
return dist
end
else
return 9999999999
end
end
function Laser:processtimer()
if ElapsedTime-elapsed-self.elapsed > self.time then
self.timer = self.OFF
end
end
function Laser:touched(touch)
-- Codea does not automatically call this method
end
--# Blips
Blips = class()
function Blips:init(randloc, randother, xloc, yloc, type, time, size)
if randloc then
self.loc = vec2(math.random(20,748),math.random(20,748))
while self.loc:dist(shiploc) < 300 do
self.loc = vec2(math.random(20,748),math.random(20,748))
end
else
self.loc = vec2(xloc,yloc)
end
if randother then
self.time = math.random(5,10)
self.size = math.random(5,30)
self.type = math.random(0,5)
else
self.time = time
self.size = size
self.type = type
end
self.elapsed = ElapsedTime
self.dots = {}
self.numdots = 25
self.stage2 = false
self.b = 0
self.circsize = nil
self.captured = false
self.dead = false
self.fxdone = false
if self.type == 0 then
self.col = color(255, 0, 0, 255)
elseif self.type == 1 then
self.col = color(255, 125, 0, 255)
elseif self.type == 2 then
self.col = color(255, 255, 0, 255)
elseif self.type == 3 then
self.col = color(0, 255, 0, 255)
elseif self.type == 4 then
self.col = color(0,0,255,255)
elseif self.type == 5 then
self.col = color(125, 0, 255, 255)
elseif self.type == 6 then
self.blend = 0
self.red = true
self.orange = false
self.yellow = false
self.green = false
self.blue = false
self.purple = false
end
end
function Blips:draw()
self.circsize = (1-((ElapsedTime-self.elapsed)/self.time))*self.size
if self.type == 6 then self:colorCycle() end
if self.circsize < 0 then self.circsize = 0 end
if not self.captured then
self:calcloc()
if not self.captured and self.circsize == 0 then self.dead = true end
if self.dead == true and self.fxdone == false and not youdead then
self:calcEffect()
end
strokeWidth(0)
ellipseMode(CENTER)
fill(self.col)
ellipse(self.loc.x,self.loc.y,self.circsize)
else
self:calcdots()
if self.stage2 ~= nil then
for i=0, #self.dots do
strokeWidth(0)
ellipseMode(CENTER)
fill(self.col)
ellipse(self.dots[i].x,self.dots[i].y,5)
end
end
end
end
function Blips:calcloc()
if self.loc:dist(shiploc) <= 75+(self.circsize/2) and self.loc:dist(shiploc) > 15+(self.circsize/2) and self.dead == false then
local ang = math.atan2(self.loc.y-shiploc.y,self.loc.x-shiploc.x)
local movement = vec2(-simpveloc,0):rotate(ang)
self.loc = self.loc + movement
elseif self.loc:dist(shiploc) <= 15+(self.circsize/2) and self.dead == false then
self.captured = true
score = score + 1
end
end
function Blips:calcEffect()
while self.fxdone == false do
if self.type == 0 then
score = score - 1
self.fxdone = true
elseif self.type == 1 then
turnaround()
self.fxdone = true
elseif self.type == 2 then
breaklasso()
self.fxdone = true
elseif self.type == 3 then
--Laser:init(x1,y1,x2,y2, rand, vert, horiz, t, t2, e)
lasers[#lasers+1] = Laser(shiploc.x+75,0,0,0,false,true,false,3,.4,ElapsedTime-elapsed)
lasers[#lasers+1] = Laser(shiploc.x-75,0,0,0,false,true,false,3,.4,ElapsedTime-elapsed)
lasers[#lasers+1] = Laser(0,shiploc.y+75,0,0,false,false,true,3,.4,ElapsedTime-elapsed)
lasers[#lasers+1] = Laser(0,shiploc.y-75,0,0,false,false,true,3,.4,ElapsedTime-elapsed)
self.fxdone = true
elseif self.type == 4 then
local newveloc = math.random(2,6)
while newveloc == simpveloc do
newveloc = math.random(2,6)
end
changeveloc(newveloc)
self.fxdone = true
elseif self.type == 5 then
--altlasso(indivoffsets,fromlasso,xoffset,yoffset,dist,angle)
altlasso(false, false, 0,0, math.random(31,119),math.random(1,360))
self.fxdone = true
elseif self.type == 6 then
self.type = math.random(0,5)
end
end
end
function Blips:calcdots()
if self.stage2 == false then
self.b=self.b+15
if self.b>75 then
self.b= 75
end
for i=0, self.numdots do
if i%2 == 0 then
self.b=self.b+3
self.dots[i] = shiploc + vec2((self.b*math.cos(math.rad(i*(360/self.numdots)))),(b*math.sin(math.rad(i*(360/self.numdots)))))
self.b=self.b-3
else
self.dots[i] = shiploc + vec2((self.b*math.cos(math.rad(i*(360/self.numdots)))),(b*math.sin(math.rad(i*(360/self.numdots)))))
end
end
if self.b == 75 then self.stage2 = true end
elseif self.stage2 then
if self.dots[0] ~= nil then
for i=#self.dots, 0, -1 do
local ang = math.atan2(self.dots[i].y-shiploc.y,self.dots[i].x-shiploc.x)
local movement = vec2(-simpveloc-1,0):rotate(ang)
self.dots[i] = self.dots[i] + movement
end
for i=#self.dots, 0, -1 do
if self.dots[i]:dist(shiploc) <= 15 then
table.remove(self.dots, i)
end
end
if #self.dots == 0 then
self.dots = nil
self.stage2 = nil
self.fxdone = true
end
else
end
end
end
function Blips:colorCycle()
if self.red then
local c2 = color(255,0,0,255)
local c1 = color(255,125,0,255)
self.col = c1:mix(c2,self.blend)
if self.blend >= 1 then
self.blend = 0
self.red = false
self.orange = true
end
elseif self.orange then
local c2 = color(255,125,0,255)
local c1 = color(255,255,0,255)
self.col = c1:mix(c2,self.blend)
if self.blend >= 1 then
self.blend = 0
self.orange = false
self.yellow = true
end
elseif self.yellow then
local c2 = color(255,255,0,255)
local c1 = color(0,255,0,255)
self.col = c1:mix(c2,self.blend)
if self.blend >= 1 then
self.blend = 0
self.yellow = false
self.green = true
end
elseif self.green then
local c2 = color(0,255,0,255)
local c1 = color(0,0,255,255)
self.col = c1:mix(c2,self.blend)
if self.blend >= 1 then
self.blend = 0
self.green = false
self.blue = true
end
elseif self.blue then
local c2 = color(0,0,255,255)
local c1 = color(125,0,255,255)
self.col = c1:mix(c2,self.blend)
if self.blend >= 1 then
self.blend = 0
self.blue = false
self.purple = true
end
elseif self.purple then
local c2 = color(125,0,255,255)
local c1 = color(255,0,0,255)
self.col = c1:mix(c2,self.blend)
if self.blend >= 1 then
self.blend = 0
self.red = true
self.purple = false
end
end
self.blend = self.blend + .03
end
--# Explosion
Explosion = class()
function Explosion:init(t)
t = t or {}
self.mesh = mesh()
local s = shader()
s.vertexProgram, s.fragmentProgram = expshader()
self.mesh.shader = s
self.mesh.texture = t.image
self.mesh.shader.friction = t.friction or .1
self.mesh.shader.separation = 0
local ft = t.factor or 10
self.mesh.shader.factor = ft
local vels = self.mesh:buffer("velocity")
local origin = self.mesh:buffer("origin")
local angv = self.mesh:buffer("angvel")
local lvl = self.mesh:buffer("level")
local m = t.rows or 20
local n = t.cols or 20
vels:resize(m*n*6)
origin:resize(m*n*6)
angv:resize(m*n*6)
lvl:resize(m*n*6)
local c = t.centre
local w,h
if type(t.image) == "string" then
local img = readImage(t.image)
w,h = img.width,img.height
else
w,h = t.image.width,t.image.height
end
local w = t.width or w
local h = t.height or h
local om = t.angularSpeed or 1/ft
local xx,y = c.x - w/2,c.y - h/2
local cl = vec2(w,h):len()/2
w,h = w/m,h/n
xx,y = xx+w/2,y+h/2
local r,th,sf,x,df,tth
sf = .3
df = math.random()
for i=1,m do
x = xx
for j = 1,n do
r = self.mesh:addRect(x,y,w,h)
self.mesh:setRectTex(r,(j-1)/n,(i-1)/m,1/n,1/m)
th = 2*noise(i*sf+df,j*sf+df)*math.pi
tth = 2*om*noise(j*sf+df,i*sf+df)*math.pi
for k=1,6 do
vels[6*r-k+1] = 20*(2-(c:dist(vec2(x,y))/cl))^2
*vec4(math.cos(th),math.sin(th),0,0)
origin[6*r-k+1] = vec2(x,y)
angv[6*r-k+1] = vec2(tth,0)
lvl[6*r-k+1] = 0
end
x = x + w
end
y = y + h
end
if t.trails then
local ntr = t.trailLength or 16
self.trails = mesh()
self.trails:setColors(255,255,255,255)
s = shader()
s.vertexProgram, s.fragmentProgram = expshader()
self.trails.shader = s
self.trails.texture = t.image
self.trails.shader.friction = t.friction or .1
self.trails.shader.factor = ft
self.trails.shader.separation = t.trailSeparation or .5
vels = self.trails:buffer("velocity")
origin = self.trails:buffer("origin")
angv = self.trails:buffer("angvel")
lvl = self.trails:buffer("level")
vels:resize(ntr*m*n*6)
origin:resize(ntr*m*n*6)
angv:resize(ntr*m*n*6)
lvl:resize(ntr*m*n*6)
local yy
xx,yy = c.x - (m-1)*w/2,c.y - (n-1)*h/2
for l=1,ntr do
y = yy
for i=1,m do
x = xx
for j = 1,n do
r = self.trails:addRect(x,y,w,h)
self.trails:setRectTex(r,(j-1)/n,(i-1)/m,1/n,1/m)
self.trails:setRectColor(r,255,255,255,l*127/ntr)
th = 2*noise(i*sf+df,j*sf+df)*math.pi
tth = 2*om*noise(j*sf+df,i*sf+df)*math.pi
for k=1,6 do
vels[6*r-k+1] = 20*(2-(c:dist(vec2(x,y))/cl))^2
*vec4(math.cos(th),math.sin(th),0,0)
origin[6*r-k+1] = vec2(x,y)
angv[6*r-k+1] = vec2(tth,0)
lvl[6*r-k+1] = l - ntr - 1
end
x = x + w
end
y = y + h
end
end
end
self.start = ElapsedTime
end
function Explosion:draw()
if not self.active then
return
end
pushStyle()
local time = ElapsedTime - self.start
if not self.paused then
if self.trails then
blendMode(SRC_ALPHA,ONE)
self.trails.shader.time = time
self.trails:draw()
end
self.mesh.shader.time = time
end
blendMode(NORMAL)
self.mesh:draw()
if not self.paused then
if self.stop and ElapsedTime > self.start + self.stop then
self:deactivate()
end
end
popStyle()
end
function Explosion:activate(t,s)
t = t or 0
self.start = ElapsedTime + t
self.stop = s
self.active = true
self.paused = false
end
function Explosion:deactivate()
self.active = false
end
function Explosion:pause()
if self.paused then
self.start = ElapsedTime - self.pausetime
self.paused = false
else
self.paused = true
self.pausetime = ElapsedTime - self.start
end
end
expshader = function()
return [[
//
// The explosion vertex shader
//
precision highp float;
//This is the current model * view * projection matrix
// Codea sets it automatically
uniform mat4 modelViewProjection;
uniform float time;
uniform float friction;
uniform float factor;
uniform float separation;
lowp vec4 gravity = vec4(0.,-1.,0.,0.);
mediump float mtime = max(0.,time)*factor;
//This is the current mesh vertex position, color and tex coord
// Set automatically
attribute vec4 position;
attribute vec4 color;
attribute vec2 texCoord;
// These are vertex buffers: initial velocity of the square,
// angular velocity,
// centre of square
attribute vec4 velocity;
attribute vec2 angvel;
attribute vec2 origin;
attribute float level;
//This is an output variable that will be passed to the fragment shader
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
//varying mediump float vLevel;
// ODE: x'' = -friction x' + gravity
// Solution: A exp(- friction * time) + B + time*gravity/friction
// Initial conditions:
// A = gravity/(friction*friction) - x'(0)/friction
// B = x(0) -A
void main()
{
//Pass the mesh color to the fragment shader
vColor = color;
vTexCoord = texCoord;
//vLevel = level;
lowp vec4 pos;
mediump float t = mtime + level * separation;
lowp float angle = t*angvel.x;
highp vec4 A = gravity/(friction*friction) - velocity/friction;
highp vec4 B = vec4(origin,0.,0.) - A;
lowp mat2 rot = mat2(cos(angle), sin(angle), -sin(angle), cos(angle));
pos = (position - vec4(origin,0.,0.));
pos.xy = rot * pos.xy;
pos += exp(-t*friction)*A + B + t * gravity/friction;
if (level != 0. && t < 1.) pos = vec4(0.,0.,0.,1.);
//Multiply the vertex position by our combined transform
gl_Position = modelViewProjection * pos;
}
]],[[
//
// A basic fragment shader
//
//This represents the current texture on the mesh
uniform lowp sampler2D texture;
//The interpolated vertex color for this fragment
varying lowp vec4 vColor;
//The interpolated texture coordinate for this fragment
varying highp vec2 vTexCoord;
void main()
{
//Sample the texture at the interpolated coordinate
lowp vec4 col = texture2D( texture, vTexCoord );
col *= vColor;
//Set the output color to the texture color
gl_FragColor = col;
}
]]
end
--# Minimap
Minimap = class()
function Minimap:init(xloc,yloc, wid)
lazors = {}
lazorvert = {}
lazortime = {}
bloops = {}
self.x = xloc
self.y = yloc
self.wid = wid
self.hei = hei
self.danger = false
self.ship = nil
self.lasso = nil
self.lassolen = 0
self.shipsize = (30/768)*wid
self.alpha = 50
self.adding = true
end
function Minimap:draw()
pushMatrix()
ellipseMode(CENTER)
lineCapMode(SQUARE)
self:acquireObjects()
if self.danger then
fill(0,0,0,255)
strokeWidth(0)
rect(self.x,self.y,self.wid,self.wid)
stroke(255,0,0,self.alpha)
else
stroke(0,255,0,255)
end
strokeWidth(3)
fill(0,0,0,255)
rectMode(CORNER)
rect(self.x,self.y,self.wid,self.wid)
for i=0, #lazors do
if lazors[i] ~= nil then
if lazorvert[i] == true then
strokeWidth(2)
local c2 = color(255,0,0,255)
local c1 = color(0,255,0,255)
local c3 = c2:mix(c1,lazortime[i])
stroke(c3)
line(self.x+lazors[i],self.y+3,self.x+lazors[i],self.y+self.wid-3)
else
strokeWidth(2)
local c2 = color(255,0,0,255)
local c1 = color(0,255,0,255)
local c3 = c2:mix(c1,lazortime[i])
stroke(c3)
line(self.x+3,self.y+lazors[i],self.x+self.wid-3, self.y+lazors[i])
end
end
end
for i=0, #bloops do
if bloops[i]~= nil then
strokeWidth(0)
fill(255,255,255,255)
ellipseMode(CENTER)
ellipse(self.x+bloops[i].x,self.y+bloops[i].y,5)
end
end
if self.lasso ~= nil and not youdead then
strokeWidth(1)
fill(0, 0, 0, 0)
stroke(0,255,255,255)
line(self.x+self.lasso.x,self.y+self.lasso.y,self.x+self.ship.x,self.y+self.ship.y)
ellipse(self.x+self.lasso.x,self.y+self.lasso.y,self.lassolen*2+3.5)
end
strokeWidth(0)
fill(255,125,0,255)
ellipseMode(CENTER)
if not youdead then
ellipse(self.x+self.ship.x,self.y+self.ship.y,7)
end
if self.danger then
rectMode(CENTER)
textMode(CENTER)
fontSize(50)
strokeWidth(3)
stroke(255,0,0,self.alpha)
fill(0,0,0,self.alpha)
rect(self.x+(self.wid/2),self.y+(self.wid/2),self.wid-15,self.wid/4-20)
fill(255,0,0,self.alpha)
text("DANGER!",self.x+(self.wid/2),self.y+(self.wid/2))
if self.adding then
self.alpha = self.alpha + 5
if self.alpha >= 255 then self.adding = false end
else
self.alpha = self.alpha - 5
if self.alpha <= 50 then self.adding = true end
end
rectMode(CORNER)
else
self.alpha = 50
end
lineCapMode(ROUND)
popMatrix()
end
function Minimap:acquireObjects()
self.lasso = nil
self.lassolen = 0
self.ship = nil
lazors = nil
lazorvert = nil
lazortime = nil
bloops = nil
lazors = {}
lazorvert = {}
lazortime = {}
bloops = {}
self.danger = false
--fetch lasers
for i=0, #lasers do
if lasers[i] ~= nil and lasers[i].alive == true then
if lasers[i].vert == true then
lazortime[i] = (ElapsedTime-elapsed-lasers[i].elapsed)/lasers[i].time
if lazortime[i]>1 then lazortime[i] = 1 end
lazorvert[i] = true
lazors[i] = (lasers[i].x1/768)*self.wid
else
lazortime[i] = (ElapsedTime-elapsed-lasers[i].elapsed)/lasers[i].time
if lazortime[i]>1 then lazortime[i] = 1 end
lazorvert[i] = false
lazors[i] = (lasers[i].y1/768)*self.wid
end
end
end
--check if you are in any danger
if lasers[0] ~= nil then
for i=0, #lasers do
if lasso ~= nil then
if lasers[i]:getDistTo(lasso,true) <= lassolength+20 and lasers[i].alive == true then
self.danger = true
end
else
if lasers[i]:getDistTo(shiploc,true) <= 15 and lasers[i].alive == true then
self.danger = true
end
end
end
end
--get the ship and lasso
self.ship = vec2((shiploc.x/768)*self.wid,(shiploc.y/768)*self.wid)
if lasso ~= nil then
self.lasso = vec2((lasso.x/768)*self.wid,(lasso.y/768)*self.wid)
self.lassolen = self.ship:dist(self.lasso)
end
--get the blips
for i = 0, #blips do
if blips[i] ~= nil and not blips[i].dead and not blips[i].captured then
bloops[i] = vec2((blips[i].loc.x/768)*self.wid,(blips[i].loc.y/768)*self.wid)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment