bevy-expedition-demo/src/lib.rs
2ndbeam a462c64786 feat: Implemented basic item drag-and-drop
- Added bundle names
- Added system to update bundle names with entity name
2026-03-11 22:40:17 +03:00

92 lines
2.8 KiB
Rust

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<Self> {
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<Name>>) {
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::<InputAction>::default())
.init_state::<GameState>()
.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::<inventory::Inventory>()
.register_type::<inventory::ActiveInventory>()
.register_type::<inventory::item::Item>()
.register_type::<inventory::ui::UiItem>()
.register_type::<inventory::ui::UiInventorySlot>()
.register_type::<inventory::ui::UiInventory>();
}
}