Created
March 20, 2015 20:01
-
-
Save pepinganos/ca193701fa16ae2435e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
------------------------------------------------------ | |
-- Fireworks with meshes for Codea | |
-- v 1.0 | |
-- Pepe, March 2015 | |
--Touch the upper part of the screen to launch bombs | |
--Touch the lower part of the screen to launch rockets | |
------------------------------------------------------ | |
--Main | |
gravity = .02 | |
size = 10 | |
fires = {} | |
particles = {} | |
function draw() | |
fill(0, 0, 0, 26) --Transparent black (25% opacity) | |
rect(0, 0,WIDTH, HEIGHT) --Erases background, automaticaly drawing tails | |
Fire.mesh:clear() | |
for i,fire in ipairs(fires) do | |
if (fire:draw()==false) then table.remove(fires,i) end | |
end | |
Fire.mesh:draw() | |
Particle.mesh:clear() | |
for i,particle in ipairs(particles) do | |
if (particle:draw()==false) then table.remove(particles,i) end | |
end | |
Particle.mesh:draw() | |
end | |
function touched(touch) | |
if touch.y>HEIGHT/2 then fireType = 0 else fireType = 1 end --Upper half: bomb; Lower: rocket | |
table.insert(fires, Fire(touch.x, 0, fireType, color(255,50,10), math.random(), math.random()*2, 180, math.random(1,4))) | |
end | |
--------------------------------- | |
Fire = class() | |
Fire.mesh = mesh() | |
Fire.mesh.texture=readImage("Planet Cute:Star") | |
function Fire:init(x,y,type,tailColor,pHue,speed,life,randomness) | |
self.pos = vec2(x, y) | |
self.life = life | |
self.col = tailColor | |
self.type = type | |
self.pHue=pHue | |
self.speed=speed | |
self.randomness = randomness | |
self.active = true | |
if self.type==0 then self.vel = vec2(0, 4 + math.random()*1.2) --BOMB | |
else self.vel = vec2(0, 2 + math.random()*1.2) --ROCKET | |
end | |
end | |
function Fire:draw() | |
if (self.pos.x > HEIGHT + 200 or | |
self.pos.x < 0 or | |
self.pos.y > WIDTH + 200 or | |
self.pos.y < -200) then self.active = false --if out of the screen | |
end | |
self.life = self.life - 1 --BOMB | |
if self.type == 0 then | |
if (self.life < 0) then | |
for i = 0, 100 do | |
table.insert(particles,Particle(self.pos, self.vel, self.pHue, 400, self.speed, 6, self.randomness)) | |
end | |
self.active = false | |
end | |
self.pos = self.pos + vec2(math.random(-1, 1),math.random(-1, 1)) | |
self.vel.y = self.vel.y - gravity | |
self.pos = self.pos + self.vel | |
self.rect = self.mesh:addRect(self.pos.x,self.pos.y,size,size,0) | |
self.mesh:setRectColor(self.rect, self.col) | |
elseif self.type == 1 then --ROCKET | |
if (self.life <0) then self.active = false end | |
self.pos = self.pos + self.vel | |
table.insert(particles, Particle(self.pos, self.vel, self.pHue, 150, math.random()/5, 3, 2)) | |
end | |
return self.active | |
end | |
--------------------------------- | |
Particle = class() | |
Particle.mesh = mesh() | |
Particle.mesh.texture=readImage("Planet Cute:Star") | |
function Particle:init(pos,vel,pHue,life,speed,pSize,randomness) | |
self.active = true | |
self.pos = vec2(pos.x, pos.y) | |
self.life = life * (math.random()) * (math.random()) * (math.random()) | |
self.col = hsv2rgb(pHue+math.random()/10,1,1) | |
self.vel = vec2(0,1):rotate(math.random(360)) * speed + vel | |
self.vel = self.vel * (math.random()*3+1) | |
self.size = pSize; | |
end | |
function Particle:draw() | |
self.vel = self.vel * .96 --friction | |
self.vel.y = self.vel.y - gravity | |
self.pos = self.pos + self.vel | |
self.life = self.life - 1 | |
if (self.life <1) then self.active = false end | |
self.rect = self.mesh:addRect(self.pos.x,self.pos.y,size,size,0) | |
self.mesh:setRectColor(self.rect, self.col) | |
return self.active; | |
end | |
--------------------------------- | |
function hsv2rgb(H,S,V) | |
if ( S == 0 ) then --//HSV from 0 to 1 | |
R = V * 255 | |
G = V * 255 | |
B = V * 255 | |
else | |
var_h = H * 6 | |
if ( var_h == 6 ) then var_h = 0 end --H must be < 1 | |
var_i = math.floor ( var_h ) | |
var_1 = V * ( 1 - S ) | |
var_2 = V * ( 1 - S * ( var_h - var_i ) ) | |
var_3 = V * ( 1 - S * ( 1 - ( var_h - var_i ) ) ) | |
if ( var_i == 0 ) then var_r = V ; var_g = var_3 ; var_b = var_1 | |
elseif ( var_i == 1 ) then var_r = var_2 ; var_g = V ; var_b = var_1 | |
elseif ( var_i == 2 ) then var_r = var_1 ; var_g = V ; var_b = var_3 | |
elseif ( var_i == 3 ) then var_r = var_1 ; var_g = var_2 ; var_b = V | |
elseif ( var_i == 4 ) then var_r = var_3 ; var_g = var_1 ; var_b = V | |
else var_r = V ; var_g = var_1 ; var_b = var_2 | |
end | |
R = var_r * 255 --RGB results from 0 to 255 | |
G = var_g * 255 | |
B = var_b * 255 | |
return color(R,G,B) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment