Add chapter 2.6
This commit is contained in:
parent
95be479636
commit
174ad7ea56
@ -17,3 +17,52 @@ pub struct Renderable {
|
|||||||
|
|
||||||
#[derive(Component, Debug)]
|
#[derive(Component, Debug)]
|
||||||
pub struct Player {}
|
pub struct Player {}
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct Viewshed {
|
||||||
|
pub visible_tiles: Vec<rltk::Point>,
|
||||||
|
pub range: i32,
|
||||||
|
pub dirty: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct Monster {}
|
||||||
|
|
||||||
|
#[derive(Component, Debug)]
|
||||||
|
pub struct Name {
|
||||||
|
pub name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Debug)]
|
||||||
|
pub struct BlocksTile {}
|
||||||
|
|
||||||
|
#[derive(Component, Debug)]
|
||||||
|
pub struct CombatStats {
|
||||||
|
pub max_hp: i32,
|
||||||
|
pub hp: i32,
|
||||||
|
pub defense: i32,
|
||||||
|
pub power: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Debug, Clone)]
|
||||||
|
pub struct WantsToMelee {
|
||||||
|
pub target: Entity,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Debug)]
|
||||||
|
pub struct SufferDamage {
|
||||||
|
pub amount: Vec<i32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SufferDamage {
|
||||||
|
pub fn new_damage(store: &mut WriteStorage<SufferDamage>, victim: Entity, amount: i32) {
|
||||||
|
if let Some(suffering) = store.get_mut(victim) {
|
||||||
|
suffering.amount.push(amount);
|
||||||
|
} else {
|
||||||
|
let dmg = SufferDamage {
|
||||||
|
amount: vec![amount],
|
||||||
|
};
|
||||||
|
store.insert(victim, dmg).expect("Unable to insert damage");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
45
src/damage_system.rs
Normal file
45
src/damage_system.rs
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
use rltk::console;
|
||||||
|
use specs::prelude::*;
|
||||||
|
|
||||||
|
use crate::components::{CombatStats, Player, SufferDamage};
|
||||||
|
|
||||||
|
pub struct DamageSystem {}
|
||||||
|
|
||||||
|
impl<'a> System<'a> for DamageSystem {
|
||||||
|
type SystemData = (
|
||||||
|
WriteStorage<'a, CombatStats>,
|
||||||
|
WriteStorage<'a, SufferDamage>,
|
||||||
|
);
|
||||||
|
|
||||||
|
fn run(&mut self, data: Self::SystemData) {
|
||||||
|
let (mut stats, mut damage) = data;
|
||||||
|
|
||||||
|
for (mut stats, damage) in (&mut stats, &damage).join() {
|
||||||
|
stats.hp -= damage.amount.iter().sum::<i32>();
|
||||||
|
}
|
||||||
|
|
||||||
|
damage.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn delete_the_dead(ecs: &mut World) {
|
||||||
|
let mut dead: Vec<Entity> = Vec::new();
|
||||||
|
{
|
||||||
|
let combat_stats = ecs.read_storage::<CombatStats>();
|
||||||
|
let players = ecs.read_storage::<Player>();
|
||||||
|
let entities = ecs.entities();
|
||||||
|
for (entity, stats) in (&entities, &combat_stats).join() {
|
||||||
|
if stats.hp < 1 {
|
||||||
|
let player = players.get(entity);
|
||||||
|
match player {
|
||||||
|
None => dead.push(entity),
|
||||||
|
Some(_) => console::log("You are dead"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for victim in dead {
|
||||||
|
ecs.delete_entity(victim).expect("Unable to delete");
|
||||||
|
}
|
||||||
|
}
|
159
src/main.rs
159
src/main.rs
@ -1,20 +1,54 @@
|
|||||||
use components::*;
|
use components::*;
|
||||||
|
use damage_system::DamageSystem;
|
||||||
use map::*;
|
use map::*;
|
||||||
|
use map_indexing_system::MapIndexingSystem;
|
||||||
|
use melee_combat_system::MeleeCombatSystem;
|
||||||
|
use monster_ai_system::*;
|
||||||
use player::*;
|
use player::*;
|
||||||
use rltk::{GameState, RGB};
|
use rltk::{GameState, Point, RGB};
|
||||||
|
use visibility_system::*;
|
||||||
|
|
||||||
use specs::prelude::*;
|
use specs::prelude::*;
|
||||||
|
|
||||||
mod components;
|
mod components;
|
||||||
|
mod damage_system;
|
||||||
mod map;
|
mod map;
|
||||||
|
mod map_indexing_system;
|
||||||
|
mod melee_combat_system;
|
||||||
|
mod monster_ai_system;
|
||||||
mod player;
|
mod player;
|
||||||
|
mod rect;
|
||||||
|
mod visibility_system;
|
||||||
|
|
||||||
|
#[derive(PartialEq, Copy, Clone)]
|
||||||
|
pub enum RunState {
|
||||||
|
AwaitingInput,
|
||||||
|
PreRun,
|
||||||
|
PlayerTurn,
|
||||||
|
MonsterTurn,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct State {
|
pub struct State {
|
||||||
ecs: World,
|
pub ecs: World,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
fn run_systems(&mut self) {
|
fn run_systems(&mut self) {
|
||||||
|
let mut vis = VisibilitySystem {};
|
||||||
|
vis.run_now(&self.ecs);
|
||||||
|
|
||||||
|
let mut mob = MonsterAI {};
|
||||||
|
mob.run_now(&self.ecs);
|
||||||
|
|
||||||
|
let mut mapindex = MapIndexingSystem {};
|
||||||
|
mapindex.run_now(&self.ecs);
|
||||||
|
|
||||||
|
let mut meleesystem = MeleeCombatSystem {};
|
||||||
|
meleesystem.run_now(&self.ecs);
|
||||||
|
|
||||||
|
let mut damagesystem = DamageSystem {};
|
||||||
|
damagesystem.run_now(&self.ecs);
|
||||||
|
|
||||||
self.ecs.maintain();
|
self.ecs.maintain();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -23,17 +57,45 @@ impl GameState for State {
|
|||||||
fn tick(&mut self, ctx: &mut rltk::Rltk) {
|
fn tick(&mut self, ctx: &mut rltk::Rltk) {
|
||||||
ctx.cls();
|
ctx.cls();
|
||||||
|
|
||||||
player_input(self, ctx);
|
let mut newrunstate;
|
||||||
self.run_systems();
|
{
|
||||||
|
let runstate = self.ecs.fetch::<RunState>();
|
||||||
|
newrunstate = *runstate;
|
||||||
|
}
|
||||||
|
|
||||||
let map = self.ecs.fetch::<Vec<TileType>>();
|
match newrunstate {
|
||||||
draw_map(&map, ctx);
|
RunState::PreRun => {
|
||||||
|
self.run_systems();
|
||||||
|
newrunstate = RunState::AwaitingInput;
|
||||||
|
}
|
||||||
|
RunState::AwaitingInput => newrunstate = player_input(self, ctx),
|
||||||
|
RunState::PlayerTurn => {
|
||||||
|
self.run_systems();
|
||||||
|
newrunstate = RunState::MonsterTurn;
|
||||||
|
}
|
||||||
|
RunState::MonsterTurn => {
|
||||||
|
self.run_systems();
|
||||||
|
newrunstate = RunState::AwaitingInput;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut runwriter = self.ecs.write_resource::<RunState>();
|
||||||
|
*runwriter = newrunstate;
|
||||||
|
}
|
||||||
|
damage_system::delete_the_dead(&mut self.ecs);
|
||||||
|
|
||||||
|
draw_map(&self.ecs, 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>();
|
||||||
|
let map = self.ecs.fetch::<Map>();
|
||||||
|
|
||||||
for (pos, render) in (&positions, &renderables).join() {
|
for (pos, render) in (&positions, &renderables).join() {
|
||||||
ctx.set(pos.x, pos.y, render.fg, render.bg, render.glyph)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -47,21 +109,98 @@ fn main() -> rltk::BError {
|
|||||||
.build()?;
|
.build()?;
|
||||||
let mut gs = State { ecs: World::new() };
|
let mut gs = State { ecs: World::new() };
|
||||||
|
|
||||||
|
gs.ecs.insert(RunState::PreRun);
|
||||||
|
|
||||||
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.insert(new_map_rooms_and_corridors());
|
gs.ecs.register::<Viewshed>();
|
||||||
|
gs.ecs.register::<Monster>();
|
||||||
|
gs.ecs.register::<Name>();
|
||||||
|
gs.ecs.register::<BlocksTile>();
|
||||||
|
gs.ecs.register::<CombatStats>();
|
||||||
|
gs.ecs.register::<WantsToMelee>();
|
||||||
|
gs.ecs.register::<SufferDamage>();
|
||||||
|
let map = Map::new_map_rooms_and_corridors();
|
||||||
|
let (player_x, player_y) = map.rooms[0].center();
|
||||||
|
|
||||||
gs.ecs
|
gs.ecs.insert(Point::new(player_x, player_y));
|
||||||
|
let player_entity = 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),
|
||||||
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(Name {
|
||||||
|
name: "Player".to_string(),
|
||||||
|
})
|
||||||
|
.with(CombatStats {
|
||||||
|
max_hp: 30,
|
||||||
|
hp: 30,
|
||||||
|
defense: 2,
|
||||||
|
power: 5,
|
||||||
|
})
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
gs.ecs.insert(player_entity);
|
||||||
|
|
||||||
|
let mut rng = rltk::RandomNumberGenerator::new();
|
||||||
|
for (i, room) in map.rooms.iter().skip(1).enumerate() {
|
||||||
|
let (x, y) = room.center();
|
||||||
|
let glyph: rltk::FontCharType;
|
||||||
|
let roll = rng.roll_dice(1, 2);
|
||||||
|
let name: String;
|
||||||
|
match roll {
|
||||||
|
1 => {
|
||||||
|
glyph = rltk::to_cp437('g');
|
||||||
|
name = "Goblin".to_string();
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
glyph = rltk::to_cp437('o');
|
||||||
|
name = "Orc".to_string();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
gs.ecs
|
||||||
|
.create_entity()
|
||||||
|
.with(Position { x, y })
|
||||||
|
.with(Renderable {
|
||||||
|
glyph,
|
||||||
|
fg: RGB::named(rltk::RED),
|
||||||
|
bg: RGB::named(rltk::BLACK),
|
||||||
|
})
|
||||||
|
.with(Viewshed {
|
||||||
|
visible_tiles: Vec::new(),
|
||||||
|
range: 8,
|
||||||
|
dirty: true,
|
||||||
|
})
|
||||||
|
.with(Monster {})
|
||||||
|
.with(Name {
|
||||||
|
name: format!("{} #{}", &name, i),
|
||||||
|
})
|
||||||
|
.with(BlocksTile {})
|
||||||
|
.with(CombatStats {
|
||||||
|
max_hp: 16,
|
||||||
|
hp: 16,
|
||||||
|
defense: 1,
|
||||||
|
power: 4,
|
||||||
|
})
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
gs.ecs.insert(map);
|
||||||
|
|
||||||
rltk::main_loop(context, gs)
|
rltk::main_loop(context, gs)
|
||||||
}
|
}
|
||||||
|
237
src/map.rs
237
src/map.rs
@ -1,4 +1,9 @@
|
|||||||
use rltk::{Rltk, RGB};
|
use std::cmp::{max, min};
|
||||||
|
|
||||||
|
use rltk::{Algorithm2D, BaseMap, Point, RandomNumberGenerator, Rltk, RGB};
|
||||||
|
use specs::prelude::*;
|
||||||
|
|
||||||
|
use crate::rect::Rect;
|
||||||
|
|
||||||
#[derive(PartialEq, Copy, Clone)]
|
#[derive(PartialEq, Copy, Clone)]
|
||||||
pub enum TileType {
|
pub enum TileType {
|
||||||
@ -6,68 +11,200 @@ 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,
|
||||||
|
pub revealed_tiles: Vec<bool>,
|
||||||
|
pub visible_tiles: Vec<bool>,
|
||||||
|
pub blocked: Vec<bool>,
|
||||||
|
pub tile_content: Vec<Vec<Entity>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_map_rooms_and_corridors() -> Vec<TileType> {
|
impl Map {
|
||||||
let mut map = vec![TileType::Wall; 80 * 50];
|
pub fn xy_idx(&self, x: i32, y: i32) -> usize {
|
||||||
|
(y as usize * self.width as usize) + x as usize
|
||||||
map
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Makes a map with solid boundaries and 400 randomly placed walls. No guarantees that it won't look awful
|
|
||||||
pub fn new_map_test() -> Vec<TileType> {
|
|
||||||
let mut map = vec![TileType::Floor; 80 * 50];
|
|
||||||
|
|
||||||
for x in 0..80 {
|
|
||||||
map[xy_idx(x, 0)] = TileType::Wall;
|
|
||||||
map[xy_idx(x, 49)] = TileType::Wall;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for y in 0..50 {
|
fn apply_room_to_map(&mut self, room: &Rect) {
|
||||||
map[xy_idx(0, y)] = TileType::Wall;
|
for y in room.y1 + 1..=room.y2 {
|
||||||
map[xy_idx(79, y)] = TileType::Wall;
|
for x in room.x1 + 1..=room.x2 {
|
||||||
}
|
let idx = self.xy_idx(x, y);
|
||||||
|
self.tiles[idx] = TileType::Floor;
|
||||||
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_horizontal_tunnel(&mut self, x1: i32, x2: i32, y: i32) {
|
||||||
|
for x in min(x1, x2)..=max(x1, x2) {
|
||||||
|
let idx = self.xy_idx(x, y);
|
||||||
|
if idx > 0 && idx < self.width as usize * self.height as usize {
|
||||||
|
self.tiles[idx as usize] = TileType::Floor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn apply_vertical_tunnel(&mut self, y1: i32, y2: i32, x: i32) {
|
||||||
|
for y in min(y1, y2)..=max(y1, y2) {
|
||||||
|
let idx = self.xy_idx(x, y);
|
||||||
|
if idx > 0 && idx < self.width as usize * self.height as usize {
|
||||||
|
self.tiles[idx as usize] = TileType::Floor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_map_rooms_and_corridors() -> Map {
|
||||||
|
let mut map = Map {
|
||||||
|
tiles: vec![TileType::Wall; 80 * 50],
|
||||||
|
rooms: Vec::new(),
|
||||||
|
width: 80,
|
||||||
|
height: 50,
|
||||||
|
revealed_tiles: vec![false; 80 * 50],
|
||||||
|
visible_tiles: vec![false; 80 * 50],
|
||||||
|
blocked: vec![false; 80 * 50],
|
||||||
|
tile_content: vec![Vec::new(); 80 * 50],
|
||||||
|
};
|
||||||
|
|
||||||
|
const MAX_ROOMS: i32 = 30;
|
||||||
|
const MIN_SIZE: i32 = 6;
|
||||||
|
const MAX_SIZE: i32 = 10;
|
||||||
|
|
||||||
|
let mut rng = RandomNumberGenerator::new();
|
||||||
|
|
||||||
|
for _ in 0..MAX_ROOMS {
|
||||||
|
let w = rng.range(MIN_SIZE, MAX_SIZE);
|
||||||
|
let h = rng.range(MIN_SIZE, MAX_SIZE);
|
||||||
|
let x = rng.roll_dice(1, map.width - w - 1) - 1;
|
||||||
|
let y = rng.roll_dice(1, map.height - h - 1) - 1;
|
||||||
|
let new_room = Rect::new(x, y, w, h);
|
||||||
|
let mut ok = true;
|
||||||
|
for other_room in map.rooms.iter() {
|
||||||
|
if new_room.intersect(other_room) {
|
||||||
|
ok = false
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if ok {
|
||||||
|
map.apply_room_to_map(&new_room);
|
||||||
|
|
||||||
|
if !map.rooms.is_empty() {
|
||||||
|
let (new_x, new_y) = new_room.center();
|
||||||
|
let (prev_x, prev_y) = map.rooms[map.rooms.len() - 1].center();
|
||||||
|
if rng.range(0, 2) == 1 {
|
||||||
|
map.apply_horizontal_tunnel(prev_x, new_x, prev_y);
|
||||||
|
map.apply_vertical_tunnel(prev_y, new_y, new_x);
|
||||||
|
} else {
|
||||||
|
map.apply_vertical_tunnel(prev_y, new_y, prev_x);
|
||||||
|
map.apply_horizontal_tunnel(prev_x, new_x, new_y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
map.rooms.push(new_room);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
map
|
||||||
|
}
|
||||||
|
|
||||||
|
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.blocked[idx]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn populate_blocked(&mut self) {
|
||||||
|
for (i, tile) in self.tiles.iter_mut().enumerate() {
|
||||||
|
self.blocked[i] = *tile == TileType::Wall;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn clear_content_index(&mut self) {
|
||||||
|
for content in self.tile_content.iter_mut() {
|
||||||
|
content.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw_map(map: &[TileType], ctx: &mut Rltk) {
|
impl Algorithm2D for Map {
|
||||||
|
fn dimensions(&self) -> rltk::Point {
|
||||||
|
Point::new(self.width, self.height)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BaseMap for Map {
|
||||||
|
fn is_opaque(&self, idx: usize) -> bool {
|
||||||
|
self.tiles[idx as usize] == TileType::Wall
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
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))
|
||||||
|
};
|
||||||
|
|
||||||
|
if self.is_exit_valid(x - 1, y - 1) {
|
||||||
|
exits.push((idx - w - 1, 1.45))
|
||||||
|
};
|
||||||
|
if self.is_exit_valid(x + 1, y - 1) {
|
||||||
|
exits.push((idx - w + 1, 1.45))
|
||||||
|
};
|
||||||
|
if self.is_exit_valid(x - 1, y + 1) {
|
||||||
|
exits.push((idx + w - 1, 1.45))
|
||||||
|
};
|
||||||
|
if self.is_exit_valid(x + 1, y + 1) {
|
||||||
|
exits.push((idx + w + 1, 1.45))
|
||||||
|
};
|
||||||
|
|
||||||
|
exits
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn draw_map(ecs: &World, ctx: &mut Rltk) {
|
||||||
|
let map = ecs.fetch::<Map>();
|
||||||
|
|
||||||
let mut y = 0;
|
let mut y = 0;
|
||||||
let mut x = 0;
|
let mut x = 0;
|
||||||
for tile in map.iter() {
|
for (idx, tile) in map.tiles.iter().enumerate() {
|
||||||
match tile {
|
if map.revealed_tiles[idx] {
|
||||||
TileType::Floor => {
|
let glyph;
|
||||||
ctx.set(
|
let mut fg;
|
||||||
x,
|
match tile {
|
||||||
y,
|
TileType::Floor => {
|
||||||
RGB::from_f32(0.5, 0.5, 0.5),
|
fg = RGB::from_f32(0.5, 0.5, 0.5);
|
||||||
RGB::from_f32(0., 0., 0.),
|
glyph = rltk::to_cp437('.');
|
||||||
rltk::to_cp437('.'),
|
}
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
TileType::Wall => {
|
TileType::Wall => {
|
||||||
ctx.set(
|
fg = RGB::from_f32(0.0, 1.0, 0.0);
|
||||||
x,
|
glyph = rltk::to_cp437('#');
|
||||||
y,
|
}
|
||||||
RGB::from_f32(0.0, 1.0, 0.0),
|
|
||||||
RGB::from_f32(0., 0., 0.),
|
|
||||||
rltk::to_cp437('#'),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
if !map.visible_tiles[idx] {
|
||||||
|
fg = fg.to_greyscale()
|
||||||
|
}
|
||||||
|
ctx.set(x, y, fg, RGB::from_f32(0., 0., 0.), glyph);
|
||||||
}
|
}
|
||||||
|
|
||||||
x += 1;
|
x += 1;
|
||||||
|
34
src/map_indexing_system.rs
Normal file
34
src/map_indexing_system.rs
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
use specs::prelude::*;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
components::{BlocksTile, Position},
|
||||||
|
map::Map,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct MapIndexingSystem {}
|
||||||
|
|
||||||
|
impl<'a> System<'a> for MapIndexingSystem {
|
||||||
|
type SystemData = (
|
||||||
|
WriteExpect<'a, Map>,
|
||||||
|
ReadStorage<'a, Position>,
|
||||||
|
ReadStorage<'a, BlocksTile>,
|
||||||
|
Entities<'a>,
|
||||||
|
);
|
||||||
|
|
||||||
|
fn run(&mut self, data: Self::SystemData) {
|
||||||
|
let (mut map, position, blockers, entities) = data;
|
||||||
|
|
||||||
|
map.populate_blocked();
|
||||||
|
map.clear_content_index();
|
||||||
|
for (entity, position) in (&entities, &position).join() {
|
||||||
|
let idx = map.xy_idx(position.x, position.y);
|
||||||
|
|
||||||
|
let _p: Option<&BlocksTile> = blockers.get(entity);
|
||||||
|
if let Some(_p) = _p {
|
||||||
|
map.blocked[idx] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
map.tile_content[idx].push(entity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
48
src/melee_combat_system.rs
Normal file
48
src/melee_combat_system.rs
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
use rltk::console;
|
||||||
|
use specs::prelude::*;
|
||||||
|
|
||||||
|
use crate::components::{CombatStats, Name, SufferDamage, WantsToMelee};
|
||||||
|
|
||||||
|
pub struct MeleeCombatSystem {}
|
||||||
|
|
||||||
|
impl<'a> System<'a> for MeleeCombatSystem {
|
||||||
|
type SystemData = (
|
||||||
|
Entities<'a>,
|
||||||
|
WriteStorage<'a, WantsToMelee>,
|
||||||
|
ReadStorage<'a, Name>,
|
||||||
|
ReadStorage<'a, CombatStats>,
|
||||||
|
WriteStorage<'a, SufferDamage>,
|
||||||
|
);
|
||||||
|
|
||||||
|
fn run(&mut self, data: Self::SystemData) {
|
||||||
|
let (entities, mut wants_melee, names, combat_stats, mut inflict_damage) = data;
|
||||||
|
|
||||||
|
for (_entity, wants_melee, name, stats) in
|
||||||
|
(&entities, &wants_melee, &names, &combat_stats).join()
|
||||||
|
{
|
||||||
|
if stats.hp > 0 {
|
||||||
|
let target_stats = combat_stats.get(wants_melee.target).unwrap();
|
||||||
|
if target_stats.hp > 0 {
|
||||||
|
let target_name = names.get(wants_melee.target).unwrap();
|
||||||
|
|
||||||
|
let damage = i32::max(0, stats.power - target_stats.defense);
|
||||||
|
|
||||||
|
if damage == 0 {
|
||||||
|
console::log(&format!(
|
||||||
|
"{} is unable to hurt {}",
|
||||||
|
&name.name, &target_name.name
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
console::log(&format!(
|
||||||
|
"{} hits {}, for {} hp.",
|
||||||
|
&name.name, &target_name.name, damage
|
||||||
|
));
|
||||||
|
SufferDamage::new_damage(&mut inflict_damage, wants_melee.target, damage)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
wants_melee.clear();
|
||||||
|
}
|
||||||
|
}
|
75
src/monster_ai_system.rs
Normal file
75
src/monster_ai_system.rs
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
use rltk::{console, Point};
|
||||||
|
use specs::prelude::*;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
components::{Monster, Position, Viewshed, WantsToMelee},
|
||||||
|
map::Map,
|
||||||
|
RunState,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct MonsterAI {}
|
||||||
|
|
||||||
|
impl<'a> System<'a> for MonsterAI {
|
||||||
|
#[allow(clippy::type_complexity)]
|
||||||
|
type SystemData = (
|
||||||
|
WriteExpect<'a, Map>,
|
||||||
|
ReadExpect<'a, Point>,
|
||||||
|
ReadExpect<'a, Entity>,
|
||||||
|
ReadExpect<'a, RunState>,
|
||||||
|
Entities<'a>,
|
||||||
|
WriteStorage<'a, Viewshed>,
|
||||||
|
ReadStorage<'a, Monster>,
|
||||||
|
WriteStorage<'a, Position>,
|
||||||
|
WriteStorage<'a, WantsToMelee>,
|
||||||
|
);
|
||||||
|
|
||||||
|
fn run(&mut self, data: Self::SystemData) {
|
||||||
|
let (
|
||||||
|
mut map,
|
||||||
|
player_pos,
|
||||||
|
player_entity,
|
||||||
|
runstate,
|
||||||
|
entities,
|
||||||
|
mut viewshed,
|
||||||
|
monster,
|
||||||
|
mut position,
|
||||||
|
mut wants_to_melee,
|
||||||
|
) = data;
|
||||||
|
|
||||||
|
if *runstate != RunState::MonsterTurn {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (entity, mut viewshed, _monster, mut pos) in
|
||||||
|
(&entities, &mut viewshed, &monster, &mut position).join()
|
||||||
|
{
|
||||||
|
let distance =
|
||||||
|
rltk::DistanceAlg::Pythagoras.distance2d(Point::new(pos.x, pos.y), *player_pos);
|
||||||
|
if distance < 1.5 {
|
||||||
|
wants_to_melee
|
||||||
|
.insert(
|
||||||
|
entity,
|
||||||
|
WantsToMelee {
|
||||||
|
target: *player_entity,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.expect("Unable to insert attack");
|
||||||
|
} else if viewshed.visible_tiles.contains(&*player_pos) {
|
||||||
|
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,
|
||||||
|
);
|
||||||
|
if path.success && path.steps.len() > 1 {
|
||||||
|
let mut idx = map.xy_idx(pos.x, pos.y);
|
||||||
|
map.blocked[idx] = false;
|
||||||
|
pos.x = path.steps[1] as i32 % map.width;
|
||||||
|
pos.y = path.steps[1] as i32 / map.width;
|
||||||
|
idx = map.xy_idx(pos.x, pos.y);
|
||||||
|
map.blocked[idx] = true;
|
||||||
|
viewshed.dirty = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,35 +1,67 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
components::{Player, Position},
|
components::{CombatStats, Player, Position, Viewshed, WantsToMelee},
|
||||||
map::{xy_idx, TileType},
|
map::{Map, TileType},
|
||||||
State,
|
RunState, State,
|
||||||
};
|
};
|
||||||
use rltk::Rltk;
|
use rltk::{console, Point, Rltk, VirtualKeyCode};
|
||||||
use specs::prelude::*;
|
use specs::prelude::*;
|
||||||
use std::cmp::{max, min};
|
use std::cmp::{max, min};
|
||||||
|
|
||||||
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 map = ecs.fetch::<Vec<TileType>>();
|
let mut viewsheds = ecs.write_storage::<Viewshed>();
|
||||||
|
let combat_stats = ecs.read_storage::<CombatStats>();
|
||||||
|
let entities = ecs.entities();
|
||||||
|
let mut wants_to_melee = ecs.write_storage::<WantsToMelee>();
|
||||||
|
let map = ecs.fetch::<Map>();
|
||||||
|
|
||||||
for (_player, pos) in (&mut players, &mut positions).join() {
|
for (entity, _player, pos, viewshed) in
|
||||||
let destination_idx = xy_idx(pos.x + delta_x, pos.y + delta_y);
|
(&entities, &mut players, &mut positions, &mut viewsheds).join()
|
||||||
if map[destination_idx] != TileType::Wall {
|
{
|
||||||
|
let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y);
|
||||||
|
|
||||||
|
for potential_target in map.tile_content[destination_idx].iter() {
|
||||||
|
let target = combat_stats.get(*potential_target);
|
||||||
|
if let Some(_target) = target {
|
||||||
|
wants_to_melee
|
||||||
|
.insert(
|
||||||
|
entity,
|
||||||
|
WantsToMelee {
|
||||||
|
target: *potential_target,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.expect("Add target failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !map.blocked[destination_idx] {
|
||||||
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));
|
||||||
|
|
||||||
|
viewshed.dirty = true;
|
||||||
|
let mut ppos = ecs.write_resource::<Point>();
|
||||||
|
ppos.x = pos.x;
|
||||||
|
ppos.y = pos.y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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::AwaitingInput,
|
||||||
Some(key) => match key {
|
Some(key) => match key {
|
||||||
rltk::VirtualKeyCode::Left => try_move_player(-1, 0, &mut gs.ecs),
|
VirtualKeyCode::Left | VirtualKeyCode::H => try_move_player(-1, 0, &mut gs.ecs),
|
||||||
rltk::VirtualKeyCode::Right => try_move_player(1, 0, &mut gs.ecs),
|
VirtualKeyCode::Right | VirtualKeyCode::L => try_move_player(1, 0, &mut gs.ecs),
|
||||||
rltk::VirtualKeyCode::Up => try_move_player(0, -1, &mut gs.ecs),
|
VirtualKeyCode::Up | VirtualKeyCode::K => try_move_player(0, -1, &mut gs.ecs),
|
||||||
rltk::VirtualKeyCode::Down => try_move_player(0, 1, &mut gs.ecs),
|
VirtualKeyCode::Down | VirtualKeyCode::J => try_move_player(0, 1, &mut gs.ecs),
|
||||||
_ => {}
|
VirtualKeyCode::Y => try_move_player(1, -1, &mut gs.ecs),
|
||||||
|
VirtualKeyCode::U => try_move_player(-1, -1, &mut gs.ecs),
|
||||||
|
VirtualKeyCode::N => try_move_player(1, 1, &mut gs.ecs),
|
||||||
|
VirtualKeyCode::B => try_move_player(-1, 1, &mut gs.ecs),
|
||||||
|
_ => return RunState::AwaitingInput,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RunState::PlayerTurn
|
||||||
}
|
}
|
||||||
|
25
src/rect.rs
Normal file
25
src/rect.rs
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
pub struct Rect {
|
||||||
|
pub x1: i32,
|
||||||
|
pub x2: i32,
|
||||||
|
pub y1: i32,
|
||||||
|
pub y2: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Rect {
|
||||||
|
pub fn new(x: i32, y: i32, w: i32, h: i32) -> Self {
|
||||||
|
Self {
|
||||||
|
x1: x,
|
||||||
|
y1: y,
|
||||||
|
x2: x + w,
|
||||||
|
y2: y + h,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn intersect(&self, other: &Rect) -> bool {
|
||||||
|
self.x1 <= other.x2 && self.x2 >= other.x1 && self.y1 <= other.y2 && self.y2 >= other.y1
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn center(&self) -> (i32, i32) {
|
||||||
|
((self.x1 + self.x2) / 2, (self.y1 + self.y2) / 2)
|
||||||
|
}
|
||||||
|
}
|
47
src/visibility_system.rs
Normal file
47
src/visibility_system.rs
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
use rltk::{field_of_view, Point};
|
||||||
|
use specs::prelude::*;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
components::{Player, Position, Viewshed},
|
||||||
|
map::Map,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct VisibilitySystem {}
|
||||||
|
|
||||||
|
impl<'a> System<'a> for VisibilitySystem {
|
||||||
|
type SystemData = (
|
||||||
|
WriteExpect<'a, Map>,
|
||||||
|
Entities<'a>,
|
||||||
|
WriteStorage<'a, Viewshed>,
|
||||||
|
WriteStorage<'a, Position>,
|
||||||
|
ReadStorage<'a, Player>,
|
||||||
|
);
|
||||||
|
|
||||||
|
fn run(&mut self, data: Self::SystemData) {
|
||||||
|
let (mut map, entities, mut viewshed, pos, player) = data;
|
||||||
|
for (ent, viewshed, pos) in (&entities, &mut viewshed, &pos).join() {
|
||||||
|
if !viewshed.dirty {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
viewshed.dirty = false;
|
||||||
|
viewshed.visible_tiles.clear();
|
||||||
|
viewshed.visible_tiles = field_of_view(Point::new(pos.x, pos.y), viewshed.range, &*map);
|
||||||
|
viewshed
|
||||||
|
.visible_tiles
|
||||||
|
.retain(|p| p.x >= 0 && p.x < map.width && p.y >= 0 && p.y < map.height);
|
||||||
|
|
||||||
|
let p: Option<&Player> = player.get(ent);
|
||||||
|
if let Some(_p) = p {
|
||||||
|
for t in map.visible_tiles.iter_mut() {
|
||||||
|
*t = false
|
||||||
|
}
|
||||||
|
for vis in viewshed.visible_tiles.iter() {
|
||||||
|
let idx = map.xy_idx(vis.x, vis.y);
|
||||||
|
map.revealed_tiles[idx] = true;
|
||||||
|
map.visible_tiles[idx] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user