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( fn collision_event_system(
mut commands: Commands, mut commands: Commands,
character_meshes: Res<CharacterMeshes>, 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"); 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 commands
.spawn_bundle(Text2dBundle { .spawn_bundle(Text2dBundle {
text: Text::from_section( text: Text::from_section(
@ -338,6 +387,7 @@ fn win_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
}, },
) )
.with_alignment(TextAlignment::CENTER), .with_alignment(TextAlignment::CENTER),
transform: Transform::from_xyz(0., 0., 4.),
..Default::default() ..Default::default()
}) })
.insert(Level); .insert(Level);

View file

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

View file

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