Last active
January 2, 2020 18:42
-
-
Save colxi/337b5df1835e11f7af6637c0b2fd7299 to your computer and use it in GitHub Desktop.
SCRIPT-8
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
{} |
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
// title: PARTYCLES | |
const buffer = new Array(128*128).fill(-1) | |
const cursor = { | |
x : 0, | |
y : 0 | |
} | |
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,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 | |
) | |
} | |
} |
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
[] |
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
{ | |
"iframeVersion": "0.1.273", | |
"lines": [ | |
96, | |
28, | |
0, | |
0, | |
0, | |
0, | |
0, | |
0 | |
] | |
} |
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
{} |
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
{} |
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
{} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment