more mario like controls
This commit is contained in:
54
src/main.rs
54
src/main.rs
@@ -7,7 +7,7 @@ use std::iter::range_step;
|
|||||||
use sdl2::video::{Window, WindowPos, OPENGL};
|
use sdl2::video::{Window, WindowPos, OPENGL};
|
||||||
use sdl2::event::{poll_event, Event};
|
use sdl2::event::{poll_event, Event};
|
||||||
use sdl2::timer::{get_ticks, delay};
|
use sdl2::timer::{get_ticks, delay};
|
||||||
use sdl2::rect::{Point, Rect};
|
use sdl2::rect::Rect;
|
||||||
use sdl2::keycode::KeyCode;
|
use sdl2::keycode::KeyCode;
|
||||||
use sdl2::pixels::Color;
|
use sdl2::pixels::Color;
|
||||||
|
|
||||||
@@ -30,12 +30,12 @@ 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_SPEED_X : f32 = 4.0;
|
||||||
const PLAYER_THRESHOLD_X : f32 = 0.2;
|
const PLAYER_THRESHOLD_X : f32 = 0.2;
|
||||||
|
|
||||||
const PLAYER_ACCELERATION_X_START : f32 = 0.01;
|
const PLAYER_ACCELERATION_X_START : f32 = 0.02;
|
||||||
const PLAYER_ACCELERATION_X_STOP : f32 = 0.08;
|
const PLAYER_ACCELERATION_X_STOP : f32 = 0.1;
|
||||||
const PLAYER_ACCELERATION_X_CHANGE : f32 = 0.018;
|
const PLAYER_ACCELERATION_X_CHANGE : f32 = 0.06;
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@@ -76,7 +76,7 @@ fn main() {
|
|||||||
|
|
||||||
let mut camera = Camera::new(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, layer.to_rect());
|
let mut camera = Camera::new(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, layer.to_rect());
|
||||||
|
|
||||||
let mut player = Player::new(290.0, 390.0);
|
let mut player = Player::new(390.0, 390.0);
|
||||||
|
|
||||||
let mut current : usize;
|
let mut current : usize;
|
||||||
let mut elapsed : usize;
|
let mut elapsed : usize;
|
||||||
@@ -111,30 +111,26 @@ fn main() {
|
|||||||
break 'main;
|
break 'main;
|
||||||
}
|
}
|
||||||
|
|
||||||
if keyboard.is_held(KeyCode::Right) {
|
|
||||||
|
if keyboard.is_held(KeyCode::Right) && (player.dx >= 0.0 || player.on_ground) {
|
||||||
let a = if player.dx > 0.0 {
|
let a = if player.dx > 0.0 {
|
||||||
PLAYER_ACCELERATION_X_START
|
PLAYER_ACCELERATION_X_START
|
||||||
} else {
|
} else {
|
||||||
PLAYER_ACCELERATION_X_CHANGE
|
PLAYER_ACCELERATION_X_CHANGE
|
||||||
};
|
};
|
||||||
|
|
||||||
let t = 8.0;
|
|
||||||
|
|
||||||
player.dx = a * PLAYER_SPEED_X + (1.0 - a) * player.dx;
|
player.dx = a * PLAYER_SPEED_X + (1.0 - a) * player.dx;
|
||||||
}
|
}
|
||||||
else if keyboard.is_held(KeyCode::Left) {
|
else if keyboard.is_held(KeyCode::Left) && (player.dx <= 0.0 || player.on_ground) {
|
||||||
let a = if player.dx < 0.0 {
|
let a = if player.dx < 0.0 {
|
||||||
PLAYER_ACCELERATION_X_START
|
PLAYER_ACCELERATION_X_START
|
||||||
} else {
|
} else {
|
||||||
PLAYER_ACCELERATION_X_CHANGE
|
PLAYER_ACCELERATION_X_CHANGE
|
||||||
};
|
};
|
||||||
|
|
||||||
let t = -8.0;
|
|
||||||
|
|
||||||
player.dx = a * -PLAYER_SPEED_X + (1.0 - a) * player.dx;
|
player.dx = a * -PLAYER_SPEED_X + (1.0 - a) * player.dx;
|
||||||
} else {
|
} else if player.on_ground {
|
||||||
let a = PLAYER_ACCELERATION_X_STOP;
|
let a = PLAYER_ACCELERATION_X_STOP;
|
||||||
let t = 0.0;
|
|
||||||
|
|
||||||
player.dx = (1.0 - a) * player.dx;
|
player.dx = (1.0 - a) * player.dx;
|
||||||
|
|
||||||
@@ -143,17 +139,17 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if keyboard.was_pressed(KeyCode::Up) {
|
|
||||||
if player.on_ground {
|
if player.on_ground {
|
||||||
player.dy = -12.0;
|
if keyboard.was_pressed(KeyCode::Up) {
|
||||||
|
player.dy = -8.0;
|
||||||
|
|
||||||
player.on_ground = false;
|
player.on_ground = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if keyboard.was_released(KeyCode::Up) {
|
if keyboard.was_released(KeyCode::Up) {
|
||||||
if player.dy < -6.0 {
|
if player.dy < -4.0 {
|
||||||
player.dy = -6.0;
|
player.dy = -4.0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,7 +177,7 @@ fn main() {
|
|||||||
d = match *tile {
|
d = match *tile {
|
||||||
Tile::Empty => d,
|
Tile::Empty => d,
|
||||||
Tile::Floor(_) => d.min(t)
|
Tile::Floor(_) => d.min(t)
|
||||||
};
|
}
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -192,6 +188,9 @@ fn main() {
|
|||||||
|
|
||||||
if d > 0.0 {
|
if d > 0.0 {
|
||||||
player.x += d;
|
player.x += d;
|
||||||
|
} else if d < 0.0 {
|
||||||
|
player.x += d;
|
||||||
|
player.dx = 0.0;
|
||||||
} else {
|
} else {
|
||||||
player.dx = 0.0;
|
player.dx = 0.0;
|
||||||
}
|
}
|
||||||
@@ -213,7 +212,7 @@ fn main() {
|
|||||||
d = match *tile {
|
d = match *tile {
|
||||||
Tile::Empty => d,
|
Tile::Empty => d,
|
||||||
Tile::Floor(_) => d.max(t)
|
Tile::Floor(_) => d.max(t)
|
||||||
};
|
}
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -224,6 +223,9 @@ fn main() {
|
|||||||
|
|
||||||
if d < 0.0 {
|
if d < 0.0 {
|
||||||
player.x += d;
|
player.x += d;
|
||||||
|
} else if d > 0.0 {
|
||||||
|
player.x += d;
|
||||||
|
player.dx = 0.0;
|
||||||
} else {
|
} else {
|
||||||
player.dx = 0.0;
|
player.dx = 0.0;
|
||||||
}
|
}
|
||||||
@@ -247,7 +249,7 @@ fn main() {
|
|||||||
d = match *tile {
|
d = match *tile {
|
||||||
Tile::Empty => d,
|
Tile::Empty => d,
|
||||||
Tile::Floor(_) => d.min(t)
|
Tile::Floor(_) => d.min(t)
|
||||||
};
|
}
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -258,6 +260,11 @@ fn main() {
|
|||||||
|
|
||||||
if d > 0.0 {
|
if d > 0.0 {
|
||||||
player.y += d;
|
player.y += d;
|
||||||
|
} else if d < 0.0 {
|
||||||
|
player.y += d;
|
||||||
|
player.dy = 0.0;
|
||||||
|
|
||||||
|
player.on_ground = true;
|
||||||
} else {
|
} else {
|
||||||
player.dy = 0.0;
|
player.dy = 0.0;
|
||||||
|
|
||||||
@@ -281,7 +288,7 @@ fn main() {
|
|||||||
d = match *tile {
|
d = match *tile {
|
||||||
Tile::Empty => d,
|
Tile::Empty => d,
|
||||||
Tile::Floor(_) => d.max(t)
|
Tile::Floor(_) => d.max(t)
|
||||||
};
|
}
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -292,6 +299,9 @@ fn main() {
|
|||||||
|
|
||||||
if d < 0.0 {
|
if d < 0.0 {
|
||||||
player.y += d;
|
player.y += d;
|
||||||
|
} else if d > 0.0 {
|
||||||
|
player.y += d;
|
||||||
|
player.dy = 0.0;
|
||||||
} else {
|
} else {
|
||||||
player.dy = 0.0;
|
player.dy = 0.0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use std::iter::repeat;
|
use std::iter::repeat;
|
||||||
use std::cmp::{min, max};
|
use std::cmp::{min, max};
|
||||||
|
|
||||||
use sdl2::rect::{Point, Rect};
|
use sdl2::rect::Rect;
|
||||||
|
|
||||||
|
|
||||||
pub struct Layer<T> {
|
pub struct Layer<T> {
|
||||||
@@ -26,7 +26,7 @@ impl<T> Layer<T> where T: Clone {
|
|||||||
pub fn get_tile(&self, x: i32, y: i32) -> Option<&T> {
|
pub fn get_tile(&self, x: i32, y: i32) -> Option<&T> {
|
||||||
let offset = (x + y * self.width) as usize;
|
let offset = (x + y * self.width) as usize;
|
||||||
|
|
||||||
if offset >= 0 && offset < self.tiles.len() {
|
if offset < self.tiles.len() {
|
||||||
Some(&self.tiles[offset])
|
Some(&self.tiles[offset])
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
|||||||
Reference in New Issue
Block a user