mirror of
https://codeberg.org/schrottkatze/mgd2-tram-championships.git
synced 2025-07-04 02:57:39 +00:00
Compare commits
2 commits
6d269d0c14
...
2e7de9f296
Author | SHA1 | Date | |
---|---|---|---|
2e7de9f296 | |||
f518a2c670 |
9 changed files with 1100 additions and 109 deletions
6
.gitignore
vendored
6
.gitignore
vendored
|
@ -1,9 +1,3 @@
|
||||||
/target
|
/target
|
||||||
/.direnv
|
/.direnv
|
||||||
|
|
||||||
|
|
||||||
# Added by cargo
|
|
||||||
#
|
|
||||||
# already existing elements were commented out
|
|
||||||
|
|
||||||
#/target
|
|
||||||
|
|
1090
Cargo.lock
generated
1090
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -5,6 +5,8 @@ edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bevy = "0.16.0"
|
bevy = "0.16.0"
|
||||||
|
bevy-inspector-egui = "0.31.0"
|
||||||
|
bevy_third_person_camera = "0.3.0"
|
||||||
env_logger = "0.11.8"
|
env_logger = "0.11.8"
|
||||||
log = "0.4.27"
|
log = "0.4.27"
|
||||||
|
|
||||||
|
|
|
@ -6,8 +6,5 @@ use bevy::prelude::*;
|
||||||
/// Sets up the camera for the entire app, with a starting position for the game. (TODO: probably change that)
|
/// Sets up the camera for the entire app, with a starting position for the game. (TODO: probably change that)
|
||||||
/// This camera is also used for menu rendering etc.
|
/// This camera is also used for menu rendering etc.
|
||||||
pub fn setup(mut c: Commands) {
|
pub fn setup(mut c: Commands) {
|
||||||
c.spawn((
|
c.spawn(Camera3d::default());
|
||||||
Camera3d::default(),
|
|
||||||
Transform::from_xyz(-20., 15., 0.).looking_at(Vec3::ZERO, Vec3::Y),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
18
src/debugging.rs
Normal file
18
src/debugging.rs
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
//! Seperate out debugging uis/plugins in it's own module for cleanliness.
|
||||||
|
use bevy::prelude::*;
|
||||||
|
use bevy_inspector_egui::{
|
||||||
|
bevy_egui::EguiPlugin,
|
||||||
|
quick::{StateInspectorPlugin, WorldInspectorPlugin},
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::AppState;
|
||||||
|
|
||||||
|
pub fn plugin(app: &mut App) {
|
||||||
|
app.add_plugins(EguiPlugin {
|
||||||
|
enable_multipass_for_primary_context: true,
|
||||||
|
})
|
||||||
|
.add_plugins((
|
||||||
|
WorldInspectorPlugin::new(),
|
||||||
|
StateInspectorPlugin::<AppState>::new(),
|
||||||
|
));
|
||||||
|
}
|
|
@ -13,11 +13,11 @@ mod scene;
|
||||||
struct GameplaySet;
|
struct GameplaySet;
|
||||||
|
|
||||||
pub fn plugin(app: &mut App) {
|
pub fn plugin(app: &mut App) {
|
||||||
app.add_plugins(camera::plugin)
|
app.add_systems(OnEnter(AppState::Ingame), scene::setup.in_set(GameplaySet))
|
||||||
.add_systems(OnEnter(AppState::Ingame), scene::setup.in_set(GameplaySet))
|
|
||||||
.add_systems(
|
.add_systems(
|
||||||
OnExit(AppState::Ingame),
|
OnExit(AppState::Ingame),
|
||||||
despawn::<cleanup::Scene>.in_set(GameplaySet),
|
despawn::<cleanup::Scene>.in_set(GameplaySet),
|
||||||
);
|
)
|
||||||
|
.add_plugins(camera::plugin);
|
||||||
app.configure_sets(Update, GameplaySet.run_if(in_state(AppState::Ingame)));
|
app.configure_sets(Update, GameplaySet.run_if(in_state(AppState::Ingame)));
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,65 +1,28 @@
|
||||||
//! Sets up the game's camera controls etc.
|
//! Sets up the game's camera controls etc.
|
||||||
|
//! Uses [bevy_third_person_camera](https://lib.rs/crates/bevy_third_person_camera)
|
||||||
|
use bevy::prelude::*;
|
||||||
|
use bevy_third_person_camera::*;
|
||||||
|
|
||||||
use bevy::{
|
use crate::AppState;
|
||||||
input::mouse::{AccumulatedMouseMotion, MouseButtonInput},
|
|
||||||
prelude::*,
|
|
||||||
};
|
|
||||||
|
|
||||||
use super::GameplaySet;
|
use super::GameplaySet;
|
||||||
|
|
||||||
pub fn plugin(app: &mut App) {
|
pub fn plugin(app: &mut App) {
|
||||||
app.init_resource::<MouseButtonsPressed>().add_systems(
|
app.add_plugins(ThirdPersonCameraPlugin)
|
||||||
Update,
|
.add_systems(
|
||||||
(
|
OnEnter(AppState::Ingame),
|
||||||
detect_mouse_button_press,
|
setup_game_camera.in_set(GameplaySet),
|
||||||
panning_basic.after(detect_mouse_button_press),
|
|
||||||
)
|
)
|
||||||
.in_set(GameplaySet),
|
.add_systems(OnExit(AppState::Ingame), remove_tpc.in_set(GameplaySet));
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resource to store current mouse button state.
|
/// Adds [ThirdPersonCamera] to our existing camera.
|
||||||
#[derive(Resource, Default)]
|
pub fn setup_game_camera(mut c: Commands, cam: Single<Entity, With<Camera3d>>) {
|
||||||
struct MouseButtonsPressed {
|
c.entity(*cam).insert(ThirdPersonCamera::default());
|
||||||
left: bool,
|
info!("Third person camera set up!")
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Detect whether mouse buttons (currently only left) are pressed.
|
/// Removes [ThirdPersonCamera] from the camera.
|
||||||
fn detect_mouse_button_press(
|
pub fn remove_tpc(mut c: Commands, cam: Single<Entity, (With<Camera3d>, With<ThirdPersonCamera>)>) {
|
||||||
mut button_events: EventReader<MouseButtonInput>,
|
c.entity(*cam).remove::<ThirdPersonCamera>();
|
||||||
mut pressed: ResMut<MouseButtonsPressed>,
|
|
||||||
) {
|
|
||||||
for event in button_events.read() {
|
|
||||||
if event.button == MouseButton::Left {
|
|
||||||
pressed.left = dbg!(event.state.is_pressed());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// is SUPPOSED to do proper panning
|
|
||||||
/// i hate 3d math
|
|
||||||
fn panning_basic(
|
|
||||||
accumulated_mouse_motion: Res<AccumulatedMouseMotion>,
|
|
||||||
mut cam_transform: Single<&mut Transform, With<Camera>>,
|
|
||||||
pressed: Res<MouseButtonsPressed>,
|
|
||||||
) {
|
|
||||||
if pressed.left && accumulated_mouse_motion.delta != Vec2::ZERO {
|
|
||||||
let yaw = Quat::from_rotation_y(accumulated_mouse_motion.delta.x / 150.);
|
|
||||||
// cam_transform.rotate_around(Vec3::ZERO, yaw);
|
|
||||||
cam_transform.rotation *= yaw;
|
|
||||||
|
|
||||||
let pitch = Quat::from_rotation_x(accumulated_mouse_motion.delta.y / 150.);
|
|
||||||
let new_rot = cam_transform.rotation * pitch;
|
|
||||||
// cam_transform.rotate_around(Vec3::ZERO, pitch);
|
|
||||||
// cam_transform.rotation *= pitch;
|
|
||||||
let up_vector = new_rot * Vec3::Y;
|
|
||||||
if up_vector.y > 0.0 {
|
|
||||||
cam_transform.rotation = new_rot;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// ???????
|
|
||||||
// TODO: figure out/understand how they do it https://github.com/The-DevBlog/bevy_third_person_camera/blob/a7c6b458573fcb0730b65eda6507ca27fb58f571/src/mouse.rs#L29-L74
|
|
||||||
//
|
|
||||||
let rot_matrix = Mat3::from_quat(cam_transform.rotation);
|
|
||||||
cam_transform.translation = rot_matrix.mul_vec3(Vec3::new(0.0, 0.0, 20.));
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
use bevy_third_person_camera::ThirdPersonCameraTarget;
|
||||||
|
use log::trace;
|
||||||
|
|
||||||
use crate::cleanup;
|
use crate::cleanup;
|
||||||
|
|
||||||
|
@ -20,6 +22,8 @@ pub fn setup(
|
||||||
c.spawn((
|
c.spawn((
|
||||||
Mesh3d(meshes.add(Cuboid::new(NORMALSPUR, 0.25, 96.))),
|
Mesh3d(meshes.add(Cuboid::new(NORMALSPUR, 0.25, 96.))),
|
||||||
MeshMaterial3d(materials.add(Color::BLACK)),
|
MeshMaterial3d(materials.add(Color::BLACK)),
|
||||||
|
ThirdPersonCameraTarget,
|
||||||
cleanup::Scene,
|
cleanup::Scene,
|
||||||
));
|
));
|
||||||
|
info!("Scene spawned!")
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,10 @@ use bevy::prelude::*;
|
||||||
|
|
||||||
mod camera;
|
mod camera;
|
||||||
mod cleanup;
|
mod cleanup;
|
||||||
|
mod debugging;
|
||||||
mod game;
|
mod game;
|
||||||
|
|
||||||
#[derive(States, Default, Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(States, Default, Debug, Clone, PartialEq, Eq, Hash, Reflect)]
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
enum AppState {
|
enum AppState {
|
||||||
Menus,
|
Menus,
|
||||||
|
@ -16,8 +17,10 @@ enum AppState {
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
.add_plugins(DefaultPlugins)
|
.add_plugins(DefaultPlugins)
|
||||||
.init_state::<AppState>()
|
|
||||||
.add_plugins(game::plugin)
|
|
||||||
.add_systems(Startup, camera::setup)
|
.add_systems(Startup, camera::setup)
|
||||||
|
.add_plugins(game::plugin)
|
||||||
|
.init_state::<AppState>()
|
||||||
|
.register_type::<AppState>()
|
||||||
|
.add_plugins(debugging::plugin)
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue