Added map gen
This commit is contained in:
parent
41aaa1fffe
commit
0868308064
49
src/main.rs
49
src/main.rs
@ -1,7 +1,7 @@
|
||||
use rltk::{GameState, Rltk, VirtualConsole, VirtualKeyCode, RGB};
|
||||
use specs::prelude::*;
|
||||
use specs_derive::Component;
|
||||
use std::{cmp::{max, min}, sync::TryLockResult};
|
||||
use std::cmp::{max, min};
|
||||
|
||||
struct State {
|
||||
ecs: World,
|
||||
@ -13,6 +13,9 @@ impl GameState for State {
|
||||
// self.run_systems();
|
||||
player_input(self, ctx);
|
||||
|
||||
let map = self.ecs.fetch::<Vec<TileType>>();
|
||||
draw_map(&map, ctx);
|
||||
|
||||
let positions = self.ecs.read_storage::<Position>();
|
||||
let renderables = self.ecs.read_storage::<Renderable>();
|
||||
|
||||
@ -41,11 +44,15 @@ struct Renderable {
|
||||
fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
|
||||
let mut positions = ecs.write_storage::<Position>();
|
||||
let mut players = ecs.write_storage::<Player>();
|
||||
let map = ecs.fetch::<Vec<TileType>>();
|
||||
|
||||
for (_player, pos) in (&mut players, &mut positions).join() {
|
||||
let destination_idx = xy_idx(pos.x + delta_x, pos.y + delta_y);
|
||||
if map[destination_idx] != TileType::Wall {
|
||||
pos.x = min(79, max(0, pos.x + delta_x));
|
||||
pos.y = min(49, max(0, pos.y + delta_y));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn player_input(gs: &mut State, ctx: &mut Rltk) {
|
||||
@ -63,7 +70,8 @@ fn player_input(gs: &mut State, ctx: &mut Rltk) {
|
||||
|
||||
#[derive(PartialEq, Copy, Clone)]
|
||||
enum TileType {
|
||||
Wall, Floor
|
||||
Wall,
|
||||
Floor,
|
||||
}
|
||||
|
||||
pub fn xy_idx(x: i32, y: i32) -> usize {
|
||||
@ -71,7 +79,7 @@ pub fn xy_idx(x: i32, y: i32) -> usize {
|
||||
}
|
||||
|
||||
fn new_map() -> Vec<TileType> {
|
||||
let mut map = vec![TileType::Floor; 80*50];
|
||||
let mut map = vec![TileType::Floor; 80 * 50];
|
||||
|
||||
for x in 0..80 {
|
||||
map[xy_idx(x, 0)] = TileType::Wall;
|
||||
@ -96,6 +104,39 @@ fn new_map() -> Vec<TileType> {
|
||||
map
|
||||
}
|
||||
|
||||
fn draw_map(map: &[TileType], ctx: &mut Rltk) {
|
||||
let mut y = 0;
|
||||
let mut x = 0;
|
||||
for tile in map.iter() {
|
||||
match tile {
|
||||
TileType::Floor => {
|
||||
ctx.set(
|
||||
x,
|
||||
y,
|
||||
RGB::from_f32(0.5, 0.5, 0.5),
|
||||
RGB::from_f32(0., 0., 0.),
|
||||
rltk::to_cp437('.'),
|
||||
);
|
||||
}
|
||||
TileType::Wall => {
|
||||
ctx.set(
|
||||
x,
|
||||
y,
|
||||
RGB::from_f32(0.0, 1.0, 0.0),
|
||||
RGB::from_f32(0., 0., 0.),
|
||||
rltk::to_cp437('#'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
x += 1;
|
||||
if x > 79 {
|
||||
x = 0;
|
||||
y += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> rltk::BError {
|
||||
use rltk::RltkBuilder;
|
||||
let context = RltkBuilder::simple80x50()
|
||||
@ -109,7 +150,7 @@ fn main() -> rltk::BError {
|
||||
|
||||
gs.ecs
|
||||
.create_entity()
|
||||
.with(Position { x: 40, y: 35 })
|
||||
.with(Position { x: 40, y: 25 })
|
||||
.with(Renderable {
|
||||
glyph: rltk::to_cp437('@'),
|
||||
fg: RGB::named(rltk::YELLOW),
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user