generated from 2ndbeam/bevy-template
109 lines
3.3 KiB
Rust
109 lines
3.3 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(Actionlike, PartialEq, Eq, Hash, Debug, Clone, Reflect, Serialize, Deserialize)]
|
|
pub enum UiAction {
|
|
Rotate,
|
|
}
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default, States)]
|
|
pub enum GameState {
|
|
#[default]
|
|
Running,
|
|
Inventory,
|
|
}
|
|
|
|
impl InputAction {
|
|
pub fn default_input_map() -> InputMap<Self> {
|
|
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)
|
|
}
|
|
}
|
|
|
|
impl UiAction {
|
|
pub fn default_input_map() -> InputMap<Self> {
|
|
InputMap::default()
|
|
.with(Self::Rotate, KeyCode::KeyR)
|
|
.with(Self::Rotate, GamepadButton::West)
|
|
}
|
|
}
|
|
|
|
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(),
|
|
input::InputAssetPlugin::<UiAction>::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,
|
|
ui::handle_input,
|
|
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>()
|
|
.add_observer(inventory::ui::on_ui_rotate);
|
|
}
|
|
}
|