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-24 10:21:08 +00:00
|
|
|
pub use crate::filters::*;
|
2022-08-28 09:04:02 +00:00
|
|
|
use crate::levels;
|
2022-08-28 17:33:41 +00:00
|
|
|
use crate::{audio_system, AppState};
|
2022-08-21 17:17:55 +00:00
|
|
|
|
|
|
|
use bevy::{
|
2022-08-24 16:51:18 +00:00
|
|
|
ecs::system::EntityCommands,
|
2022-08-21 17:17:55 +00:00
|
|
|
input::{keyboard::KeyCode, Input},
|
|
|
|
prelude::{shape::Quad, *},
|
2022-08-24 16:51:18 +00:00
|
|
|
sprite::Mesh2dHandle,
|
2022-08-21 17:17:55 +00:00
|
|
|
};
|
|
|
|
use bevy_rapier2d::prelude::*;
|
2022-08-24 21:11:59 +00:00
|
|
|
use rapier2d::geometry::CollisionEventFlags;
|
2022-08-27 08:17:49 +00:00
|
|
|
use std::collections::BTreeSet;
|
2022-08-21 17:17:55 +00:00
|
|
|
|
2022-08-25 13:52:28 +00:00
|
|
|
pub struct FirstLevel(pub LevelId);
|
|
|
|
|
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>()
|
2022-08-29 07:53:50 +00:00
|
|
|
.add_event::<ChangeCharacterEvent>()
|
2022-08-21 17:17:55 +00:00
|
|
|
.init_resource::<CharacterMeshes>()
|
|
|
|
.insert_resource(CurrentLevel(None))
|
2022-08-27 08:17:49 +00:00
|
|
|
.init_resource::<CharacterList>()
|
2022-08-28 09:04:02 +00:00
|
|
|
.init_resource::<levels::ZoomTimer>()
|
2022-08-21 17:17:55 +00:00
|
|
|
.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-24 11:45:13 +00:00
|
|
|
.with_system(change_character_system)
|
2022-08-24 11:26:25 +00:00
|
|
|
.with_system(player_movement_system)
|
2022-08-25 09:31:12 +00:00
|
|
|
.with_system(level_keyboard_system)
|
2022-08-28 09:04:02 +00:00
|
|
|
.with_system(camera_system)
|
2022-08-29 07:53:50 +00:00
|
|
|
.with_system(character_particle_effect_system)
|
|
|
|
.with_system(kill_character_system),
|
2022-08-24 16:51:18 +00:00
|
|
|
)
|
|
|
|
.add_system_set(
|
|
|
|
SystemSet::on_update(AppState::Win)
|
2022-08-24 11:26:25 +00:00
|
|
|
.with_system(player_movement_system)
|
2022-08-25 09:31:12 +00:00
|
|
|
.with_system(level_keyboard_system)
|
2022-08-28 09:04:02 +00:00
|
|
|
.with_system(camera_system)
|
2022-08-27 07:08:14 +00:00
|
|
|
.with_system(character_particle_effect_system)
|
|
|
|
.with_system(move_win_text_system),
|
2022-08-21 17:17:55 +00:00
|
|
|
)
|
2022-08-27 12:51:18 +00:00
|
|
|
.add_system_to_stage(CoreStage::PostUpdate, char_char_collision_event_system)
|
2022-08-27 13:26:31 +00:00
|
|
|
.add_system_to_stage(CoreStage::PostUpdate, char_platform_collision_event_system)
|
2022-08-27 13:30:28 +00:00
|
|
|
// collision event system might remove items, therefore, we should detect platforms first before removing them
|
2022-08-27 17:24:31 +00:00
|
|
|
.add_system_to_stage(
|
|
|
|
CoreStage::PostUpdate,
|
|
|
|
collision_event_system.after(char_platform_collision_event_system),
|
|
|
|
);
|
2022-08-21 17:17:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Events
|
|
|
|
|
2022-08-25 09:31:12 +00:00
|
|
|
pub struct LevelStartupEvent;
|
2022-08-21 17:17:55 +00:00
|
|
|
|
2022-08-29 07:53:50 +00:00
|
|
|
pub struct ChangeCharacterEvent;
|
|
|
|
|
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-27 08:17:49 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct CharacterList(pub BTreeSet<Entity>);
|
|
|
|
|
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
|
|
|
|
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-24 11:26:25 +00:00
|
|
|
#[derive(Component)]
|
|
|
|
pub struct Player;
|
|
|
|
|
2022-08-25 12:47:20 +00:00
|
|
|
#[derive(Component)]
|
2022-08-27 12:51:18 +00:00
|
|
|
pub struct Platform;
|
|
|
|
|
|
|
|
#[derive(Component)]
|
2022-08-27 13:26:31 +00:00
|
|
|
pub struct PlatformCount(usize);
|
|
|
|
|
|
|
|
impl PlatformCount {
|
|
|
|
fn increment(&mut self) {
|
|
|
|
self.0 += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn decrement(&mut self) {
|
|
|
|
self.0 = self.0.saturating_sub(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn reset(&mut self) {
|
|
|
|
self.0 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_landed(&self) -> bool {
|
2022-08-27 17:24:31 +00:00
|
|
|
self.0 != 0
|
2022-08-27 13:26:31 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-25 12:47:20 +00:00
|
|
|
|
2022-08-26 21:54:49 +00:00
|
|
|
#[derive(Component)]
|
|
|
|
pub struct Melty(pub Color);
|
|
|
|
|
2022-08-27 07:08:14 +00:00
|
|
|
#[derive(Component)]
|
|
|
|
pub struct WinText;
|
|
|
|
|
2022-08-21 17:17:55 +00:00
|
|
|
// Systems
|
|
|
|
|
|
|
|
fn setup(
|
2022-08-25 13:52:28 +00:00
|
|
|
first_level: Res<FirstLevel>,
|
2022-08-21 17:17:55 +00:00
|
|
|
mut current_level: ResMut<CurrentLevel>,
|
|
|
|
mut level_startup_event: EventWriter<LevelStartupEvent>,
|
2022-08-28 09:09:48 +00:00
|
|
|
mut camera_query: Query<&mut Transform, With<Camera>>,
|
2022-08-28 09:04:02 +00:00
|
|
|
mut zoom_timer: ResMut<levels::ZoomTimer>,
|
2022-08-21 17:17:55 +00:00
|
|
|
) {
|
2022-08-25 09:31:12 +00:00
|
|
|
if current_level.0.is_none() {
|
2022-08-25 13:52:28 +00:00
|
|
|
current_level.0 = Some(first_level.0);
|
2022-08-25 09:31:12 +00:00
|
|
|
}
|
|
|
|
|
2022-08-28 09:04:02 +00:00
|
|
|
crate::levels::setup_level(&mut level_startup_event, &mut camera_query, &mut zoom_timer);
|
2022-08-21 17:17:55 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 16:51:18 +00:00
|
|
|
pub fn spawn_characters<I: IntoIterator<Item = (Transform, Color)>>(
|
2022-08-24 11:26:25 +00:00
|
|
|
commands: &mut Commands,
|
|
|
|
character_meshes: &Res<CharacterMeshes>,
|
|
|
|
materials: &mut ResMut<Assets<ColorMaterial>>,
|
2022-08-27 08:17:49 +00:00
|
|
|
character_list: &mut ResMut<CharacterList>,
|
2022-08-24 11:26:25 +00:00
|
|
|
|
2022-08-24 16:51:18 +00:00
|
|
|
characters: I,
|
2022-08-24 11:26:25 +00:00
|
|
|
) {
|
2022-08-24 16:51:18 +00:00
|
|
|
for (i, (transform, color)) in characters.into_iter().enumerate() {
|
2022-08-24 11:26:25 +00:00
|
|
|
spawn_character(
|
|
|
|
commands,
|
|
|
|
character_meshes,
|
|
|
|
materials,
|
2022-08-27 08:17:49 +00:00
|
|
|
character_list,
|
2022-08-24 16:51:18 +00:00
|
|
|
transform,
|
|
|
|
color,
|
|
|
|
i == 0,
|
2022-08-24 11:26:25 +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>,
|
2022-08-21 21:46:07 +00:00
|
|
|
materials: &mut ResMut<Assets<ColorMaterial>>,
|
2022-08-27 08:17:49 +00:00
|
|
|
character_list: &mut ResMut<CharacterList>,
|
2022-08-24 08:54:15 +00:00
|
|
|
mut transform: Transform,
|
2022-08-21 21:46:07 +00:00
|
|
|
color: Color,
|
2022-08-24 11:26:25 +00:00
|
|
|
is_player: bool,
|
2022-08-21 17:17:55 +00:00
|
|
|
) {
|
2022-08-24 08:54:15 +00:00
|
|
|
transform.translation.z = transform.translation.z.max(1.0);
|
|
|
|
|
2022-08-24 11:26:25 +00:00
|
|
|
let color_mesh_2d_bundle: ColorMesh2dBundle = ColorMesh2dBundle {
|
|
|
|
mesh: character_meshes.square.clone(),
|
|
|
|
material: materials.add(ColorMaterial::from(color)),
|
|
|
|
transform,
|
|
|
|
..default()
|
|
|
|
};
|
2022-08-21 17:17:55 +00:00
|
|
|
|
2022-08-24 11:26:25 +00:00
|
|
|
let mut entity_commands: EntityCommands = commands.spawn_bundle(color_mesh_2d_bundle);
|
|
|
|
|
|
|
|
entity_commands
|
2022-08-23 10:02:58 +00:00
|
|
|
.insert(Level)
|
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.))
|
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,
|
|
|
|
})
|
2022-08-25 12:47:20 +00:00
|
|
|
.insert(ActiveEvents::COLLISION_EVENTS)
|
|
|
|
.with_children(|c| {
|
|
|
|
c.spawn_bundle(TransformBundle::from_transform(Transform::from_xyz(
|
|
|
|
0., -33., 0.,
|
|
|
|
)))
|
|
|
|
.insert(Sensor)
|
|
|
|
.insert(Collider::cuboid(30., 0.5))
|
|
|
|
.insert(ActiveEvents::COLLISION_EVENTS)
|
2022-08-27 13:26:31 +00:00
|
|
|
.insert(PlatformCount(0));
|
2022-08-25 12:47:20 +00:00
|
|
|
});
|
2022-08-21 17:17:55 +00:00
|
|
|
|
2022-08-27 08:17:49 +00:00
|
|
|
character_list.0.insert(entity_commands.id());
|
|
|
|
|
2022-08-24 11:26:25 +00:00
|
|
|
if is_player {
|
|
|
|
entity_commands.insert(Player);
|
2022-08-21 17:17:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-25 10:32:06 +00:00
|
|
|
pub fn spawn_platforms<I: IntoIterator<Item = (Transform, Vec2)>>(
|
|
|
|
commands: &mut Commands,
|
|
|
|
meshes: &mut ResMut<Assets<Mesh>>,
|
|
|
|
materials: &mut ResMut<Assets<ColorMaterial>>,
|
|
|
|
|
|
|
|
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<Assets<Mesh>>,
|
|
|
|
materials: &mut ResMut<Assets<ColorMaterial>>,
|
|
|
|
|
|
|
|
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.))
|
2022-08-27 12:51:18 +00:00
|
|
|
.insert(Level)
|
|
|
|
.insert(Platform);
|
2022-08-25 10:32:06 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 21:54:49 +00:00
|
|
|
pub fn spawn_melty_platform(
|
|
|
|
commands: &mut Commands,
|
|
|
|
meshes: &mut ResMut<Assets<Mesh>>,
|
|
|
|
materials: &mut ResMut<Assets<ColorMaterial>>,
|
|
|
|
asset_server: &Res<AssetServer>,
|
|
|
|
|
|
|
|
transform: Transform,
|
|
|
|
color: Color,
|
|
|
|
) {
|
|
|
|
commands
|
|
|
|
.spawn_bundle(ColorMesh2dBundle {
|
|
|
|
mesh: meshes
|
|
|
|
.add(Mesh::from(Quad {
|
|
|
|
size: Vec2 { x: 96., y: 16. },
|
|
|
|
flip: false,
|
|
|
|
}))
|
|
|
|
.into(),
|
|
|
|
material: materials.add(ColorMaterial::from(color)),
|
|
|
|
transform,
|
|
|
|
..default()
|
|
|
|
})
|
|
|
|
.insert(Collider::cuboid(48., 8.))
|
|
|
|
.insert(Melty(color))
|
|
|
|
.insert(Level)
|
2022-08-27 17:24:31 +00:00
|
|
|
.insert(Platform)
|
2022-08-26 21:54:49 +00:00
|
|
|
.with_children(|c| {
|
|
|
|
c.spawn_bundle(SpriteBundle {
|
|
|
|
texture: asset_server.get_handle("melty.png"),
|
|
|
|
transform: Transform::from_xyz(0., 0., 0.5),
|
|
|
|
..default()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-27 12:51:18 +00:00
|
|
|
fn char_char_collision_event_system(
|
2022-08-21 17:17:55 +00:00
|
|
|
mut commands: Commands,
|
2022-08-27 12:51:18 +00:00
|
|
|
|
2022-08-21 17:17:55 +00:00
|
|
|
mut collision_events: EventReader<CollisionEvent>,
|
2022-08-27 22:10:41 +00:00
|
|
|
character_query: Query<(&CharacterColor, &Transform, Option<&Player>)>,
|
2022-08-27 12:51:18 +00:00
|
|
|
|
|
|
|
mut character_list: ResMut<CharacterList>,
|
2022-08-23 10:02:58 +00:00
|
|
|
mut app_state: ResMut<State<AppState>>,
|
2022-08-27 12:51:18 +00:00
|
|
|
character_meshes: Res<CharacterMeshes>,
|
|
|
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
2022-08-28 07:28:08 +00:00
|
|
|
audio_assets: Res<audio_system::AudioAssets>,
|
|
|
|
audio: Res<Audio>,
|
2022-08-21 17:17:55 +00:00
|
|
|
) {
|
2022-08-23 10:02:58 +00:00
|
|
|
for collision_event in collision_events.iter() {
|
2022-08-27 17:24:31 +00:00
|
|
|
if let CollisionEvent::Started(e1, e2, _flags) = collision_event {
|
2022-08-27 12:51:18 +00:00
|
|
|
if let (
|
2022-08-27 17:24:31 +00:00
|
|
|
Ok((c1_color, c1_transform, c1_player)),
|
|
|
|
Ok((c2_color, c2_transform, c2_player)),
|
|
|
|
) = (character_query.get(*e1), character_query.get(*e2))
|
|
|
|
{
|
2022-08-27 12:51:18 +00:00
|
|
|
character_list.0.remove(e1);
|
|
|
|
character_list.0.remove(e2);
|
|
|
|
commands.entity(*e1).despawn_recursive();
|
|
|
|
commands.entity(*e2).despawn_recursive();
|
|
|
|
|
2022-08-27 17:24:31 +00:00
|
|
|
let new_color =
|
|
|
|
(Vec4::from(c1_color.0) + Vec4::from(c2_color.0)).clamp(Vec4::ZERO, Vec4::ONE);
|
2022-08-27 12:51:18 +00:00
|
|
|
|
|
|
|
// If color approximately white
|
|
|
|
if app_state.current() == &AppState::Game && new_color.min_element() >= 0.9 {
|
|
|
|
app_state.replace(AppState::Win).ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
// position character based on current player location
|
|
|
|
spawn_character(
|
|
|
|
&mut commands,
|
|
|
|
&character_meshes,
|
|
|
|
&mut materials,
|
|
|
|
&mut character_list,
|
|
|
|
if c1_player.is_some() {
|
|
|
|
*c1_transform
|
|
|
|
} else if c2_player.is_some() {
|
|
|
|
*c2_transform
|
|
|
|
} else {
|
|
|
|
Transform::identity().with_translation(
|
|
|
|
(c1_transform.translation + c2_transform.translation) * 0.5,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
new_color.into(),
|
|
|
|
c1_player.is_some() || c2_player.is_some(),
|
|
|
|
);
|
|
|
|
|
2022-08-28 17:33:41 +00:00
|
|
|
audio_system::play_audio(&audio_assets.warp_notes, &audio, new_color, 1.0);
|
2022-08-27 12:51:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-27 13:26:31 +00:00
|
|
|
fn char_platform_collision_event_system(
|
2022-08-27 12:51:18 +00:00
|
|
|
mut collision_events: EventReader<CollisionEvent>,
|
2022-08-27 13:26:31 +00:00
|
|
|
mut platform_count_query: Query<&mut PlatformCount>,
|
2022-08-27 12:51:18 +00:00
|
|
|
platform_query: Query<&Platform>,
|
|
|
|
) {
|
|
|
|
// detect platform + player collisions only
|
|
|
|
for collision_event in collision_events.iter() {
|
2022-08-27 17:24:31 +00:00
|
|
|
match collision_event {
|
2022-08-25 12:47:20 +00:00
|
|
|
CollisionEvent::Started(e1, e2, flags) => {
|
2022-08-27 12:51:18 +00:00
|
|
|
if *flags == CollisionEventFlags::SENSOR {
|
2022-08-27 17:24:31 +00:00
|
|
|
if let (Ok(mut platform_count), Ok(_)) =
|
|
|
|
(platform_count_query.get_mut(*e1), platform_query.get(*e2))
|
|
|
|
{
|
2022-08-27 13:26:31 +00:00
|
|
|
platform_count.increment();
|
2022-08-27 17:24:31 +00:00
|
|
|
} else if let (Ok(mut platform_count), Ok(_)) =
|
|
|
|
(platform_count_query.get_mut(*e2), platform_query.get(*e1))
|
|
|
|
{
|
2022-08-27 13:26:31 +00:00
|
|
|
platform_count.increment();
|
2022-08-27 17:24:31 +00:00
|
|
|
}
|
2022-08-21 17:17:55 +00:00
|
|
|
}
|
2022-08-25 12:47:20 +00:00
|
|
|
}
|
2022-08-28 17:33:41 +00:00
|
|
|
|
2022-08-25 12:47:20 +00:00
|
|
|
CollisionEvent::Stopped(e1, e2, flags) => {
|
|
|
|
if *flags == CollisionEventFlags::SENSOR {
|
2022-08-27 17:24:31 +00:00
|
|
|
if let (Ok(mut platform_count), Ok(_)) =
|
|
|
|
(platform_count_query.get_mut(*e1), platform_query.get(*e2))
|
|
|
|
{
|
|
|
|
platform_count.decrement();
|
|
|
|
} else if let (Ok(mut platform_count), Ok(_)) =
|
|
|
|
(platform_count_query.get_mut(*e2), platform_query.get(*e1))
|
|
|
|
{
|
|
|
|
platform_count.decrement();
|
|
|
|
}
|
2022-08-24 21:11:59 +00:00
|
|
|
}
|
2022-08-21 17:17:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-27 12:51:18 +00:00
|
|
|
fn collision_event_system(
|
|
|
|
mut commands: Commands,
|
|
|
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
|
|
|
mut collision_events: EventReader<CollisionEvent>,
|
|
|
|
mut character_query: Query<(
|
|
|
|
&mut CharacterColor,
|
|
|
|
&Transform,
|
|
|
|
&mut Handle<ColorMaterial>,
|
|
|
|
Option<&Player>,
|
|
|
|
)>,
|
|
|
|
pass_through_filter_query: Query<&PassThroughFilter>,
|
|
|
|
melty_query: Query<&Melty>,
|
2022-08-29 11:12:33 +00:00
|
|
|
mut character_list: ResMut<CharacterList>,
|
|
|
|
mut change_character_event: EventWriter<ChangeCharacterEvent>,
|
2022-08-27 12:51:18 +00:00
|
|
|
) {
|
|
|
|
for collision_event in collision_events.iter() {
|
2022-08-27 17:24:31 +00:00
|
|
|
if let CollisionEvent::Started(e1, e2, flags) = collision_event {
|
|
|
|
if flags.is_empty() {
|
|
|
|
if let (Ok((c_color, _c_transform, _c_material, _c_player)), Ok(melty)) =
|
|
|
|
(character_query.get_mut(*e1), melty_query.get(*e2))
|
|
|
|
{
|
|
|
|
if (Vec4::from(melty.0) - Vec4::from(c_color.0)).max_element() <= 0. {
|
|
|
|
commands.entity(*e2).despawn_recursive();
|
|
|
|
}
|
|
|
|
} else if let (Ok((c_color, _c_transform, _c_material, _c_player)), Ok(melty)) =
|
|
|
|
(character_query.get_mut(*e2), melty_query.get(*e1))
|
|
|
|
{
|
|
|
|
if (Vec4::from(melty.0) - Vec4::from(c_color.0)).max_element() <= 0. {
|
|
|
|
commands.entity(*e1).despawn_recursive();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if *flags == CollisionEventFlags::SENSOR {
|
2022-08-29 11:12:33 +00:00
|
|
|
if let (Ok((mut c_color, _c_transform, mut c_material, c_player)), Ok(filter)) = (
|
2022-08-27 17:24:31 +00:00
|
|
|
character_query.get_mut(*e1),
|
|
|
|
pass_through_filter_query.get(*e2),
|
|
|
|
) {
|
|
|
|
c_color.0 = filter.apply(c_color.0);
|
2022-08-29 11:12:33 +00:00
|
|
|
if c_color.0.as_hsla_f32()[2] < 0.1 {
|
|
|
|
commands.entity(*e1).despawn_recursive();
|
|
|
|
character_list.0.remove(e1);
|
|
|
|
if c_player.is_some() {
|
|
|
|
change_character_event.send(ChangeCharacterEvent);
|
|
|
|
}
|
|
|
|
}
|
2022-08-27 17:24:31 +00:00
|
|
|
*c_material = materials.add(ColorMaterial::from(c_color.0));
|
|
|
|
} else if let (
|
2022-08-29 11:12:33 +00:00
|
|
|
Ok((mut c_color, _c_transform, mut c_material, c_player)),
|
2022-08-27 17:24:31 +00:00
|
|
|
Ok(filter),
|
|
|
|
) = (
|
|
|
|
character_query.get_mut(*e2),
|
|
|
|
pass_through_filter_query.get(*e1),
|
|
|
|
) {
|
|
|
|
c_color.0 = filter.apply(c_color.0);
|
2022-08-29 11:12:33 +00:00
|
|
|
if c_color.0.as_hsla_f32()[2] < 0.1 {
|
|
|
|
commands.entity(*e2).despawn_recursive();
|
|
|
|
character_list.0.remove(e2);
|
|
|
|
if c_player.is_some() {
|
|
|
|
change_character_event.send(ChangeCharacterEvent);
|
|
|
|
}
|
|
|
|
}
|
2022-08-27 17:24:31 +00:00
|
|
|
*c_material = materials.add(ColorMaterial::from(c_color.0));
|
|
|
|
}
|
|
|
|
}
|
2022-08-27 12:51:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 11:26:25 +00:00
|
|
|
fn change_character_system(
|
|
|
|
mut commands: Commands,
|
2022-08-21 17:17:55 +00:00
|
|
|
keyboard_input: Res<Input<KeyCode>>,
|
2022-08-24 11:45:13 +00:00
|
|
|
characters: Query<(Entity, &CharacterColor, Option<&Player>)>,
|
2022-08-27 08:17:49 +00:00
|
|
|
character_list: Res<CharacterList>,
|
2022-08-28 07:28:08 +00:00
|
|
|
audio_assets: Res<audio_system::AudioAssets>,
|
|
|
|
audio: Res<Audio>,
|
2022-08-29 07:53:50 +00:00
|
|
|
change_character_event: EventReader<ChangeCharacterEvent>,
|
2022-08-21 17:17:55 +00:00
|
|
|
) {
|
2022-08-29 07:53:50 +00:00
|
|
|
if !keyboard_input.just_pressed(KeyCode::Tab) && change_character_event.is_empty() {
|
2022-08-24 16:51:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-08-24 11:26:25 +00:00
|
|
|
|
2022-08-27 08:17:49 +00:00
|
|
|
if let Some((player_entity, _color, _)) = characters
|
|
|
|
.iter()
|
|
|
|
.find(|(_entity, _color, player)| player.is_some())
|
|
|
|
.or_else(|| characters.iter().next())
|
|
|
|
{
|
|
|
|
commands.entity(player_entity).remove::<Player>();
|
|
|
|
if let Some(new_player_entity) = character_list
|
|
|
|
.0
|
|
|
|
.range(player_entity..)
|
|
|
|
.nth(1)
|
|
|
|
.or_else(|| character_list.0.iter().next())
|
|
|
|
{
|
|
|
|
commands.entity(*new_player_entity).insert(Player);
|
|
|
|
if let Ok((_entity, color, _player)) = characters.get(*new_player_entity) {
|
2022-08-28 07:28:08 +00:00
|
|
|
audio_system::play_audio(&audio_assets.reverb_notes, &audio, color.0.into(), 1.0);
|
2022-08-27 08:17:49 +00:00
|
|
|
}
|
2022-08-24 11:26:25 +00:00
|
|
|
}
|
2022-08-21 17:17:55 +00:00
|
|
|
}
|
2022-08-24 11:26:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn player_movement_system(
|
|
|
|
keyboard_input: Res<Input<KeyCode>>,
|
2022-08-28 07:28:08 +00:00
|
|
|
mut characters: Query<(&mut Velocity, &Children, &CharacterColor), With<Player>>,
|
2022-08-27 13:26:31 +00:00
|
|
|
mut platform_count_query: Query<&mut PlatformCount>,
|
2022-08-28 07:28:08 +00:00
|
|
|
audio_assets: Res<audio_system::AudioAssets>,
|
|
|
|
audio: Res<Audio>,
|
2022-08-24 11:26:25 +00:00
|
|
|
) {
|
2022-08-24 16:51:18 +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);
|
2022-08-24 11:26:25 +00:00
|
|
|
|
2022-08-28 07:28:08 +00:00
|
|
|
for (mut velocity, children, color) in characters.iter_mut() {
|
2022-08-24 16:51:18 +00:00
|
|
|
velocity.linvel.x = 200. * (right_pressed as i8 - left_pressed as i8) as f32;
|
2022-08-24 11:26:25 +00:00
|
|
|
|
2022-08-27 17:24:31 +00:00
|
|
|
let mut platform_count: Mut<PlatformCount> =
|
|
|
|
platform_count_query.get_mut(children[0]).unwrap();
|
2022-08-27 13:26:31 +00:00
|
|
|
if keyboard_input.just_pressed(KeyCode::Space) && platform_count.is_landed() {
|
2022-08-28 07:28:08 +00:00
|
|
|
audio_system::play_audio(&audio_assets.notes, &audio, color.0.into(), 0.5);
|
2022-08-25 12:47:20 +00:00
|
|
|
velocity.linvel.y = 700.;
|
2022-08-27 13:26:31 +00:00
|
|
|
platform_count.reset();
|
2022-08-24 16:51:18 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-23 10:02:58 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 06:35:05 +00:00
|
|
|
fn character_particle_effect_system(
|
2022-08-24 21:11:59 +00:00
|
|
|
player_character: Query<(&Transform, &CharacterColor), With<Player>>,
|
2022-08-24 07:58:56 +00:00
|
|
|
mut particle_effect: ResMut<crate::particle_effect::ParticleEffectResource>,
|
2022-08-24 06:35:05 +00:00
|
|
|
) {
|
2022-08-24 21:11:59 +00:00
|
|
|
if let Ok((transform, color)) = player_character.get_single() {
|
2022-08-24 11:26:25 +00:00
|
|
|
particle_effect.translation = transform.translation;
|
|
|
|
particle_effect.color = color.0;
|
2022-08-24 06:35:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-25 10:32:06 +00:00
|
|
|
fn win_setup(
|
|
|
|
mut commands: Commands,
|
|
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
|
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
|
|
|
asset_server: Res<AssetServer>,
|
|
|
|
) {
|
2022-08-23 10:42:56 +00:00
|
|
|
let font = asset_server.get_handle("UacariLegacy-Thin.ttf");
|
2022-08-25 10:32:06 +00:00
|
|
|
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()
|
|
|
|
})
|
2022-08-27 07:08:14 +00:00
|
|
|
.insert(Level)
|
|
|
|
.insert(WinText);
|
2022-08-23 10:42:56 +00:00
|
|
|
commands
|
|
|
|
.spawn_bundle(Text2dBundle {
|
|
|
|
text: Text::from_section(
|
|
|
|
"Press ENTER to level up",
|
|
|
|
TextStyle {
|
|
|
|
font,
|
2022-08-25 13:39:16 +00:00
|
|
|
font_size: 36.0,
|
2022-08-23 10:42:56 +00:00
|
|
|
color: Color::WHITE,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.with_alignment(TextAlignment::CENTER),
|
2022-08-25 10:32:06 +00:00
|
|
|
transform: Transform::from_xyz(0., 0., 4.),
|
2022-08-23 10:42:56 +00:00
|
|
|
..Default::default()
|
|
|
|
})
|
2022-08-27 07:08:14 +00:00
|
|
|
.insert(Level)
|
|
|
|
.insert(WinText);
|
2022-08-21 17:17:55 +00:00
|
|
|
}
|
2022-08-23 22:45:57 +00:00
|
|
|
|
2022-08-28 09:04:02 +00:00
|
|
|
fn camera_system(
|
|
|
|
mut camera_query: Query<(&Camera, &mut OrthographicProjection, &mut Transform)>,
|
2022-08-24 11:26:25 +00:00
|
|
|
characters: Query<&Transform, (Without<Camera>, With<Player>)>,
|
2022-08-24 08:47:09 +00:00
|
|
|
time: Res<Time>,
|
2022-08-28 09:04:02 +00:00
|
|
|
mut zoom_timer: ResMut<levels::ZoomTimer>,
|
2022-08-23 22:45:57 +00:00
|
|
|
) {
|
2022-08-28 17:33:41 +00:00
|
|
|
fn lerp(x: f32, y: f32, t: f32) -> f32 {
|
|
|
|
((y - x) * t) + x
|
2022-08-28 09:04:02 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 08:47:09 +00:00
|
|
|
const MARGIN: f32 = 300.0;
|
|
|
|
const FOLLOW_SPEED: f32 = std::f32::consts::PI;
|
2022-08-28 09:04:02 +00:00
|
|
|
const ZOOM_SPEED: f32 = std::f32::consts::E;
|
2022-08-23 22:45:57 +00:00
|
|
|
|
2022-08-24 11:26:25 +00:00
|
|
|
for character_transform in characters.iter() {
|
2022-08-28 09:04:02 +00:00
|
|
|
let (camera, mut projection, mut camera_transform) = camera_query.single_mut();
|
2022-08-24 11:26:25 +00:00
|
|
|
|
|
|
|
let size: Vec2 = camera.logical_viewport_size().unwrap();
|
|
|
|
let half_height: f32 = size.y * 0.5;
|
2022-08-26 13:38:13 +00:00
|
|
|
let mut target_translation = character_transform.translation;
|
2022-08-26 09:08:49 +00:00
|
|
|
// prevent camera from going too low
|
|
|
|
target_translation.y = target_translation.y.max(half_height - MARGIN);
|
|
|
|
|
2022-08-26 13:38:13 +00:00
|
|
|
camera_transform.translation = camera_transform
|
|
|
|
.translation
|
|
|
|
.lerp(target_translation, time.delta_seconds() * FOLLOW_SPEED);
|
2022-08-24 11:26:25 +00:00
|
|
|
|
2022-08-28 09:04:02 +00:00
|
|
|
if zoom_timer.0.finished() {
|
|
|
|
projection.scale = lerp(projection.scale, 1.0, time.delta_seconds() * ZOOM_SPEED);
|
2022-08-28 09:09:48 +00:00
|
|
|
} else {
|
|
|
|
projection.scale = 3.0;
|
|
|
|
camera_transform.translation = Vec3::ZERO;
|
|
|
|
zoom_timer.0.tick(time.delta());
|
|
|
|
}
|
2022-08-28 09:04:02 +00:00
|
|
|
|
2022-08-24 11:26:25 +00:00
|
|
|
// always make sure that camera is away from the object in order to render them
|
|
|
|
camera_transform.translation.z = 999.0;
|
2022-08-23 22:45:57 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-25 09:31:12 +00:00
|
|
|
|
2022-08-27 07:08:14 +00:00
|
|
|
fn move_win_text_system(
|
|
|
|
camera_query: Query<&Transform, With<Camera>>,
|
|
|
|
mut win_text_query: Query<&mut Transform, (With<WinText>, Without<Camera>)>,
|
|
|
|
) {
|
|
|
|
let camera_pos = camera_query.single();
|
|
|
|
for mut pos in win_text_query.iter_mut() {
|
|
|
|
pos.translation.x = camera_pos.translation.x;
|
|
|
|
pos.translation.y = camera_pos.translation.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-25 09:31:12 +00:00
|
|
|
fn level_keyboard_system(
|
|
|
|
mut commands: Commands,
|
|
|
|
mut current_level: ResMut<CurrentLevel>,
|
|
|
|
mut level_startup_event: EventWriter<LevelStartupEvent>,
|
2022-08-28 09:09:48 +00:00
|
|
|
mut camera_query: Query<&mut Transform, With<Camera>>,
|
2022-08-25 09:31:12 +00:00
|
|
|
keyboard_input: Res<Input<KeyCode>>,
|
2022-08-27 08:17:49 +00:00
|
|
|
mut character_list: ResMut<CharacterList>,
|
2022-08-25 09:31:12 +00:00
|
|
|
level_query: Query<Entity, With<Level>>,
|
|
|
|
mut app_state: ResMut<State<AppState>>,
|
2022-08-28 09:04:02 +00:00
|
|
|
mut zoom_timer: ResMut<levels::ZoomTimer>,
|
2022-08-25 09:31:12 +00:00
|
|
|
) {
|
|
|
|
if app_state.current() == &AppState::Win && keyboard_input.just_pressed(KeyCode::Return) {
|
|
|
|
current_level.0 = Some(LevelId(
|
|
|
|
current_level.0.map_or(0, |level_id| level_id.0 + 1),
|
|
|
|
));
|
|
|
|
app_state.replace(AppState::Game).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
if keyboard_input.just_pressed(KeyCode::R) {
|
2022-08-27 08:17:49 +00:00
|
|
|
character_list.0.clear();
|
2022-08-25 09:31:12 +00:00
|
|
|
for entity in level_query.iter() {
|
|
|
|
commands.entity(entity).despawn_recursive();
|
|
|
|
}
|
|
|
|
if app_state.replace(AppState::Game).is_err() {
|
2022-08-28 17:33:41 +00:00
|
|
|
crate::levels::setup_level(
|
|
|
|
&mut level_startup_event,
|
|
|
|
&mut camera_query,
|
|
|
|
&mut zoom_timer,
|
|
|
|
);
|
2022-08-25 09:31:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-29 07:53:50 +00:00
|
|
|
|
|
|
|
fn kill_character_system(
|
|
|
|
mut commands: Commands,
|
|
|
|
character_query: Query<(Entity, &Transform, Option<&Player>), With<CharacterColor>>,
|
|
|
|
mut character_list: ResMut<CharacterList>,
|
|
|
|
mut change_character_event: EventWriter<ChangeCharacterEvent>,
|
|
|
|
) {
|
|
|
|
for (entity, transform, player) in character_query.iter() {
|
|
|
|
if transform.translation.y < -512. {
|
|
|
|
commands.entity(entity).despawn_recursive();
|
|
|
|
character_list.0.remove(&entity);
|
|
|
|
if player.is_some() {
|
|
|
|
change_character_event.send(ChangeCharacterEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|