From 7b334b33c020a60b2444e9a6c2111bc3d2bf99fe Mon Sep 17 00:00:00 2001 From: nquidox Date: Thu, 16 Apr 2026 22:34:54 +0300 Subject: [PATCH] constants moved --- src/states/linear/constants.rs | 15 ++++ src/states/linear/systems.rs | 4 - src/states/linear/systems_track.rs | 133 ++++++++++++++++++----------- 3 files changed, 96 insertions(+), 56 deletions(-) create mode 100644 src/states/linear/constants.rs diff --git a/src/states/linear/constants.rs b/src/states/linear/constants.rs new file mode 100644 index 0000000..a5689ba --- /dev/null +++ b/src/states/linear/constants.rs @@ -0,0 +1,15 @@ +use crate::FACTOR; +use bevy::color::Color; + +pub const CENTER_X: f32 = 0.0; +pub const CENTER_Y: f32 = 0.0; + +// pub const FACTOR_RADIUS: f32 = FACTOR as f32 / 2.0; + +pub const STEP: f32 = FACTOR as f32 / SCALE; +pub const SCALE: f32 = 2.0; + +//COLORS +pub const PINK: Color = Color::srgb_u8(250, 0, 155); +pub const BLUE: Color = Color::srgb_u8(0, 0, 255); +pub const GREEN: Color = Color::srgb_u8(0, 255, 0); diff --git a/src/states/linear/systems.rs b/src/states/linear/systems.rs index af68fe8..d48d23c 100644 --- a/src/states/linear/systems.rs +++ b/src/states/linear/systems.rs @@ -3,10 +3,6 @@ use bevy::prelude::*; use states::linear::*; use std::f32::consts::FRAC_PI_2; -const PINK: Color = Color::srgb_u8(250, 0, 155); -const BLUE: Color = Color::srgb_u8(0, 0, 255); -const GREEN: Color = Color::srgb_u8(0, 255, 0); - pub fn draw_track_gizmos(track: Res, mut gizmos: Gizmos) { let mut current_pos = track.start_point; let mut current_dir = track.start_direction.normalize(); diff --git a/src/states/linear/systems_track.rs b/src/states/linear/systems_track.rs index 60b0172..c4d7e75 100644 --- a/src/states/linear/systems_track.rs +++ b/src/states/linear/systems_track.rs @@ -1,47 +1,71 @@ -use std::f32::consts::FRAC_PI_2; +use crate::FACTOR; use crate::states::linear::*; -use crate::{FACTOR}; use bevy::prelude::*; use bevy::reflect::List; - -pub const CENTER_X: f32 = 0.0; -pub const CENTER_Y: f32 = 0.0; - -// pub const FACTOR_RADIUS: f32 = FACTOR as f32 / 2.0; - -pub const STEP: f32 = FACTOR as f32 / SCALE; -pub const SCALE: f32 = 2.0; - +use std::f32::consts::FRAC_PI_2; pub fn setup_linear_track() -> Track { println!("Построение трека начато"); - Track { - start_point: Vec2 { - x: CENTER_X - FACTOR as f32 * 7.0, - y: CENTER_Y - FACTOR as f32 * 4.0, + Track { + start_point: Vec2 { + x: CENTER_X - FACTOR as f32 * 7.0, + y: CENTER_Y - FACTOR as f32 * 4.0, + }, + start_direction: Vec2::X, + segments: vec![ + PathSegment::Line { length: STEP * 6.0 }, + PathSegment::Turn { + radius: STEP, + left: true, }, - start_direction: Vec2::X, - segments: vec![ - PathSegment::Line { length: STEP * 6.0 }, - PathSegment::Turn { radius: STEP, left: true }, - PathSegment::Line { length: STEP }, - PathSegment::Turn { radius: STEP, left: false }, - PathSegment::Line { length: STEP * 18.0 }, - PathSegment::Turn { radius: STEP, left: true }, - PathSegment::Line { length: STEP * 4.0 }, - PathSegment::Turn { radius: STEP, left: true }, - PathSegment::Turn { radius: STEP, left: false }, - PathSegment::Line { length: STEP * 4.0 }, - PathSegment::Turn { radius: STEP, left: true }, - PathSegment::Line { length: STEP * 20.0 }, - PathSegment::Turn { radius: STEP, left: true }, - PathSegment::Line { length: STEP * 5.0 }, - PathSegment::Turn { radius: STEP, left: true }, - PathSegment::Line { length: STEP * 10.0 }, - PathSegment::Turn { radius: STEP, left: false }, - PathSegment::Line { length: STEP * 2.0 }, - ], - } + PathSegment::Line { length: STEP }, + PathSegment::Turn { + radius: STEP, + left: false, + }, + PathSegment::Line { + length: STEP * 18.0, + }, + PathSegment::Turn { + radius: STEP, + left: true, + }, + PathSegment::Line { length: STEP * 4.0 }, + PathSegment::Turn { + radius: STEP, + left: true, + }, + PathSegment::Turn { + radius: STEP, + left: false, + }, + PathSegment::Line { length: STEP * 4.0 }, + PathSegment::Turn { + radius: STEP, + left: true, + }, + PathSegment::Line { + length: STEP * 20.0, + }, + PathSegment::Turn { + radius: STEP, + left: true, + }, + PathSegment::Line { length: STEP * 5.0 }, + PathSegment::Turn { + radius: STEP, + left: true, + }, + PathSegment::Line { + length: STEP * 10.0, + }, + PathSegment::Turn { + radius: STEP, + left: false, + }, + PathSegment::Line { length: STEP * 2.0 }, + ], + } } pub fn precalculate_track(track: &Track) -> PrecalculatedTrack { @@ -50,10 +74,10 @@ pub fn precalculate_track(track: &Track) -> PrecalculatedTrack { let mut current_pos = track.start_point; let mut current_dir = track.start_direction; - + // на первом проходе заполняем все, кроме t_start и t_end, // потому что мы не можем посчитать их без общей длины - for seg in track.segments.iter(){ + for seg in track.segments.iter() { match seg { PathSegment::Line { length } => { let calc_seg = PTSegment { @@ -73,12 +97,12 @@ pub fn precalculate_track(track: &Track) -> PrecalculatedTrack { current_pos = current_pos + length * current_dir; // направление не меняем, пропуск // накапливаем длину - cumulative_length.push(cumulative_length[cumulative_length.len() -1] + *length); + cumulative_length.push(cumulative_length[cumulative_length.len() - 1] + *length); } - PathSegment::Turn {radius, left } => { + PathSegment::Turn { radius, left } => { let arc = helper_arc_calculator(current_pos, current_dir, *left, *radius); - + let calc_seg = PTSegment { t_start: 0.0, t_end: 0.0, @@ -95,31 +119,32 @@ pub fn precalculate_track(track: &Track) -> PrecalculatedTrack { current_pos = arc.end_pos; current_dir = arc.normal; // накапливаем длину - cumulative_length.push(cumulative_length[cumulative_length.len() -1] + (FRAC_PI_2 * radius)); + cumulative_length + .push(cumulative_length[cumulative_length.len() - 1] + (FRAC_PI_2 * radius)); } } } - + let last_index = cumulative_length.len() - 1; let total_length = cumulative_length[last_index]; - + // итерируемся уже по прекалькулированному вектору // заполняем точки на треке в пересчете на общую длину println!("Precalculated track:"); for (i, seg) in pt.segments.iter_mut().enumerate() { seg.t_start = cumulative_length[i] / total_length; if i < last_index { - seg.t_end = cumulative_length[i+1] / total_length; + seg.t_end = cumulative_length[i + 1] / total_length; } else { seg.t_end = 1.0; } println!("{:?}", seg); } - + pt } -pub fn helper_arc_calculator(pos: Vec2, dir: Vec2, turn_to: bool, radius: f32) -> ArcCalculation{ +pub fn helper_arc_calculator(pos: Vec2, dir: Vec2, turn_to: bool, radius: f32) -> ArcCalculation { let (normal, sign): (Vec2, f32) = if turn_to { (Vec2::new(-dir.y, dir.x), -1.0) } else { @@ -128,14 +153,18 @@ pub fn helper_arc_calculator(pos: Vec2, dir: Vec2, turn_to: bool, radius: f32) - let center = pos + normal * radius; let start_vec = pos - center; - let initial_vec = if turn_to { -start_vec.perp() } else { -start_vec }; + let initial_vec = if turn_to { + -start_vec.perp() + } else { + -start_vec + }; let angle = initial_vec.to_angle(); - - ArcCalculation{ + + ArcCalculation { normal, center, start_angle: angle, sweep_sign: sign, end_pos: center + normal.perp() * radius * sign, } -} \ No newline at end of file +}