Skip to content

Instantly share code, notes, and snippets.

@youxkei
Created April 30, 2017 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save youxkei/af8743a4d536e9df3ba12af3ee125d2f to your computer and use it in GitHub Desktop.
Save youxkei/af8743a4d536e9df3ba12af3ee125d2f to your computer and use it in GitHub Desktop.
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