Skip to content

Instantly share code, notes, and snippets.

@Synvox
Created October 5, 2012 18:19
Show Gist options
  • Save Synvox/3841479 to your computer and use it in GitHub Desktop.
Save Synvox/3841479 to your computer and use it in GitHub Desktop.
Neon Defender
--# Main
function setup()
displayMode(FULLSCREEN)
instances={}
currentIndex=0
cannon=Cannon()
table.insert(instances,cannon)
touchDown=false
shooting=false
end
function dist(o1,o2)
return math.sqrt(math.pow((o1.x-o2.x),2)+math.pow((o1.y-o2.y),2))
end
function draw()
background(0, 0, 0, 255)
stroke(0,128,255)
noFill()
strokeWidth(3)
ellipseMode(RADIUS)
if touchDown and not shooting then
if CurrentTouch.x < WIDTH/2 then
cannon.direction = cannon.direction + 0.05
else
cannon.direction = cannon.direction - 0.05
end
end
if math.random(0,50) == 1 then
table.insert(instances,Enemy())
end
for i,obj in ipairs(instances) do
currentIndex = i
obj:draw()
end
end
function touched(t)
if t.state == ENDED then
touchDown=false
else
touchDown=true
end
end
--# Cannon
Cannon = class()
function Cannon:init()
self.x = WIDTH/2
self.y = 0
self.direction = math.pi/2
self.size=32
self.timer=10
self.type="cannon"
end
function Cannon:draw()
pushMatrix()
stroke(255, 0, 0, 255)
noFill()
local btnObj={}
btnObj.x=WIDTH - 100
btnObj.y=300
if CurrentTouch.state==BEGAN and touchDown and dist(btnObj,CurrentTouch) < 50 then
touchDown=false
shooting=true
cannon.direction = cannon.direction + 0.05
fill(255, 0, 0, 255)
for i=0,1 do
local particle=Particle(
self.x+50*math.cos(self.direction),
self.y+50*math.sin(self.direction)
)
particle.vy=10
table.insert(instances,particle)
end
local bullet=CannonBullet(
self.x+50*math.cos(self.direction),
self.y+50*math.sin(self.direction)
)
bullet.vx=math.cos(self.direction)*10
bullet.vy=math.sin(self.direction)*10
table.insert(instances,bullet)
else
shooting=false
end
if self.direction<0 then
self.direction=0
end
if self.direction>math.pi then
self.direction=math.pi
end
ellipse(btnObj.x,btnObj.y,50)
noFill()
stroke(0, 128, 255, 255)
popMatrix()
ellipse(self.x,self.y,self.size)
strokeWidth(6)
line(
self.x+40*math.cos(self.direction),
self.y+40*math.sin(self.direction),
self.x+50*math.cos(self.direction),
self.y+50*math.sin(self.direction)
)
end
--# CannonBullet
CannonBullet = class()
function CannonBullet:init(x,y)
self.x = x
self.y = y
self.vx=0
self.vy=0
self.type="bullet"
sound(SOUND_HIT, 36256)
end
function CannonBullet:draw()
self.x = self.x + self.vx
self.y = self.y + self.vy
if self.y>HEIGHT then
table.remove(instances,currentIndex)
end
for i,obj in ipairs(instances) do
if obj.type=="enemy" and dist(self,obj)<30 then
--sound(SOUND_PICKUP, 42272)
sound(SOUND_SHOOT, 42273)
for i=0,10 do
table.insert(instances,Particle(self.x,self.y))
end
obj.dead=true
table.remove(instances,currentIndex)
end
end
strokeWidth(2)
ellipse(self.x,self.y,3)
end
--# Enemy
Enemy = class()
function Enemy:init()
self.x = -25
self.vx=6
if math.random(0,2)>1 then
self.x = WIDTH
self.vx=-6
end
self.y = HEIGHT - 100 + math.random(-50,50)
self.type="enemy"
self.dead=false
end
function Enemy:draw()
if self.dead then
table.remove(instances,currentIndex)
end
if self.x<-25 or self.x>WIDTH+25 then
self.vx = self.vx * -1.2
self.y=self.y-30
end
self.x = self.x + self.vx
fill(255,255,255)
noStroke()
rect(self.x-25,self.y-5,50,10)
end
--# Particle
Particle = class()
function Particle:init(x,y)
self.x = x
self.y = y
self.vx = math.random(-5,5)
self.vy = math.random(-5,5)
end
function Particle:draw()
self.x = self.x + self.vx
self.y = self.y + self.vy
self.vy = self.vy - 1
if self.y<0 then
table.remove(instances,currentIndex)
end
rect(self.x,self.y,4,4)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment