spawn ball + run in set
This commit is contained in:
parent
7b334b33c0
commit
5ba555b062
5 changed files with 92 additions and 9 deletions
35
src/states/linear/components_ball.rs
Normal file
35
src/states/linear/components_ball.rs
Normal 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()
|
||||
}
|
||||
}
|
||||
|
|
@ -9,7 +9,10 @@ pub const CENTER_Y: f32 = 0.0;
|
|||
pub const STEP: f32 = FACTOR as f32 / SCALE;
|
||||
pub const SCALE: f32 = 2.0;
|
||||
|
||||
//COLORS
|
||||
// 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);
|
||||
pub const GREEN: Color = Color::srgb_u8(0, 255, 0);
|
||||
|
|
|
|||
|
|
@ -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::*;
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,23 +1,31 @@
|
|||
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)))
|
||||
.add_systems(OnExit (LinearState), cleanup);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
fn setup(mut commands: Commands) {
|
||||
let build_track = setup_linear_track();
|
||||
let precalculated_track = precalculate_track(&build_track);
|
||||
|
||||
|
||||
commands.insert_resource(build_track);
|
||||
commands.insert_resource(precalculated_track);
|
||||
}
|
||||
|
|
@ -30,4 +38,5 @@ fn cleanup(mut commands: Commands, query: Query<Entity, With<LinearStateMarker>>
|
|||
|
||||
// зачищаем ресурсы по типу
|
||||
commands.remove_resource::<Track>();
|
||||
}
|
||||
commands.remove_resource::<PrecalculatedTrack>();
|
||||
}
|
||||
|
|
|
|||
27
src/states/linear/system_ball.rs
Normal file
27
src/states/linear/system_ball.rs
Normal 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
|
||||
));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue