moved xy_idx() to map.rs
This commit is contained in:
parent
ffdc1fb1cf
commit
ff6f7ff7b2
|
@ -56,7 +56,3 @@ fn main() {
|
|||
|
||||
rltk::main_loop(context, gs);
|
||||
}
|
||||
|
||||
fn xy_idx(x: i32, y: i32) -> usize {
|
||||
(y as usize * 80) + x as usize
|
||||
}
|
||||
|
|
16
src/map.rs
16
src/map.rs
|
@ -2,17 +2,21 @@ use rltk::{Console, Rltk, RGB};
|
|||
|
||||
use crate::entity::TileType;
|
||||
|
||||
pub fn xy_idx(x: i32, y: i32) -> usize {
|
||||
(y as usize * 80) + x as usize
|
||||
}
|
||||
|
||||
pub fn new() -> Vec<TileType> {
|
||||
let mut map = vec![TileType::Floor; 80 * 50];
|
||||
|
||||
(0..80).into_iter().for_each(|x| {
|
||||
map[crate::xy_idx(x, 0)] = TileType::Wall;
|
||||
map[crate::xy_idx(x, 49)] = TileType::Wall;
|
||||
map[xy_idx(x, 0)] = TileType::Wall;
|
||||
map[xy_idx(x, 49)] = TileType::Wall;
|
||||
});
|
||||
|
||||
(0..50).into_iter().for_each(|y| {
|
||||
map[crate::xy_idx(0, y)] = TileType::Wall;
|
||||
map[crate::xy_idx(79, y)] = TileType::Wall;
|
||||
map[xy_idx(0, y)] = TileType::Wall;
|
||||
map[xy_idx(79, y)] = TileType::Wall;
|
||||
});
|
||||
|
||||
//rando-splat of walls
|
||||
|
@ -21,8 +25,8 @@ pub fn new() -> Vec<TileType> {
|
|||
(0..400).into_iter().for_each(|_| {
|
||||
let x = rng.roll_dice(1, 79);
|
||||
let y = rng.roll_dice(1, 49);
|
||||
let idx = crate::xy_idx(x, y);
|
||||
if idx != crate::xy_idx(40, 25) {
|
||||
let idx = xy_idx(x, y);
|
||||
if idx != xy_idx(40, 25) {
|
||||
map[idx] = TileType::Wall;
|
||||
}
|
||||
});
|
||||
|
|
|
@ -2,6 +2,7 @@ use rltk::{Rltk, VirtualKeyCode};
|
|||
use specs::prelude::*;
|
||||
|
||||
use crate::entity::{Player, Position, TileType};
|
||||
use crate::map;
|
||||
use crate::player;
|
||||
use crate::state::State;
|
||||
|
||||
|
@ -12,7 +13,7 @@ pub fn try_move(dx: i32, dy: i32, ecs: &mut World) {
|
|||
|
||||
(&mut players, &mut posns).join().into_iter().for_each(
|
||||
|(_player, pos)| {
|
||||
let dest_idx = crate::xy_idx(pos.x + dx, pos.y + dy);
|
||||
let dest_idx = map::xy_idx(pos.x + dx, pos.y + dy);
|
||||
if map[dest_idx] != TileType::Wall {
|
||||
pos.x += dx;
|
||||
pos.y += dy;
|
||||
|
|
Loading…
Reference in New Issue