From e7f2188091fe9093837d4d5f29aa6d608606bb41 Mon Sep 17 00:00:00 2001 From: tuxmain Date: Thu, 25 Aug 2022 12:32:06 +0200 Subject: [PATCH] Spawn platforms function --- src/game.rs | 52 ++++++++++++++++++++++++++++++++++++++++- src/levels.rs | 10 +++++++- src/levels/game_over.rs | 29 +++++++++++++++++++++-- src/levels/level0.rs | 35 ++++++++++++++++----------- src/levels/level1.rs | 46 +++++++++++++----------------------- 5 files changed, 125 insertions(+), 47 deletions(-) diff --git a/src/game.rs b/src/game.rs index 8e47fb1..e1499b3 100644 --- a/src/game.rs +++ b/src/game.rs @@ -171,6 +171,37 @@ pub fn spawn_character( } } +pub fn spawn_platforms>( + commands: &mut Commands, + meshes: &mut ResMut>, + materials: &mut ResMut>, + + 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>, + materials: &mut ResMut>, + + 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, @@ -325,8 +356,26 @@ fn character_particle_effect_system( } } -fn win_setup(mut commands: Commands, asset_server: Res) { +fn win_setup( + mut commands: Commands, + mut meshes: ResMut>, + mut materials: ResMut>, + asset_server: Res, +) { 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) { }, ) .with_alignment(TextAlignment::CENTER), + transform: Transform::from_xyz(0., 0., 4.), ..Default::default() }) .insert(Level); diff --git a/src/levels.rs b/src/levels.rs index c6f446e..ef9557f 100644 --- a/src/levels.rs +++ b/src/levels.rs @@ -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, + ), } } } diff --git a/src/levels/game_over.rs b/src/levels/game_over.rs index f03026d..5fa3372 100644 --- a/src/levels/game_over.rs +++ b/src/levels/game_over.rs @@ -2,12 +2,19 @@ use crate::game::*; use bevy::prelude::*; -pub fn setup(commands: &mut Commands, asset_server: &Res) { +pub fn setup( + commands: &mut Commands, + meshes: &mut ResMut>, + character_meshes: &Res, + materials: &mut ResMut>, + audio: &Res>, + asset_server: &Res, +) { 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) { ..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, + ); } diff --git a/src/levels/level0.rs b/src/levels/level0.rs index 5b72da6..3679121 100644 --- a/src/levels/level0.rs +++ b/src/levels/level0.rs @@ -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, materials: &mut ResMut>, audio: &Res>, + asset_server: &Res, ) { + 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, diff --git a/src/levels/level1.rs b/src/levels/level1.rs index 8bcf4bb..ce3906b 100644 --- a/src/levels/level1.rs +++ b/src/levels/level1.rs @@ -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>, asset_server: &Res, ) { - 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,