a simple tile system

This commit is contained in:
2015-01-02 15:23:45 +01:00
parent e313bbf80a
commit 4cca0eac2c
6 changed files with 126 additions and 63 deletions

6
Cargo.lock generated
View File

@@ -2,11 +2,11 @@
name = "super-matte-bros" name = "super-matte-bros"
version = "0.0.1" version = "0.0.1"
dependencies = [ dependencies = [
"sdl2 0.0.12 (registry+https://github.com/rust-lang/crates.io-index)", "sdl2 0.0.13 (git+https://github.com/AngryLawyer/rust-sdl2)",
] ]
[[package]] [[package]]
name = "sdl2" name = "sdl2"
version = "0.0.12" version = "0.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://github.com/AngryLawyer/rust-sdl2#1e9a96755780b757ed8833c16b17396b33e5eb15"

View File

@@ -3,8 +3,8 @@ name = "super-matte-bros"
version = "0.0.1" version = "0.0.1"
authors = ["logaritmisk <anders.e.olsson@gmail.com>"] authors = ["logaritmisk <anders.e.olsson@gmail.com>"]
[dependencies] [dependencies.sdl2]
sdl2 = "0.0.12" git = "https://github.com/AngryLawyer/rust-sdl2"
[[bin]] [[bin]]
name = "super-matte-bros" name = "super-matte-bros"

View File

@@ -1,5 +1,7 @@
extern crate sdl2; extern crate sdl2;
use std::num::SignedInt;
use std::cmp::{max, min}; use std::cmp::{max, min};
use sdl2::video::{Window, WindowPos, OPENGL}; use sdl2::video::{Window, WindowPos, OPENGL};
@@ -9,41 +11,47 @@ use sdl2::rect::Rect;
use sdl2::keycode::KeyCode; use sdl2::keycode::KeyCode;
use sdl2::pixels::Color; use sdl2::pixels::Color;
use vec::Vec2;
use tile::Layer;
use player::Player;
mod vec; mod vec;
mod tile;
mod player; mod player;
const SCREEN_WIDTH : int = 800; const SCREEN_WIDTH : i32 = 960;
const SCREEN_HEIGHT : int = 600; const SCREEN_HEIGHT : i32 = 640;
const TILE_WIDTH : i32 = 32;
const TILE_HEIGHT : i32 = 32;
const MS_PER_UPDATE : uint = 10; const MS_PER_UPDATE : uint = 10;
struct Object { #[deriving(Clone)]
position: vec::Vec2, enum Tile {
color: Color, Empty,
w: f32, Floor
h: f32,
} }
impl Object {
fn new(x: f32, y: f32, w: f32, h: f32, color: Color) -> Object { struct Camera {
Object { x: i32,
position: vec::Vec2 { x: x, y: y }, y: i32,
color: color, width: i32,
w: w, height: i32
h: h
}
} }
fn render(&self, renderer: &sdl2::render::Renderer) { impl Camera {
let _ = renderer.set_draw_color(self.color); fn new(x: i32, y: i32, width: i32, height: i32) -> Camera {
let _ = renderer.fill_rect(&self.get_rect()); Camera {
x: x,
y: y,
width: width,
height: height
} }
fn get_rect(&self) -> Rect {
Rect::new((self.position.x - (self.w / 2.0)) as i32, (self.position.y - (self.h / 2.0)) as i32, self.w as i32, self.h as i32)
} }
} }
@@ -51,7 +59,7 @@ impl Object {
fn main() { fn main() {
sdl2::init(sdl2::INIT_EVERYTHING); sdl2::init(sdl2::INIT_EVERYTHING);
let window = match Window::new("Super Matte Bros", WindowPos::PosCentered, WindowPos::PosCentered, SCREEN_WIDTH, SCREEN_HEIGHT, OPENGL) { let window = match Window::new("Super Matte Bros", WindowPos::PosCentered, WindowPos::PosCentered, SCREEN_WIDTH as int, SCREEN_HEIGHT as int, OPENGL) {
Ok(window) => window, Ok(window) => window,
Err(err) => panic!("failed to create window: {}", err) Err(err) => panic!("failed to create window: {}", err)
}; };
@@ -61,12 +69,18 @@ fn main() {
Err(err) => panic!("failed to create renderer: {}", err) Err(err) => panic!("failed to create renderer: {}", err)
}; };
let mut objects = Vec::new(); let mut camera = Camera::new(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
let mut layer = Layer::new(30, 20, Tile::Empty);
objects.push(Object::new(162.5, 400.0, 325.0, 5.0, Color::RGB(0, 0, 255))); for x in range(0, 14) {
objects.push(Object::new(637.5, 380.0, 325.0, 5.0, Color::RGB(0, 0, 255))); layer.set_tile(x, 14, Tile::Floor);
}
let mut player = player::Player::new(290.0, 390.0); for x in range(17, 30) {
layer.set_tile(x, 13, Tile::Floor);
}
let mut player = Player::new(290.0, 390.0);
let mut current : uint; let mut current : uint;
let mut elapsed : uint; let mut elapsed : uint;
@@ -114,9 +128,15 @@ fn main() {
player.on_ground = false; player.on_ground = false;
for object in objects.iter() { for y in range(0, 20) {
if collision_detection(&object.get_rect(), &player.get_rect()) { for x in range(0, 30) {
let intersect = collision_intersect(&object.get_rect(), &player.get_rect()); match *layer.get_tile(x, y) {
Tile::Empty => (),
Tile::Floor => {
let object = Rect::new(x * TILE_WIDTH, y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT);
if collision_detection(&object, &player.get_rect()) {
let intersect = collision_intersect(&object, &player.get_rect());
if intersect.w >= intersect.h { if intersect.w >= intersect.h {
let mut delta = intersect.h as f32; let mut delta = intersect.h as f32;
@@ -141,6 +161,9 @@ fn main() {
} }
} }
} }
}
}
}
lag -= MS_PER_UPDATE; lag -= MS_PER_UPDATE;
} }
@@ -148,8 +171,16 @@ fn main() {
let _ = renderer.set_draw_color(Color::RGB(0, 0, 0)); let _ = renderer.set_draw_color(Color::RGB(0, 0, 0));
let _ = renderer.clear(); let _ = renderer.clear();
for object in objects.iter() { for y in range(0, 20) {
object.render(&renderer); for x in range(0, 30) {
match *layer.get_tile(x, y) {
Tile::Empty => (),
Tile::Floor => {
let _ = renderer.set_draw_color(Color::RGB(0, 0, 255));
let _ = renderer.fill_rect(&Rect::new(x * TILE_WIDTH, y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT));
}
}
}
} }
player.render(&renderer); player.render(&renderer);

View File

@@ -5,19 +5,26 @@ use sdl2::render::Renderer;
use sdl2::pixels::Color; use sdl2::pixels::Color;
use sdl2::rect::Rect; use sdl2::rect::Rect;
use vec::Vec2;
use vec; use vec;
pub struct Player { pub struct Player {
pub position: vec::Vec2, pub position: Vec2<f32>,
pub velocity: vec::Vec2, pub velocity: Vec2<f32>,
pub gravity: f32, pub gravity: f32,
pub on_ground: bool pub on_ground: bool
} }
impl Player { impl Player {
pub fn new(x: f32, y: f32) -> Player { pub fn new(x: f32, y: f32) -> Player {
Player { position: vec::Vec2 { x: x, y: y }, velocity: vec::Vec2 { x: 0.0, y: 0.0 }, gravity: 0.3, on_ground: false } Player {
position: Vec2 { x: x, y: y },
velocity: Vec2 { x: 0.0, y: 0.0 },
gravity: 0.3,
on_ground: false
}
} }
pub fn update(&mut self) { pub fn update(&mut self) {
@@ -39,6 +46,6 @@ impl Player {
} }
pub fn get_rect(&self) -> Rect { pub fn get_rect(&self) -> Rect {
Rect::new(self.position.x as i32 - 5, self.position.y as i32 - 5, 10, 10) Rect::new(self.position.x as i32 - 16, self.position.y as i32 - 16, 32, 32)
} }
} }

25
src/tile.rs Normal file
View File

@@ -0,0 +1,25 @@
pub struct Layer<T> {
tiles: Vec<T>,
width: i32
}
impl<T> Layer<T> where T: Clone {
pub fn new(width: i32, height: i32, tile: T) -> Layer<T> {
Layer {
tiles: Vec::from_elem((width * height) as uint, tile),
width: width
}
}
pub fn get_tile(&self, x: i32, y: i32) -> &T {
let offset = (x + y * self.width) as uint;
self.tiles.index(&offset)
}
pub fn set_tile(&mut self, x: i32, y: i32, tile: T) {
let offset = (x + y * self.width) as uint;
*self.tiles.index_mut(&offset) = tile;
}
}

View File

@@ -1,4 +1,4 @@
pub struct Vec2 { pub struct Vec2<T> {
pub x: f32, pub x: T,
pub y: f32, pub y: T
} }