refactor():

* moved all reusable parts to core module / folder
 * moved all game specific code to game module
 * fixed small issue with camera & other minor tweaks
This commit is contained in:
kaosat.dev 2023-07-26 23:59:28 +02:00
parent 178e789832
commit 6b769e1ba1
19 changed files with 143 additions and 128 deletions

View File

@ -3,11 +3,11 @@ use bevy::prelude::*;
use bevy::core_pipeline::bloom::{BloomSettings, BloomCompositeMode}; use bevy::core_pipeline::bloom::{BloomSettings, BloomCompositeMode};
use bevy::core_pipeline::tonemapping::{Tonemapping, DebandDither}; use bevy::core_pipeline::tonemapping::{Tonemapping, DebandDither};
use super::CameraTracking; use super::CameraTrackingOffset;
pub fn camera_replace_proxies ( pub fn camera_replace_proxies (
mut commands: Commands, mut commands: Commands,
mut added_cameras: Query<(Entity, &mut Camera), (Added<Camera>, With<CameraTracking>)>, mut added_cameras: Query<(Entity, &mut Camera), (Added<Camera>, With<CameraTrackingOffset>)>,
) { ) {
for (entity, mut camera) in added_cameras.iter_mut(){ for (entity, mut camera) in added_cameras.iter_mut(){
@ -18,11 +18,11 @@ pub fn camera_replace_proxies (
DebandDither::Enabled DebandDither::Enabled
) )
.insert( .insert(
Tonemapping::None Tonemapping::BlenderFilmic
) )
.insert( .insert(
BloomSettings{ BloomSettings{
intensity: 0.08, intensity: 0.01,
composite_mode:BloomCompositeMode::Additive, composite_mode:BloomCompositeMode::Additive,
..default() ..default()
} }

View File

@ -27,7 +27,6 @@ impl Default for CameraTrackingOffset {
impl CameraTrackingOffset { impl CameraTrackingOffset {
fn new (input: Vec3) -> Self { fn new (input: Vec3) -> Self {
println!("NEEEW");
CameraTrackingOffset(input) CameraTrackingOffset(input)
} }
} }

28
src/core/mod.rs Normal file
View File

@ -0,0 +1,28 @@
pub mod process_gltf;
pub use process_gltf::*;
pub mod camera;
pub use camera::*;
pub mod lighting;
pub use lighting::*;
pub mod relationships;
pub use relationships::*;
pub mod physics;
pub use physics::*;
use bevy::prelude::*;
pub struct CorePlugin;
impl Plugin for CorePlugin {
fn build(&self, app: &mut App) {
app
.add_plugins((
ProcessGltfPlugin,
LightingPlugin,
CameraPlugin,
PhysicsPlugin
));
}
}

View File

@ -9,7 +9,7 @@ pub use controls::*;
use bevy::prelude::*; use bevy::prelude::*;
// use crate::state::{GameState}; // use crate::state::{GameState};
use super::Collider; use crate::Collider;
pub struct PhysicsPlugin; pub struct PhysicsPlugin;
impl Plugin for PhysicsPlugin { impl Plugin for PhysicsPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {

103
src/game.rs Normal file
View File

@ -0,0 +1,103 @@
use bevy::prelude::*;
use bevy_rapier3d::prelude::*;
use crate::insert_dependant_component;
// this file is just for demo purposes, contains various types of components, systems etc
#[derive(Component, Reflect, Default, Debug, )]
#[reflect(Component)]
pub enum SoundMaterial{
Metal,
Wood,
Rock,
Cloth,
Squishy,
#[default]
None
}
#[derive(Component, Reflect, Default, Debug, )]
#[reflect(Component)]
/// Demo marker component
pub struct Player;
#[derive(Component, Reflect, Default, Debug, )]
#[reflect(Component)]
/// Demo component showing auto injection of components
pub struct ShouldBeWithPlayer;
#[derive(Component, Reflect, Default, Debug, )]
#[reflect(Component)]
/// Demo marker component
pub struct Interactible;
fn player_move_demo(
keycode: Res<Input<KeyCode>>,
mut players: Query<&mut Transform, With<Player>>,
){
let speed = 0.2;
if let Ok(mut player) = players.get_single_mut() {
if keycode.pressed(KeyCode::Left) {
player.translation.x += speed;
}
if keycode.pressed(KeyCode::Right) {
player.translation.x -= speed;
}
if keycode.pressed(KeyCode::Up) {
player.translation.z += speed;
}
if keycode.pressed(KeyCode::Down) {
player.translation.z -= speed;
}
}
}
// collision tests/debug
pub fn test_collision_events(
mut collision_events: EventReader<CollisionEvent>,
mut contact_force_events: EventReader<ContactForceEvent>,
)
{
for collision_event in collision_events.iter() {
println!("collision");
match collision_event {
CollisionEvent::Started(entity1, entity2 ,_) => {
println!("collision started")
}
CollisionEvent::Stopped(entity1, entity2 ,_) => {
println!("collision ended")
}
}
}
for contact_force_event in contact_force_events.iter() {
println!("Received contact force event: {:?}", contact_force_event);
}
}
pub struct DemoPlugin;
impl Plugin for DemoPlugin {
fn build(&self, app: &mut App) {
app
.register_type::<Interactible>()
.register_type::<SoundMaterial>()
.register_type::<Player>()
// little helper utility, to automatically inject components that are dependant on an other component
// ie, here an Entity with a Player component should also always have a ShouldBeWithPlayer component
// you get a warning if you use this, as I consider this to be stop-gap solution (usually you should have either a bundle, or directly define all needed components)
.add_systems(Update, (
insert_dependant_component::<Player, ShouldBeWithPlayer>,
player_move_demo, //.run_if(in_state(AppState::Running)),
test_collision_events
))
;
}
}

View File

@ -3,51 +3,18 @@ use bevy::{prelude::*, asset::ChangeWatcher, gltf::Gltf};
use bevy_editor_pls::prelude::*; use bevy_editor_pls::prelude::*;
use bevy_rapier3d::prelude::*; use bevy_rapier3d::prelude::*;
mod process_gltf; mod core;
use process_gltf::*; use crate::core::*;
mod camera;
use camera::*;
mod lighting;
use lighting::*;
mod relationships;
use relationships::*;
mod physics;
use physics::*;
mod game;
use crate::game::*;
#[derive(Component, Reflect, Default, Debug, )] #[derive(Component, Reflect, Default, Debug, )]
#[reflect(Component)] #[reflect(Component)]
/// Demo marker component /// helper marker component
pub struct Player;
#[derive(Component, Reflect, Default, Debug, )]
#[reflect(Component)]
/// Demo component showing auto injection of components
pub struct ShouldBeWithPlayer;
#[derive(Component, Reflect, Default, Debug, )]
#[reflect(Component)]
/// Demo marker component
pub struct LoadedMarker; pub struct LoadedMarker;
#[derive(Component, Reflect, Default, Debug, )]
#[reflect(Component)]
/// Demo marker component
pub struct Interactible;
#[derive(Component, Reflect, Default, Debug, )]
#[reflect(Component)]
/// Demo marker component
pub struct MeshCollider;
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)] #[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)]
enum AppState { enum AppState {
#[default] #[default]
@ -57,22 +24,6 @@ enum AppState {
use bevy::{prelude::*};
#[derive(Component, Reflect, Default, Debug, )]
#[reflect(Component)]
pub enum SoundMaterial{
Metal,
Wood,
Rock,
Cloth,
Squishy,
#[default]
None
}
fn main(){ fn main(){
App::new() App::new()
.add_plugins(( .add_plugins((
@ -91,30 +42,14 @@ fn main(){
RapierPhysicsPlugin::<NoUserData>::default(), RapierPhysicsPlugin::<NoUserData>::default(),
RapierDebugRenderPlugin::default(), RapierDebugRenderPlugin::default(),
// our custom plugins // our custom plugins
ProcessGltfPlugin, CorePlugin, // reusable plugins
LightingPlugin, DemoPlugin // specific to our game
CameraPlugin,
PhysicsPlugin
)) ))
.register_type::<Interactible>()
.register_type::<MeshCollider>()
.register_type::<SoundMaterial>()
.register_type::<Player>()
// little helper utility, to automatically inject components that are dependant on an other component
// ie, here an Entity with a Player component should also always have a ShouldBeWithPlayer component
// you get a warning if you use this, as I consider this to be stop-gap solution (usually you should have either a bundle, or directly define all needed components)
.add_systems(Update, insert_dependant_component::<Player, ShouldBeWithPlayer>)
.add_state::<AppState>() .add_state::<AppState>()
.add_systems(Startup, setup) .add_systems(Startup, setup)
.add_systems(Update, ( .add_systems(Update, (
spawn_level.run_if(in_state(AppState::Loading)), spawn_level.run_if(in_state(AppState::Loading)),
player_move_demo.run_if(in_state(AppState::Running)),
test_collision_events
)) ))
.run(); .run();
} }
@ -171,53 +106,3 @@ fn spawn_level(
} }
fn player_move_demo(
keycode: Res<Input<KeyCode>>,
mut players: Query<&mut Transform, With<Player>>,
){
let speed = 0.2;
if let Ok(mut player) = players.get_single_mut() {
if keycode.pressed(KeyCode::Left) {
player.translation.x += speed;
}
if keycode.pressed(KeyCode::Right) {
player.translation.x -= speed;
}
if keycode.pressed(KeyCode::Up) {
player.translation.z += speed;
}
if keycode.pressed(KeyCode::Down) {
player.translation.z -= speed;
}
}
}
// collision tests
pub fn test_collision_events(
mut collision_events: EventReader<CollisionEvent>,
mut contact_force_events: EventReader<ContactForceEvent>,
)
{
for collision_event in collision_events.iter() {
println!("collision");
match collision_event {
CollisionEvent::Started(entity1, entity2 ,_) => {
println!("collision started")
}
CollisionEvent::Stopped(entity1, entity2 ,_) => {
println!("collision ended")
}
}
}
for contact_force_event in contact_force_events.iter() {
println!("Received contact force event: {:?}", contact_force_event);
}
}