add player spawn event
This commit is contained in:
parent
44ff56592e
commit
a24e6e39a6
2 changed files with 56 additions and 19 deletions
|
@ -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(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue