Skip to content

Instantly share code, notes, and snippets.

@colxi
Last active August 9, 2020 21:45
Show Gist options
  • Save colxi/ba79ddac3daf78cc4730a9136be807fc to your computer and use it in GitHub Desktop.
Save colxi/ba79ddac3daf78cc4730a9136be807fc to your computer and use it in GitHub Desktop.
SCRIPT-8
// title: SandBox
// author : colxi - colxi.info - contact@colxi.info
// [ work in progress ]
const buffer = new Array(128*128).fill(-1)
const cursor = {
x : 60,
y : 60
}
const materials = {
sand: 1,
oil : 3,
water : 5,
solid : 6,
empty : -1
}
Object.defineProperty(materials, materials.sand, {value: 'sand'})
Object.defineProperty(materials, materials.solid, {value: 'solid'})
Object.defineProperty(materials, materials.water, {value: 'water'})
let selectedMaterial = 'sand'
function selectNextMaterial(){
const names = Object.keys(materials)
let i = names.indexOf(selectedMaterial)
i++
selectedMaterial = names[ i ] || names[ 0 ]
}
function getParticle(x,y){
return buffer[ (y*128) + x ]
}
function setParticle(x,y,m){
if(m === -1) buffer[ (y*128) + x ] = -1
else{
if(typeof m==='number') buffer[ (y*128) + x ] = m
else buffer[ (y*128) + x ] = materials[ m ]
}
}
init = state => {
}
update = (state, input, elapsed) => {
if(input.right) cursor.x++
if(input.left) cursor.x--
if(input.down) cursor.y++
if(input.up) cursor.y--
if(input.bPressed) selectNextMaterial()
if(input.a) setParticle(cursor.x,cursor.y,selectedMaterial)
for (let _y=0; _y<128; _y++){
const y =127 - _y // frame ? _y : 127 - _y
for( let _x=0; _x<128; _x++ ){
const x =( y%2 ) ? _x : 127 - _x
let material = getParticle(x,y)
if( material === -1 ) continue
if(material===materials.solid) continue
setParticle(x,y,-1)
if(getParticle(x,y+1) === -1 ){
setParticle(x,y+1,material)
}else{
let direction = (Math.random() >= 0.5) ? 1 : -1
if(getParticle(x+(1*direction),y) === -1 ){
setParticle(x+(1*direction), y ,material)
}else setParticle(x,y,material)
}
}
}
}
let frame = false
draw = state => {
frame = frame ? false :true
clear()
setPixel(cursor.x, cursor.y, 1)
print(1, 1,'A-APPLY | B-CHANGE | CURSOR: MOVE')
print(1, 10,'SELECTED : ' + selectedMaterial)
for (var y=0; y<128; y++){
for( let x=0; x<128; x++ ){
const particle = getParticle(x,y)
if(particle === -1 ) continue
setPixel(x, y, particle)
}
}
}
class Vector{
constructor(x,y) {
this.x =x || 0
this.y =y || 0
}
}
class Particle {
constructor(options){
this.material = options.material
this.position = new Vector(
options.coordinate.x,
options.coordinate.y
)
this.speed = new Vector(
options.speed.x,
options.speed.y
)
this.acceleration = new Vector(
options.acceleration.x,
options.acceleration.y
)
}
}
{
"iframeVersion": "0.1.273",
"lines": [
99,
29,
0,
0,
0,
0,
0,
0
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment