Skip to content

Commit

Permalink
Add player stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
janhohenheim committed Sep 5, 2024
1 parent 8c54fe4 commit e33994f
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 26 deletions.
69 changes: 69 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ edition = "2021"
bevy = { version = "0.14", features = ["wayland"] }
rand = "0.8"
blenvy = "0.1.0-alpha.1"
avian3d = { git = "https://github.com/Jondolf/avian", version = "0.1.2" }
avian3d = { git = "https://github.com/Jondolf/avian" }
bevy-inspector-egui = { version = "0.25.2", optional = true }

leafwing-input-manager = "0.15.0"

# Compile low-severity logs out of native builds for performance.
log = { version = "0.4", features = [
Expand Down
21 changes: 16 additions & 5 deletions src/demo/level.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! Spawn the main level.
use bevy::{ecs::world::Command, prelude::*};
use bevy::{color::palettes::tailwind, ecs::world::Command, prelude::*};
use blenvy::*;

use crate::demo::player::SpawnPlayer;
use crate::{demo::player::SpawnPlayer, screens::Screen};

pub(super) fn plugin(_app: &mut App) {
// No setup required for this plugin.
Expand All @@ -14,7 +15,17 @@ pub(super) fn plugin(_app: &mut App) {
/// Functions that accept only `&mut World` as their parameter implement [`Command`].
/// We use this style when a command requires no configuration.
pub fn spawn_level(world: &mut World) {
// The only thing we have in our level is a player,
// but add things like walls etc. here.
SpawnPlayer { max_speed: 400.0 }.apply(world);
world.spawn((
Name::new("Level"),
BlueprintInfo::from_path("levels/World.glb"),
SpawnBlueprint,
HideUntilReady,
GameWorldTag,
StateScoped(Screen::Gameplay),
));
world.insert_resource(AmbientLight {
color: tailwind::SKY_100.into(),
brightness: 400.0,
});
SpawnPlayer::default().apply(world);
}
29 changes: 29 additions & 0 deletions src/demo/player/camera.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use bevy::prelude::*;

pub(super) fn plugin(app: &mut App) {
app.register_type::<PlayerCamera>();
}

#[derive(Debug, Clone, Copy, PartialEq, Component, Reflect, Default)]
#[reflect(Component)]
pub struct PlayerCamera {
pub follow: Transform,
pub offset: Transform,
pub look_at: Option<Vec3>,
}

impl PlayerCamera {
pub fn transform(self) -> Transform {
self.follow * self.offset
}
}

pub fn spawn_player_camera(world: &mut World) {
world.spawn((
Name::new("Camera"),
Camera3dBundle::default(),
PlayerCamera::default(),
IsDefaultUiCamera,
//create_camera_action_input_manager_bundle(),
));
}
11 changes: 5 additions & 6 deletions src/demo/player.rs → src/demo/player/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use crate::{
AppSet,
};

pub mod camera;

pub(super) fn plugin(app: &mut App) {
app.register_type::<Player>();
app.load_resource::<PlayerAssets>();
Expand All @@ -34,11 +36,8 @@ pub(super) fn plugin(app: &mut App) {
pub struct Player;

/// A command to spawn the player character.
#[derive(Debug)]
pub struct SpawnPlayer {
/// See [`MovementController::max_speed`].
pub max_speed: f32,
}
#[derive(Debug, Default)]
pub struct SpawnPlayer;

impl Command for SpawnPlayer {
fn apply(self, world: &mut World) {
Expand Down Expand Up @@ -74,7 +73,7 @@ fn spawn_player(
index: player_animation.get_atlas_index(),
},
MovementController {
max_speed: config.max_speed,
max_speed: 0.0,
..default()
},
ScreenWrap,
Expand Down
14 changes: 13 additions & 1 deletion src/dev_tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ pub(super) fn plugin(app: &mut App) {

app.add_plugins((
DebugUiPlugin,
WorldInspectorPlugin::new(),
WorldInspectorPlugin::default().run_if(is_inspector_active),
PhysicsDebugPlugin::default(),
));
app.add_systems(
Update,
toggle_debug_mode.run_if(input_just_pressed(TOGGLE_KEY)),
);

app.init_resource::<InspectorActive>();

// Disable physics gizmos by default.
app.insert_gizmo_config(
PhysicsGizmos {
Expand All @@ -40,11 +43,20 @@ pub(super) fn plugin(app: &mut App) {

const TOGGLE_KEY: KeyCode = KeyCode::Backquote;

#[derive(Resource, Default)]
struct InspectorActive(bool);

fn toggle_debug_mode(
mut options: ResMut<UiDebugOptions>,
mut config_store: ResMut<GizmoConfigStore>,
mut is_inspector_active: ResMut<InspectorActive>,
) {
options.toggle();
let physics_gizmos = config_store.config_mut::<PhysicsGizmos>().0;
physics_gizmos.enabled = options.enabled;
is_inspector_active.0 = options.enabled;
}

fn is_inspector_active(is_active: Res<InspectorActive>) -> bool {
is_active.as_ref().0
}
14 changes: 3 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod dev_tools;
mod screens;
mod theme;
mod third_party_setup;
mod ui_camera;

use bevy::{
asset::AssetMetaCheck,
Expand Down Expand Up @@ -62,6 +63,7 @@ impl Plugin for AppPlugin {
screens::plugin,
theme::plugin,
third_party_setup::plugin,
ui_camera::plugin,
));

// Enable dev tools for dev builds.
Expand All @@ -84,15 +86,5 @@ enum AppSet {
}

fn spawn_camera(mut commands: Commands) {
commands.spawn((
Name::new("Camera"),
Camera2dBundle::default(),
// Render all UI to this camera.
// Not strictly necessary since we only use one camera,
// but if we don't use this component, our UI will disappear as soon
// as we add another camera. This includes indirect ways of adding cameras like using
// [ui node outlines](https://bevyengine.org/news/bevy-0-14/#ui-node-outline-gizmos)
// for debugging. So it's good to have this here for future-proofing.
IsDefaultUiCamera,
));
commands.add(ui_camera::spawn_ui_camera);
}
55 changes: 55 additions & 0 deletions src/third_party_setup/lwim.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use bevy::prelude::*;
use leafwing_input_manager::prelude::*;

pub(super) fn plugin(app: &mut App) {
app.add_plugins((
InputManagerPlugin::<PlayerAction>::default(),
InputManagerPlugin::<CameraAction>::default(),
));
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Actionlike, Reflect, Default)]
pub(crate) enum PlayerAction {
#[default]
#[actionlike(DualAxis)]
Move,
Sprint,
Jump,
Interact,
}

#[derive(Actionlike, PartialEq, Eq, Clone, Copy, Hash, Debug, Reflect)]
#[actionlike(DualAxis)]
pub enum CameraAction {
RotateCamera,
}

impl PlayerAction {
pub fn default_input_map() -> InputMap<Self> {
let mut input_map = InputMap::default();

// Default gamepad input bindings
input_map.insert(PlayerAction::PedalLeft, GamepadButtonType::LeftTrigger);
input_map.insert(PlayerAction::PedalRight, GamepadButtonType::RightTrigger);

// Default keyboard input bindings
input_map.insert(PlayerAction::PedalLeft, KeyCode::KeyA);
input_map.insert(PlayerAction::PedalRight, KeyCode::KeyD);

input_map
}
}

impl CameraAction {
pub fn default_input_map() -> InputMap<Self> {
let mut input_map = InputMap::default();

// Default gamepad input bindings
input_map.insert(CameraAction::RotateCamera, DualAxis::left_stick());

// Default keyboard input bindings
input_map.insert(CameraAction::RotateCamera, DualAxis::mouse_motion());

input_map
}
}
3 changes: 2 additions & 1 deletion src/third_party_setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use bevy::prelude::*;

mod avian;
mod blenvy;
mod lwim;

pub(super) fn plugin(app: &mut App) {
app.add_plugins((blenvy::plugin, avian::plugin));
app.add_plugins((blenvy::plugin, avian::plugin, lwim::plugin));
}
Loading

0 comments on commit e33994f

Please sign in to comment.