diff --git a/Cargo.lock b/Cargo.lock index 8b4d53a..06b99c8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2422,7 +2422,7 @@ dependencies = [ [[package]] name = "expedition_demo" -version = "0.2.0" +version = "0.1.0" dependencies = [ "bevy", "bevy_common_assets", diff --git a/src/input/mod.rs b/src/input/mod.rs index 1960295..5145335 100644 --- a/src/input/mod.rs +++ b/src/input/mod.rs @@ -1,14 +1,13 @@ use bevy::prelude::*; use leafwing_input_manager::prelude::*; use serde::{ - Deserialize, Serialize, + Deserialize }; pub mod plugin; #[derive(Actionlike, PartialEq, Eq, Hash, Debug, Clone, Reflect, Serialize, Deserialize)] -#[reflect(PartialEq, Hash, Debug, Clone, Serialize, Deserialize)] pub enum InputAction { #[actionlike(Axis)] Move, @@ -29,7 +28,6 @@ impl InputAction { } #[derive(Actionlike, PartialEq, Eq, Hash, Debug, Clone, Reflect, Serialize, Deserialize)] -#[reflect(PartialEq, Hash, Debug, Clone, Serialize, Deserialize)] pub enum UiAction { Rotate, } diff --git a/src/input/plugin.rs b/src/input/plugin.rs index d0f8b14..30052c1 100644 --- a/src/input/plugin.rs +++ b/src/input/plugin.rs @@ -1,33 +1,13 @@ -use std::{ - any::{ - Any, - TypeId, - }, - collections::HashMap, - hash::Hash, - marker::PhantomData, -}; +use std::{any::{Any, TypeId}, collections::HashMap, hash::Hash, marker::PhantomData}; -use bevy::{ - prelude::*, - reflect::Reflectable, -}; +use bevy::{prelude::*, reflect::GetTypeRegistration}; use bevy_common_assets::toml::TomlAssetPlugin; -use leafwing_input_manager::{ - Actionlike, - plugin::InputManagerPlugin, - prelude::*, -}; -use serde::{ - Deserialize, - Serialize, - de::DeserializeOwned, -}; +use leafwing_input_manager::{Actionlike, plugin::InputManagerPlugin, prelude::*}; +use serde::{Deserialize, Serialize, de::DeserializeOwned}; const INPUT_ASSET_EXTENSIONS: [&'static str; 1] = ["input.toml"]; -#[derive(Clone, Debug, Serialize, Deserialize, Default, PartialEq, Eq, Reflect)] -#[reflect(Clone, Debug, Serialize, Deserialize, Default, PartialEq)] +#[derive(Clone, Debug, Serialize, Deserialize, Default, PartialEq, Eq)] pub struct MultiInput { pub keyboard: Option>, pub mouse: Option>, @@ -40,8 +20,7 @@ impl From for InputKind { } } -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Reflect)] -#[reflect(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] #[serde(untagged)] pub enum InputKind { Button(MultiInput), @@ -50,7 +29,6 @@ pub enum InputKind { } #[derive(Default, Deref, DerefMut, Debug, Asset, Reflect, Clone, Serialize, Deserialize, PartialEq, Eq)] -#[reflect(Debug, Clone, PartialEq)] pub struct InputAsset where Name: Sized + Hash + Eq + Reflect + TypePath + Actionlike { #[serde(flatten)] @@ -227,18 +205,17 @@ impl From> for InputAsset } } -#[derive(Resource, Debug, Deref, DerefMut, Reflect, Clone, PartialEq, Eq)] -#[reflect(Resource, Debug, Clone, PartialEq)] -pub struct InputAssetHandle (Option>>); +#[derive(Resource, Deref)] +pub struct InputAssetHandle (Option>>); #[derive(Debug)] -pub struct InputAssetPlugin { +pub struct InputAssetPlugin { _phantom: PhantomData, extensions: &'static [&'static str], } impl InputAssetPlugin - where T: Sized + Hash + Eq + Reflectable + Actionlike + DeserializeOwned { + where T: Sized + Hash + Eq + Reflect + TypePath + Actionlike + DeserializeOwned + GetTypeRegistration { pub fn new(extensions: &'static [&'static str]) -> Self { Self { _phantom: PhantomData, @@ -248,7 +225,7 @@ impl InputAssetPlugin } impl Default for InputAssetPlugin - where T: Sized + Hash + Eq + Reflectable + Actionlike + DeserializeOwned { + where T: Sized + Hash + Eq + Reflect + TypePath + Actionlike + DeserializeOwned + GetTypeRegistration { fn default() -> Self { Self { _phantom: PhantomData, @@ -258,11 +235,12 @@ impl Default for InputAssetPlugin } impl Plugin for InputAssetPlugin - where T: Sized + Hash + Eq + Reflectable + Actionlike + DeserializeOwned { + where T: Sized + Hash + Eq + Reflect + TypePath + Actionlike + DeserializeOwned + GetTypeRegistration +{ fn build(&self, app: &mut App) { app.add_plugins(( - InputManagerPlugin::::default(), TomlAssetPlugin::>::new(&self.extensions), + InputManagerPlugin::::default() )); } } diff --git a/src/inventory/item.rs b/src/inventory/item.rs index d61a1d4..e3523b1 100644 --- a/src/inventory/item.rs +++ b/src/inventory/item.rs @@ -2,8 +2,7 @@ use std::mem::swap; use bevy::prelude::*; -#[derive(Component, Clone, Debug, Reflect, PartialEq, Eq)] -#[reflect(Component, Clone, Debug, PartialEq)] +#[derive(Component, Clone, Debug, Reflect)] pub struct Item { pub size: UVec2, pub position: Option, diff --git a/src/inventory/mod.rs b/src/inventory/mod.rs index 88d15a7..0cf99f7 100644 --- a/src/inventory/mod.rs +++ b/src/inventory/mod.rs @@ -2,17 +2,15 @@ use bevy::prelude::*; pub mod item; -/// Marker that this inventory will show up when UI is built -#[derive(Component, Reflect, Default, Clone, Copy, Debug, PartialEq, Eq)] -#[reflect(Component, Clone, Default, Debug, PartialEq)] -pub struct ActiveInventory; - -#[derive(Component, Reflect, Default, Clone, Copy, Debug, PartialEq, Eq)] -#[reflect(Component, Clone, Default, Debug, PartialEq)] +#[derive(Component, Reflect, Default)] pub struct Inventory { pub size: UVec2, } +/// Marker that this inventory will show up when UI is built +#[derive(Component, Reflect)] +pub struct ActiveInventory; + impl Inventory { pub fn new(size: UVec2) -> Self { Self { size } @@ -60,23 +58,6 @@ impl Inventory { self.can_fit(item_query, children.as_slice(), *size, queried_position) } - pub fn can_replace( - &self, - item_query: Query<&item::Item>, - contained_items: &[Entity], - replacable_item: Entity, - queried_item: &item::Item, - ) -> bool { - let Some(position) = &queried_item.position else { - warn!("Trying to query rotated item without position"); - return false; - }; - let children = contained_items.iter() - .filter_map(|e| if e.ne(&&replacable_item) { Some(*e) } else { None }) - .collect::>(); - self.can_fit(item_query, children.as_slice(), queried_item.size, *position) - } - pub fn can_rotate( &self, item_query: Query<&item::Item>, @@ -98,6 +79,23 @@ impl Inventory { self.can_fit(item_query, children.as_slice(), rotated_item.size, *position) } + pub fn can_replace( + &self, + item_query: Query<&item::Item>, + contained_items: &[Entity], + replacable_item: Entity, + queried_item: &item::Item, + ) -> bool { + let Some(position) = &queried_item.position else { + warn!("Trying to query rotated item without position"); + return false; + }; + let children = contained_items.iter() + .filter_map(|e| if e.ne(&&replacable_item) { Some(*e) } else { None }) + .collect::>(); + self.can_fit(item_query, children.as_slice(), queried_item.size, *position) + } + fn find_free_space_inner( &self, item_query: Query<&item::Item>, diff --git a/src/layout/container.rs b/src/layout/container.rs index 2c0019e..5236a26 100644 --- a/src/layout/container.rs +++ b/src/layout/container.rs @@ -11,12 +11,11 @@ use crate::{ }, }; -use super::*; - const CRATE_CLOSED_ASSET: &'static str = "sprites/interactive/crate_closed.png"; -#[derive(Component, Clone, Copy, Default, Reflect, Debug, PartialEq, Eq)] -#[reflect(Component, Clone, Default, Debug, PartialEq)] +use super::*; + +#[derive(Component)] #[require(Sprite, InteractiveObject, Inventory)] pub struct Container; diff --git a/src/layout/door.rs b/src/layout/door.rs index ddd849f..fac9ea3 100644 --- a/src/layout/door.rs +++ b/src/layout/door.rs @@ -8,17 +8,10 @@ use super::*; const DOOR_OPENED_ASSET: &'static str = "sprites/interactive/door_opened.png"; const DOOR_CLOSED_ASSET: &'static str = "sprites/interactive/door_closed.png"; -#[derive(Component, Clone, Copy, Reflect, PartialEq, Eq, Debug)] -#[reflect(Component, Clone, Default, PartialEq, Debug)] +#[derive(Component)] #[require(Sprite, InteractiveObject)] pub struct Door(pub i8); -impl Default for Door { - fn default() -> Self { - Self(1) - } -} - fn on_door_interact( event: On, mut commands: Commands, diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 6a55002..3a7a3f9 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -4,20 +4,16 @@ pub mod container; pub mod door; pub mod systems; -#[derive(Component, Debug, PartialEq, Eq, Default, Clone, Copy, Reflect)] -#[reflect(Component, Debug, PartialEq, Default, Clone)] +#[derive(Component)] pub struct MayInteract; -#[derive(Component, Debug, PartialEq, Eq, Default, Clone, Copy, Reflect)] -#[reflect(Component, Debug, PartialEq, Default, Clone)] +#[derive(Component, Default)] pub struct InteractiveObject; -#[derive(Component, Debug, PartialEq, Eq, Default, Clone, Copy, Reflect)] -#[reflect(Component, Debug, PartialEq, Default, Clone)] +#[derive(Component)] pub struct Locked; -#[derive(EntityEvent, Reflect, Clone, Copy, PartialEq, Eq, Debug)] -#[reflect(Event, Debug, PartialEq, Clone)] +#[derive(EntityEvent)] pub struct InteractionEvent { pub entity: Entity, } diff --git a/src/lib.rs b/src/lib.rs index 9382ba8..6332bca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,32 +1,37 @@ use bevy::{ prelude::*, - ui_widgets::ScrollbarPlugin, + ui_widgets::ScrollbarPlugin }; use bevy_rapier2d::{ prelude::*, - rapier::prelude::IntegrationParameters, + rapier::prelude::IntegrationParameters }; +pub mod player; +pub mod layout; pub mod input; pub mod inventory; -pub mod layout; -pub mod player; +pub mod ui; #[cfg(test)] mod tests; -pub mod ui; pub const PIXELS_PER_METER: f32 = 16.0; pub struct ExpeditionPlugin; -#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default, States, Reflect)] -#[reflect(Clone, PartialEq, Debug, Default, Hash, State)] +#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default, States)] pub enum GameState { #[default] Running, Inventory, } +pub fn insert_entity_name(names: Query<(Entity, &mut Name), Added>) { + for (entity, mut name) in names { + name.mutate(|name| name.insert_str(0, format!("{entity}: ").as_str())); + } +} + fn camera_bundle() -> impl Bundle { ( Camera2d, @@ -45,12 +50,6 @@ fn camera_bundle() -> impl Bundle { ) } -fn insert_entity_name(names: Query<(Entity, &mut Name), Added>) { - for (entity, mut name) in names { - name.mutate(|name| name.insert_str(0, format!("{entity}: ").as_str())); - } -} - fn setup_global(mut commands: Commands) { commands.spawn(camera_bundle()); commands.spawn(ui::UiRoot::new()); @@ -58,39 +57,34 @@ fn setup_global(mut commands: Commands) { impl Plugin for ExpeditionPlugin { fn build(&self, app: &mut App) { - let rapier_init = RapierContextInitialization::InitializeDefaultRapierContext { - integration_parameters: IntegrationParameters { - length_unit: PIXELS_PER_METER, - ..default() - }, - rapier_configuration: RapierConfiguration { - gravity: Vec2::ZERO, - physics_pipeline_active: true, - scaled_shape_subdivision: 10, - force_update_from_transform_changes: false, - }, - }; app.add_plugins(( - RapierDebugRenderPlugin::default(), - RapierPhysicsPlugin::<()>::default() - .with_custom_initialization(rapier_init), - ScrollbarPlugin, input::plugin::InputAssetPlugin::::default(), input::plugin::InputAssetPlugin::::default(), + ScrollbarPlugin, + RapierPhysicsPlugin::<()>::default() + .with_custom_initialization(RapierContextInitialization::InitializeDefaultRapierContext { + integration_parameters: IntegrationParameters { + length_unit: PIXELS_PER_METER, + ..default() + }, + rapier_configuration: RapierConfiguration { + gravity: Vec2::ZERO, + physics_pipeline_active: true, + scaled_shape_subdivision: 10, + force_update_from_transform_changes: false, + }, + }), + RapierDebugRenderPlugin::default(), )) .init_state::() .insert_resource(ui::WindowSize::default()) - .add_systems(Startup, ( - setup_global, - layout::systems::setup_world, - player::systems::setup_player, - )) + .add_systems(Startup, (player::systems::setup_player, setup_global, layout::systems::setup_world)) .add_systems(Update, ( - insert_entity_name, - layout::systems::detect_interact_collisions, player::systems::handle_input, ui::update_window_size, ui::handle_input, + insert_entity_name, + layout::systems::detect_interact_collisions, )) .add_systems(OnEnter(GameState::Inventory), ui::inventory::systems::setup_ui_inventory) .add_systems(OnExit(GameState::Inventory), ui::inventory::systems::clear_ui_inventory) diff --git a/src/player/mod.rs b/src/player/mod.rs index 841d711..3409370 100644 --- a/src/player/mod.rs +++ b/src/player/mod.rs @@ -9,23 +9,18 @@ use crate::{ pub mod systems; -#[derive(Component, Clone, Copy, Reflect, PartialEq, Debug)] -#[reflect(Component, Clone, Default, PartialEq, Debug)] +#[derive(Component, Reflect)] pub struct Player { // px/s speed: f32, } -impl Default for Player { - fn default() -> Self { - Self { speed: PIXELS_PER_METER * 0.8 } - } -} - pub fn player_bundle(asset_server: &Res) -> impl Bundle { let image = asset_server.load("sprites/player/player.png"); ( - Player::default(), + Player { + speed: PIXELS_PER_METER * 0.8, + }, Sprite::from_image(image), Transform::from_xyz(0f32, 0f32, 1f32), Action::default_input_map(), diff --git a/src/ui/inventory/mod.rs b/src/ui/inventory/mod.rs index 3e81139..3d81fc3 100644 --- a/src/ui/inventory/mod.rs +++ b/src/ui/inventory/mod.rs @@ -14,36 +14,29 @@ pub mod bundles; pub mod observers; pub mod systems; -#[derive(Component, Debug, PartialEq, Eq, Default, Clone, Copy, Reflect)] -#[reflect(Component, Debug, PartialEq, Default, Clone)] +#[derive(Component, Reflect)] #[require(Node)] pub struct UiInventoryManager; -#[derive(Component, Debug, PartialEq, Eq, Clone, Copy, Reflect)] -#[reflect(Component, Debug, PartialEq, Clone)] +#[derive(Component, Reflect)] #[require(Node)] pub struct UiInventory(pub Entity); -#[derive(Component, Debug, PartialEq, Eq, Default, Clone, Copy, Reflect)] -#[reflect(Component, Debug, PartialEq, Default, Clone)] +#[derive(Component, Reflect)] #[require(Node, ImageNode)] pub struct UiInventorySlot(pub UVec2); -#[derive(Component, Debug, PartialEq, Eq, Clone, Copy, Reflect)] -#[reflect(Component, Debug, PartialEq, Clone)] +#[derive(Component, Reflect)] #[require(Node, ImageNode)] pub struct UiItem(pub Entity); -#[derive(Component, Debug, PartialEq, Eq, Default, Clone, Copy, Reflect)] -#[reflect(Component, Debug, PartialEq, Default, Clone)] +#[derive(Component, Reflect)] pub struct HoveredItem; -#[derive(Component, Debug, PartialEq, Eq, Default, Clone, Copy, Reflect)] -#[reflect(Component, Debug, PartialEq, Default, Clone)] +#[derive(Component, Reflect)] pub struct HoveredSlot; -#[derive(Component, Debug, PartialEq, Eq, Clone, Reflect)] -#[reflect(Component, Debug, PartialEq, Clone)] +#[derive(Component, Reflect)] pub struct DraggedItem(pub Item, pub UVec2); fn ui_item_node_data(item: &Item) -> (Val, Val, Val, Val, UiTransform) { diff --git a/src/ui/mod.rs b/src/ui/mod.rs index e67f792..b3d338b 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -8,9 +8,8 @@ use crate::input::UiAction; pub mod inventory; -#[derive(Component, Reflect, Debug, Default, PartialEq, Eq, Clone, Copy)] +#[derive(Component, Reflect)] #[require(Node)] -#[reflect(Component, Debug, Default, PartialEq, Clone)] pub struct UiRoot; #[derive(Resource, Deref, DerefMut, Default)]