Skip to content

Instantly share code, notes, and snippets.

@winkerVSbecks
Last active February 4, 2019 12:52
Show Gist options
  • Save winkerVSbecks/2cea0ddcdd12f23557912f375beece10 to your computer and use it in GitHub Desktop.
Save winkerVSbecks/2cea0ddcdd12f23557912f375beece10 to your computer and use it in GitHub Desktop.
SCRIPT-8
// title: Space Zap
const size = 128;
const targets = [[32, 32], [32, 64], [72, 64], [96, 96]];
initialState = {
rotation: 0,
point: [64, 64],
stars: range(64).map(() => ({
x: random(0, size),
y: random(0, size),
type: random(0, 7),
})),
targets: targets,
meteors: targets.map(c => [
regularPolygon(c, 6, 16),
regularPolygon(c, 6, 12),
regularPolygon(c, 6, 8),
regularPolygon(c, 6, 4),
]),
};
const drawStar = ({ x, y, type }) => {
sprite(x, y, type);
};
const drawMeteor = angle => (parts, mIdx) => {
parts.forEach((m, idx) => polyStroke(m, angle + 45 * mIdx, 5 - idx));
};
const drawBeam = (point, target) => {
if (point && target) {
line(point[0], point[1], target[0], target[1], 0);
}
};
update = (state, input, elapsed) => {
state.totalElapsed += elapsed;
state.rotation = getRotation(state.totalElapsed);
state.playhead = state.totalElapsed / 4000;
if (state.playhead > 1) {
state.totalElapsed = 0;
state.playhead = 0;
state.point = [random(0, 40), random(0, 40)];
}
// Choose one of the N targets based on loop time
const targetIndex = Math.floor(state.playhead * state.targets.length);
const target = state.targets[targetIndex];
// Determine a rate at which we will step forward each frame,
// making it dependent on the time elapsed since last frame
const rate = (4 * elapsed) / 1000;
if (target) {
state.target = target;
// Interpolate toward the target point at this rate
const next = [
lerp(state.point[0], target[0], rate),
lerp(state.point[1], target[1], rate),
];
state.point = next;
}
};
draw = state => {
clear();
const { angle } = state;
rectFill(0, 0, size, size, 7);
state.stars.forEach(drawStar);
state.meteors.forEach(drawMeteor(state.rotation));
drawBeam(state.point, state.target);
};
/**
* 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 ",
" ",
" ",
" ",
" "
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment