Spawn platforms function

This commit is contained in:
Pascal Engélibert 2022-08-25 12:32:06 +02:00
parent 10abb0a676
commit e7f2188091
Signed by: tuxmain
GPG key ID: 3504BC6D362F7DCA
5 changed files with 125 additions and 47 deletions

View file

@ -171,6 +171,37 @@ pub fn spawn_character(
}
}
pub fn spawn_platforms<I: IntoIterator<Item = (Transform, Vec2)>>(
commands: &mut Commands,
meshes: &mut ResMut<Assets<Mesh>>,
materials: &mut ResMut<Assets<ColorMaterial>>,
platforms: I,
) {
for (transform, size) in platforms.into_iter() {
spawn_platform(commands, meshes, materials, transform, size);
}
}
pub fn spawn_platform(
commands: &mut Commands,
meshes: &mut ResMut<Assets<Mesh>>,
materials: &mut ResMut<Assets<ColorMaterial>>,
transform: Transform,
size: Vec2,
) {
commands
.spawn_bundle(ColorMesh2dBundle {
mesh: meshes.add(Mesh::from(Quad { size, flip: false })).into(),
material: materials.add(ColorMaterial::from(Color::GRAY)),
transform,
..default()
})
.insert(Collider::cuboid(size.x / 2., size.y / 2.))
.insert(Level);
}
fn collision_event_system(
mut commands: Commands,
character_meshes: Res<CharacterMeshes>,
@ -325,8 +356,26 @@ fn character_particle_effect_system(
}
}
fn win_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
fn win_setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
asset_server: Res<AssetServer>,
) {
let font = asset_server.get_handle("UacariLegacy-Thin.ttf");
commands
.spawn_bundle(ColorMesh2dBundle {
mesh: meshes
.add(Mesh::from(Quad {
size: Vec2 { x: 512., y: 64. },
flip: false,
}))
.into(),
material: materials.add(ColorMaterial::from(Color::rgba(0., 0., 0., 0.9))),
transform: Transform::from_xyz(0., 0., 3.),
..default()
})
.insert(Level);
commands
.spawn_bundle(Text2dBundle {
text: Text::from_section(
@ -338,6 +387,7 @@ fn win_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
},
)
.with_alignment(TextAlignment::CENTER),
transform: Transform::from_xyz(0., 0., 4.),
..Default::default()
})
.insert(Level);

View file

@ -43,6 +43,7 @@ pub fn post_setup_level(
&character_meshes,
&mut materials,
&audio,
&asset_server,
),
1 => level1::setup(
&mut commands,
@ -52,7 +53,14 @@ pub fn post_setup_level(
&audio,
&asset_server,
),
_ => game_over::setup(&mut commands, &asset_server),
_ => game_over::setup(
&mut commands,
&mut meshes,
&character_meshes,
&mut materials,
&audio,
&asset_server,
),
}
}
}

View file

@ -2,12 +2,19 @@ use crate::game::*;
use bevy::prelude::*;
pub fn setup(commands: &mut Commands, asset_server: &Res<AssetServer>) {
pub fn setup(
commands: &mut Commands,
meshes: &mut ResMut<Assets<Mesh>>,
character_meshes: &Res<CharacterMeshes>,
materials: &mut ResMut<Assets<ColorMaterial>>,
audio: &Res<crossbeam_channel::Sender<AudioMsg>>,
asset_server: &Res<AssetServer>,
) {
let font = asset_server.get_handle("UacariLegacy-Thin.ttf");
commands
.spawn_bundle(Text2dBundle {
text: Text::from_section(
"GAME OVER",
"Thank you for playing!",
TextStyle {
font: font.clone(),
font_size: 48.0,
@ -33,4 +40,22 @@ pub fn setup(commands: &mut Commands, asset_server: &Res<AssetServer>) {
..Default::default()
})
.insert(Level);
spawn_platform(
commands,
meshes,
materials,
Transform::from_xyz(0.0, -256.0, 0.0),
Vec2 { x: 800.0, y: 16.0 },
);
spawn_character(
commands,
character_meshes,
materials,
audio,
Transform::from_xyz(-128., -64., 0.),
Color::RED,
true,
);
}

View file

@ -1,7 +1,6 @@
use crate::game::*;
use bevy::prelude::{shape::Quad, *};
use bevy_rapier2d::prelude::*;
use bevy::prelude::*;
pub fn setup(
commands: &mut Commands,
@ -9,22 +8,32 @@ pub fn setup(
character_meshes: &Res<CharacterMeshes>,
materials: &mut ResMut<Assets<ColorMaterial>>,
audio: &Res<crossbeam_channel::Sender<AudioMsg>>,
asset_server: &Res<AssetServer>,
) {
let font = asset_server.get_handle("UacariLegacy-Thin.ttf");
commands
.spawn_bundle(ColorMesh2dBundle {
mesh: meshes
.add(Mesh::from(Quad {
size: Vec2 { x: 800.0, y: 16.0 },
flip: false,
}))
.into(),
material: materials.add(ColorMaterial::from(Color::GRAY)),
transform: Transform::from_xyz(0.0, -256.0, 0.0),
..default()
.spawn_bundle(Text2dBundle {
text: Text::from_section(
"Combine the colors to synthetize a white light.\nTab to switch; Arrows to move.",
TextStyle {
font,
font_size: 32.0,
color: Color::WHITE,
},
)
.with_alignment(TextAlignment::CENTER),
..Default::default()
})
.insert(Collider::cuboid(400., 8.))
.insert(Level);
spawn_platform(
commands,
meshes,
materials,
Transform::from_xyz(0.0, -256.0, 0.0),
Vec2 { x: 800.0, y: 16.0 },
);
spawn_characters(
commands,
character_meshes,

View file

@ -1,7 +1,6 @@
use crate::game::*;
use bevy::prelude::{shape::Quad, *};
use bevy_rapier2d::prelude::*;
use bevy::prelude::*;
pub fn setup(
commands: &mut Commands,
@ -11,34 +10,21 @@ pub fn setup(
audio: &Res<crossbeam_channel::Sender<AudioMsg>>,
asset_server: &Res<AssetServer>,
) {
commands
.spawn_bundle(ColorMesh2dBundle {
mesh: meshes
.add(Mesh::from(Quad {
size: Vec2 { x: 800.0, y: 16.0 },
flip: false,
}))
.into(),
material: materials.add(ColorMaterial::from(Color::GRAY)),
transform: Transform::from_xyz(0.0, -256.0, 0.0),
..default()
})
.insert(Collider::cuboid(400., 8.))
.insert(Level);
commands
.spawn_bundle(ColorMesh2dBundle {
mesh: meshes
.add(Mesh::from(Quad {
size: Vec2 { x: 400.0, y: 16.0 },
flip: false,
}))
.into(),
material: materials.add(ColorMaterial::from(Color::GRAY)),
transform: Transform::from_xyz(256.0, -128.0, 0.0),
..default()
})
.insert(Collider::cuboid(200., 8.))
.insert(Level);
spawn_platforms(
commands,
meshes,
materials,
[
(
Transform::from_xyz(0.0, -256.0, 0.0),
Vec2 { x: 800.0, y: 16.0 },
),
(
Transform::from_xyz(256.0, -128.0, 0.0),
Vec2 { x: 400.0, y: 16.0 },
),
],
);
spawn_characters(
commands,