Skip to content

Instantly share code, notes, and snippets.

@deactive
Created February 17, 2015 07:47
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 deactive/c9b2c1380d68778e5bec to your computer and use it in GitHub Desktop.
Save deactive/c9b2c1380d68778e5bec to your computer and use it in GitHub Desktop.
Shmup bullet pattern generator
--# Main
-- PatternGen
parameter.integer("shotfrequency",1,10,4)
parameter.integer("speed",0,20,1)
parameter.integer("objects",1,20,10)
parameter.integer("btype",0,2,0)
function setup()
dnl = false
getImage()
Playerpos = vec2(0,0)
timer = 0
downloadtimer = 0
a = 0
fps = FPS()
end
function draw()
background(40, 40, 50)
if dnl == true then
downloadtimer = downloadtimer + 1
if downloadtimer == 1 then
FoeMissileSprite = readImage("Documents:FoeBullets-1")
fm = FoeMissilesm()
end
fm:draw()
timer = (timer+1)%100
a = a + 0.5
if a == 360 then
a = 0
end
if timer%shotfrequency == 0 then
for i=1,objects do
fm:spawnMissile(a-i*18,vec2(WIDTH/2,HEIGHT/2),speed,btype)
end
end
fps:draw()
end
end
function getImage()
http.request("https://dl.dropboxusercontent.com/s/6srl2d0omvc709l/FoeBullets-1.png", gotImage)
end
function gotImage(img)
saveImage("Documents:FoeBullets-1",img)
sound(SOUND_PICKUP, 16598)
dnl = true
end
--# Bullets
--# Bullets
FoeMissilesm = class()
FoeMissiles = 0
function FoeMissilesm:init()
self.missiles = {}
self.texture = FoeMissileSprite
self.spriteSize = vec2(9, 9)
self.mesh = mesh()
self.mesh.texture = self.texture
self.index = 0
self.count = 0
self.forcepause = 0
self.shaketimer = 0
t = 0
pos = vec2(0,0)
w = 40
h = 40
c = color(129, 255, 0, 255)
sc = 1 -- (math.sin(v+t*5)+1)/2*0.35 + 0.65
rotation = 0
rot = 0
index = 0
c = color(0,0)
end
function FoeMissilesm:spawnMissile(a,pos2,speed,btype)
if table.maxn(self.missiles) < 2500 and self.forcepause == 0 then
self.count = self.count + 1
index = self.mesh:addRect(pos2.x, pos2.y, 12, 12)
c = color(197, 0, 197, 255)
if btype == 0 then
self.mesh:setRectTex(index,0,0,0.30,1)
c = color(255, 0, 86, 255)
end
if btype == 1 then
self.mesh:setRectTex(index,0.299,0,0.36,1)
c = color(21, 209, 248, 255)
end
if btype == 2 then
self.mesh:setRectTex(index,0.66,0,0.333,1)
c = color(177, 0, 255, 255)
speed = speed * 2
end
--self.mesh:setRectColor(index, c)
rot = math.atan((pos2.x - Playerpos.x)/(Playerpos.y - pos2.y)) - 15
-- text("rot "..v.angle,250,100)
if pos2.y <= Playerpos.y then
rot = rot + 15.7
end
table.insert(self.missiles,FoeBolt(index,pos2.x,pos2.y,a,speed,btype,rot,c))
if self.count == 2000 then
self.forcepause = 1
end
end
if table.maxn(self.missiles) == 0 then
self.forcepause = 0
self.count = 0
self.mesh:clear()
self.missiles = {}
end
end
function FoeMissilesm:removeRect(index,k)
---self.mesh:setRect(index,0, 0, 0, 0)
--table.remove(self.missiles,k)
end
function FoeMissilesm:draw(dt)
text("items : "..table.maxn(self.missiles),100,110)
self.mesh:draw()
c = color(129, 255, 0, 255)
sc = 1 -- (math.sin(v+t*5)+1)/2*0.35 + 0.65
rotation = 0
if self.shaketimer >= 0 then
self.shaketimer = self.shaketimer - 0.5
ws = math.random(self.shaketimer*(-1),self.shaketimer)
hs = math.random(self.shaketimer*(-1),self.shaketimer)
else
ws = 0
hs = 0
end
for k,v in ipairs(self.missiles) do
pos = vec2(v.x,v.y)
v.width = v.width + 0.1
if v.btype == 0 then
rotation = t*50
v.speed = v.speed + 0.05
end
if v.btype == 1 then
rotation = v.rotation
v.speed = v.speed + 0.02
end
if v.btype == 2 then
v.speed = v.speed + 0.02
end
self.mesh:setRect(v.index, pos.x+ws, pos.y+hs,v.width/2 * sc, v.width/2 * sc, rotation)
self.mesh:setRectColor(v.index,v.color.r,v.color.g,v.color.b,v.alpha)
if pos.x < -20 or pos.x > WIDTH + 20 then
--self:removeRect(v.index,k)
v.alpha = 0
table.remove(self.missiles,k)
end
if pos.y < -20 or pos.y > HEIGHT + 20 then
--self:removeRect(v.index,k)
v.alpha = 0
table.remove(self.missiles,k)
end
v.x = v.x + v.xa * v.speed
v.y = v.y - v.ya * v.speed
if PlayerHit == true then
PlayerHitTimer = PlayerHitTimer + 1
if PlayerHitTimer > 500 then
PlayerHit = false
PlayerHitTimer = 0
end
end
end
FoeMissilesCount = self.count
t = t + DeltaTime * 0.1
end
function FoeMissilesm:Shake()
self.shaketimer = 20
end
FoeBolt = class()
function FoeBolt:init(i,x,y,a,speed,btype,rotation,color1)
self.index = i
self.x = x
self.y = y
self.angle = a
self.rotation = rotation
self.xa = math.sin(math.rad(self.angle))
self.ya = math.cos(math.rad(self.angle))
self.timer = 0
self.pow = 3
self.width = width or 20
self.speed = speed or 0.25
self.btype = btype or 0
self.pos = vec2(self.x,self.y)
self.alpha = 255
self.color = color1 or color(171, 156, 156, 255)
end
--# Utils
FPS = class()
function FPS:init(frac)
self.val = 60
self.frac = frac or 0.01
-- self.t0 = os.clock()
self.t0 = ElapsedTime
end
function FPS:draw()
pushStyle()
local vShift = 0
-- if jobs:active() then vShift = 30 end
-- if jobs.active then vShift = 30 end
-- update FPS value with some smoothing
local old = self.val
local frac = self.frac
-- local t1 = os.clock()
local t1 = ElapsedTime
local delta = t1 - self.t0
self.t0 = t1
local new = 1/delta or old
if new>65 then new=65 end
local ratio = new/old
if 0.5<ratio and ratio<2 then new = old*(1-frac)+ new*frac end
self.val = new
-- write the FPS on the screen
if math.floor(new) >= 60 then
fill(48, 255, 0, 255)
else
fill(255, 0, 0, 255)
end
-- fontSize(25)
--font("BITDUST TWO")
rectMode(CENTER)
text(math.floor(new).." FPS",WIDTH/2,20-vShift)
popStyle()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment