32 lines
1.3 KiB
JavaScript
32 lines
1.3 KiB
JavaScript
// Generates a hand-tunable wavy ground polyline: a human picks the
|
|
// amplitude/frequency knobs per level, this just avoids typing out every point.
|
|
export function generateWave(startX, endX, step, baseY, amplitude, frequency, phase = 0) {
|
|
const points = [];
|
|
for (let x = startX; x <= endX; x += step) {
|
|
const y = baseY + Math.sin(x / frequency + phase) * amplitude;
|
|
points.push({ x, y: Math.round(y) });
|
|
}
|
|
return points;
|
|
}
|
|
|
|
// Splices a straight ramp of the given rise (positive = downhill to the right)
|
|
// between two x positions into an existing point array, in place.
|
|
export function withRamp(points, fromX, toX, rise) {
|
|
const from = points.find((p) => p.x >= fromX) || points[points.length - 1];
|
|
const to = points.find((p) => p.x >= toX) || points[points.length - 1];
|
|
const fromIndex = points.indexOf(from);
|
|
const toIndex = points.indexOf(to);
|
|
if (toIndex <= fromIndex) return points;
|
|
const baseY = from.y;
|
|
for (let i = fromIndex; i <= toIndex; i++) {
|
|
const t = (points[i].x - from.x) / (to.x - from.x);
|
|
points[i].y = Math.round(baseY + rise * t);
|
|
}
|
|
// flatten everything after the ramp to the new height
|
|
const newY = points[toIndex].y;
|
|
for (let i = toIndex + 1; i < points.length; i++) {
|
|
points[i].y = newY;
|
|
}
|
|
return points;
|
|
}
|