pub mod player; pub mod layout; pub mod input; pub mod inventory; pub mod ui; #[cfg(test)] mod tests; use bevy::prelude::*; use leafwing_input_manager::prelude::*; use serde::{Deserialize, Serialize}; pub struct ExpeditionPlugin; #[derive(Actionlike, PartialEq, Eq, Hash, Debug, Clone, Reflect, Serialize, Deserialize)] pub enum InputAction { #[actionlike(Axis)] Move, ToggleInventory, Interact, } #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default, States)] pub enum GameState { #[default] Running, Inventory, } impl InputAction { pub fn default_input_map() -> InputMap { let input_map = InputMap::default() .with_axis(Self::Move, VirtualAxis::ad()) .with_axis(Self::Move, GamepadAxis::LeftStickX) .with(Self::ToggleInventory, KeyCode::KeyI) .with(Self::ToggleInventory, GamepadButton::Select) .with(Self::Interact, KeyCode::KeyE) .with(Self::Interact, GamepadButton::East); input_map } } 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, Camera { clear_color: ClearColorConfig::Custom(Color::hsl(0.02, 0.67, 0.65)), ..default() }, Projection::Orthographic(OrthographicProjection { scaling_mode: bevy::camera::ScalingMode::FixedVertical { viewport_height: 384., }, scale: 1., ..OrthographicProjection::default_2d() }), Name::new("Camera2d"), ) } fn setup_global(mut commands: Commands) { commands.spawn(camera_bundle()); commands.spawn(ui::UiRoot::new()); } impl Plugin for ExpeditionPlugin { fn build(&self, app: &mut App) { app.add_plugins(input::InputAssetPlugin::::default()) .init_state::() .insert_resource(ui::WindowSize::default()) .add_systems(Startup, (player::setup_player, setup_global)) .add_systems(Update, ( player::handle_input, ui::update_window_size, insert_entity_name, )) .add_systems(OnEnter(GameState::Inventory), inventory::ui::setup_ui_inventory) .add_systems(OnExit(GameState::Inventory), inventory::ui::clear_ui_inventory) .register_type::() .register_type::() .register_type::() .register_type::() .register_type::() .register_type::(); } }