spawn ball + run in set

This commit is contained in:
nquidox 2026-04-16 23:05:31 +03:00
parent 7b334b33c0
commit 5ba555b062
5 changed files with 92 additions and 9 deletions

View file

@ -0,0 +1,35 @@
use bevy::prelude::Component;
use rand::prelude::IndexedRandom;
#[derive(Component, Debug, Clone, Copy)]
pub struct RoundBall {
pub ball_type: RoundBallType,
pub track_progress: f32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum RoundBallType {
#[default]
Red,
Green,
Blue,
Purple,
}
impl RoundBallType {
pub const fn asset_path(&self) -> &'static str {
match self {
RoundBallType::Red => "sprites/round/red.png",
RoundBallType::Green => "sprites/round/green.png",
RoundBallType::Blue => "sprites/round/blue.png",
RoundBallType::Purple => "sprites/round/purple.png",
}
}
pub fn random_from_4() -> Self {
[Self::Red, Self::Green, Self::Blue, Self::Purple]
.choose(&mut rand::rng())
.copied()
.unwrap_or_default()
}
}

View file

@ -9,6 +9,9 @@ pub const CENTER_Y: f32 = 0.0;
pub const STEP: f32 = FACTOR as f32 / SCALE;
pub const SCALE: f32 = 2.0;
// Z-INDEXES
pub const ROUND_BALL_Z: f32 = 10.0;
// COLORS
pub const PINK: Color = Color::srgb_u8(250, 0, 155);
pub const BLUE: Color = Color::srgb_u8(0, 0, 255);

View file

@ -11,3 +11,12 @@ pub use components_track::*;
mod systems_track;
pub use systems_track::*;
mod components_ball;
pub use components_ball::*;
mod system_ball;
pub use system_ball::*;
mod constants;
pub use constants::*;

View file

@ -1,15 +1,23 @@
use bevy::prelude::*;
use crate::states::AppState::LinearState;
use crate::states::level::GameOver;
use crate::states::linear::*;
use bevy::prelude::*;
pub struct LinearPlugin;
// создаем енум, чтоб не указывать run_if(in_state) для каждой системы
#[derive(SystemSet, Debug, Hash, PartialEq, Eq, Clone)]
pub enum LinearUpdateSet {
Track,
}
impl Plugin for LinearPlugin {
fn build(&self, app: &mut App) {
app
.add_systems(OnEnter (LinearState), setup)
.add_systems(Update, (draw_track_gizmos).run_if(in_state(LinearState)))
app.add_systems(OnEnter(LinearState), setup)
.configure_sets(Update, LinearUpdateSet::Track.run_if(in_state(LinearState)))
.add_systems(
Update,
(draw_track_gizmos, spawn_round_ball).in_set(LinearUpdateSet::Track),
)
.add_systems(OnExit(LinearState), cleanup);
}
}
@ -30,4 +38,5 @@ fn cleanup(mut commands: Commands, query: Query<Entity, With<LinearStateMarker>>
// зачищаем ресурсы по типу
commands.remove_resource::<Track>();
commands.remove_resource::<PrecalculatedTrack>();
}

View file

@ -0,0 +1,27 @@
use bevy::asset::ErasedAssetLoader;
use bevy::prelude::*;
use crate::states::linear::*;
pub fn spawn_round_ball(
track: Res<PrecalculatedTrack>,
mut commands: Commands,
asset_server: Res<AssetServer>,
) {
let ball_type = RoundBallType::random_from_4();
let image: Handle<Image> = asset_server.load(ball_type.asset_path());
commands.spawn((
Sprite {
image,
custom_size: Some(Vec2::splat(STEP)),
..default()
},
Transform::from_xyz(0.0, 0.0, ROUND_BALL_Z),
RoundBall {
ball_type,
track_progress: 0.0,
},
LinearStateMarker
));
}