add player spawn event

This commit is contained in:
TudbuT 2024-11-23 18:22:41 +01:00
parent 44ff56592e
commit a24e6e39a6
2 changed files with 56 additions and 19 deletions

View file

@ -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::<PlayerSpawnEvent>()
.add_plugins((player_plugin, scene_plugin));
}

View file

@ -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<Assets<Mesh>>,
materials: &mut ResMut<Assets<ColorMaterial>>,
assets: &Res<AssetServer>,
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(),
));
}
}