Skip to content

Instantly share code, notes, and snippets.

@winkerVSbecks
Last active February 4, 2019 15:00
Show Gist options
  • Save winkerVSbecks/badd4f3b48e41f24a41d2650122ce6be to your computer and use it in GitHub Desktop.
Save winkerVSbecks/badd4f3b48e41f24a41d2650122ce6be to your computer and use it in GitHub Desktop.
SCRIPT-8
// title: Space Zapper
const size = 128;
initialState = {
rotation: 0,
frameCount: 0,
flame: 8,
point: [64, 64],
stars: range(64).map(() => ({
x: random(0, size),
y: random(0, size),
type: random(0, 7),
})),
trail: [[64, 64]].map(c => [
regularPolygon(c, 6, 20),
regularPolygon(c, 6, 16),
regularPolygon(c, 6, 12),
regularPolygon(c, 6, 8),
]),
};
const drawStar = ({ x, y, type }) => {
sprite(x, y, type);
};
const drawTrail = angle => parts => {
parts.forEach((m, idx) => polyStroke(m, angle, 5 - idx));
};
update = (state, input, elapsed) => {
state.totalElapsed += elapsed;
state.rotation = getRotation(state.totalElapsed);
state.frameCount += 1;
if (state.frameCount % 2 === 0) {
state.flame = state.flame === 10 ? 8 : state.flame + 1;
}
};
draw = state => {
clear();
const { angle } = state;
rectFill(0, 0, size, size, 7);
state.stars.forEach(drawStar);
state.trail.forEach(drawTrail(state.rotation));
sprite(62, 62, state.flame);
};
/**
* Utils
*/
function regularPolygon([cx, cy], sideCount, radius, offset = 0) {
const angle = 360 / sideCount;
const vertexIndices = range(sideCount);
return vertexIndices
.map(index => {
return {
theta: degreesToRadians(offset + angle * index),
r: radius,
};
})
.map(({ r, theta }) => [
cx + r * Math.cos(theta),
cy + r * Math.sin(theta),
]);
}
function degreesToRadians(angleInDegrees) {
return (Math.PI * angleInDegrees) / 180;
}
function getRotation(milliseconds) {
return milliseconds / 20;
}
function lerp(min, max, t) {
return min * (1 - t) + max * t;
}
{
"0": [
" 0 ",
"000 ",
" 0 ",
" ",
" ",
" ",
" ",
" ",
0
],
"1": [
"1 ",
" ",
" ",
" ",
" ",
" ",
" ",
" "
],
"2": [
"3 ",
" ",
" ",
" ",
" ",
" ",
" ",
" "
],
"3": [
"4 ",
" ",
" ",
" ",
" ",
" ",
" ",
" "
],
"4": [
" ",
" ",
" 5 ",
" 545 ",
" 5 ",
" ",
" ",
" "
],
"5": [
" ",
" ",
" 6 ",
" 6 6 ",
" 6 ",
" ",
" ",
" ",
0
],
"6": [
" ",
" ",
" ",
" 1 ",
" ",
" ",
" ",
" "
],
"7": [
" ",
" ",
" ",
" 2 ",
" ",
" ",
" ",
" "
],
"8": [
"000000 ",
"0000 ",
"0003 ",
"0033 ",
"0333 ",
"0 ",
" ",
" ",
1
],
"9": [
"000000 ",
"0000 ",
"0003 ",
"0033 ",
"0 ",
"0 ",
" ",
" "
],
"10": [
"000000 ",
"00003 ",
"00033 ",
"00333 ",
"0 ",
"0 ",
" ",
" "
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment