diff --git a/src/game.rs b/src/game.rs index 246b29b..305f338 100644 --- a/src/game.rs +++ b/src/game.rs @@ -33,7 +33,7 @@ fn setup(mut commands: Commands) { custom_size: Some(Vec2::new(80.0, 50.0)), ..default() }, - transform: Transform::from_xyz(0.0, -100.0, 0.0), + transform: Transform::from_xyz(-100.0, -250.0, 0.0), ..default() }); commands diff --git a/src/game/camera.rs b/src/game/camera.rs index af50b50..c4c81b4 100644 --- a/src/game/camera.rs +++ b/src/game/camera.rs @@ -9,7 +9,7 @@ impl Plugin for GameCameraPlugin { fn build(&self, app: &mut App) { app.register_inspectable::() .add_startup_system(camera_setup) - .add_system(camera_system); + .add_system_to_stage(CoreStage::PostUpdate, camera_system); } } diff --git a/src/game/kinematic.rs b/src/game/kinematic.rs index a0e79eb..51ffdee 100644 --- a/src/game/kinematic.rs +++ b/src/game/kinematic.rs @@ -9,7 +9,8 @@ pub struct KinematicPlugin; impl Plugin for KinematicPlugin { fn build(&self, app: &mut App) { - app.register_type::() + app.register_type::() + .register_type::() .register_type::() .add_system(kinematic_movement); } @@ -17,6 +18,7 @@ impl Plugin for KinematicPlugin { #[derive(Bundle)] pub struct KinematicBundle { + pub kinematic: KinematicState, pub rigidbody: RigidBody, pub velocity: Velocity, pub gravity_scale: GravityScale, @@ -32,8 +34,9 @@ pub struct KinematicBundle { impl Default for KinematicBundle { fn default() -> Self { KinematicBundle { + kinematic: KinematicState::default(), rigidbody: RigidBody::Dynamic, - gravity_scale: GravityScale(4.0), + gravity_scale: GravityScale(3.0), locked_axes: LockedAxes::ROTATION_LOCKED, events: ActiveEvents::COLLISION_EVENTS, collisions: ActiveCollisionTypes::all(), @@ -45,6 +48,36 @@ impl Default for KinematicBundle { } } +#[derive(Component, Reflect, Default)] +#[reflect(Component)] +pub struct KinematicState { + on_ground: bool, + did_jump: bool, + air_jump_counter: u8, +} + +impl KinematicState { + #[inline] + pub fn on_ground(&self) -> bool { + self.on_ground + } + #[inline] + pub fn did_jump(&self) -> bool { + self.did_jump + } + #[inline] + pub fn air_jump_counter(&self) -> u8 { + self.air_jump_counter + } + + pub fn can_jump(&self) -> bool { + if self.on_ground && !self.did_jump { + return true; + } + false + } +} + #[derive(Component, Reflect)] #[reflect(Component)] pub struct KinematicProperties { @@ -54,6 +87,8 @@ pub struct KinematicProperties { pub air_speed: f32, pub air_acceleration: f32, pub air_friction: f32, + pub jump_height: f32, + pub air_jumps: u8, } impl Default for KinematicProperties { @@ -65,6 +100,8 @@ impl Default for KinematicProperties { air_speed: 100.0, air_acceleration: 10.0, air_friction: 10.0, + jump_height: 150.0, + air_jumps: 1, } } } @@ -73,29 +110,44 @@ impl Default for KinematicProperties { #[reflect(Component)] pub struct KinematicInput { pub movement: Vec2, + pub want_jump: bool, } fn kinematic_movement( time: Res