Skip to content
Snippets Groups Projects
Commit 7fbe2b41 authored by Per Lindgren's avatar Per Lindgren
Browse files

jump

parent 6dacd0a9
No related branches found
No related tags found
No related merge requests found
#![allow(unused)]
use bevy::{core::FixedTimestep, prelude::*};
use std::f32::consts::PI;
const PLAYER_SHEET: &str = "character_zombie_sheet.png";
fn main() {
......@@ -10,7 +11,8 @@ fn main() {
width: 1920.,
height: 1080.,
vsync: true,
resizable: false,
cursor_visible: false,
// resizable: false,
..Default::default()
})
.add_startup_system(setup.system())
......@@ -48,24 +50,32 @@ fn setup(
},
..Default::default()
})
.insert(Player::default());
.insert(Player {
y: bottom / 2.,
..Player::default()
});
}
enum State {
#[derive(Copy, Clone)]
enum Direction {
Right,
Left,
Idle,
}
pub struct Player {
state: State,
direction: Direction,
jump: bool,
index: u32,
y: f32,
}
impl Default for Player {
fn default() -> Self {
Player {
state: State::Idle,
direction: Direction::Idle,
jump: false,
index: 3,
y: 0.,
}
}
}
......@@ -76,44 +86,90 @@ fn player_movement(
keyboard_input: Res<Input<KeyCode>>,
) {
if let Ok((mut player, mut transform, mut sprite)) = query.single_mut() {
let (index, state, dir) = if keyboard_input.pressed(KeyCode::Right) {
let (index, state, dir) = if player.jump {
// already in jump
let mut index = player.index + 1;
match index {
4 => {
sprite.index = 7;
}
8 => {
sprite.index = 8;
}
60 => {
sprite.index = 3;
}
_ => {}
};
if index == 64 {
index = 0;
player.jump = false
};
if index > 8 && index < (60) {
transform.translation.y =
player.y + (((index - 8) as f32) * PI / (60.0 - 8.0)).sin() * 200.0;
} else {
transform.translation.y = player.y
}
(index, player.direction, 0.)
} else if keyboard_input.pressed(KeyCode::Space) {
// start jump
player.jump = true;
sprite.index = 3;
let index = 0;
(index, player.direction, 0.)
} else if keyboard_input.pressed(KeyCode::Right) {
sprite.flip_x = false;
match player.state {
match player.direction {
// already walking
State::Right => {
Direction::Right => {
let index = (player.index + 1) % 32;
sprite.index = (4 * 9) + index / 4;
(index, State::Right, 2.)
(index, Direction::Right, 2.)
}
// starting walking
_ => {
let index = 3 * 4;
sprite.index = (4 * 9) + index / 4;
(index, State::Right, 2.)
(index, Direction::Right, 2.)
}
}
} else if keyboard_input.pressed(KeyCode::Left) {
sprite.flip_x = true;
match player.state {
match player.direction {
// already walking
State::Left => {
Direction::Left => {
let index = (player.index + 1) % 32;
sprite.index = (4 * 9) + index / 4;
(index, State::Left, -2.)
(index, Direction::Left, -2.)
}
// starting walking
_ => {
let index = 3 * 4;
sprite.index = (4 * 9) + index / 4;
(index, State::Left, -2.)
(index, Direction::Left, -2.)
}
}
} else {
match player.direction {
// already idle
Direction::Idle => {
let index = (player.index + 1) % 256;
sprite.index = if index > 224 { 0 } else { 9 * 1 + 6 };
(index, Direction::Idle, 0.)
}
// start idle
_ => {
player.index = 0;
sprite.index = 0;
(0, State::Idle, 0.)
(0, Direction::Idle, 0.)
}
}
};
player.state = state;
player.direction = state;
player.index = index;
transform.translation.x += dir;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment