Compare commits
2 Commits
6132fa522b
...
648040095d
| Author | SHA1 | Date | |
|---|---|---|---|
| 648040095d | |||
| afdb14ca77 |
56
src/main.rs
56
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 specs::prelude::*;
|
||||||
use std::cmp::{max, min};
|
use std::cmp::{max, min};
|
||||||
mod rect;
|
mod rect;
|
||||||
@ -10,52 +10,60 @@ pub use map::*;
|
|||||||
extern crate specs_derive;
|
extern crate specs_derive;
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
struct Position {
|
pub struct Position {
|
||||||
x: i32,
|
x: i32,
|
||||||
y: i32,
|
y: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
struct Renderable {
|
pub struct Renderable {
|
||||||
glyph: u8,
|
glyph: u8,
|
||||||
fg: RGB,
|
fg: RGB,
|
||||||
bg: RGB,
|
bg: RGB,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct State {
|
pub struct State {
|
||||||
ecs: World
|
ecs: World,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
struct LeftMover {}
|
pub struct LeftMover {}
|
||||||
|
|
||||||
#[derive(Component, Debug)]
|
#[derive(Component, Debug)]
|
||||||
struct Player {}
|
pub struct Player {}
|
||||||
|
|
||||||
fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
|
fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
|
||||||
let mut positions = ecs.write_storage::<Position>();
|
let mut positions = ecs.write_storage::<Position>();
|
||||||
let mut players = ecs.write_storage::<Player>();
|
let mut players = ecs.write_storage::<Player>();
|
||||||
let map = ecs.fetch::<Vec<TileType>>();
|
let map = ecs.fetch::<Map>();
|
||||||
|
|
||||||
for (_player, pos) in (&mut players, &mut positions).join() {
|
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));
|
let y = min(49, max(0, pos.y + delta_y));
|
||||||
if map[xy_idx(x,y)] != TileType::Wall {
|
if map.tile_at(x,y) != TileType::Wall {
|
||||||
pos.x = x;
|
pos.x = x;
|
||||||
pos.y = y;
|
pos.y = y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn player_input(gs: &mut State, ctx: &mut Rltk) {
|
pub fn player_input(gs: &mut State, ctx: &mut Rltk) {
|
||||||
// Player movement
|
// Player movement
|
||||||
match ctx.key {
|
match ctx.key {
|
||||||
None => {} // Nothing happened
|
None => {} // Nothing happened
|
||||||
Some(key) => match key {
|
Some(key) => match key {
|
||||||
VirtualKeyCode::Left => try_move_player(-1, 0, &mut gs.ecs),
|
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::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::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::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,17 +72,17 @@ fn player_input(gs: &mut State, ctx: &mut Rltk) {
|
|||||||
struct LeftWalker {}
|
struct LeftWalker {}
|
||||||
|
|
||||||
impl<'a> System<'a> for LeftWalker {
|
impl<'a> System<'a> for LeftWalker {
|
||||||
type SystemData = (ReadStorage<'a, LeftMover>,
|
type SystemData = (ReadStorage<'a, LeftMover>, WriteStorage<'a, Position>);
|
||||||
WriteStorage<'a, Position>);
|
|
||||||
|
|
||||||
fn run(&mut self, (lefty, mut pos): Self::SystemData) {
|
fn run(&mut self, (lefty, mut pos): Self::SystemData) {
|
||||||
for (_lefty, pos) in (&lefty, &mut pos).join() {
|
for (_lefty, pos) in (&lefty, &mut pos).join() {
|
||||||
pos.x -= 1;
|
pos.x -= 1;
|
||||||
if pos.x < 0 { pos.x = 79; }
|
if pos.x < 0 {
|
||||||
|
pos.x = 79;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
fn run_systems(&mut self) {
|
fn run_systems(&mut self) {
|
||||||
@ -89,8 +97,8 @@ impl GameState for State {
|
|||||||
ctx.cls();
|
ctx.cls();
|
||||||
self.run_systems();
|
self.run_systems();
|
||||||
player_input(self, ctx);
|
player_input(self, ctx);
|
||||||
let map = self.ecs.fetch::<Vec<TileType>>();
|
let map = self.ecs.fetch::<Map>();
|
||||||
draw_map(&map, ctx);
|
map.draw_map(ctx);
|
||||||
let positions = self.ecs.read_storage::<Position>();
|
let positions = self.ecs.read_storage::<Position>();
|
||||||
let renderables = self.ecs.read_storage::<Renderable>();
|
let renderables = self.ecs.read_storage::<Renderable>();
|
||||||
|
|
||||||
@ -105,9 +113,10 @@ fn main() {
|
|||||||
let context = RltkBuilder::simple80x50()
|
let context = RltkBuilder::simple80x50()
|
||||||
.with_title("Roguelike Tutorial")
|
.with_title("Roguelike Tutorial")
|
||||||
.build();
|
.build();
|
||||||
let mut gs = State {
|
let mut gs = State { ecs: World::new() };
|
||||||
ecs: World::new()
|
let map = Map::new_map_rooms_and_corridors();
|
||||||
};
|
let (player_x, player_y) = map.rooms[0].center();
|
||||||
|
|
||||||
gs.ecs.register::<Position>();
|
gs.ecs.register::<Position>();
|
||||||
gs.ecs.register::<Renderable>();
|
gs.ecs.register::<Renderable>();
|
||||||
gs.ecs.register::<LeftMover>();
|
gs.ecs.register::<LeftMover>();
|
||||||
@ -115,7 +124,10 @@ fn main() {
|
|||||||
|
|
||||||
gs.ecs
|
gs.ecs
|
||||||
.create_entity()
|
.create_entity()
|
||||||
.with(Position { x: 40, y: 25 })
|
.with(Position {
|
||||||
|
x: player_x,
|
||||||
|
y: player_y,
|
||||||
|
})
|
||||||
.with(Renderable {
|
.with(Renderable {
|
||||||
glyph: rltk::to_cp437('@'),
|
glyph: rltk::to_cp437('@'),
|
||||||
fg: RGB::named(rltk::YELLOW),
|
fg: RGB::named(rltk::YELLOW),
|
||||||
@ -136,9 +148,7 @@ fn main() {
|
|||||||
.with(LeftMover {})
|
.with(LeftMover {})
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
gs.ecs.insert(map);
|
||||||
gs.ecs.insert(new_map_rooms_and_corridors());
|
|
||||||
|
|
||||||
rltk::main_loop(context, gs);
|
rltk::main_loop(context, gs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
87
src/map.rs
87
src/map.rs
@ -8,63 +8,53 @@ pub enum TileType {
|
|||||||
Floor,
|
Floor,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn xy_idx(x: i32, y: i32) -> usize {
|
pub struct Map {
|
||||||
(y as usize * 80) + x as usize
|
pub tiles: Vec<TileType>,
|
||||||
|
pub rooms: Vec<Rect>,
|
||||||
|
pub width: i32,
|
||||||
|
pub height: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_map_test() -> Vec<TileType> {
|
impl Map {
|
||||||
let mut map = vec![TileType::Floor; 80 * 50];
|
pub fn xy_idx(&self, x: i32, y: i32) -> usize {
|
||||||
|
((y * self.width) + x) as usize
|
||||||
// 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.
|
pub fn tile_at(&self, x: i32, y:i32) -> TileType {
|
||||||
// First, obtain the thread-local RNG:
|
self.tiles[self.xy_idx(x, y)]
|
||||||
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(&mut self, room: &Rect) {
|
||||||
}
|
|
||||||
|
|
||||||
fn apply_room_to_map(room: &Rect, map: &mut Vec<TileType>) {
|
|
||||||
for x in room.x1..room.x2 {
|
for x in room.x1..room.x2 {
|
||||||
for y in room.y1..room.y2 {
|
for y in room.y1..room.y2 {
|
||||||
map[xy_idx(x, y)] = TileType::Floor
|
let idx = self.xy_idx(x,y);
|
||||||
|
self.tiles[idx] = TileType::Floor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn apply_horizontal_tunnel(map: &mut Vec<TileType>, x1: i32, x2: i32, y: i32) {
|
fn apply_horizontal_tunnel(&mut self, x1: i32, x2: i32, y: i32) {
|
||||||
for x in min(x1, x2)..=max(x1, x2) {
|
for x in min(x1, x2)..=max(x1, x2) {
|
||||||
map[xy_idx(x, y)] = TileType::Floor
|
let idx=self.xy_idx(x, y);
|
||||||
|
self.tiles[idx] = TileType::Floor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn apply_vertical_tunnel(map: &mut Vec<TileType>, x: i32, y1: i32, y2: i32) {
|
fn apply_vertical_tunnel(&mut self, x: i32, y1: i32, y2: i32) {
|
||||||
for y in min(y1, y2)..=max(y1, y2) {
|
for y in min(y1, y2)..=max(y1, y2) {
|
||||||
map[xy_idx(x, y)] = TileType::Floor
|
let idx=self.xy_idx(x, y);
|
||||||
|
self.tiles[idx] = TileType::Floor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_map_rooms_and_corridors() -> Vec<TileType> {
|
pub fn new_map_rooms_and_corridors() -> Map {
|
||||||
let mut map = vec![TileType::Wall; 80 * 50];
|
let mut map = Map {
|
||||||
|
tiles: vec![TileType::Wall; 80 * 50],
|
||||||
|
rooms: Vec::new(),
|
||||||
|
width: 80,
|
||||||
|
height: 50,
|
||||||
|
};
|
||||||
|
|
||||||
let mut rooms: Vec<Rect> = Vec::new();
|
|
||||||
const MAX_ROOMS: i32 = 30;
|
const MAX_ROOMS: i32 = 30;
|
||||||
const MIN_SIZE: i32 = 6;
|
const MIN_SIZE: i32 = 6;
|
||||||
const MAX_SIZE: i32 = 10;
|
const MAX_SIZE: i32 = 10;
|
||||||
@ -78,45 +68,43 @@ pub fn new_map_rooms_and_corridors() -> Vec<TileType> {
|
|||||||
let y = rng.roll_dice(1, 50 - h - 1) - 1;
|
let y = rng.roll_dice(1, 50 - h - 1) - 1;
|
||||||
let new_room = Rect::new(x, y, w, h);
|
let new_room = Rect::new(x, y, w, h);
|
||||||
let mut ok = true;
|
let mut ok = true;
|
||||||
for other_room in rooms.iter() {
|
for other_room in map.rooms.iter() {
|
||||||
if new_room.intersect(other_room) {
|
if new_room.intersect(other_room) {
|
||||||
ok = false
|
ok = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ok {
|
if ok {
|
||||||
apply_room_to_map(&new_room, &mut map);
|
map.apply_room_to_map(&new_room);
|
||||||
if rooms.len() > 0 {
|
if map.rooms.len() > 0 {
|
||||||
let r1_center = new_room.center();
|
let r1_center = new_room.center();
|
||||||
let r2_center = rooms[rooms.len() - 1].center();
|
let r2_center = map.rooms[map.rooms.len() - 1].center();
|
||||||
if rng.range(0, 2) == 1 {
|
if rng.range(0, 2) == 1 {
|
||||||
apply_horizontal_tunnel(&mut map, r1_center.0, r2_center.0, r1_center.1);
|
map.apply_horizontal_tunnel(r1_center.0, r2_center.0, r1_center.1);
|
||||||
apply_vertical_tunnel(
|
map.apply_vertical_tunnel(
|
||||||
&mut map,
|
|
||||||
max(r1_center.0, r2_center.0),
|
max(r1_center.0, r2_center.0),
|
||||||
r1_center.1,
|
r1_center.1,
|
||||||
r2_center.1,
|
r2_center.1,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
apply_vertical_tunnel(&mut map, r1_center.0, r1_center.1, r2_center.1);
|
map.apply_vertical_tunnel(r1_center.0, r1_center.1, r2_center.1);
|
||||||
apply_horizontal_tunnel(
|
map.apply_horizontal_tunnel(
|
||||||
&mut map,
|
|
||||||
r1_center.0,
|
r1_center.0,
|
||||||
r2_center.0,
|
r2_center.0,
|
||||||
max(r1_center.1, r2_center.1),
|
max(r1_center.1, r2_center.1),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rooms.push(new_room);
|
map.rooms.push(new_room);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
map
|
map
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw_map(map: &[TileType], ctx: &mut Rltk) {
|
pub fn draw_map(&self, ctx: &mut Rltk) {
|
||||||
let mut y = 0;
|
let mut y = 0;
|
||||||
let mut x = 0;
|
let mut x = 0;
|
||||||
for tile in map.iter() {
|
for tile in self.tiles.iter() {
|
||||||
// Render a tile depending upon the tile type
|
// Render a tile depending upon the tile type
|
||||||
match tile {
|
match tile {
|
||||||
TileType::Floor => {
|
TileType::Floor => {
|
||||||
@ -147,3 +135,4 @@ pub fn draw_map(map: &[TileType], ctx: &mut Rltk) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user