diff --git a/src/game.rs b/src/game.rs index 2ef6a37..c08533e 100644 --- a/src/game.rs +++ b/src/game.rs @@ -1,7 +1,7 @@ use bevy::prelude::*; use bevy_rapier2d::prelude::*; use player::player_plugin; -use scene::scene_plugin; +use scene::{scene_plugin, PlayerSpawnEvent}; use crate::AppState; @@ -12,5 +12,7 @@ mod set; pub const WORLD_DEPTH: f32 = 0.5; pub fn game_plugin(app: &mut App) { - app.add_plugins((player_plugin, scene_plugin)); + (app) + .add_event::() + .add_plugins((player_plugin, scene_plugin)); } diff --git a/src/game/scene.rs b/src/game/scene.rs index f436ab7..e3337cb 100644 --- a/src/game/scene.rs +++ b/src/game/scene.rs @@ -14,6 +14,13 @@ use crate::AppState; #[derive(Component)] struct Block; +#[derive(Event)] +pub struct PlayerSpawnEvent { + x: f32, + y: f32, + block_size: f32, +} + pub(super) fn scene_plugin(app: &mut App) { app.add_plugins(EditorPlugin::default()); // app.add_systems(, ) @@ -76,23 +83,22 @@ pub(super) fn import_text_world( let x = i as f32 * wi.block_size; let y = current_y as f32 * wi.block_size; - let mut command = commands.spawn(( - Block, - MaterialMesh2dBundle { - mesh: meshes - .add(Rectangle::new(wi.block_size * len as f32, wi.block_size)) - .into(), - material: materials.add(assets.load(tex)), - transform: Transform::from_xyz(x, y, WORLD_DEPTH), - ..Default::default() - }, - )); - if collider { - command.insert(( - RigidBody::Fixed, - Collider::cuboid(wi.block_size / 2.0 * len as f32, wi.block_size / 2.0), - Velocity::default(), - )); + if tex == "[player]" { + commands.trigger(PlayerSpawnEvent { + x, + y, + block_size: wi.block_size, + }) + } else { + spawn_block( + &mut commands, + &mut meshes, + &mut materials, + &assets, + tex, + (x, y, len, wi.block_size), + collider, + ); } println!("spawned {possible_block:?} at {x}, {y}"); @@ -104,3 +110,32 @@ pub(super) fn import_text_world( } } } + +fn spawn_block( + commands: &mut Commands, + meshes: &mut ResMut>, + materials: &mut ResMut>, + assets: &Res, + tex: String, + (x, y, len, block_size): (f32, f32, usize, f32), + collider: bool, +) { + let mut command = commands.spawn(( + Block, + MaterialMesh2dBundle { + mesh: meshes + .add(Rectangle::new(block_size * len as f32, block_size)) + .into(), + material: materials.add(assets.load(tex)), + transform: Transform::from_xyz(x, y, WORLD_DEPTH), + ..Default::default() + }, + )); + if collider { + command.insert(( + RigidBody::Fixed, + Collider::cuboid(block_size / 2.0 * len as f32, block_size / 2.0), + Velocity::default(), + )); + } +}