2023-09-28 12:10:45 +00:00
pub mod in_game ;
pub use in_game ::* ;
pub mod in_main_menu ;
pub use in_main_menu ::* ;
pub mod picking ;
pub use picking ::* ;
2023-10-13 10:53:26 +00:00
use crate ::{
insert_dependant_component ,
state ::{ AppState , GameState } ,
} ;
2023-07-26 21:59:28 +00:00
use bevy ::prelude ::* ;
use bevy_rapier3d ::prelude ::* ;
// this file is just for demo purposes, contains various types of components, systems etc
2023-10-13 10:53:26 +00:00
#[ derive(Component, Reflect, Default, Debug) ]
2023-07-26 21:59:28 +00:00
#[ reflect(Component) ]
2023-10-13 10:53:26 +00:00
pub enum SoundMaterial {
Metal ,
Wood ,
Rock ,
Cloth ,
Squishy ,
#[ default ]
None ,
2023-07-26 21:59:28 +00:00
}
2023-10-13 10:53:26 +00:00
#[ derive(Component, Reflect, Default, Debug) ]
2023-07-26 21:59:28 +00:00
#[ reflect(Component) ]
/// Demo marker component
pub struct Player ;
2023-10-13 10:53:26 +00:00
#[ derive(Component, Reflect, Default, Debug) ]
2023-07-26 21:59:28 +00:00
#[ reflect(Component) ]
2023-10-13 10:53:26 +00:00
/// Demo component showing auto injection of components
2023-07-26 21:59:28 +00:00
pub struct ShouldBeWithPlayer ;
2023-10-13 10:53:26 +00:00
#[ derive(Component, Reflect, Default, Debug) ]
2023-07-26 21:59:28 +00:00
#[ reflect(Component) ]
/// Demo marker component
pub struct Interactible ;
fn player_move_demo (
keycode : Res < Input < KeyCode > > ,
mut players : Query < & mut Transform , With < Player > > ,
2023-10-13 10:53:26 +00:00
) {
2023-07-26 21:59:28 +00:00
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 > ,
2023-10-13 10:53:26 +00:00
) {
2023-11-11 21:58:00 +00:00
for collision_event in collision_events . read ( ) {
2023-07-26 21:59:28 +00:00
println! ( " collision " ) ;
match collision_event {
2023-10-13 10:53:26 +00:00
CollisionEvent ::Started ( _entity1 , _entity2 , _ ) = > {
2023-07-26 21:59:28 +00:00
println! ( " collision started " )
}
2023-10-13 10:53:26 +00:00
CollisionEvent ::Stopped ( _entity1 , _entity2 , _ ) = > {
2023-07-26 21:59:28 +00:00
println! ( " collision ended " )
}
}
}
2023-11-11 21:58:00 +00:00
for contact_force_event in contact_force_events . read ( ) {
2023-07-26 21:59:28 +00:00
println! ( " Received contact force event: {:?} " , contact_force_event ) ;
}
}
2023-09-28 12:10:45 +00:00
pub struct GamePlugin ;
impl Plugin for GamePlugin {
2023-10-13 10:53:26 +00:00
fn build ( & self , app : & mut App ) {
app . add_plugins ( PickingPlugin )
. 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)),
2023-11-11 21:58:00 +00:00
// test_collision_events
spawn_test ,
2023-10-13 10:53:26 +00:00
)
. run_if ( in_state ( GameState ::InGame ) ) ,
)
. add_systems ( OnEnter ( AppState ::MenuRunning ) , setup_main_menu )
. add_systems ( OnExit ( AppState ::MenuRunning ) , teardown_main_menu )
. add_systems ( Update , ( main_menu ) )
. add_systems ( OnEnter ( AppState ::AppRunning ) , setup_game ) ;
}
2023-07-26 21:59:28 +00:00
}