Change from dots to lines

This removes the need for Dot (now Point) to hold rendering logic
This commit is contained in:
2024-10-01 22:35:18 -06:00
parent 1b09b4e235
commit 35ae384a43

View File

@@ -3,21 +3,18 @@ use std::io::stdin;
use macroquad::prelude::*;
const DOT_RADIUS: f32 = 2.0;
const THICKNESS: f32 = 2.0;
const TAIL_LEN: usize = 250;
#[derive(Clone,Copy)]
struct Dot {
struct Point {
x: f32,
y: f32,
}
impl Dot {
impl Point {
fn new(x: f32, y: f32) -> Self {
Self {x,y}
}
fn draw(&self, color: Color) {
draw_circle(self.x, self.y,DOT_RADIUS, color);
}
}
struct DebugWindow {
@@ -53,7 +50,7 @@ enum Axis {
Two
}
struct Tail {
points: [Dot; TAIL_LEN],
points: [Point; TAIL_LEN],
head: usize,
axis: Axis,
/// What the tail treats as 0
@@ -71,7 +68,7 @@ impl Tail {
};
Self {
// start all the dots off screen
points: [Dot::new(-1.,-1.); TAIL_LEN],
points: [Point::new(-1.,-1.); TAIL_LEN],
head: 0,
axis,
x_offset,
@@ -88,15 +85,15 @@ impl Tail {
} else {
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 => {
// 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 {
self.head = 0
} else {
@@ -106,21 +103,37 @@ impl Tail {
self.points[self.head] = dot;
}
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 {
index-self.head
} else {
index + (self.points.len() - self.head)
};
let faded = virtual_index as f32 / self.points.len() as f32;
let faded_inverse = 1. - faded;
let color = Color { r: faded_inverse, g: faded, b: 0.2, a: faded+0.1};
// dots fade
dot.draw(Color { r: faded_inverse, g: faded, b: 0.2, a: faded+0.1});
let prev = self.previous(index);
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()
}
}
@@ -189,7 +202,7 @@ async fn main() {
if show_debug {
let mut debug = DebugWindow::new();
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.draw();
}