fading-tail #2

Merged
Oliver merged 8 commits from fading-tail into master 2026-01-29 20:15:07 +00:00
Showing only changes of commit 35ae384a43 - Show all commits

View File

@@ -3,21 +3,18 @@ use std::io::stdin;
use macroquad::prelude::*; use macroquad::prelude::*;
const DOT_RADIUS: f32 = 2.0; const THICKNESS: f32 = 2.0;
const TAIL_LEN: usize = 250; const TAIL_LEN: usize = 250;
#[derive(Clone,Copy)] #[derive(Clone,Copy)]
struct Dot { struct Point {
x: f32, x: f32,
y: f32, y: f32,
} }
impl Dot { impl Point {
fn new(x: f32, y: f32) -> Self { fn new(x: f32, y: f32) -> Self {
Self {x,y} Self {x,y}
} }
fn draw(&self, color: Color) {
draw_circle(self.x, self.y,DOT_RADIUS, color);
}
} }
struct DebugWindow { struct DebugWindow {
@@ -53,7 +50,7 @@ enum Axis {
Two Two
} }
struct Tail { struct Tail {
points: [Dot; TAIL_LEN], points: [Point; TAIL_LEN],
head: usize, head: usize,
axis: Axis, axis: Axis,
/// What the tail treats as 0 /// What the tail treats as 0
@@ -71,7 +68,7 @@ impl Tail {
}; };
Self { Self {
// start all the dots off screen // start all the dots off screen
points: [Dot::new(-1.,-1.); TAIL_LEN], points: [Point::new(-1.,-1.); TAIL_LEN],
head: 0, head: 0,
axis, axis,
x_offset, x_offset,
@@ -88,15 +85,15 @@ impl Tail {
} else { } else {
self.x_offset = self.x_offset + (delta_time * self.px_per_s) self.x_offset = self.x_offset + (delta_time * self.px_per_s)
} }
self.push(Dot::new(self.x_offset, self.y_offset+y_displace)); self.push(Point::new(self.x_offset, self.y_offset+y_displace));
}, },
Axis::Two => { Axis::Two => {
// both x and y will get displaced // both x and y will get displaced
self.push(Dot::new(self.x_offset+x_displace,self.y_offset+y_displace)) self.push(Point::new(self.x_offset+x_displace,self.y_offset+y_displace))
}, },
} }
} }
fn push(&mut self, dot: Dot) { fn push(&mut self, dot: Point) {
if self.head >= self.points.len() -1 { if self.head >= self.points.len() -1 {
self.head = 0 self.head = 0
} else { } else {
@@ -106,21 +103,37 @@ impl Tail {
self.points[self.head] = dot; self.points[self.head] = dot;
} }
fn draw(&self) { fn draw(&self) {
self.points.into_iter().enumerate().for_each(|(index, dot) |{ self.points.into_iter().enumerate().for_each(|(index, point) |{
// dots fade
let virtual_index = if index > self.head { let virtual_index = if index > self.head {
index-self.head index-self.head
} else { } else {
index + (self.points.len() - self.head) index + (self.points.len() - self.head)
}; };
let faded = virtual_index as f32 / self.points.len() as f32; let faded = virtual_index as f32 / self.points.len() as f32;
let faded_inverse = 1. - faded; let faded_inverse = 1. - faded;
let color = Color { r: faded_inverse, g: faded, b: 0.2, a: faded+0.1};
// dots fade let prev = self.previous(index);
dot.draw(Color { r: faded_inverse, g: faded, b: 0.2, a: faded+0.1});
if self.head == index {
draw_circle(point.x, point.y,THICKNESS*2., color);
} else if point.x == 0. || point.y == 0. || prev.x == -1. || prev.y == -1. {
draw_circle(point.x, point.y,THICKNESS/2., color);
} else {
draw_line(point.x, point.y, prev.x, prev.y, THICKNESS, color);
}
}); });
} }
fn dots(&self) -> usize { fn previous(&self, index: usize) -> Point {
let prev = if index > 0 {
index -1
} else {
self.points.len() -1
};
self.points[prev]
}
fn points(&self) -> usize {
self.points.len() self.points.len()
} }
} }
@@ -189,7 +202,7 @@ async fn main() {
if show_debug { if show_debug {
let mut debug = DebugWindow::new(); let mut debug = DebugWindow::new();
debug.add_line(format!("FPS {}, Latency {:.2}ms", get_fps(), delta_time * 1000.0)); debug.add_line(format!("FPS {}, Latency {:.2}ms", get_fps(), delta_time * 1000.0));
debug.add_line(format!("Dots {}", tail.dots())); debug.add_line(format!("Points {}", tail.points()));
debug.add_line(format!("Cursor Pos {:?}", mouse_position())); debug.add_line(format!("Cursor Pos {:?}", mouse_position()));
debug.draw(); debug.draw();
} }