Created
April 30, 2017 11:20
-
-
Save youxkei/af8743a4d536e9df3ba12af3ee125d2f 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
const wait = function*(bullet, n) { | |
for(let i = 0; i < n; ++i) { | |
bullet = yield { | |
isChangeScript: false, | |
changedBullet: bullet, | |
newBullets: [], | |
}; | |
} | |
return bullet; | |
}; | |
const script = function*(bullet) { | |
while (true) { | |
bullet = yield* wait(bullet, 1); | |
bullet = yield { | |
isChangeScript: false, | |
changedBullet: { | |
x: bullet.x, | |
y: bullet.y, | |
angle: bullet.angle + 1, | |
script, | |
scriptGenerator: null, | |
}, | |
newBullets: [ | |
{ | |
x: 0, | |
y: 0, | |
angle: 0, | |
script, | |
scriptGenerator: null, | |
}, | |
], | |
}; | |
} | |
}; | |
runScript = bullet => { | |
const scriptGenerator = bullet.scriptGenerator === null ? bullet.script(bullet) : bullet.scriptGenerator; | |
const { done, value } = scriptGenerator.next(bullet); | |
if (value === undefined) { | |
return []; | |
} | |
const { isChangeScript, changedBullet, newBullets } = value; | |
return [ | |
{ | |
x: changedBullet.x, | |
y: changedBullet.y, | |
angle: changedBullet.angle, | |
script: changedBullet.script, | |
scriptGenerator: isChangeScript ? null : scriptGenerator, | |
}, | |
...newBullets, | |
]; | |
} | |
let bullets = [ | |
{ | |
x: 0, | |
y: 0, | |
angle: 0, | |
script, | |
scriptGenerator: null, | |
}, | |
]; | |
bullets = bullets.map(runScript).reduce((accumulated, bullet) => [...accumulated, ...bullet], []); | |
console.log(bullets); | |
console.log(''); | |
bullets = bullets.map(runScript).reduce((accumulated, bullet) => [...accumulated, ...bullet], []); | |
console.log(bullets); | |
console.log(''); | |
bullets = bullets.map(runScript).reduce((accumulated, bullet) => [...accumulated, ...bullet], []); | |
console.log(bullets); | |
console.log(''); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment