A while back, I spent a little time thinking about KITT without arrays, only the head index and computed distance. Here’s my current new KITT on the block in 1D. The only thing it doesn’t do (yet) is the brightness blend when switching directions, but I think that could be done by running two simultaneous waves going in opposite directions…
// GLOBAL VARIABLES
export var head = 0;
export var speed = 2.5;
var size = 0.3
export var dir = 1;
// short functions that calculate the position and speed
// of the bolt, in both directions
var modes = array(2);
modes[0] = forward;
modes[1] = reverse;
// UI sliders
export function sliderSpeed(v) {
speed = max(0.025,10 * v);
}
export function sliderSize(v) {
size = max(0.005,(0.5- (v/2)));
}
function forward() {
head += speed;
if (head >= (pixelCount-1)) dir = 1;
}
function reverse() {
head -= speed;
if (head <= 0) dir = 0;
}
// DRAWING
export function beforeRender(delta) {
modes[dir]();
}
export function render(index) {
var m = ((dir) ? (index-head) : (head-index)) / pixelCount;
m = (m > 0) ? 1-m : 0;
hsv(0,1, (m > size) * m*m*m*m)
}