constants moved

This commit is contained in:
nquidox 2026-04-16 22:34:54 +03:00
parent 0f7882d3fb
commit 7b334b33c0
3 changed files with 96 additions and 56 deletions

View file

@ -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);

View file

@ -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<Track>, mut gizmos: Gizmos) {
let mut current_pos = track.start_point;
let mut current_dir = track.start_direction.normalize();

View file

@ -1,17 +1,8 @@
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!("Построение трека начато");
@ -23,22 +14,55 @@ pub fn setup_linear_track() -> Track {
start_direction: Vec2::X,
segments: vec![
PathSegment::Line { length: STEP * 6.0 },
PathSegment::Turn { radius: STEP, left: true },
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::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::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::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::Turn {
radius: STEP,
left: true,
},
PathSegment::Line {
length: STEP * 10.0,
},
PathSegment::Turn {
radius: STEP,
left: false,
},
PathSegment::Line { length: STEP * 2.0 },
],
}
@ -53,7 +77,7 @@ pub fn precalculate_track(track: &Track) -> PrecalculatedTrack {
// на первом проходе заполняем все, кроме 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,10 +97,10 @@ 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 {
@ -95,7 +119,8 @@ 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));
}
}
}
@ -109,7 +134,7 @@ pub fn precalculate_track(track: &Track) -> PrecalculatedTrack {
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;
}
@ -119,7 +144,7 @@ pub fn precalculate_track(track: &Track) -> PrecalculatedTrack {
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,10 +153,14 @@ 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,