Skip to content

Instantly share code, notes, and snippets.

@gabrielflorit
Forked from icarito/README.md
Last active February 10, 2019 05:09
Show Gist options
  • Save gabrielflorit/8a0fb2b374bc1063b8e4c8fb913e6f1b to your computer and use it in GitHub Desktop.
Save gabrielflorit/8a0fb2b374bc1063b8e4c8fb913e6f1b to your computer and use it in GitHub Desktop.
SCRIPT-8
// title: Fire effect with FPS
// adapted from http://fabiensanglard.net/doom_fire_psx/
const width = 128
const height = 64
const offset = 64
var fb = []
range(0, width).forEach((x)=>
{
fb[x] = []
range(0, height-1).forEach((y)=>
{
fb[x][y]=7
})
fb[x][height-1] = 1
})
initialState = {
firePixels: fb,
frame: 0,
fps: 0
}
update = (state, input, elapsed) => {
state.fps = Math.floor(1000/elapsed)
state.frame = state.frame + 1
if(!(state.frame%2==0)){
return
}
for (let y of range(height-2, 0, -1)) {
for (let x of range(0, width)) {
var rand = Math.round(Math.random());
var rand2 = Math.round(Math.random());
var dst = Math.round(clamp(x - rand + rand2, 0, width-1))
state.firePixels[dst][y-rand * 2] = clamp(state.firePixels[x][y+1] + rand, 0, 7)
}
}
}
draw = (state)=> {
clear()
print(0, 0, `fps: ${state.fps}`, 0)
let counter = 0
for (let x of range(0, width)) {
for (let y of range(0, height)) {
line(x,y + offset,x,y + offset,state.firePixels[x][y])
counter++
}
}
print(0, 8, `line calls: ${counter}`, 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment