public
Created

Generates a little flame. Works in Codea.

  • Download Gist
flame.lua
Lua
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
-- a little flame in a functional style
 
function particle(step, style, t)
local newStyle = step(style, t)
return {
style = newStyle,
next = function()
return particle(step, newStyle, t + 1)
end
}
end
 
function flame(particles, t)
local newParticles = evolve(particles, t)
return {
particles = newParticles,
next = function()
return flame(newParticles, t + 1)
end
}
end
 
function evolve(particles, t)
return generate(mutate(prune(particles)), t)
end
 
function mutate(particles)
return map(particles, function(p)
return p.next()
end)
end
 
function prune(particles)
return filter(particles, function(p)
return p.style.a > 0
end)
end
 
function each(things, fn)
for i, thing in ipairs(things) do
fn(thing)
end
end
 
function map(things, convert)
local mapped = {}
each(things, function(thing)
table.insert(mapped, convert(thing))
end)
return mapped
end
 
function cons(things, ...)
local all = {}
each(things, function(t)
table.insert(all, t)
end)
each({...}, function(t)
table.insert(all, t)
end)
return all
end
 
function filter(things, fn)
local filtered = {}
for i, thing in ipairs(things) do
if fn(thing) then
table.insert(filtered, thing)
end
end
return filtered
end
 
function spark(style, t)
return {
x = style.x - math.sin(t * 0.436),
y = style.y + 1,
r = style.r + (math.sin(t * style.o) * 40),
g = style.g + (math.sin(t * style.o) * 33),
b = style.b + (math.sin(t * style.o) * 15),
a = (style.a - 4 * style.o) * 0.97,
o = style.o * 0.977,
radius = 5 - t * 0.03
}
end
 
function generate(particles, t)
return cons(particles,
particle(spark, {
x = -3,
y = 2,
r = 255,
g = 40,
b = 10,
a = 220,
o = 0.5
} , 9),
particle(spark, {
x = 2,
y = 2,
r = 150,
g = 73,
b = 10,
a = 220,
o = 0.5
} , 4),
particle(spark, {
x = -1,
y = 1,
r = 255,
g = 128,
b = 51,
a = 220,
o = math.sin(t) * 0.9
}, t % 10),
particle(spark, {
x = -2,
y = 0,
r = 111,
g = 40,
b = 40,
a = 191,
o = 0.714
} , 9),
particle(spark, {
x = 1,
y = 0,
r = 170,
g = 83,
b = 32,
a = 220,
o = 0.4 + (math.sin(t) * 0.362)
}, t % 3),
particle(spark, {
x = 0.7,
y = 1,
r = 168,
g = 83,
b = 5,
a = 250,
o = 0.4
}, t % 4),
particle(spark, {
x = 1,
y = 0,
r = 177,
g = 72,
b = 17,
a = 219,
o = 1.154
}, 0),
particle(spark, {
x = -0.5,
y = 1,
r = 189,
g = 20,
b = 17,
a = 219,
o = 0.6
}, 3)
)
end
 
 
 
f = flame({}, 0)
 
function draw()
background(59, 51, 82, 255)
translate(WIDTH/2,HEIGHT/2)
each(f.particles, function(p)
drawParticle(p)
end)
f = f.next()
end
 
function drawParticle(p)
fill(p.style.r, p.style.g, p.style.b, p.style.a)
ellipse(p.style.x * 2, p.style.y * 2, p.style.radius * 2, p.style.radius * 2)
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.