sync lappy
This commit is contained in:
parent
64b772cec1
commit
67c7ad77ce
@ -1,7 +1,6 @@
|
|||||||
|
use rltk::RGB;
|
||||||
use specs::prelude::*;
|
use specs::prelude::*;
|
||||||
use specs_derive::Component;
|
use specs_derive::Component;
|
||||||
use rltk::{RGB};
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Position {
|
pub struct Position {
|
||||||
@ -21,10 +20,21 @@ pub struct Player {}
|
|||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Viewshed {
|
pub struct Viewshed {
|
||||||
pub visible_tiles : Vec<rltk::Point>,
|
pub visible_tiles: Vec<rltk::Point>,
|
||||||
pub range : i32,
|
pub range: i32,
|
||||||
pub dirty : bool
|
pub dirty: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component, Debug)]
|
#[derive(Component, Debug)]
|
||||||
pub struct Monster {}
|
pub struct Monster {}
|
||||||
|
|
||||||
|
#[derive(Component, Debug)]
|
||||||
|
pub struct Name {
|
||||||
|
pub name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq, Copy, Clone)]
|
||||||
|
pub enum RunState {
|
||||||
|
Paused,
|
||||||
|
Running,
|
||||||
|
}
|
||||||
|
|||||||
75
src/main.rs
75
src/main.rs
@ -1,4 +1,4 @@
|
|||||||
use rltk::{GameState, Rltk, RGB};
|
use rltk::{GameState, Point, Rltk, RGB};
|
||||||
use specs::prelude::*;
|
use specs::prelude::*;
|
||||||
|
|
||||||
mod components;
|
mod components;
|
||||||
@ -16,14 +16,20 @@ pub use rect::Rect;
|
|||||||
mod visibility_system;
|
mod visibility_system;
|
||||||
pub use visibility_system::*;
|
pub use visibility_system::*;
|
||||||
|
|
||||||
|
mod monster_ai_system;
|
||||||
|
pub use monster_ai_system::*;
|
||||||
|
|
||||||
pub struct State {
|
pub struct State {
|
||||||
pub ecs: World,
|
pub ecs: World,
|
||||||
|
pub runstate: RunState,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
fn run_systems(&mut self) {
|
fn run_systems(&mut self) {
|
||||||
let mut vis = VisibilitySystem{};
|
let mut vis = VisibilitySystem {};
|
||||||
vis.run_now(&self.ecs);
|
vis.run_now(&self.ecs);
|
||||||
|
let mut mob = MonsterAI {};
|
||||||
|
mob.run_now(&self.ecs);
|
||||||
self.ecs.maintain();
|
self.ecs.maintain();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -32,8 +38,12 @@ impl GameState for State {
|
|||||||
fn tick(&mut self, ctx: &mut Rltk) {
|
fn tick(&mut self, ctx: &mut Rltk) {
|
||||||
ctx.cls();
|
ctx.cls();
|
||||||
|
|
||||||
player_input(self, ctx);
|
if self.runstate == RunState::Running {
|
||||||
self.run_systems();
|
self.run_systems();
|
||||||
|
self.runstate = RunState::Paused;
|
||||||
|
} else {
|
||||||
|
self.runstate = player_input(self, ctx);
|
||||||
|
}
|
||||||
|
|
||||||
// let map = self.ecs.fetch::<Map>();
|
// let map = self.ecs.fetch::<Map>();
|
||||||
draw_map(&self.ecs, ctx);
|
draw_map(&self.ecs, ctx);
|
||||||
@ -44,7 +54,9 @@ impl GameState for State {
|
|||||||
|
|
||||||
for (pos, render) in (&positions, &renderables).join() {
|
for (pos, render) in (&positions, &renderables).join() {
|
||||||
let idx = map.xy_idx(pos.x, pos.y);
|
let idx = map.xy_idx(pos.x, pos.y);
|
||||||
if map.visible_tiles[idx] {ctx.set(pos.x, pos.y, render.fg, render.bg, render.glyph) }
|
if map.visible_tiles[idx] {
|
||||||
|
ctx.set(pos.x, pos.y, render.fg, render.bg, render.glyph)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -54,17 +66,21 @@ fn main() -> rltk::BError {
|
|||||||
let context = RltkBuilder::simple80x50()
|
let context = RltkBuilder::simple80x50()
|
||||||
.with_title("Rougelike Tutorial")
|
.with_title("Rougelike Tutorial")
|
||||||
.build()?;
|
.build()?;
|
||||||
let mut gs = State { ecs: World::new() };
|
let mut gs = State {
|
||||||
|
ecs: World::new(),
|
||||||
|
runstate: RunState::Running,
|
||||||
|
};
|
||||||
gs.ecs.register::<Position>();
|
gs.ecs.register::<Position>();
|
||||||
gs.ecs.register::<Renderable>();
|
gs.ecs.register::<Renderable>();
|
||||||
gs.ecs.register::<Player>();
|
gs.ecs.register::<Player>();
|
||||||
gs.ecs.register::<Viewshed>();
|
gs.ecs.register::<Viewshed>();
|
||||||
gs.ecs.register::<Monster>();
|
gs.ecs.register::<Monster>();
|
||||||
|
gs.ecs.register::<Name>();
|
||||||
|
|
||||||
let map = Map::new_map_rooms_and_corridors();
|
let map = Map::new_map_rooms_and_corridors();
|
||||||
let (player_x, player_y) = map.rooms[0].center();
|
let (player_x, player_y) = map.rooms[0].center();
|
||||||
|
|
||||||
|
gs.ecs.insert(Point::new(player_x, player_y));
|
||||||
gs.ecs
|
gs.ecs
|
||||||
.create_entity()
|
.create_entity()
|
||||||
.with(Position {
|
.with(Position {
|
||||||
@ -77,34 +93,55 @@ fn main() -> rltk::BError {
|
|||||||
bg: RGB::named(rltk::BLACK),
|
bg: RGB::named(rltk::BLACK),
|
||||||
})
|
})
|
||||||
.with(Player {})
|
.with(Player {})
|
||||||
.with(Viewshed{ visible_tiles : Vec::new(), range : 8, dirty : true})
|
.with(Viewshed {
|
||||||
|
visible_tiles: Vec::new(),
|
||||||
|
range: 8,
|
||||||
|
dirty: true,
|
||||||
|
})
|
||||||
|
.with(Name {
|
||||||
|
name: "Player".to_string(),
|
||||||
|
})
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let mut rng = rltk::RandomNumberGenerator::new();
|
let mut rng = rltk::RandomNumberGenerator::new();
|
||||||
for room in map.rooms.iter().skip(1) {
|
for (i, room) in map.rooms.iter().skip(1).enumerate() {
|
||||||
let (x,y) = room.center();
|
let (x, y) = room.center();
|
||||||
|
|
||||||
let glyph : rltk::FontCharType;
|
let glyph: rltk::FontCharType;
|
||||||
|
let name: String;
|
||||||
let roll = rng.roll_dice(1, 2);
|
let roll = rng.roll_dice(1, 2);
|
||||||
match roll {
|
match roll {
|
||||||
1 => { glyph = rltk::to_cp437('g') },
|
1 => {
|
||||||
_ => { glyph = rltk::to_cp437('o') }
|
glyph = rltk::to_cp437('g');
|
||||||
|
name = "Goblin".to_string()
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
glyph = rltk::to_cp437('o');
|
||||||
|
name = "Orc".to_string()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
gs.ecs.create_entity()
|
gs.ecs
|
||||||
.with(Position{ x, y })
|
.create_entity()
|
||||||
.with(Renderable{
|
.with(Position { x, y })
|
||||||
|
.with(Renderable {
|
||||||
glyph: glyph,
|
glyph: glyph,
|
||||||
fg: RGB::named(rltk::RED),
|
fg: RGB::named(rltk::RED),
|
||||||
bg: RGB::named(rltk::BLACK),
|
bg: RGB::named(rltk::BLACK),
|
||||||
})
|
})
|
||||||
.with(Viewshed{ visible_tiles : Vec::new(), range: 8, dirty: true})
|
.with(Viewshed {
|
||||||
|
visible_tiles: Vec::new(),
|
||||||
|
range: 8,
|
||||||
|
dirty: true,
|
||||||
|
})
|
||||||
|
.with(Monster {})
|
||||||
|
.with(Name {
|
||||||
|
name: format!("{} #{}", &name, i),
|
||||||
|
})
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
}
|
}
|
||||||
gs.ecs.insert(map);
|
gs.ecs.insert(map);
|
||||||
|
|
||||||
// Main loop runner
|
// Main loop runner
|
||||||
rltk::main_loop(context, gs)
|
rltk::main_loop(context, gs)
|
||||||
}
|
}
|
||||||
|
|
||||||
86
src/map.rs
86
src/map.rs
@ -1,7 +1,7 @@
|
|||||||
use super::{Rect};
|
use super::Rect;
|
||||||
use rltk::{RandomNumberGenerator, Rltk, RGB, Algorithm2D, Point, BaseMap};
|
use rltk::{Algorithm2D, BaseMap, Point, RandomNumberGenerator, Rltk, SmallVec, RGB};
|
||||||
use std::{cmp::{max, min}};
|
|
||||||
use specs::prelude::*;
|
use specs::prelude::*;
|
||||||
|
use std::cmp::{max, min};
|
||||||
|
|
||||||
#[derive(PartialEq, Copy, Clone)]
|
#[derive(PartialEq, Copy, Clone)]
|
||||||
pub enum TileType {
|
pub enum TileType {
|
||||||
@ -11,12 +11,12 @@ pub enum TileType {
|
|||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Map {
|
pub struct Map {
|
||||||
pub tiles : Vec::<TileType>,
|
pub tiles: Vec<TileType>,
|
||||||
pub rooms : Vec::<Rect>,
|
pub rooms: Vec<Rect>,
|
||||||
pub width : i32,
|
pub width: i32,
|
||||||
pub height : i32,
|
pub height: i32,
|
||||||
pub revealed_tiles : Vec<bool>,
|
pub revealed_tiles: Vec<bool>,
|
||||||
pub visible_tiles : Vec<bool>
|
pub visible_tiles: Vec<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Map {
|
impl Map {
|
||||||
@ -52,13 +52,13 @@ impl Map {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_map_rooms_and_corridors() -> Map {
|
pub fn new_map_rooms_and_corridors() -> Map {
|
||||||
let mut map = Map{
|
let mut map = Map {
|
||||||
tiles : vec![TileType::Wall; 80*50],
|
tiles: vec![TileType::Wall; 80 * 50],
|
||||||
rooms : Vec::new(),
|
rooms: Vec::new(),
|
||||||
width : 80,
|
width: 80,
|
||||||
height : 50,
|
height: 50,
|
||||||
revealed_tiles : vec![false; 80*50],
|
revealed_tiles: vec![false; 80 * 50],
|
||||||
visible_tiles : vec![false; 80*50]
|
visible_tiles: vec![false; 80 * 50],
|
||||||
};
|
};
|
||||||
|
|
||||||
const MAX_ROOMS: i32 = 30;
|
const MAX_ROOMS: i32 = 30;
|
||||||
@ -99,6 +99,43 @@ impl Map {
|
|||||||
map
|
map
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_exit_valid(&self, x: i32, y: i32) -> bool {
|
||||||
|
if x < 1 || x > self.width - 1 || y < 1 || y > self.height - 1 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
let idx = self.xy_idx(x, y);
|
||||||
|
self.tiles[idx as usize] != TileType::Wall
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_available_exits(&self, idx: usize) -> rltk::SmallVec<[(usize, f32); 10]> {
|
||||||
|
let mut exits = rltk::SmallVec::new();
|
||||||
|
let x = idx as i32 % self.width;
|
||||||
|
let y = idx as i32 / self.width;
|
||||||
|
let w = self.width as usize;
|
||||||
|
|
||||||
|
// cardinal directions
|
||||||
|
if self.is_exit_valid(x - 1, y) {
|
||||||
|
exits.push((idx - 1, 1.0))
|
||||||
|
};
|
||||||
|
if self.is_exit_valid(x + 1, y) {
|
||||||
|
exits.push((idx + 1, 1.0))
|
||||||
|
};
|
||||||
|
if self.is_exit_valid(x, y - 1) {
|
||||||
|
exits.push((idx - w, 1.0))
|
||||||
|
};
|
||||||
|
if self.is_exit_valid(x, y + 1) {
|
||||||
|
exits.push((idx + w, 1.0))
|
||||||
|
};
|
||||||
|
|
||||||
|
exits
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_pathing_distance(&self, idx1: usize, idx2: usize) -> f32 {
|
||||||
|
let w = self.width as usize;
|
||||||
|
let p1 = Point::new(idx1 % w, idx1 / w);
|
||||||
|
let p2 = Point::new(idx2 % w, idx2 / w);
|
||||||
|
rltk::DistanceAlg::Pythagoras.distance2d(p1, p2)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Algorithm2D for Map {
|
impl Algorithm2D for Map {
|
||||||
@ -140,10 +177,6 @@ impl BaseMap for Map {
|
|||||||
// map
|
// map
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
pub fn draw_map(ecs: &World, ctx: &mut Rltk) {
|
pub fn draw_map(ecs: &World, ctx: &mut Rltk) {
|
||||||
let map = ecs.fetch::<Map>();
|
let map = ecs.fetch::<Map>();
|
||||||
|
|
||||||
@ -163,14 +196,15 @@ pub fn draw_map(ecs: &World, ctx: &mut Rltk) {
|
|||||||
fg = RGB::from_f32(0., 1.0, 0.);
|
fg = RGB::from_f32(0., 1.0, 0.);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !map.visible_tiles[idx] { fg = fg.to_greyscale() }
|
if !map.visible_tiles[idx] {
|
||||||
|
fg = fg.to_greyscale()
|
||||||
|
}
|
||||||
ctx.set(x, y, fg, RGB::from_f32(0., 0., 0.), glyph);
|
ctx.set(x, y, fg, RGB::from_f32(0., 0., 0.), glyph);
|
||||||
}
|
}
|
||||||
x += 1;
|
x += 1;
|
||||||
if x > 79 {
|
if x > 79 {
|
||||||
x = 0;
|
x = 0;
|
||||||
y += 1;
|
y += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
41
src/monster_ai_system.rs
Normal file
41
src/monster_ai_system.rs
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
use super::{Map, Monster, Name, Position, Viewshed};
|
||||||
|
use rltk::{console, Point};
|
||||||
|
use specs::prelude::*;
|
||||||
|
|
||||||
|
pub struct MonsterAI {}
|
||||||
|
|
||||||
|
impl<'a> System<'a> for MonsterAI {
|
||||||
|
#[allow(clippy::type_complexity)]
|
||||||
|
type SystemData = (
|
||||||
|
WriteExpect<'a, Map>,
|
||||||
|
ReadExpect<'a, Point>,
|
||||||
|
WriteStorage<'a, Viewshed>,
|
||||||
|
ReadStorage<'a, Monster>,
|
||||||
|
ReadStorage<'a, Name>,
|
||||||
|
WriteStorage<'a, Position>,
|
||||||
|
);
|
||||||
|
|
||||||
|
fn run(&mut self, data: Self::SystemData) {
|
||||||
|
let (mut map, player_pos, mut viewshed, monster, name, mut position) = data;
|
||||||
|
|
||||||
|
for (mut viewshed, _monster, name, mut pos) in
|
||||||
|
(&mut viewshed, &monster, &name, &mut position).join()
|
||||||
|
{
|
||||||
|
if viewshed.visible_tiles.contains(&*player_pos) {
|
||||||
|
console::log(&format!("{} shouts insults at you", name.name));
|
||||||
|
let path = rltk::a_star_search(
|
||||||
|
map.xy_idx(pos.x, pos.y) as i32,
|
||||||
|
map.xy_idx(player_pos.x, player_pos.y) as i32,
|
||||||
|
&mut *map,
|
||||||
|
);
|
||||||
|
console::log(path.success);
|
||||||
|
if path.success && path.steps.len() > 1 {
|
||||||
|
pos.x = path.steps[1] as i32 % map.width;
|
||||||
|
pos.y = path.steps[1] as i32 / map.width;
|
||||||
|
console::log("here 2");
|
||||||
|
viewshed.dirty = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,12 +1,13 @@
|
|||||||
use rltk::{VirtualKeyCode, Rltk};
|
use super::{Map, Player, Position, RunState, State, TileType, Viewshed};
|
||||||
|
use rltk::{Point, Rltk, VirtualKeyCode};
|
||||||
use specs::prelude::*;
|
use specs::prelude::*;
|
||||||
use super::{Position, Player, TileType, Map, State, Viewshed};
|
use std::cmp::{max, min};
|
||||||
use std::cmp::{min, max};
|
|
||||||
|
|
||||||
pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
|
pub 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 mut viewsheds = ecs.write_storage::<Viewshed>();
|
let mut viewsheds = ecs.write_storage::<Viewshed>();
|
||||||
|
let mut ppos = ecs.write_resource::<Point>();
|
||||||
let map = ecs.fetch::<Map>();
|
let map = ecs.fetch::<Map>();
|
||||||
|
|
||||||
for (_player, pos, viewshed) in (&mut players, &mut positions, &mut viewsheds).join() {
|
for (_player, pos, viewshed) in (&mut players, &mut positions, &mut viewsheds).join() {
|
||||||
@ -14,32 +15,34 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
|
|||||||
if map.tiles[destination_idx] != TileType::Wall {
|
if map.tiles[destination_idx] != TileType::Wall {
|
||||||
pos.x = min(79, max(0, pos.x + delta_x));
|
pos.x = min(79, max(0, pos.x + delta_x));
|
||||||
pos.y = min(49, max(0, pos.y + delta_y));
|
pos.y = min(49, max(0, pos.y + delta_y));
|
||||||
|
ppos.x = pos.x;
|
||||||
|
ppos.y = pos.y;
|
||||||
viewshed.dirty = true
|
viewshed.dirty = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn player_input(gs: &mut State, ctx: &mut Rltk) {
|
pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState {
|
||||||
match ctx.key {
|
match ctx.key {
|
||||||
None => {}
|
None => return RunState::Paused,
|
||||||
Some(key) => match key {
|
Some(key) => match key {
|
||||||
VirtualKeyCode::Left |
|
VirtualKeyCode::Left | VirtualKeyCode::Numpad4 | VirtualKeyCode::H => {
|
||||||
VirtualKeyCode::Numpad4 |
|
try_move_player(-1, 0, &mut gs.ecs)
|
||||||
VirtualKeyCode::H => try_move_player(-1, 0, &mut gs.ecs),
|
}
|
||||||
|
|
||||||
VirtualKeyCode::Right |
|
VirtualKeyCode::Right | VirtualKeyCode::Numpad6 | VirtualKeyCode::L => {
|
||||||
VirtualKeyCode::Numpad6 |
|
try_move_player(1, 0, &mut gs.ecs)
|
||||||
VirtualKeyCode::L => try_move_player(1, 0, &mut gs.ecs),
|
}
|
||||||
|
|
||||||
VirtualKeyCode::Up |
|
VirtualKeyCode::Up | VirtualKeyCode::Numpad8 | VirtualKeyCode::K => {
|
||||||
VirtualKeyCode::Numpad8 |
|
try_move_player(0, -1, &mut gs.ecs)
|
||||||
VirtualKeyCode::K => try_move_player(0, -1, &mut gs.ecs),
|
}
|
||||||
|
|
||||||
VirtualKeyCode::Down |
|
VirtualKeyCode::Down | VirtualKeyCode::Numpad2 | VirtualKeyCode::J => {
|
||||||
VirtualKeyCode::Numpad2 |
|
try_move_player(0, 1, &mut gs.ecs)
|
||||||
VirtualKeyCode::J => try_move_player(0, 1, &mut gs.ecs),
|
}
|
||||||
_ => {}
|
_ => return RunState::Paused,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
RunState::Running
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user