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

View File

@@ -1,5 +1,7 @@
extern crate sdl2;
use std::num::SignedInt;
use std::cmp::{max, min};
use sdl2::video::{Window, WindowPos, OPENGL};
@@ -9,49 +11,55 @@ use sdl2::rect::Rect;
use sdl2::keycode::KeyCode;
use sdl2::pixels::Color;
use vec::Vec2;
use tile::Layer;
use player::Player;
mod vec;
mod tile;
mod player;
const SCREEN_WIDTH : int = 800;
const SCREEN_HEIGHT : int = 600;
const SCREEN_WIDTH : i32 = 960;
const SCREEN_HEIGHT : i32 = 640;
const TILE_WIDTH : i32 = 32;
const TILE_HEIGHT : i32 = 32;
const MS_PER_UPDATE : uint = 10;
struct Object {
position: vec::Vec2,
color: Color,
w: f32,
h: f32,
#[deriving(Clone)]
enum Tile {
Empty,
Floor
}
impl Object {
fn new(x: f32, y: f32, w: f32, h: f32, color: Color) -> Object {
Object {
position: vec::Vec2 { x: x, y: y },
color: color,
w: w,
h: h
struct Camera {
x: i32,
y: i32,
width: i32,
height: i32
}
impl Camera {
fn new(x: i32, y: i32, width: i32, height: i32) -> Camera {
Camera {
x: x,
y: y,
width: width,
height: height
}
}
fn render(&self, renderer: &sdl2::render::Renderer) {
let _ = renderer.set_draw_color(self.color);
let _ = renderer.fill_rect(&self.get_rect());
}
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)
}
}
fn main() {
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,
Err(err) => panic!("failed to create window: {}", err)
};
@@ -61,12 +69,18 @@ fn main() {
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)));
objects.push(Object::new(637.5, 380.0, 325.0, 5.0, Color::RGB(0, 0, 255)));
for x in range(0, 14) {
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 elapsed : uint;
@@ -114,30 +128,39 @@ fn main() {
player.on_ground = false;
for object in objects.iter() {
if collision_detection(&object.get_rect(), &player.get_rect()) {
let intersect = collision_intersect(&object.get_rect(), &player.get_rect());
for y in range(0, 20) {
for x in range(0, 30) {
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 intersect.w >= intersect.h {
let mut delta = intersect.h as f32;
if collision_detection(&object, &player.get_rect()) {
let intersect = collision_intersect(&object, &player.get_rect());
if player.velocity.y >= 0.0 {
delta *= -1.0;
if intersect.w >= intersect.h {
let mut delta = intersect.h as f32;
if player.velocity.y >= 0.0 {
delta *= -1.0;
}
player.position.y += delta;
player.velocity.y = 0.0;
player.on_ground = true;
} else {
let mut delta = intersect.w as f32;
if player.velocity.x >= 0.0 {
delta *= -1.0;
}
player.position.x += delta;
player.velocity.x = 0.0;
}
}
}
player.position.y += delta;
player.velocity.y = 0.0;
player.on_ground = true;
} else {
let mut delta = intersect.w as f32;
if player.velocity.x >= 0.0 {
delta *= -1.0;
}
player.position.x += delta;
player.velocity.x = 0.0;
}
}
}
@@ -148,8 +171,16 @@ fn main() {
let _ = renderer.set_draw_color(Color::RGB(0, 0, 0));
let _ = renderer.clear();
for object in objects.iter() {
object.render(&renderer);
for y in range(0, 20) {
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);

View File

@@ -5,19 +5,26 @@ use sdl2::render::Renderer;
use sdl2::pixels::Color;
use sdl2::rect::Rect;
use vec::Vec2;
use vec;
pub struct Player {
pub position: vec::Vec2,
pub velocity: vec::Vec2,
pub position: Vec2<f32>,
pub velocity: Vec2<f32>,
pub gravity: f32,
pub on_ground: bool
}
impl 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) {
@@ -39,6 +46,6 @@ impl Player {
}
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 x: f32,
pub y: f32,
pub struct Vec2<T> {
pub x: T,
pub y: T
}