Compare commits
2 commits
e86ca82bc3
...
be96747d6a
Author | SHA1 | Date | |
---|---|---|---|
be96747d6a | |||
3f79054ed7 |
1 changed files with 34 additions and 19 deletions
53
src/game.rs
53
src/game.rs
|
@ -9,7 +9,6 @@ use bevy::{
|
||||||
sprite::Mesh2dHandle, ecs::system::EntityCommands,
|
sprite::Mesh2dHandle, ecs::system::EntityCommands,
|
||||||
};
|
};
|
||||||
use bevy_rapier2d::prelude::*;
|
use bevy_rapier2d::prelude::*;
|
||||||
use std::collections::BTreeSet;
|
|
||||||
|
|
||||||
pub enum AudioMsg {
|
pub enum AudioMsg {
|
||||||
Color([f32; 3]),
|
Color([f32; 3]),
|
||||||
|
@ -34,13 +33,13 @@ impl Plugin for GamePlugin {
|
||||||
.add_system_set(
|
.add_system_set(
|
||||||
SystemSet::on_update(AppState::Game)
|
SystemSet::on_update(AppState::Game)
|
||||||
.with_system(crate::levels::post_setup_level)
|
.with_system(crate::levels::post_setup_level)
|
||||||
|
.with_system(change_character_system)
|
||||||
.with_system(player_movement_system)
|
.with_system(player_movement_system)
|
||||||
.with_system(move_camera)
|
.with_system(move_camera)
|
||||||
.with_system(character_particle_effect_system),
|
.with_system(character_particle_effect_system),
|
||||||
)
|
)
|
||||||
.add_system_set(
|
.add_system_set(
|
||||||
SystemSet::on_update(AppState::Win)
|
SystemSet::on_update(AppState::Win)
|
||||||
.with_system(change_character_system)
|
|
||||||
.with_system(player_movement_system)
|
.with_system(player_movement_system)
|
||||||
.with_system(move_camera)
|
.with_system(move_camera)
|
||||||
.with_system(character_particle_effect_system),
|
.with_system(character_particle_effect_system),
|
||||||
|
@ -186,14 +185,16 @@ fn collision_event_system(
|
||||||
character_meshes: Res<CharacterMeshes>,
|
character_meshes: Res<CharacterMeshes>,
|
||||||
mut materials: ResMut<Assets<ColorMaterial>>,
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
||||||
mut collision_events: EventReader<CollisionEvent>,
|
mut collision_events: EventReader<CollisionEvent>,
|
||||||
character_query: Query<(&CharacterColor, &Transform)>,
|
character_query: Query<(&CharacterColor, &Transform, Option<&Player>)>,
|
||||||
mut app_state: ResMut<State<AppState>>,
|
mut app_state: ResMut<State<AppState>>,
|
||||||
audio: Res<crossbeam_channel::Sender<AudioMsg>>,
|
audio: Res<crossbeam_channel::Sender<AudioMsg>>,
|
||||||
) {
|
) {
|
||||||
for collision_event in collision_events.iter() {
|
for collision_event in collision_events.iter() {
|
||||||
if let CollisionEvent::Started(e1, e2, flags) = collision_event {
|
if let CollisionEvent::Started(e1, e2, flags) = collision_event {
|
||||||
if flags.is_empty() {
|
if flags.is_empty() {
|
||||||
if let (Ok((c1_color, c1_transform)), Ok((c2_color, _c2_transform))) =
|
if let (
|
||||||
|
Ok((c1_color, c1_transform, c1_player)),
|
||||||
|
Ok((c2_color, c2_transform, c2_player))) =
|
||||||
(character_query.get(*e1), character_query.get(*e2))
|
(character_query.get(*e1), character_query.get(*e2))
|
||||||
{
|
{
|
||||||
// TODO completely remove particles
|
// TODO completely remove particles
|
||||||
|
@ -210,15 +211,28 @@ fn collision_event_system(
|
||||||
app_state.replace(AppState::Win).ok();
|
app_state.replace(AppState::Win).ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
spawn_character(
|
// position character based on current player location
|
||||||
&mut commands,
|
if let Some(_player) = c1_player {
|
||||||
&character_meshes,
|
spawn_character(
|
||||||
&mut materials,
|
&mut commands,
|
||||||
&audio,
|
&character_meshes,
|
||||||
*c1_transform,
|
&mut materials,
|
||||||
new_color.into(),
|
&audio,
|
||||||
true,
|
*c1_transform,
|
||||||
);
|
new_color.into(),
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
} else if let Some(_player) = c2_player {
|
||||||
|
spawn_character(
|
||||||
|
&mut commands,
|
||||||
|
&character_meshes,
|
||||||
|
&mut materials,
|
||||||
|
&audio,
|
||||||
|
*c2_transform,
|
||||||
|
new_color.into(),
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -229,7 +243,7 @@ fn change_character_system(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
|
|
||||||
keyboard_input: Res<Input<KeyCode>>,
|
keyboard_input: Res<Input<KeyCode>>,
|
||||||
characters: Query<(Entity, &Transform, &CharacterColor, Option<&Player>)>,
|
characters: Query<(Entity, &CharacterColor, Option<&Player>)>,
|
||||||
audio: Res<crossbeam_channel::Sender<AudioMsg>>,
|
audio: Res<crossbeam_channel::Sender<AudioMsg>>,
|
||||||
) {
|
) {
|
||||||
if !keyboard_input.just_pressed(KeyCode::Tab) { return; }
|
if !keyboard_input.just_pressed(KeyCode::Tab) { return; }
|
||||||
|
@ -238,7 +252,7 @@ fn change_character_system(
|
||||||
let mut player_count: usize = 0;
|
let mut player_count: usize = 0;
|
||||||
|
|
||||||
// find player idx
|
// find player idx
|
||||||
for (_entity, _transform, _color, player) in characters.iter() {
|
for (_entity, _color, player) in characters.iter() {
|
||||||
if let Some(_player) = player {
|
if let Some(_player) = player {
|
||||||
player_idx = player_count;
|
player_idx = player_count;
|
||||||
}
|
}
|
||||||
|
@ -250,7 +264,7 @@ fn change_character_system(
|
||||||
player_count = 0;
|
player_count = 0;
|
||||||
|
|
||||||
// exchange `Player` component from old `player_idx` to new `next_player_idx`
|
// exchange `Player` component from old `player_idx` to new `next_player_idx`
|
||||||
for (entity, _transform, color, _player) in characters.iter() {
|
for (entity, color, _player) in characters.iter() {
|
||||||
if player_count == player_idx {
|
if player_count == player_idx {
|
||||||
commands.entity(entity).remove::<Player>();
|
commands.entity(entity).remove::<Player>();
|
||||||
}
|
}
|
||||||
|
@ -260,6 +274,7 @@ fn change_character_system(
|
||||||
audio
|
audio
|
||||||
.send(AudioMsg::Color([color.0.r(), color.0.g(), color.0.b()]))
|
.send(AudioMsg::Color([color.0.r(), color.0.g(), color.0.b()]))
|
||||||
.ok();
|
.ok();
|
||||||
|
audio.send(AudioMsg::Jump).ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
player_count += 1;
|
player_count += 1;
|
||||||
|
|
Loading…
Reference in a new issue