Level management & win

This commit is contained in:
Pascal Engélibert 2022-08-23 12:02:58 +02:00
parent 55ab83588a
commit 214e55d7ac
Signed by: tuxmain
GPG key ID: 3504BC6D362F7DCA
7 changed files with 355 additions and 151 deletions

View file

@ -1,17 +1,22 @@
# Bevyjam
## Controls
* **Move**: arrows
* **Switch character**: Tab
* **Level up**: Enter (when character is white)
## TODO
* name
* scene management
* stream audio (with HexoSynthDSP)
* color filters
* level design
* win
* menu GUI
* (?) can jump only from a surface (no mid-air jump)
* (?) multiplayer
* make WASM build work again (replace hanabi)
* Text is not displayed after menu (on win state and on game over level)
* level reset
## Build

View file

@ -13,6 +13,9 @@ use bevy_hanabi::*;
use bevy_rapier2d::prelude::*;
use std::collections::BTreeSet;
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
pub struct LevelId(pub u32);
pub struct GamePlugin;
impl Plugin for GamePlugin {
@ -21,24 +24,29 @@ impl Plugin for GamePlugin {
.init_resource::<CharacterMeshes>()
.insert_resource(CurrentLevel(None))
.add_system_set(SystemSet::on_enter(AppState::Game).with_system(setup))
.add_system_set(SystemSet::on_enter(AppState::Win).with_system(win_setup))
.add_system_set(
SystemSet::on_exit(AppState::Win).with_system(crate::levels::despawn_level),
)
.add_system_set(
SystemSet::on_update(AppState::Game)
.with_system(post_setup_level)
.with_system(crate::levels::post_setup_level)
.with_system(keyboard_input_system),
)
.add_system_set(SystemSet::on_update(AppState::Win).with_system(keyboard_input_system))
.add_system_to_stage(CoreStage::PostUpdate, collision_event_system);
}
}
// Events
struct LevelStartupEvent(Entity);
pub struct LevelStartupEvent(pub Entity);
// Resources
struct CurrentLevel(Option<Entity>);
pub struct CurrentLevel(pub Option<LevelId>);
struct CharacterMeshes {
pub struct CharacterMeshes {
square: Mesh2dHandle,
}
@ -58,20 +66,20 @@ impl FromWorld for CharacterMeshes {
// Components
#[derive(Clone, Component, Copy, Eq, Hash, PartialEq)]
struct LevelId(u32);
#[derive(Component)]
pub struct Level;
#[derive(Clone, Component, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
struct CharacterId(u32);
pub struct CharacterId(pub u32);
#[derive(Clone, Component, Copy, Eq, Hash, PartialEq)]
struct SelectedCharacterId(Option<CharacterId>);
pub struct SelectedCharacterId(pub Option<CharacterId>);
#[derive(Component)]
struct CharacterIdList(BTreeSet<CharacterId>);
pub struct CharacterIdList(pub BTreeSet<CharacterId>);
#[derive(Clone, Component, PartialEq)]
struct CharacterColor(Color);
pub struct CharacterColor(pub Color);
// Systems
@ -80,69 +88,16 @@ fn setup(
mut current_level: ResMut<CurrentLevel>,
mut level_startup_event: EventWriter<LevelStartupEvent>,
) {
let level_entity = commands
.spawn()
.insert(LevelId(0))
.insert(SelectedCharacterId(None))
.insert(CharacterIdList(BTreeSet::new()))
.id();
current_level.0 = Some(level_entity);
commands
.spawn_bundle(TransformBundle::from(Transform::from_xyz(0.0, -256.0, 0.0)))
.insert(Collider::cuboid(400., 10.));
level_startup_event.send(LevelStartupEvent(level_entity));
let level_id = LevelId(current_level.0.map_or(0, |level_id| level_id.0 + 1));
crate::levels::setup_level(
&mut commands,
&mut current_level,
&mut level_startup_event,
level_id,
);
}
// This is a bad design, but it's the only way I found
fn post_setup_level(
mut commands: Commands,
character_meshes: Res<CharacterMeshes>,
mut effects: ResMut<Assets<EffectAsset>>,
mut materials: ResMut<Assets<ColorMaterial>>,
mut level_query: Query<(&mut SelectedCharacterId, &mut CharacterIdList)>,
mut level_startup_event: EventReader<LevelStartupEvent>,
) {
for LevelStartupEvent(level_entity) in level_startup_event.iter() {
if let Ok((mut selected_character_id, mut character_id_list)) =
level_query.get_mut(*level_entity)
{
spawn_character(
&mut commands,
&character_meshes,
&mut effects,
&mut materials,
&mut selected_character_id,
&mut character_id_list,
Transform::from_xyz(-128., -64., 0.),
Color::RED,
);
spawn_character(
&mut commands,
&character_meshes,
&mut effects,
&mut materials,
&mut selected_character_id,
&mut character_id_list,
Transform::from_xyz(0., -64., 0.),
Color::GREEN,
);
spawn_character(
&mut commands,
&character_meshes,
&mut effects,
&mut materials,
&mut selected_character_id,
&mut character_id_list,
Transform::from_xyz(128., -64., 0.),
Color::BLUE,
);
}
}
}
fn spawn_character(
pub fn spawn_character(
commands: &mut Commands,
character_meshes: &Res<CharacterMeshes>,
effects: &mut ResMut<Assets<EffectAsset>>,
@ -184,6 +139,7 @@ fn spawn_character(
transform,
..default()
})
.insert(Level)
.insert(character_id)
.insert(CharacterColor(color))
.insert(RigidBody::Dynamic)
@ -239,22 +195,19 @@ fn collision_event_system(
character_meshes: Res<CharacterMeshes>,
mut materials: ResMut<Assets<ColorMaterial>>,
mut effects: ResMut<Assets<EffectAsset>>,
current_level: Res<CurrentLevel>,
mut collision_events: EventReader<CollisionEvent>,
character_query: Query<(&CharacterId, &CharacterColor, &Transform)>,
mut level_query: Query<(&mut SelectedCharacterId, &mut CharacterIdList)>,
mut app_state: ResMut<State<AppState>>,
) {
if let Some(level_entity) = current_level.0 {
for collision_event in collision_events.iter() {
if let CollisionEvent::Started(e1, e2, flags) = collision_event {
if flags.is_empty() {
if let (
Ok((c1_id, c1_color, c1_transform)),
Ok((c2_id, c2_color, _c2_transform)),
) = (character_query.get(*e1), character_query.get(*e2))
if let (Ok((c1_id, c1_color, c1_transform)), Ok((c2_id, c2_color, _c2_transform))) =
(character_query.get(*e1), character_query.get(*e2))
{
let (mut selected_character_id, mut character_id_list) =
level_query.get_mut(level_entity).unwrap();
level_query.single_mut();
character_id_list.0.remove(c1_id);
character_id_list.0.remove(c2_id);
selected_character_id.0 = None;
@ -263,6 +216,17 @@ fn collision_event_system(
commands.entity(*e1).despawn_recursive();
commands.entity(*e2).despawn_recursive();
let new_color = (Vec4::from(c1_color.0) + Vec4::from(c2_color.0))
.clamp(Vec4::ZERO, Vec4::ONE);
// If color approximately white
if app_state.current() == &AppState::Game
&& 4. - new_color.length_squared() < 0.1
{
println!("win");
app_state.replace(AppState::Win).ok();
}
spawn_character(
&mut commands,
&character_meshes,
@ -271,18 +235,16 @@ fn collision_event_system(
&mut selected_character_id,
&mut character_id_list,
*c1_transform,
(Vec4::from(c1_color.0) + Vec4::from(c2_color.0)).into(),
new_color.into(),
);
}
}
}
}
}
}
fn keyboard_input_system(
keyboard_input: Res<Input<KeyCode>>,
current_level: Res<CurrentLevel>,
mut characters: Query<(
&CharacterId,
&mut Velocity,
@ -294,11 +256,9 @@ fn keyboard_input_system(
mut effect: Query<&mut ParticleEffect>,
dsp_assets: Res<DspAssets>,
audio: Res<Audio>,
mut app_state: ResMut<State<AppState>>,
) {
if let Some(level_entity) = current_level.0 {
if let Ok((mut selected_character_id, character_id_list)) =
level_query.get_mut(level_entity)
{
if let Ok((mut selected_character_id, character_id_list)) = level_query.get_single_mut() {
if keyboard_input.just_pressed(KeyCode::Tab) {
audio.play(dsp_assets.graph(&sine_wave));
@ -360,9 +320,30 @@ fn keyboard_input_system(
}
}
}
if app_state.current() == &AppState::Win && keyboard_input.just_pressed(KeyCode::Return) {
app_state.replace(AppState::Game).unwrap();
}
}
// TODO: the text is not visible, I don't know why.
fn win_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
println!("win system");
let font = asset_server.get_handle("Cantarell-VF.otf");
commands.spawn_bundle(Text2dBundle {
text: Text::from_section(
"Press ENTER to level up",
TextStyle {
font,
font_size: 32.0,
color: Color::WHITE,
},
)
.with_alignment(TextAlignment::CENTER),
..Default::default()
});
}
// Sounds
pub fn sine_wave() -> impl AudioUnit32 {

74
src/levels.rs Normal file
View file

@ -0,0 +1,74 @@
#![allow(clippy::too_many_arguments)]
mod game_over;
mod level0;
mod level1;
use crate::game::*;
use bevy::prelude::*;
use bevy_hanabi::*;
use std::collections::BTreeSet;
pub fn setup_level(
commands: &mut Commands,
current_level: &mut ResMut<CurrentLevel>,
level_startup_event: &mut EventWriter<LevelStartupEvent>,
level_id: LevelId,
) {
let level_entity = commands
.spawn()
.insert(Level)
.insert(SelectedCharacterId(None))
.insert(CharacterIdList(BTreeSet::new()))
.id();
current_level.0 = Some(level_id);
level_startup_event.send(LevelStartupEvent(level_entity));
}
pub fn despawn_level(mut commands: Commands, level_query: Query<Entity, With<Level>>) {
for entity in level_query.iter() {
commands.entity(entity).despawn_recursive();
}
}
// This is a bad design, to be refactored some days
pub fn post_setup_level(
mut commands: Commands,
character_meshes: Res<CharacterMeshes>,
mut effects: ResMut<Assets<EffectAsset>>,
mut materials: ResMut<Assets<ColorMaterial>>,
current_level: Res<CurrentLevel>,
mut level_query: Query<(&mut SelectedCharacterId, &mut CharacterIdList)>,
mut level_startup_event: EventReader<LevelStartupEvent>,
asset_server: Res<AssetServer>,
) {
for LevelStartupEvent(level_entity) in level_startup_event.iter() {
if let Ok((mut selected_character_id, mut character_id_list)) =
level_query.get_mut(*level_entity)
{
if let Some(level_id) = current_level.0 {
match level_id.0 {
0 => level0::setup(
&mut commands,
&character_meshes,
&mut effects,
&mut materials,
&mut selected_character_id,
&mut character_id_list,
),
1 => level1::setup(
&mut commands,
&character_meshes,
&mut effects,
&mut materials,
&mut selected_character_id,
&mut character_id_list,
),
_ => game_over::setup(&mut commands, &asset_server),
}
}
}
}
}

36
src/levels/game_over.rs Normal file
View file

@ -0,0 +1,36 @@
use crate::game::*;
use bevy::prelude::*;
pub fn setup(commands: &mut Commands, asset_server: &Res<AssetServer>) {
let font = asset_server.get_handle("Cantarell-VF.otf");
commands
.spawn_bundle(Text2dBundle {
text: Text::from_section(
"GAME OVER",
TextStyle {
font: font.clone(),
font_size: 48.0,
color: Color::WHITE,
},
)
.with_alignment(TextAlignment::CENTER),
transform: Transform::from_xyz(0., -128.0, 0.),
..Default::default()
})
.insert(Level);
commands
.spawn_bundle(Text2dBundle {
text: Text::from_section(
"There is no more light to combine.",
TextStyle {
font,
font_size: 32.0,
color: Color::WHITE,
},
)
.with_alignment(TextAlignment::CENTER),
..Default::default()
})
.insert(Level);
}

50
src/levels/level0.rs Normal file
View file

@ -0,0 +1,50 @@
use crate::game::*;
use bevy::prelude::*;
use bevy_hanabi::*;
use bevy_rapier2d::prelude::*;
pub fn setup(
commands: &mut Commands,
character_meshes: &Res<CharacterMeshes>,
effects: &mut ResMut<Assets<EffectAsset>>,
materials: &mut ResMut<Assets<ColorMaterial>>,
selected_character_id: &mut Mut<SelectedCharacterId>,
character_id_list: &mut Mut<CharacterIdList>,
) {
commands
.spawn_bundle(TransformBundle::from(Transform::from_xyz(0.0, -256.0, 0.0)))
.insert(Collider::cuboid(400., 10.))
.insert(Level);
spawn_character(
commands,
character_meshes,
effects,
materials,
selected_character_id,
character_id_list,
Transform::from_xyz(-128., -64., 0.),
Color::RED,
);
spawn_character(
commands,
character_meshes,
effects,
materials,
selected_character_id,
character_id_list,
Transform::from_xyz(0., -64., 0.),
Color::GREEN,
);
spawn_character(
commands,
character_meshes,
effects,
materials,
selected_character_id,
character_id_list,
Transform::from_xyz(128., -64., 0.),
Color::BLUE,
);
}

56
src/levels/level1.rs Normal file
View file

@ -0,0 +1,56 @@
use crate::game::*;
use bevy::prelude::*;
use bevy_hanabi::*;
use bevy_rapier2d::prelude::*;
pub fn setup(
commands: &mut Commands,
character_meshes: &Res<CharacterMeshes>,
effects: &mut ResMut<Assets<EffectAsset>>,
materials: &mut ResMut<Assets<ColorMaterial>>,
selected_character_id: &mut Mut<SelectedCharacterId>,
character_id_list: &mut Mut<CharacterIdList>,
) {
commands
.spawn_bundle(TransformBundle::from(Transform::from_xyz(0.0, -256.0, 0.0)))
.insert(Collider::cuboid(400., 10.))
.insert(Level);
commands
.spawn_bundle(TransformBundle::from(Transform::from_xyz(
256.0, -128.0, 0.0,
)))
.insert(Collider::cuboid(200., 10.))
.insert(Level);
spawn_character(
commands,
character_meshes,
effects,
materials,
selected_character_id,
character_id_list,
Transform::from_xyz(128., 64., 0.),
Color::BLUE,
);
spawn_character(
commands,
character_meshes,
effects,
materials,
selected_character_id,
character_id_list,
Transform::from_xyz(-128., -128., 0.),
Color::RED,
);
spawn_character(
commands,
character_meshes,
effects,
materials,
selected_character_id,
character_id_list,
Transform::from_xyz(0., -128., 0.),
Color::GREEN,
);
}

View file

@ -1,4 +1,5 @@
mod game;
mod levels;
mod menu;
use bevy::{
@ -14,6 +15,7 @@ use bevy_rapier2d::prelude::*;
enum AppState {
Menu,
Game,
Win,
}
fn main() {