complete switch from character id to Player component as 'tag'

This commit is contained in:
Nixon 2022-08-24 19:26:25 +08:00
parent ce37ddd0bd
commit e86ca82bc3
4 changed files with 149 additions and 160 deletions

View file

@ -6,7 +6,7 @@ use crate::AppState;
use bevy::{
input::{keyboard::KeyCode, Input},
prelude::{shape::Quad, *},
sprite::Mesh2dHandle,
sprite::Mesh2dHandle, ecs::system::EntityCommands,
};
use bevy_rapier2d::prelude::*;
use std::collections::BTreeSet;
@ -34,13 +34,14 @@ impl Plugin for GamePlugin {
.add_system_set(
SystemSet::on_update(AppState::Game)
.with_system(crate::levels::post_setup_level)
.with_system(keyboard_input_system)
.with_system(player_movement_system)
.with_system(move_camera)
.with_system(character_particle_effect_system),
)
.add_system_set(
SystemSet::on_update(AppState::Win)
.with_system(keyboard_input_system)
.with_system(change_character_system)
.with_system(player_movement_system)
.with_system(move_camera)
.with_system(character_particle_effect_system),
)
@ -79,18 +80,12 @@ impl FromWorld for CharacterMeshes {
#[derive(Component)]
pub struct Level;
#[derive(Clone, Component, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct CharacterId(pub u32);
#[derive(Clone, Component, Copy, Eq, Hash, PartialEq)]
pub struct SelectedCharacterId(pub Option<CharacterId>);
#[derive(Component)]
pub struct CharacterIdList(pub BTreeSet<CharacterId>);
#[derive(Clone, Component, PartialEq)]
pub struct CharacterColor(pub Color);
#[derive(Component)]
pub struct Player;
// Systems
fn setup(
@ -109,36 +104,60 @@ fn setup(
);
}
pub fn spawn_characters(
commands: &mut Commands,
character_meshes: &Res<CharacterMeshes>,
materials: &mut ResMut<Assets<ColorMaterial>>,
audio: &Res<crossbeam_channel::Sender<AudioMsg>>,
transforms: &mut Vec<Transform>,
colors: &Vec<Color>,
) {
spawn_character(
commands,
character_meshes,
materials,
audio,
transforms[0],
colors[0],
true,
);
for c in 1 .. transforms.len() {
spawn_character(
commands,
character_meshes,
materials,
audio,
transforms[c],
colors[c],
false,
);
}
}
pub fn spawn_character(
commands: &mut Commands,
character_meshes: &Res<CharacterMeshes>,
materials: &mut ResMut<Assets<ColorMaterial>>,
selected_character_id: &mut Mut<SelectedCharacterId>,
character_id_list: &mut Mut<CharacterIdList>,
audio: &Res<crossbeam_channel::Sender<AudioMsg>>,
mut transform: Transform,
color: Color,
is_player: bool,
) {
transform.translation.z = transform.translation.z.max(1.0);
let character_id = CharacterId(
character_id_list
.0
.iter()
.last()
.map_or(0, |last_character_id| last_character_id.0 + 1),
);
character_id_list.0.insert(character_id);
commands
.spawn_bundle(ColorMesh2dBundle {
let color_mesh_2d_bundle: ColorMesh2dBundle = ColorMesh2dBundle {
mesh: character_meshes.square.clone(),
material: materials.add(ColorMaterial::from(color)),
transform,
..default()
})
};
let mut entity_commands: EntityCommands = commands.spawn_bundle(color_mesh_2d_bundle);
entity_commands
.insert(Level)
.insert(character_id)
.insert(CharacterColor(color))
.insert(RigidBody::Dynamic)
.insert(Collider::cuboid(32., 32.))
@ -154,9 +173,8 @@ pub fn spawn_character(
.insert(ExternalImpulse::default())
.insert(ActiveEvents::COLLISION_EVENTS);
// If no character is selected, then select this one
if selected_character_id.0.is_none() {
selected_character_id.0 = Some(character_id);
if is_player {
entity_commands.insert(Player);
audio
.send(AudioMsg::Color([color.r(), color.g(), color.b()]))
.ok();
@ -168,23 +186,16 @@ fn collision_event_system(
character_meshes: Res<CharacterMeshes>,
mut materials: ResMut<Assets<ColorMaterial>>,
mut collision_events: EventReader<CollisionEvent>,
character_query: Query<(&CharacterId, &CharacterColor, &Transform)>,
mut level_query: Query<(&mut SelectedCharacterId, &mut CharacterIdList)>,
character_query: Query<(&CharacterColor, &Transform)>,
mut app_state: ResMut<State<AppState>>,
audio: Res<crossbeam_channel::Sender<AudioMsg>>,
) {
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))) =
if let (Ok((c1_color, c1_transform)), Ok((c2_color, _c2_transform))) =
(character_query.get(*e1), character_query.get(*e2))
{
let (mut selected_character_id, mut character_id_list) =
level_query.single_mut();
character_id_list.0.remove(c1_id);
character_id_list.0.remove(c2_id);
selected_character_id.0 = None;
// TODO completely remove particles
commands.entity(*e1).despawn_recursive();
commands.entity(*e2).despawn_recursive();
@ -203,11 +214,10 @@ fn collision_event_system(
&mut commands,
&character_meshes,
&mut materials,
&mut selected_character_id,
&mut character_id_list,
&audio,
*c1_transform,
new_color.into(),
true,
);
}
}
@ -215,47 +225,60 @@ fn collision_event_system(
}
}
fn keyboard_input_system(
fn change_character_system(
mut commands: Commands,
keyboard_input: Res<Input<KeyCode>>,
mut characters: Query<(&CharacterId, &mut Velocity, &CharacterColor)>,
mut level_query: Query<(&mut SelectedCharacterId, &CharacterIdList)>,
mut app_state: ResMut<State<AppState>>,
characters: Query<(Entity, &Transform, &CharacterColor, Option<&Player>)>,
audio: Res<crossbeam_channel::Sender<AudioMsg>>,
) {
if let Ok((mut selected_character_id, character_id_list)) = level_query.get_single_mut() {
if keyboard_input.just_pressed(KeyCode::Tab) {
let selected = if let Some(selected_character_id) = &mut selected_character_id.0 {
*selected_character_id = *character_id_list
.0
.range(*selected_character_id..)
.nth(1)
.unwrap_or_else(|| character_id_list.0.iter().next().unwrap());
*selected_character_id
} else {
selected_character_id.0 = Some(CharacterId(0));
CharacterId(0)
};
if !keyboard_input.just_pressed(KeyCode::Tab) { return; }
if let Some((_character_id, _velocity, color)) = characters
.iter_mut()
.find(|(character_id, _velocity, _color)| **character_id == selected)
{
let mut player_idx: usize = 0;
let mut player_count: usize = 0;
// find player idx
for (_entity, _transform, _color, player) in characters.iter() {
if let Some(_player) = player {
player_idx = player_count;
}
player_count += 1;
}
// calculate next player index
let next_player_idx = (player_idx + 1) % player_count;
player_count = 0;
// exchange `Player` component from old `player_idx` to new `next_player_idx`
for (entity, _transform, color, _player) in characters.iter() {
if player_count == player_idx {
commands.entity(entity).remove::<Player>();
}
if player_count == next_player_idx {
commands.entity(entity).insert(Player);
audio
.send(AudioMsg::Color([color.0.r(), color.0.g(), color.0.b()]))
.ok();
}
player_count += 1;
}
}
fn player_movement_system(
keyboard_input: Res<Input<KeyCode>>,
mut characters: Query<&mut Velocity, With<Player>>,
mut app_state: ResMut<State<AppState>>,
audio: Res<crossbeam_channel::Sender<AudioMsg>>,
) {
let right_pressed: bool =
keyboard_input.pressed(KeyCode::Right) || keyboard_input.pressed(KeyCode::D);
let left_pressed: bool =
keyboard_input.pressed(KeyCode::Left) || keyboard_input.pressed(KeyCode::A);
if let Some(selected_character_id) = &selected_character_id.0 {
if let Some((_character_id, mut velocity, _color)) = characters
.iter_mut()
.find(|(character_id, _velocity, _color)| *character_id == selected_character_id)
{
for mut velocity in characters.iter_mut() {
velocity.linvel.x = 200. * (right_pressed as i8 - left_pressed as i8) as f32;
if keyboard_input.just_pressed(KeyCode::Space) {
@ -263,8 +286,6 @@ fn keyboard_input_system(
velocity.linvel.y = 500.;
}
}
}
}
if app_state.current() == &AppState::Win && keyboard_input.just_pressed(KeyCode::Return) {
app_state.replace(AppState::Game).unwrap();
@ -272,21 +293,13 @@ fn keyboard_input_system(
}
fn character_particle_effect_system(
mut characters: Query<(&CharacterId, &Transform, &CharacterColor)>,
characters: Query<(&Transform, &CharacterColor), With<Player>>,
mut particle_effect: ResMut<crate::particle_effect::ParticleEffectResource>,
mut level_query: Query<&SelectedCharacterId>,
) {
if let Ok(selected_character_id) = level_query.get_single_mut() {
if let Some(selected_character_id) = &selected_character_id.0 {
if let Some((_character_id, transform, color)) = characters
.iter_mut()
.find(|(character_id, _transform, _color)| *character_id == selected_character_id)
{
for (transform, color) in characters.iter() {
particle_effect.translation = transform.translation;
particle_effect.color = color.0;
}
}
}
}
fn win_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
@ -309,29 +322,25 @@ fn win_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
fn move_camera(
mut camera_query: Query<(&Camera, &mut Transform)>,
characters: Query<(&CharacterId, &Transform), Without<Camera>>,
level_query: Query<&SelectedCharacterId>,
characters: Query<&Transform, (Without<Camera>, With<Player>)>,
time: Res<Time>,
) {
const MARGIN: f32 = 300.0;
const FOLLOW_SPEED: f32 = std::f32::consts::PI;
if let Ok(selected_character_id) = level_query.get_single() {
if let Some(selected_character_id) = &selected_character_id.0 {
if let Some((_character_id, transform)) = characters
.iter()
.find(|(character_id, _transform)| *character_id == selected_character_id)
{
for character_transform in characters.iter() {
let (camera, mut camera_transform) =
camera_query.single_mut();
let size: Vec2 = camera.logical_viewport_size().unwrap();
let half_height: f32 = size.y * 0.5;
camera_transform.translation = camera_transform.translation.lerp(transform.translation, time.delta_seconds() * FOLLOW_SPEED);
camera_transform.translation = camera_transform.translation.lerp(
character_transform.translation, time.delta_seconds() * FOLLOW_SPEED
);
// prevent camera from going too low
camera_transform.translation.y = camera_transform.translation.y.max(half_height-MARGIN);
// always make sure that camera is away from the object in order to render them
camera_transform.translation.z = 999.0;
}
}
}
}

View file

@ -7,7 +7,6 @@ mod level1;
use crate::game::*;
use bevy::prelude::*;
use std::collections::BTreeSet;
pub fn setup_level(
commands: &mut Commands,
@ -19,8 +18,6 @@ pub fn setup_level(
let level_entity = commands
.spawn()
.insert(Level)
.insert(SelectedCharacterId(None))
.insert(CharacterIdList(BTreeSet::new()))
.id();
current_level.0 = Some(level_id);
@ -41,36 +38,27 @@ pub fn post_setup_level(
character_meshes: Res<CharacterMeshes>,
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>,
audio: Res<crossbeam_channel::Sender<AudioMsg>>,
) {
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 materials,
&mut selected_character_id,
&mut character_id_list,
&audio,
),
1 => level1::setup(
&mut commands,
&character_meshes,
&mut materials,
&mut selected_character_id,
&mut character_id_list,
&audio,
),
_ => game_over::setup(&mut commands, &asset_server),
}
}
}
}
}

View file

@ -7,8 +7,8 @@ pub fn setup(
commands: &mut Commands,
character_meshes: &Res<CharacterMeshes>,
materials: &mut ResMut<Assets<ColorMaterial>>,
selected_character_id: &mut Mut<SelectedCharacterId>,
character_id_list: &mut Mut<CharacterIdList>,
// selected_character_id: &mut Mut<SelectedCharacterId>,
// character_id_list: &mut Mut<CharacterIdList>,
audio: &Res<crossbeam_channel::Sender<AudioMsg>>,
) {
commands
@ -20,30 +20,27 @@ pub fn setup(
commands,
character_meshes,
materials,
selected_character_id,
character_id_list,
audio,
Transform::from_xyz(-128., -64., 0.),
Color::RED,
true,
);
spawn_character(
commands,
character_meshes,
materials,
selected_character_id,
character_id_list,
audio,
Transform::from_xyz(0., -64., 0.),
Color::GREEN,
false,
);
spawn_character(
commands,
character_meshes,
materials,
selected_character_id,
character_id_list,
audio,
Transform::from_xyz(128., -64., 0.),
Color::BLUE,
false,
);
}

View file

@ -7,8 +7,6 @@ pub fn setup(
commands: &mut Commands,
character_meshes: &Res<CharacterMeshes>,
materials: &mut ResMut<Assets<ColorMaterial>>,
selected_character_id: &mut Mut<SelectedCharacterId>,
character_id_list: &mut Mut<CharacterIdList>,
audio: &Res<crossbeam_channel::Sender<AudioMsg>>,
) {
commands
@ -26,30 +24,27 @@ pub fn setup(
commands,
character_meshes,
materials,
selected_character_id,
character_id_list,
audio,
Transform::from_xyz(128., 64., 0.),
Color::BLUE,
true,
);
spawn_character(
commands,
character_meshes,
materials,
selected_character_id,
character_id_list,
audio,
Transform::from_xyz(-128., -128., 0.),
Color::RED,
false,
);
spawn_character(
commands,
character_meshes,
materials,
selected_character_id,
character_id_list,
audio,
Transform::from_xyz(0., -128., 0.),
Color::GREEN,
false,
);
}