Skip to content

Commit e33994f

Browse files
committed
Add player stuff
1 parent 8c54fe4 commit e33994f

File tree

10 files changed

+213
-26
lines changed

10 files changed

+213
-26
lines changed

Cargo.lock

+69
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ edition = "2021"
88
bevy = { version = "0.14", features = ["wayland"] }
99
rand = "0.8"
1010
blenvy = "0.1.0-alpha.1"
11-
avian3d = { git = "https://github.com/Jondolf/avian", version = "0.1.2" }
11+
avian3d = { git = "https://github.com/Jondolf/avian" }
1212
bevy-inspector-egui = { version = "0.25.2", optional = true }
13-
13+
leafwing-input-manager = "0.15.0"
1414

1515
# Compile low-severity logs out of native builds for performance.
1616
log = { version = "0.4", features = [

src/demo/level.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
//! Spawn the main level.
22
3-
use bevy::{ecs::world::Command, prelude::*};
3+
use bevy::{color::palettes::tailwind, ecs::world::Command, prelude::*};
4+
use blenvy::*;
45

5-
use crate::demo::player::SpawnPlayer;
6+
use crate::{demo::player::SpawnPlayer, screens::Screen};
67

78
pub(super) fn plugin(_app: &mut App) {
89
// No setup required for this plugin.
@@ -14,7 +15,17 @@ pub(super) fn plugin(_app: &mut App) {
1415
/// Functions that accept only `&mut World` as their parameter implement [`Command`].
1516
/// We use this style when a command requires no configuration.
1617
pub fn spawn_level(world: &mut World) {
17-
// The only thing we have in our level is a player,
18-
// but add things like walls etc. here.
19-
SpawnPlayer { max_speed: 400.0 }.apply(world);
18+
world.spawn((
19+
Name::new("Level"),
20+
BlueprintInfo::from_path("levels/World.glb"),
21+
SpawnBlueprint,
22+
HideUntilReady,
23+
GameWorldTag,
24+
StateScoped(Screen::Gameplay),
25+
));
26+
world.insert_resource(AmbientLight {
27+
color: tailwind::SKY_100.into(),
28+
brightness: 400.0,
29+
});
30+
SpawnPlayer::default().apply(world);
2031
}

src/demo/player/camera.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use bevy::prelude::*;
2+
3+
pub(super) fn plugin(app: &mut App) {
4+
app.register_type::<PlayerCamera>();
5+
}
6+
7+
#[derive(Debug, Clone, Copy, PartialEq, Component, Reflect, Default)]
8+
#[reflect(Component)]
9+
pub struct PlayerCamera {
10+
pub follow: Transform,
11+
pub offset: Transform,
12+
pub look_at: Option<Vec3>,
13+
}
14+
15+
impl PlayerCamera {
16+
pub fn transform(self) -> Transform {
17+
self.follow * self.offset
18+
}
19+
}
20+
21+
pub fn spawn_player_camera(world: &mut World) {
22+
world.spawn((
23+
Name::new("Camera"),
24+
Camera3dBundle::default(),
25+
PlayerCamera::default(),
26+
IsDefaultUiCamera,
27+
//create_camera_action_input_manager_bundle(),
28+
));
29+
}

src/demo/player.rs src/demo/player/mod.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use crate::{
1818
AppSet,
1919
};
2020

21+
pub mod camera;
22+
2123
pub(super) fn plugin(app: &mut App) {
2224
app.register_type::<Player>();
2325
app.load_resource::<PlayerAssets>();
@@ -34,11 +36,8 @@ pub(super) fn plugin(app: &mut App) {
3436
pub struct Player;
3537

3638
/// A command to spawn the player character.
37-
#[derive(Debug)]
38-
pub struct SpawnPlayer {
39-
/// See [`MovementController::max_speed`].
40-
pub max_speed: f32,
41-
}
39+
#[derive(Debug, Default)]
40+
pub struct SpawnPlayer;
4241

4342
impl Command for SpawnPlayer {
4443
fn apply(self, world: &mut World) {
@@ -74,7 +73,7 @@ fn spawn_player(
7473
index: player_animation.get_atlas_index(),
7574
},
7675
MovementController {
77-
max_speed: config.max_speed,
76+
max_speed: 0.0,
7877
..default()
7978
},
8079
ScreenWrap,

src/dev_tools.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@ pub(super) fn plugin(app: &mut App) {
1818

1919
app.add_plugins((
2020
DebugUiPlugin,
21-
WorldInspectorPlugin::new(),
21+
WorldInspectorPlugin::default().run_if(is_inspector_active),
2222
PhysicsDebugPlugin::default(),
2323
));
2424
app.add_systems(
2525
Update,
2626
toggle_debug_mode.run_if(input_just_pressed(TOGGLE_KEY)),
2727
);
28+
29+
app.init_resource::<InspectorActive>();
30+
2831
// Disable physics gizmos by default.
2932
app.insert_gizmo_config(
3033
PhysicsGizmos {
@@ -40,11 +43,20 @@ pub(super) fn plugin(app: &mut App) {
4043

4144
const TOGGLE_KEY: KeyCode = KeyCode::Backquote;
4245

46+
#[derive(Resource, Default)]
47+
struct InspectorActive(bool);
48+
4349
fn toggle_debug_mode(
4450
mut options: ResMut<UiDebugOptions>,
4551
mut config_store: ResMut<GizmoConfigStore>,
52+
mut is_inspector_active: ResMut<InspectorActive>,
4653
) {
4754
options.toggle();
4855
let physics_gizmos = config_store.config_mut::<PhysicsGizmos>().0;
4956
physics_gizmos.enabled = options.enabled;
57+
is_inspector_active.0 = options.enabled;
58+
}
59+
60+
fn is_inspector_active(is_active: Res<InspectorActive>) -> bool {
61+
is_active.as_ref().0
5062
}

src/lib.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod dev_tools;
66
mod screens;
77
mod theme;
88
mod third_party_setup;
9+
mod ui_camera;
910

1011
use bevy::{
1112
asset::AssetMetaCheck,
@@ -62,6 +63,7 @@ impl Plugin for AppPlugin {
6263
screens::plugin,
6364
theme::plugin,
6465
third_party_setup::plugin,
66+
ui_camera::plugin,
6567
));
6668

6769
// Enable dev tools for dev builds.
@@ -84,15 +86,5 @@ enum AppSet {
8486
}
8587

8688
fn spawn_camera(mut commands: Commands) {
87-
commands.spawn((
88-
Name::new("Camera"),
89-
Camera2dBundle::default(),
90-
// Render all UI to this camera.
91-
// Not strictly necessary since we only use one camera,
92-
// but if we don't use this component, our UI will disappear as soon
93-
// as we add another camera. This includes indirect ways of adding cameras like using
94-
// [ui node outlines](https://bevyengine.org/news/bevy-0-14/#ui-node-outline-gizmos)
95-
// for debugging. So it's good to have this here for future-proofing.
96-
IsDefaultUiCamera,
97-
));
89+
commands.add(ui_camera::spawn_ui_camera);
9890
}

src/third_party_setup/lwim.rs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use bevy::prelude::*;
2+
use leafwing_input_manager::prelude::*;
3+
4+
pub(super) fn plugin(app: &mut App) {
5+
app.add_plugins((
6+
InputManagerPlugin::<PlayerAction>::default(),
7+
InputManagerPlugin::<CameraAction>::default(),
8+
));
9+
}
10+
11+
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Actionlike, Reflect, Default)]
12+
pub(crate) enum PlayerAction {
13+
#[default]
14+
#[actionlike(DualAxis)]
15+
Move,
16+
Sprint,
17+
Jump,
18+
Interact,
19+
}
20+
21+
#[derive(Actionlike, PartialEq, Eq, Clone, Copy, Hash, Debug, Reflect)]
22+
#[actionlike(DualAxis)]
23+
pub enum CameraAction {
24+
RotateCamera,
25+
}
26+
27+
impl PlayerAction {
28+
pub fn default_input_map() -> InputMap<Self> {
29+
let mut input_map = InputMap::default();
30+
31+
// Default gamepad input bindings
32+
input_map.insert(PlayerAction::PedalLeft, GamepadButtonType::LeftTrigger);
33+
input_map.insert(PlayerAction::PedalRight, GamepadButtonType::RightTrigger);
34+
35+
// Default keyboard input bindings
36+
input_map.insert(PlayerAction::PedalLeft, KeyCode::KeyA);
37+
input_map.insert(PlayerAction::PedalRight, KeyCode::KeyD);
38+
39+
input_map
40+
}
41+
}
42+
43+
impl CameraAction {
44+
pub fn default_input_map() -> InputMap<Self> {
45+
let mut input_map = InputMap::default();
46+
47+
// Default gamepad input bindings
48+
input_map.insert(CameraAction::RotateCamera, DualAxis::left_stick());
49+
50+
// Default keyboard input bindings
51+
input_map.insert(CameraAction::RotateCamera, DualAxis::mouse_motion());
52+
53+
input_map
54+
}
55+
}

src/third_party_setup/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use bevy::prelude::*;
22

33
mod avian;
44
mod blenvy;
5+
mod lwim;
56

67
pub(super) fn plugin(app: &mut App) {
7-
app.add_plugins((blenvy::plugin, avian::plugin));
8+
app.add_plugins((blenvy::plugin, avian::plugin, lwim::plugin));
89
}

0 commit comments

Comments
 (0)