added acceleration to player
This commit is contained in:
52
src/main.rs
52
src/main.rs
@@ -1,5 +1,6 @@
|
|||||||
extern crate sdl2;
|
extern crate sdl2;
|
||||||
|
|
||||||
|
|
||||||
use std::num::Float;
|
use std::num::Float;
|
||||||
use std::iter::range_step;
|
use std::iter::range_step;
|
||||||
|
|
||||||
@@ -29,6 +30,13 @@ const TILE_HEIGHT : i32 = 32;
|
|||||||
|
|
||||||
const MS_PER_UPDATE : usize = 10;
|
const MS_PER_UPDATE : usize = 10;
|
||||||
|
|
||||||
|
const PLAYER_SPEED_X : f32 = 8.0;
|
||||||
|
const PLAYER_THRESHOLD_X : f32 = 0.2;
|
||||||
|
|
||||||
|
const PLAYER_ACCELERATION_X_START : f32 = 0.01;
|
||||||
|
const PLAYER_ACCELERATION_X_STOP : f32 = 0.08;
|
||||||
|
const PLAYER_ACCELERATION_X_CHANGE : f32 = 0.018;
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
enum Tile {
|
enum Tile {
|
||||||
@@ -104,11 +112,35 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if keyboard.is_held(KeyCode::Right) {
|
if keyboard.is_held(KeyCode::Right) {
|
||||||
player.dx = 4.0;
|
let a = if player.dx > 0.0 {
|
||||||
}
|
PLAYER_ACCELERATION_X_START
|
||||||
|
} else {
|
||||||
|
PLAYER_ACCELERATION_X_CHANGE
|
||||||
|
};
|
||||||
|
|
||||||
if keyboard.is_held(KeyCode::Left) {
|
let t = 8.0;
|
||||||
player.dx = -4.0;
|
|
||||||
|
player.dx = a * PLAYER_SPEED_X + (1.0 - a) * player.dx;
|
||||||
|
}
|
||||||
|
else if keyboard.is_held(KeyCode::Left) {
|
||||||
|
let a = if player.dx < 0.0 {
|
||||||
|
PLAYER_ACCELERATION_X_START
|
||||||
|
} else {
|
||||||
|
PLAYER_ACCELERATION_X_CHANGE
|
||||||
|
};
|
||||||
|
|
||||||
|
let t = -8.0;
|
||||||
|
|
||||||
|
player.dx = a * -PLAYER_SPEED_X + (1.0 - a) * player.dx;
|
||||||
|
} else {
|
||||||
|
let a = PLAYER_ACCELERATION_X_STOP;
|
||||||
|
let t = 0.0;
|
||||||
|
|
||||||
|
player.dx = (1.0 - a) * player.dx;
|
||||||
|
|
||||||
|
if player.dx.abs() <= PLAYER_THRESHOLD_X {
|
||||||
|
player.dx = 0.0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if keyboard.was_pressed(KeyCode::Up) {
|
if keyboard.was_pressed(KeyCode::Up) {
|
||||||
@@ -119,18 +151,6 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if keyboard.was_released(KeyCode::Right) {
|
|
||||||
if player.dx > 0.0 {
|
|
||||||
player.dx = 0.0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if keyboard.was_released(KeyCode::Left) {
|
|
||||||
if player.dx < 0.0 {
|
|
||||||
player.dx = 0.0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if keyboard.was_released(KeyCode::Up) {
|
if keyboard.was_released(KeyCode::Up) {
|
||||||
if player.dy < -6.0 {
|
if player.dy < -6.0 {
|
||||||
player.dy = -6.0;
|
player.dy = -6.0;
|
||||||
|
|||||||
Reference in New Issue
Block a user