Add automatic falling

main
Elijah Voigt 3 weeks ago
parent f89d52bfbf
commit 459740a0ef

@ -8,6 +8,7 @@ fn main() {
game_type: GameType::Two, game_type: GameType::Two,
..default() ..default()
}) })
.init_state::<Falling>()
.add_systems(Startup, init_pieces) .add_systems(Startup, init_pieces)
.add_systems( .add_systems(
Update, Update,
@ -15,6 +16,8 @@ fn main() {
kb_movement.run_if(on_event::<KeyboardInput>), kb_movement.run_if(on_event::<KeyboardInput>),
update_position, update_position,
update_orientation, update_orientation,
toggle_falling.run_if(on_event::<KeyboardInput>),
falling.run_if(in_state(Falling::On)).run_if(clock_cycle(1.0))
), ),
) )
.add_systems(Update, draw_grid) .add_systems(Update, draw_grid)
@ -23,7 +26,7 @@ fn main() {
const SCALE: f32 = 30.0; const SCALE: f32 = 30.0;
#[derive(Component, Default, Debug, Clone, Copy)] #[derive(Component, Default, Debug, Clone, Copy, PartialEq)]
#[require(Transform, Visibility)] #[require(Transform, Visibility)]
struct GridPosition { struct GridPosition {
x: usize, x: usize,
@ -32,23 +35,31 @@ struct GridPosition {
// TODO: Restirct movement to within the grid // TODO: Restirct movement to within the grid
impl GridPosition { impl GridPosition {
fn move_up(&mut self) { fn move_up(&self) -> Self {
if self.y + 1 < 20 { Self {
self.y = self.y.saturating_add(1); y: if self.y + 1 < 20 { self.y.saturating_add(1) } else { self.y },
x: self.x
} }
} }
fn move_down(&mut self) { fn move_down(&mut self) -> Self {
self.y = self.y.saturating_sub(1); Self {
y: self.y.saturating_sub(1),
x: self.x
}
} }
fn move_left(&mut self) { fn move_left(&mut self) -> Self {
self.x = self.x.saturating_sub(1); Self {
x: self.x.saturating_sub(1),
y: self.y
}
} }
fn move_right(&mut self) { fn move_right(&mut self) -> Self {
if self.x + 1 < 10 { Self {
self.x = self.x.saturating_add(1); x: if self.x + 1 < 10 { self.x.saturating_add(1) } else { self.x },
y: self.y
} }
} }
} }
@ -131,6 +142,13 @@ impl From<&Orientation> for Quat {
} }
} }
#[derive(States, Clone, Eq, PartialEq, Debug, Hash, Default, Component)]
enum Falling {
#[default]
On,
Off
}
fn init_pieces( fn init_pieces(
mut commands: Commands, mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
@ -191,10 +209,10 @@ fn kb_movement(mut events: EventReader<KeyboardInput>, mut query: Query<(&mut Gr
// Check if children would be outside play area... // Check if children would be outside play area...
query.iter_mut().for_each(|(mut gp, mut o)| { query.iter_mut().for_each(|(mut gp, mut o)| {
match key_code { match key_code {
KeyCode::ArrowUp => gp.move_up(), KeyCode::ArrowUp => *gp = gp.move_up(),
KeyCode::ArrowDown => gp.move_down(), KeyCode::ArrowDown => *gp = gp.move_down(),
KeyCode::ArrowLeft => gp.move_left(), KeyCode::ArrowLeft => *gp = gp.move_left(),
KeyCode::ArrowRight => gp.move_right(), KeyCode::ArrowRight => *gp = gp.move_right(),
KeyCode::Enter => *o = o.next(), KeyCode::Enter => *o = o.next(),
_ => () _ => ()
} }
@ -214,3 +232,44 @@ fn draw_grid(mut gizmos: Gizmos) {
) )
.outer_edges(); .outer_edges();
} }
fn toggle_falling(
mut events: EventReader<KeyboardInput>,
curr: Res<State<Falling>>,
mut next: ResMut<NextState<Falling>>,
) {
events.read().for_each(|KeyboardInput { state, key_code, .. }| {
if let ButtonState::Pressed = state && *key_code == KeyCode::Space {
next.set(match curr.get() {
Falling::On => Falling::Off,
Falling::Off => Falling::On,
});
}
})
}
fn falling(
mut query: Query<&mut GridPosition>
) {
query.iter_mut ().for_each(|mut gp| {
let next = gp.move_down();
if next != *gp {
*gp = next;
} else {
// Remove the falling component from this entity
}
});
}
// Run condition that returns `true` every `n` seconds
fn clock_cycle(n: f32) -> impl FnMut (Res<Time>, Local<f32>) -> bool {
move |t: Res<Time>, mut buf: Local<f32>| -> bool {
*buf += t.delta_secs();
if *buf > n {
*buf = 0.0;
true
} else {
false
}
}
}

Loading…
Cancel
Save