locate player in room
This commit is contained in:
parent
6132fa522b
commit
afdb14ca77
62
src/main.rs
62
src/main.rs
@ -1,4 +1,4 @@
|
||||
use rltk::{Console, GameState, Rltk, RGB, VirtualKeyCode};
|
||||
use rltk::{Console, GameState, Rltk, VirtualKeyCode, RGB};
|
||||
use specs::prelude::*;
|
||||
use std::cmp::{max, min};
|
||||
mod rect;
|
||||
@ -10,27 +10,27 @@ pub use map::*;
|
||||
extern crate specs_derive;
|
||||
|
||||
#[derive(Component)]
|
||||
struct Position {
|
||||
pub struct Position {
|
||||
x: i32,
|
||||
y: i32,
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
struct Renderable {
|
||||
pub struct Renderable {
|
||||
glyph: u8,
|
||||
fg: RGB,
|
||||
bg: RGB,
|
||||
}
|
||||
|
||||
struct State {
|
||||
ecs: World
|
||||
pub struct State {
|
||||
ecs: World,
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
struct LeftMover {}
|
||||
pub struct LeftMover {}
|
||||
|
||||
#[derive(Component, Debug)]
|
||||
struct Player {}
|
||||
pub struct Player {}
|
||||
|
||||
fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
|
||||
let mut positions = ecs.write_storage::<Position>();
|
||||
@ -38,24 +38,32 @@ fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
|
||||
let map = ecs.fetch::<Vec<TileType>>();
|
||||
|
||||
for (_player, pos) in (&mut players, &mut positions).join() {
|
||||
let x = min(79 , max(0, pos.x + delta_x));
|
||||
let x = min(79, max(0, pos.x + delta_x));
|
||||
let y = min(49, max(0, pos.y + delta_y));
|
||||
if map[xy_idx(x,y)] != TileType::Wall {
|
||||
if map[xy_idx(x, y)] != TileType::Wall {
|
||||
pos.x = x;
|
||||
pos.y = y;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn player_input(gs: &mut State, ctx: &mut Rltk) {
|
||||
pub fn player_input(gs: &mut State, ctx: &mut Rltk) {
|
||||
// Player movement
|
||||
match ctx.key {
|
||||
None => {} // Nothing happened
|
||||
Some(key) => match key {
|
||||
VirtualKeyCode::Left => try_move_player(-1, 0, &mut gs.ecs),
|
||||
VirtualKeyCode::Numpad4 => try_move_player(-1, 0, &mut gs.ecs),
|
||||
VirtualKeyCode::H => try_move_player(-1, 0, &mut gs.ecs),
|
||||
VirtualKeyCode::Right => try_move_player(1, 0, &mut gs.ecs),
|
||||
VirtualKeyCode::Numpad6 => try_move_player(1, 0, &mut gs.ecs),
|
||||
VirtualKeyCode::L => try_move_player(1, 0, &mut gs.ecs),
|
||||
VirtualKeyCode::Up => try_move_player(0, -1, &mut gs.ecs),
|
||||
VirtualKeyCode::Numpad8 => try_move_player(0, -1, &mut gs.ecs),
|
||||
VirtualKeyCode::K => try_move_player(0, -1, &mut gs.ecs),
|
||||
VirtualKeyCode::Down => try_move_player(0, 1, &mut gs.ecs),
|
||||
VirtualKeyCode::Numpad2 => try_move_player(0, 1, &mut gs.ecs),
|
||||
VirtualKeyCode::J => try_move_player(0, 1, &mut gs.ecs),
|
||||
_ => {}
|
||||
},
|
||||
}
|
||||
@ -64,28 +72,28 @@ fn player_input(gs: &mut State, ctx: &mut Rltk) {
|
||||
struct LeftWalker {}
|
||||
|
||||
impl<'a> System<'a> for LeftWalker {
|
||||
type SystemData = (ReadStorage<'a, LeftMover>,
|
||||
WriteStorage<'a, Position>);
|
||||
type SystemData = (ReadStorage<'a, LeftMover>, WriteStorage<'a, Position>);
|
||||
|
||||
fn run(&mut self, (lefty, mut pos) : Self::SystemData) {
|
||||
for (_lefty,pos) in (&lefty, &mut pos).join() {
|
||||
fn run(&mut self, (lefty, mut pos): Self::SystemData) {
|
||||
for (_lefty, pos) in (&lefty, &mut pos).join() {
|
||||
pos.x -= 1;
|
||||
if pos.x < 0 { pos.x = 79; }
|
||||
if pos.x < 0 {
|
||||
pos.x = 79;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl State {
|
||||
fn run_systems(&mut self) {
|
||||
let mut lw = LeftWalker{};
|
||||
let mut lw = LeftWalker {};
|
||||
lw.run_now(&self.ecs);
|
||||
self.ecs.maintain();
|
||||
}
|
||||
}
|
||||
|
||||
impl GameState for State {
|
||||
fn tick(&mut self, ctx : &mut Rltk) {
|
||||
fn tick(&mut self, ctx: &mut Rltk) {
|
||||
ctx.cls();
|
||||
self.run_systems();
|
||||
player_input(self, ctx);
|
||||
@ -105,9 +113,10 @@ fn main() {
|
||||
let context = RltkBuilder::simple80x50()
|
||||
.with_title("Roguelike Tutorial")
|
||||
.build();
|
||||
let mut gs = State {
|
||||
ecs: World::new()
|
||||
};
|
||||
let mut gs = State { ecs: World::new() };
|
||||
let (rooms, map) = new_map_rooms_and_corridors();
|
||||
let (player_x, player_y) = rooms[0].center();
|
||||
|
||||
gs.ecs.register::<Position>();
|
||||
gs.ecs.register::<Renderable>();
|
||||
gs.ecs.register::<LeftMover>();
|
||||
@ -115,13 +124,16 @@ fn main() {
|
||||
|
||||
gs.ecs
|
||||
.create_entity()
|
||||
.with(Position { x: 40, y: 25 })
|
||||
.with(Position {
|
||||
x: player_x,
|
||||
y: player_y,
|
||||
})
|
||||
.with(Renderable {
|
||||
glyph: rltk::to_cp437('@'),
|
||||
fg: RGB::named(rltk::YELLOW),
|
||||
bg: RGB::named(rltk::BLACK),
|
||||
})
|
||||
.with(Player{})
|
||||
.with(Player {})
|
||||
.build();
|
||||
|
||||
for i in 0..10 {
|
||||
@ -137,8 +149,8 @@ fn main() {
|
||||
.build();
|
||||
}
|
||||
|
||||
gs.ecs.insert(new_map_rooms_and_corridors());
|
||||
gs.ecs.insert(rooms);
|
||||
gs.ecs.insert(map);
|
||||
|
||||
rltk::main_loop(context, gs);
|
||||
}
|
||||
|
||||
|
||||
33
src/map.rs
33
src/map.rs
@ -12,35 +12,6 @@ pub fn xy_idx(x: i32, y: i32) -> usize {
|
||||
(y as usize * 80) + x as usize
|
||||
}
|
||||
|
||||
fn new_map_test() -> Vec<TileType> {
|
||||
let mut map = vec![TileType::Floor; 80 * 50];
|
||||
|
||||
// Make the boundaries walls
|
||||
for x in 0..80 {
|
||||
map[xy_idx(x, 0)] = TileType::Wall;
|
||||
map[xy_idx(x, 49)] = TileType::Wall;
|
||||
}
|
||||
for y in 0..50 {
|
||||
map[xy_idx(0, y)] = TileType::Wall;
|
||||
map[xy_idx(79, y)] = TileType::Wall;
|
||||
}
|
||||
|
||||
// Now we'll randomly splat a bunch of walls. It won't be pretty, but it's a decent illustration.
|
||||
// First, obtain the thread-local RNG:
|
||||
let mut rng = rltk::RandomNumberGenerator::new();
|
||||
|
||||
for _i in 0..400 {
|
||||
let x = rng.roll_dice(1, 79);
|
||||
let y = rng.roll_dice(1, 49);
|
||||
let idx = xy_idx(x, y);
|
||||
if idx != xy_idx(40, 25) {
|
||||
map[idx] = TileType::Wall;
|
||||
}
|
||||
}
|
||||
|
||||
map
|
||||
}
|
||||
|
||||
fn apply_room_to_map(room: &Rect, map: &mut Vec<TileType>) {
|
||||
for x in room.x1..room.x2 {
|
||||
for y in room.y1..room.y2 {
|
||||
@ -61,7 +32,7 @@ fn apply_vertical_tunnel(map: &mut Vec<TileType>, x: i32, y1: i32, y2: i32) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_map_rooms_and_corridors() -> Vec<TileType> {
|
||||
pub fn new_map_rooms_and_corridors() -> (Vec<Rect>,Vec<TileType>) {
|
||||
let mut map = vec![TileType::Wall; 80 * 50];
|
||||
|
||||
let mut rooms: Vec<Rect> = Vec::new();
|
||||
@ -110,7 +81,7 @@ pub fn new_map_rooms_and_corridors() -> Vec<TileType> {
|
||||
}
|
||||
}
|
||||
|
||||
map
|
||||
(rooms,map)
|
||||
}
|
||||
|
||||
pub fn draw_map(map: &[TileType], ctx: &mut Rltk) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user