Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Alien cake addict: Allow holding movement keys #2072

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 52 additions & 37 deletions examples/game/alien_cake_addict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ struct Player {
entity: Option<Entity>,
i: usize,
j: usize,
move_cooldown: f32,
}

#[derive(Default)]
Expand Down Expand Up @@ -82,6 +83,8 @@ const RESET_FOCUS: [f32; 3] = [
BOARD_SIZE_J as f32 / 2.0 - 0.5,
];

const MOVE_COOLDOWN: f32 = 0.3;

fn setup_cameras(mut commands: Commands, mut game: ResMut<Game>) {
game.camera_should_focus = Vec3::from(RESET_FOCUS);
game.camera_is_focus = game.camera_should_focus;
Expand Down Expand Up @@ -191,49 +194,61 @@ fn move_player(
keyboard_input: Res<Input<KeyCode>>,
mut game: ResMut<Game>,
mut transforms: Query<&mut Transform>,
time: Res<Time>,
) {
let mut moved = false;
let mut rotation = 0.0;
if keyboard_input.just_pressed(KeyCode::Up) {
if game.player.i < BOARD_SIZE_I - 1 {
game.player.i += 1;
game.player.move_cooldown = f32::max(game.player.move_cooldown - time.delta_seconds(), 0.);
RedlineTriad marked this conversation as resolved.
Show resolved Hide resolved

if game.player.move_cooldown == 0. {
let mut moved = false;
let mut rotation = 0.0;

let up = keyboard_input.pressed(KeyCode::Up) || keyboard_input.pressed(KeyCode::W);
let down = keyboard_input.pressed(KeyCode::Down) || keyboard_input.pressed(KeyCode::S);
let right = keyboard_input.pressed(KeyCode::Right) || keyboard_input.pressed(KeyCode::D);
let left = keyboard_input.pressed(KeyCode::Left) || keyboard_input.pressed(KeyCode::A);

if up {
if game.player.i < BOARD_SIZE_I - 1 {
game.player.i += 1;
}
rotation = -std::f32::consts::FRAC_PI_2;
moved = true;
}
rotation = -std::f32::consts::FRAC_PI_2;
moved = true;
}
if keyboard_input.just_pressed(KeyCode::Down) {
if game.player.i > 0 {
game.player.i -= 1;
if down {
if game.player.i > 0 {
game.player.i -= 1;
}
rotation = std::f32::consts::FRAC_PI_2;
moved = true;
}
rotation = std::f32::consts::FRAC_PI_2;
moved = true;
}
if keyboard_input.just_pressed(KeyCode::Right) {
if game.player.j < BOARD_SIZE_J - 1 {
game.player.j += 1;
if right {
if game.player.j < BOARD_SIZE_J - 1 {
game.player.j += 1;
}
rotation = std::f32::consts::PI;
moved = true;
}
rotation = std::f32::consts::PI;
moved = true;
}
if keyboard_input.just_pressed(KeyCode::Left) {
if game.player.j > 0 {
game.player.j -= 1;
if left {
if game.player.j > 0 {
game.player.j -= 1;
}
rotation = 0.0;
moved = true;
}
rotation = 0.0;
moved = true;
}

// move on the board
if moved {
*transforms.get_mut(game.player.entity.unwrap()).unwrap() = Transform {
translation: Vec3::new(
game.player.i as f32,
game.board[game.player.j][game.player.i].height,
game.player.j as f32,
),
rotation: Quat::from_rotation_y(rotation),
..Default::default()
};
// move on the board
if moved {
game.player.move_cooldown += MOVE_COOLDOWN;
*transforms.get_mut(game.player.entity.unwrap()).unwrap() = Transform {
translation: Vec3::new(
game.player.i as f32,
game.board[game.player.j][game.player.i].height,
game.player.j as f32,
),
rotation: Quat::from_rotation_y(rotation),
..Default::default()
};
}
}

// eat the cake!
Expand Down