2022-08-21 17:53:08 +00:00
|
|
|
#![allow(clippy::precedence)]
|
2022-08-22 18:03:19 +00:00
|
|
|
#![allow(clippy::too_many_arguments)]
|
2022-08-21 17:53:08 +00:00
|
|
|
|
2022-08-21 17:17:55 +00:00
|
|
|
use crate::AppState;
|
|
|
|
|
|
|
|
use bevy::{
|
|
|
|
input::{keyboard::KeyCode, Input},
|
|
|
|
prelude::{shape::Quad, *},
|
2022-08-22 21:34:54 +00:00
|
|
|
sprite::Mesh2dHandle,
|
2022-08-21 17:17:55 +00:00
|
|
|
};
|
|
|
|
use bevy_fundsp::prelude::*;
|
|
|
|
use bevy_hanabi::*;
|
|
|
|
use bevy_rapier2d::prelude::*;
|
|
|
|
use std::collections::BTreeSet;
|
|
|
|
|
2022-08-23 10:02:58 +00:00
|
|
|
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
|
|
|
|
pub struct LevelId(pub u32);
|
|
|
|
|
2022-08-21 17:17:55 +00:00
|
|
|
pub struct GamePlugin;
|
|
|
|
|
|
|
|
impl Plugin for GamePlugin {
|
|
|
|
fn build(&self, app: &mut App) {
|
|
|
|
app.add_event::<LevelStartupEvent>()
|
|
|
|
.init_resource::<CharacterMeshes>()
|
|
|
|
.insert_resource(CurrentLevel(None))
|
|
|
|
.add_system_set(SystemSet::on_enter(AppState::Game).with_system(setup))
|
2022-08-23 10:02:58 +00:00
|
|
|
.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),
|
|
|
|
)
|
2022-08-21 17:17:55 +00:00
|
|
|
.add_system_set(
|
|
|
|
SystemSet::on_update(AppState::Game)
|
2022-08-23 10:02:58 +00:00
|
|
|
.with_system(crate::levels::post_setup_level)
|
2022-08-21 17:17:55 +00:00
|
|
|
.with_system(keyboard_input_system),
|
|
|
|
)
|
2022-08-23 10:02:58 +00:00
|
|
|
.add_system_set(SystemSet::on_update(AppState::Win).with_system(keyboard_input_system))
|
2022-08-21 17:17:55 +00:00
|
|
|
.add_system_to_stage(CoreStage::PostUpdate, collision_event_system);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Events
|
|
|
|
|
2022-08-23 10:02:58 +00:00
|
|
|
pub struct LevelStartupEvent(pub Entity);
|
2022-08-21 17:17:55 +00:00
|
|
|
|
|
|
|
// Resources
|
|
|
|
|
2022-08-23 10:02:58 +00:00
|
|
|
pub struct CurrentLevel(pub Option<LevelId>);
|
2022-08-21 17:17:55 +00:00
|
|
|
|
2022-08-23 10:02:58 +00:00
|
|
|
pub struct CharacterMeshes {
|
2022-08-21 17:17:55 +00:00
|
|
|
square: Mesh2dHandle,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromWorld for CharacterMeshes {
|
|
|
|
fn from_world(world: &mut World) -> Self {
|
|
|
|
let mut meshes = world.get_resource_mut::<Assets<Mesh>>().unwrap();
|
|
|
|
Self {
|
|
|
|
square: meshes
|
|
|
|
.add(Mesh::from(Quad {
|
|
|
|
size: Vec2 { x: 64.0, y: 64.0 },
|
|
|
|
flip: false,
|
|
|
|
}))
|
|
|
|
.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Components
|
|
|
|
|
2022-08-23 10:02:58 +00:00
|
|
|
#[derive(Component)]
|
|
|
|
pub struct Level;
|
2022-08-21 17:17:55 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Component, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
2022-08-23 10:02:58 +00:00
|
|
|
pub struct CharacterId(pub u32);
|
2022-08-21 17:17:55 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Component, Copy, Eq, Hash, PartialEq)]
|
2022-08-23 10:02:58 +00:00
|
|
|
pub struct SelectedCharacterId(pub Option<CharacterId>);
|
2022-08-21 17:17:55 +00:00
|
|
|
|
|
|
|
#[derive(Component)]
|
2022-08-23 10:02:58 +00:00
|
|
|
pub struct CharacterIdList(pub BTreeSet<CharacterId>);
|
2022-08-21 17:17:55 +00:00
|
|
|
|
2022-08-22 21:34:54 +00:00
|
|
|
#[derive(Clone, Component, PartialEq)]
|
2022-08-23 10:02:58 +00:00
|
|
|
pub struct CharacterColor(pub Color);
|
2022-08-22 21:34:54 +00:00
|
|
|
|
2022-08-21 17:17:55 +00:00
|
|
|
// Systems
|
|
|
|
|
|
|
|
fn setup(
|
|
|
|
mut commands: Commands,
|
|
|
|
mut current_level: ResMut<CurrentLevel>,
|
|
|
|
mut level_startup_event: EventWriter<LevelStartupEvent>,
|
|
|
|
) {
|
2022-08-23 10:02:58 +00:00
|
|
|
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,
|
|
|
|
);
|
2022-08-21 17:17:55 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 10:02:58 +00:00
|
|
|
pub fn spawn_character(
|
2022-08-21 17:17:55 +00:00
|
|
|
commands: &mut Commands,
|
|
|
|
character_meshes: &Res<CharacterMeshes>,
|
|
|
|
effects: &mut ResMut<Assets<EffectAsset>>,
|
2022-08-21 21:46:07 +00:00
|
|
|
materials: &mut ResMut<Assets<ColorMaterial>>,
|
2022-08-21 17:17:55 +00:00
|
|
|
selected_character_id: &mut Mut<SelectedCharacterId>,
|
|
|
|
character_id_list: &mut Mut<CharacterIdList>,
|
|
|
|
transform: Transform,
|
2022-08-21 21:46:07 +00:00
|
|
|
color: Color,
|
2022-08-21 17:17:55 +00:00
|
|
|
) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
let mut gradient = Gradient::new();
|
2022-08-21 21:46:07 +00:00
|
|
|
gradient.add_key(
|
|
|
|
0.0,
|
|
|
|
(Vec4::from(color) + Vec4::new(0.1, 0.1, 0.1, 0.0))
|
|
|
|
.clamp(Vec4::new(0., 0., 0., 0.), Vec4::new(1., 1., 1., 0.)),
|
|
|
|
);
|
|
|
|
gradient.add_key(
|
|
|
|
0.2,
|
|
|
|
(Vec4::from(color) + Vec4::new(0.1, 0.1, 0.1, 0.0))
|
|
|
|
.clamp(Vec4::new(0., 0., 0., 0.), Vec4::new(1., 1., 1., 1.)),
|
|
|
|
);
|
|
|
|
gradient.add_key(
|
|
|
|
1.0,
|
|
|
|
(Vec4::from(color) + Vec4::new(0.1, 0.1, 0.1, 0.0))
|
|
|
|
.clamp(Vec4::new(0., 0., 0., 0.), Vec4::new(1., 1., 1., 0.)),
|
|
|
|
);
|
2022-08-21 17:17:55 +00:00
|
|
|
commands
|
2022-08-22 21:34:54 +00:00
|
|
|
.spawn_bundle(ColorMesh2dBundle {
|
2022-08-21 17:17:55 +00:00
|
|
|
mesh: character_meshes.square.clone(),
|
2022-08-21 21:46:07 +00:00
|
|
|
material: materials.add(ColorMaterial::from(color)),
|
2022-08-21 17:17:55 +00:00
|
|
|
transform,
|
|
|
|
..default()
|
|
|
|
})
|
2022-08-23 10:02:58 +00:00
|
|
|
.insert(Level)
|
2022-08-21 17:17:55 +00:00
|
|
|
.insert(character_id)
|
2022-08-22 21:34:54 +00:00
|
|
|
.insert(CharacterColor(color))
|
2022-08-21 17:17:55 +00:00
|
|
|
.insert(RigidBody::Dynamic)
|
|
|
|
.insert(Collider::cuboid(32., 32.))
|
|
|
|
.insert(ExternalForce::default())
|
2022-08-22 16:56:11 +00:00
|
|
|
.insert(Velocity::default())
|
|
|
|
.insert(GravityScale(10.0))
|
|
|
|
.insert(LockedAxes::ROTATION_LOCKED)
|
2022-08-22 18:03:19 +00:00
|
|
|
.insert(Friction::new(0.8))
|
2022-08-21 17:17:55 +00:00
|
|
|
.insert(Damping {
|
|
|
|
linear_damping: 0.5,
|
|
|
|
angular_damping: 0.5,
|
|
|
|
})
|
|
|
|
.insert(ExternalImpulse::default())
|
|
|
|
.insert(ActiveEvents::COLLISION_EVENTS)
|
|
|
|
.with_children(|c| {
|
|
|
|
c.spawn_bundle(ParticleEffectBundle {
|
|
|
|
effect: ParticleEffect::new(
|
|
|
|
effects.add(
|
|
|
|
EffectAsset {
|
2022-08-21 21:46:07 +00:00
|
|
|
name: "Particles".into(),
|
2022-08-21 17:17:55 +00:00
|
|
|
capacity: 4096,
|
|
|
|
spawner: Spawner::rate(30.0.into())
|
|
|
|
.with_active(selected_character_id.0.is_none()),
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
.init(PositionCircleModifier {
|
|
|
|
radius: 30.0,
|
|
|
|
speed: 20.0.into(),
|
|
|
|
dimension: ShapeDimension::Surface,
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.init(ParticleLifetimeModifier { lifetime: 0.8 })
|
|
|
|
.render(SizeOverLifetimeModifier {
|
|
|
|
gradient: Gradient::constant(Vec2::splat(4.0)),
|
|
|
|
})
|
|
|
|
.render(ColorOverLifetimeModifier { gradient }),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
transform: Transform::from_xyz(0., 0., 0.1),
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// If no character is selected, then select this one
|
|
|
|
if selected_character_id.0.is_none() {
|
|
|
|
selected_character_id.0 = Some(character_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn collision_event_system(
|
|
|
|
mut commands: Commands,
|
2022-08-22 21:34:54 +00:00
|
|
|
character_meshes: Res<CharacterMeshes>,
|
|
|
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
|
|
|
mut effects: ResMut<Assets<EffectAsset>>,
|
2022-08-21 17:17:55 +00:00
|
|
|
mut collision_events: EventReader<CollisionEvent>,
|
2022-08-22 21:34:54 +00:00
|
|
|
character_query: Query<(&CharacterId, &CharacterColor, &Transform)>,
|
2022-08-21 17:17:55 +00:00
|
|
|
mut level_query: Query<(&mut SelectedCharacterId, &mut CharacterIdList)>,
|
2022-08-23 10:02:58 +00:00
|
|
|
mut app_state: ResMut<State<AppState>>,
|
2022-08-21 17:17:55 +00:00
|
|
|
) {
|
2022-08-23 10:02:58 +00:00
|
|
|
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))
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
|
|
|
|
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
|
2022-08-21 17:17:55 +00:00
|
|
|
{
|
2022-08-23 10:02:58 +00:00
|
|
|
app_state.replace(AppState::Win).ok();
|
2022-08-21 17:17:55 +00:00
|
|
|
}
|
2022-08-23 10:02:58 +00:00
|
|
|
|
|
|
|
spawn_character(
|
|
|
|
&mut commands,
|
|
|
|
&character_meshes,
|
|
|
|
&mut effects,
|
|
|
|
&mut materials,
|
|
|
|
&mut selected_character_id,
|
|
|
|
&mut character_id_list,
|
|
|
|
*c1_transform,
|
|
|
|
new_color.into(),
|
|
|
|
);
|
2022-08-21 17:17:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn keyboard_input_system(
|
|
|
|
keyboard_input: Res<Input<KeyCode>>,
|
|
|
|
mut characters: Query<(
|
|
|
|
&CharacterId,
|
2022-08-22 18:03:19 +00:00
|
|
|
&mut Velocity,
|
2022-08-21 17:17:55 +00:00
|
|
|
&mut ExternalImpulse,
|
|
|
|
&mut ExternalForce,
|
|
|
|
&Children,
|
|
|
|
)>,
|
|
|
|
mut level_query: Query<(&mut SelectedCharacterId, &CharacterIdList)>,
|
|
|
|
mut effect: Query<&mut ParticleEffect>,
|
|
|
|
dsp_assets: Res<DspAssets>,
|
|
|
|
audio: Res<Audio>,
|
2022-08-23 10:02:58 +00:00
|
|
|
mut app_state: ResMut<State<AppState>>,
|
2022-08-21 17:17:55 +00:00
|
|
|
) {
|
2022-08-23 10:02:58 +00:00
|
|
|
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));
|
2022-08-22 16:56:11 +00:00
|
|
|
|
2022-08-23 10:02:58 +00:00
|
|
|
let selected = if let Some(selected_character_id) = &mut selected_character_id.0 {
|
2022-08-22 18:03:19 +00:00
|
|
|
if let Some((_character_id, _velocity, _impulse, _force, children)) = characters
|
|
|
|
.iter_mut()
|
|
|
|
.find(|(character_id, _velocity, _impulse, _force, _children)| {
|
2022-08-23 10:02:58 +00:00
|
|
|
*character_id == selected_character_id
|
2022-08-22 18:03:19 +00:00
|
|
|
}) {
|
2022-08-21 17:17:55 +00:00
|
|
|
effect
|
|
|
|
.get_mut(children[0])
|
|
|
|
.unwrap()
|
|
|
|
.maybe_spawner()
|
|
|
|
.unwrap()
|
2022-08-23 10:02:58 +00:00
|
|
|
.set_active(false);
|
2022-08-21 17:17:55 +00:00
|
|
|
}
|
2022-08-22 16:56:11 +00:00
|
|
|
|
2022-08-23 10:02:58 +00:00
|
|
|
*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 let Some((_character_id, _velocity, _impulse, _force, children)) = characters
|
|
|
|
.iter_mut()
|
|
|
|
.find(|(character_id, _velocity, _impulse, _force, _children)| {
|
|
|
|
**character_id == selected
|
|
|
|
}) {
|
|
|
|
effect
|
|
|
|
.get_mut(children[0])
|
|
|
|
.unwrap()
|
|
|
|
.maybe_spawner()
|
|
|
|
.unwrap()
|
|
|
|
.set_active(true);
|
|
|
|
}
|
|
|
|
}
|
2022-08-22 16:56:11 +00:00
|
|
|
|
2022-08-23 10:02:58 +00:00
|
|
|
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, _impulse, _force, _children)) = characters
|
|
|
|
.iter_mut()
|
|
|
|
.find(|(character_id, _velocity, _impulse, _force, _children)| {
|
|
|
|
*character_id == selected_character_id
|
|
|
|
}) {
|
|
|
|
velocity.linvel.x = 200. * (right_pressed as i8 - left_pressed as i8) as f32;
|
|
|
|
|
|
|
|
if keyboard_input.just_pressed(KeyCode::Space) {
|
|
|
|
velocity.linvel.y = 500.;
|
2022-08-21 17:17:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-23 10:02:58 +00:00
|
|
|
|
|
|
|
if app_state.current() == &AppState::Win && keyboard_input.just_pressed(KeyCode::Return) {
|
|
|
|
app_state.replace(AppState::Game).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn win_setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
2022-08-23 10:42:56 +00:00
|
|
|
let font = asset_server.get_handle("UacariLegacy-Thin.ttf");
|
|
|
|
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()
|
|
|
|
})
|
|
|
|
.insert(Level);
|
2022-08-21 17:17:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sounds
|
|
|
|
|
|
|
|
pub fn sine_wave() -> impl AudioUnit32 {
|
|
|
|
sine_hz(440.0) >> split::<U2>() * 0.2
|
|
|
|
}
|