gui, inventory, symmetrical shadowcasting, bugfixes
This commit is contained in:
parent
5b7eac3165
commit
986adb6fce
10 changed files with 355 additions and 71 deletions
|
|
@ -64,3 +64,27 @@ impl SufferDamage {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Debug)]
|
||||||
|
pub struct Item {}
|
||||||
|
|
||||||
|
#[derive(Component, Debug)]
|
||||||
|
pub struct Potion {
|
||||||
|
pub heal_amount: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Debug, Clone)]
|
||||||
|
pub struct InBackpack {
|
||||||
|
pub owner: Entity,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Debug, Clone)]
|
||||||
|
pub struct WantsToPickupItem {
|
||||||
|
pub collected_by: Entity,
|
||||||
|
pub item: Entity,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Debug)]
|
||||||
|
pub struct WantsToDrinkPotion {
|
||||||
|
pub potion: Entity,
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
use super::{CombatStats, GameLog, Name, Player, SufferDamage};
|
use super::{gamelog::GameLog, CombatStats, Name, Player, SufferDamage};
|
||||||
use rltk::console;
|
|
||||||
use specs::prelude::*;
|
use specs::prelude::*;
|
||||||
|
|
||||||
pub struct DamageSystem {}
|
pub struct DamageSystem {}
|
||||||
|
|
@ -38,7 +37,7 @@ pub fn delete_the_dead(ecs: &mut World) {
|
||||||
}
|
}
|
||||||
dead.push(entity)
|
dead.push(entity)
|
||||||
}
|
}
|
||||||
Some(_) => console::log("You died."),
|
Some(_) => log.entries.push(format!("YOU DIED!")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
53
src/gui.rs
53
src/gui.rs
|
|
@ -1,5 +1,5 @@
|
||||||
use super::{CombatStats, GameLog, Map, Name, Player, Point, Position};
|
use super::{gamelog::GameLog, CombatStats, InBackpack, Map, Name, Player, Point, Position, State};
|
||||||
use rltk::{Rltk, RGB};
|
use rltk::{Rltk, VirtualKeyCode, RGB};
|
||||||
use specs::prelude::*;
|
use specs::prelude::*;
|
||||||
|
|
||||||
pub fn draw_ui(ecs: &World, ctx: &mut Rltk) {
|
pub fn draw_ui(ecs: &World, ctx: &mut Rltk) {
|
||||||
|
|
@ -110,3 +110,52 @@ fn draw_tooltips(ecs: &World, ctx: &mut Rltk) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq, Copy, Clone)]
|
||||||
|
pub enum ItemMenuResult {
|
||||||
|
Cancel,
|
||||||
|
NoResponse,
|
||||||
|
Selected,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn show_inventory(gs: &mut State, ctx: &mut Rltk) -> (ItemMenuResult, Option<Entity>) {
|
||||||
|
let player_entity = gs.ecs.fetch::<Entity>();
|
||||||
|
let names = gs.ecs.read_storage::<Name>();
|
||||||
|
let backpack = gs.ecs.read_storage::<InBackpack>();
|
||||||
|
let entities = gs.ecs.entities();
|
||||||
|
|
||||||
|
let inventory = (&backpack, &names).join().filter(|item| item.0.owner == *player_entity);
|
||||||
|
let count = inventory.count();
|
||||||
|
|
||||||
|
let mut y = (25 - (count / 2)) as i32;
|
||||||
|
ctx.draw_box(15, y - 2, 31, (count + 3) as i32, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK));
|
||||||
|
ctx.print_color(18, y - 2, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "Inventory");
|
||||||
|
ctx.print_color(18, y + count as i32 + 1, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "ESC to cancel");
|
||||||
|
|
||||||
|
let mut equippable: Vec<Entity> = Vec::new();
|
||||||
|
let mut j = 0;
|
||||||
|
for (entity, _pack, name) in (&entities, &backpack, &names).join().filter(|item| item.1.owner == *player_entity) {
|
||||||
|
ctx.set(17, y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), rltk::to_cp437('('));
|
||||||
|
ctx.set(18, y, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), 97 + j as rltk::FontCharType);
|
||||||
|
ctx.set(19, y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), rltk::to_cp437(')'));
|
||||||
|
|
||||||
|
ctx.print(21, y, &name.name.to_string());
|
||||||
|
equippable.push(entity);
|
||||||
|
y += 1;
|
||||||
|
j += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
match ctx.key {
|
||||||
|
None => (ItemMenuResult::NoResponse, None),
|
||||||
|
Some(key) => match key {
|
||||||
|
VirtualKeyCode::Escape => (ItemMenuResult::Cancel, None),
|
||||||
|
_ => {
|
||||||
|
let selection = rltk::letter_to_option(key);
|
||||||
|
if selection > -1 && selection < count as i32 {
|
||||||
|
return (ItemMenuResult::Selected, Some(equippable[selection as usize]));
|
||||||
|
}
|
||||||
|
(ItemMenuResult::NoResponse, None)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
68
src/inventory_system.rs
Normal file
68
src/inventory_system.rs
Normal file
|
|
@ -0,0 +1,68 @@
|
||||||
|
use super::{gamelog::GameLog, CombatStats, InBackpack, Name, Position, Potion, WantsToDrinkPotion, WantsToPickupItem};
|
||||||
|
use specs::prelude::*;
|
||||||
|
|
||||||
|
pub struct ItemCollectionSystem {}
|
||||||
|
|
||||||
|
impl<'a> System<'a> for ItemCollectionSystem {
|
||||||
|
#[allow(clippy::type_complexity)]
|
||||||
|
type SystemData = (
|
||||||
|
ReadExpect<'a, Entity>,
|
||||||
|
WriteExpect<'a, GameLog>,
|
||||||
|
WriteStorage<'a, WantsToPickupItem>,
|
||||||
|
WriteStorage<'a, Position>,
|
||||||
|
ReadStorage<'a, Name>,
|
||||||
|
WriteStorage<'a, InBackpack>,
|
||||||
|
);
|
||||||
|
|
||||||
|
fn run(&mut self, data: Self::SystemData) {
|
||||||
|
let (player_entity, mut gamelog, mut wants_pickup, mut positions, names, mut backpack) = data;
|
||||||
|
|
||||||
|
for pickup in wants_pickup.join() {
|
||||||
|
positions.remove(pickup.item);
|
||||||
|
backpack.insert(pickup.item, InBackpack { owner: pickup.collected_by }).expect("Unable to pickup item.");
|
||||||
|
|
||||||
|
if pickup.collected_by == *player_entity {
|
||||||
|
gamelog.entries.push(format!("You pick up the {}.", names.get(pickup.item).unwrap().name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
wants_pickup.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct PotionUseSystem {}
|
||||||
|
impl<'a> System<'a> for PotionUseSystem {
|
||||||
|
#[allow(clippy::type_complexity)]
|
||||||
|
type SystemData = (
|
||||||
|
ReadExpect<'a, Entity>,
|
||||||
|
WriteExpect<'a, GameLog>,
|
||||||
|
Entities<'a>,
|
||||||
|
WriteStorage<'a, WantsToDrinkPotion>,
|
||||||
|
ReadStorage<'a, Name>,
|
||||||
|
ReadStorage<'a, Potion>,
|
||||||
|
WriteStorage<'a, CombatStats>,
|
||||||
|
);
|
||||||
|
|
||||||
|
fn run(&mut self, data: Self::SystemData) {
|
||||||
|
let (player_entity, mut gamelog, entities, mut wants_drink, names, potions, mut combat_stats) = data;
|
||||||
|
|
||||||
|
for (entity, drink, stats) in (&entities, &wants_drink, &mut combat_stats).join() {
|
||||||
|
let potion = potions.get(drink.potion);
|
||||||
|
match potion {
|
||||||
|
None => {}
|
||||||
|
Some(potion) => {
|
||||||
|
stats.hp = i32::min(stats.max_hp, stats.hp + potion.heal_amount);
|
||||||
|
if entity == *player_entity {
|
||||||
|
gamelog.entries.push(format!(
|
||||||
|
"You quaff the {}, and heal {} hp.",
|
||||||
|
names.get(drink.potion).unwrap().name,
|
||||||
|
potion.heal_amount
|
||||||
|
));
|
||||||
|
}
|
||||||
|
entities.delete(drink.potion).expect("Delete failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
wants_drink.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
106
src/main.rs
106
src/main.rs
|
|
@ -1,4 +1,4 @@
|
||||||
use rltk::{GameState, Point, Rltk, RGB};
|
use rltk::{GameState, Point, Rltk};
|
||||||
use specs::prelude::*;
|
use specs::prelude::*;
|
||||||
|
|
||||||
mod components;
|
mod components;
|
||||||
|
|
@ -10,8 +10,8 @@ use player::*;
|
||||||
mod rect;
|
mod rect;
|
||||||
pub use rect::Rect;
|
pub use rect::Rect;
|
||||||
mod gamelog;
|
mod gamelog;
|
||||||
use gamelog::GameLog;
|
|
||||||
mod gui;
|
mod gui;
|
||||||
|
mod spawner;
|
||||||
mod visibility_system;
|
mod visibility_system;
|
||||||
use visibility_system::VisibilitySystem;
|
use visibility_system::VisibilitySystem;
|
||||||
mod monster_ai_system;
|
mod monster_ai_system;
|
||||||
|
|
@ -22,7 +22,10 @@ mod damage_system;
|
||||||
use damage_system::*;
|
use damage_system::*;
|
||||||
mod melee_combat_system;
|
mod melee_combat_system;
|
||||||
use melee_combat_system::MeleeCombatSystem;
|
use melee_combat_system::MeleeCombatSystem;
|
||||||
|
mod inventory_system;
|
||||||
|
use inventory_system::*;
|
||||||
|
|
||||||
|
// Embedded resources for use in wasm build
|
||||||
rltk::embedded_resource!(TERMINAL8X8, "../resources/terminal8x8.jpg");
|
rltk::embedded_resource!(TERMINAL8X8, "../resources/terminal8x8.jpg");
|
||||||
rltk::embedded_resource!(SCANLINESFS, "../resources/scanlines.fs");
|
rltk::embedded_resource!(SCANLINESFS, "../resources/scanlines.fs");
|
||||||
rltk::embedded_resource!(SCANLINESVS, "../resources/scanlines.vs");
|
rltk::embedded_resource!(SCANLINESVS, "../resources/scanlines.vs");
|
||||||
|
|
@ -33,6 +36,7 @@ pub enum RunState {
|
||||||
PreRun,
|
PreRun,
|
||||||
PlayerTurn,
|
PlayerTurn,
|
||||||
MonsterTurn,
|
MonsterTurn,
|
||||||
|
ShowInventory,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct State {
|
pub struct State {
|
||||||
|
|
@ -51,14 +55,35 @@ impl State {
|
||||||
melee_system.run_now(&self.ecs);
|
melee_system.run_now(&self.ecs);
|
||||||
let mut damage_system = DamageSystem {};
|
let mut damage_system = DamageSystem {};
|
||||||
damage_system.run_now(&self.ecs);
|
damage_system.run_now(&self.ecs);
|
||||||
|
let mut inventory_system = ItemCollectionSystem {};
|
||||||
|
inventory_system.run_now(&self.ecs);
|
||||||
|
let mut potion_system = PotionUseSystem {};
|
||||||
|
potion_system.run_now(&self.ecs);
|
||||||
self.ecs.maintain();
|
self.ecs.maintain();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GameState for State {
|
impl GameState for State {
|
||||||
fn tick(&mut self, ctx: &mut Rltk) {
|
fn tick(&mut self, ctx: &mut Rltk) {
|
||||||
|
// Clear screen
|
||||||
ctx.cls();
|
ctx.cls();
|
||||||
|
|
||||||
|
// Draw map and ui
|
||||||
|
draw_map(&self.ecs, ctx);
|
||||||
|
{
|
||||||
|
let positions = self.ecs.read_storage::<Position>();
|
||||||
|
let renderables = self.ecs.read_storage::<Renderable>();
|
||||||
|
let map = self.ecs.fetch::<Map>();
|
||||||
|
|
||||||
|
for (pos, render) in (&positions, &renderables).join() {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
gui::draw_ui(&self.ecs, ctx);
|
||||||
|
}
|
||||||
|
|
||||||
let mut new_runstate;
|
let mut new_runstate;
|
||||||
{
|
{
|
||||||
let runstate = self.ecs.fetch::<RunState>();
|
let runstate = self.ecs.fetch::<RunState>();
|
||||||
|
|
@ -68,6 +93,7 @@ impl GameState for State {
|
||||||
match new_runstate {
|
match new_runstate {
|
||||||
RunState::PreRun => {
|
RunState::PreRun => {
|
||||||
self.run_systems();
|
self.run_systems();
|
||||||
|
self.ecs.maintain();
|
||||||
new_runstate = RunState::AwaitingInput;
|
new_runstate = RunState::AwaitingInput;
|
||||||
}
|
}
|
||||||
RunState::AwaitingInput => {
|
RunState::AwaitingInput => {
|
||||||
|
|
@ -75,12 +101,29 @@ impl GameState for State {
|
||||||
}
|
}
|
||||||
RunState::PlayerTurn => {
|
RunState::PlayerTurn => {
|
||||||
self.run_systems();
|
self.run_systems();
|
||||||
|
self.ecs.maintain();
|
||||||
new_runstate = RunState::MonsterTurn;
|
new_runstate = RunState::MonsterTurn;
|
||||||
}
|
}
|
||||||
RunState::MonsterTurn => {
|
RunState::MonsterTurn => {
|
||||||
self.run_systems();
|
self.run_systems();
|
||||||
|
self.ecs.maintain();
|
||||||
new_runstate = RunState::AwaitingInput;
|
new_runstate = RunState::AwaitingInput;
|
||||||
}
|
}
|
||||||
|
RunState::ShowInventory => {
|
||||||
|
let result = gui::show_inventory(self, ctx);
|
||||||
|
match result.0 {
|
||||||
|
gui::ItemMenuResult::Cancel => new_runstate = RunState::AwaitingInput,
|
||||||
|
gui::ItemMenuResult::NoResponse => {}
|
||||||
|
gui::ItemMenuResult::Selected => {
|
||||||
|
let item_entity = result.1.unwrap();
|
||||||
|
let mut intent = self.ecs.write_storage::<WantsToDrinkPotion>();
|
||||||
|
intent
|
||||||
|
.insert(*self.ecs.fetch::<Entity>(), WantsToDrinkPotion { potion: item_entity })
|
||||||
|
.expect("Unable to insert intent.");
|
||||||
|
new_runstate = RunState::PlayerTurn;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
@ -89,20 +132,6 @@ impl GameState for State {
|
||||||
}
|
}
|
||||||
|
|
||||||
damage_system::delete_the_dead(&mut self.ecs);
|
damage_system::delete_the_dead(&mut self.ecs);
|
||||||
draw_map(&self.ecs, ctx);
|
|
||||||
|
|
||||||
let positions = self.ecs.read_storage::<Position>();
|
|
||||||
let renderables = self.ecs.read_storage::<Renderable>();
|
|
||||||
let map = self.ecs.fetch::<Map>();
|
|
||||||
|
|
||||||
for (pos, render) in (&positions, &renderables).join() {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
gui::draw_ui(&self.ecs, ctx);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -126,47 +155,20 @@ fn main() -> rltk::BError {
|
||||||
gs.ecs.register::<CombatStats>();
|
gs.ecs.register::<CombatStats>();
|
||||||
gs.ecs.register::<WantsToMelee>();
|
gs.ecs.register::<WantsToMelee>();
|
||||||
gs.ecs.register::<SufferDamage>();
|
gs.ecs.register::<SufferDamage>();
|
||||||
|
gs.ecs.register::<Item>();
|
||||||
|
gs.ecs.register::<Potion>();
|
||||||
|
gs.ecs.register::<InBackpack>();
|
||||||
|
gs.ecs.register::<WantsToPickupItem>();
|
||||||
|
gs.ecs.register::<WantsToDrinkPotion>();
|
||||||
|
|
||||||
let map = Map::new_map_rooms_and_corridors();
|
let map = Map::new_map_rooms_and_corridors();
|
||||||
let (player_x, player_y) = map.rooms[0].centre();
|
let (player_x, player_y) = map.rooms[0].centre();
|
||||||
|
|
||||||
let player_entity = gs
|
let player_entity = spawner::player(&mut gs.ecs, player_x, player_y);
|
||||||
.ecs
|
|
||||||
.create_entity()
|
|
||||||
.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(Viewshed { visible_tiles: Vec::new(), range: 8, dirty: true })
|
|
||||||
.with(Name { name: "Player".to_string() })
|
|
||||||
.with(CombatStats { max_hp: 30, hp: 30, defence: 2, power: 5 })
|
|
||||||
.build();
|
|
||||||
|
|
||||||
let mut rng = rltk::RandomNumberGenerator::new();
|
gs.ecs.insert(rltk::RandomNumberGenerator::new());
|
||||||
for (i, room) in map.rooms.iter().skip(1).enumerate() {
|
for room in map.rooms.iter().skip(1) {
|
||||||
let (x, y) = room.centre();
|
spawner::spawn_room(&mut gs.ecs, room);
|
||||||
let glyph: rltk::FontCharType;
|
|
||||||
let name: String;
|
|
||||||
let roll = rng.roll_dice(1, 2);
|
|
||||||
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: 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, defence: 1, power: 4 })
|
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gs.ecs.insert(map);
|
gs.ecs.insert(map);
|
||||||
|
|
|
||||||
|
|
@ -179,7 +179,7 @@ impl BaseMap for Map {
|
||||||
exits.push((idx - w + 1, 1.45));
|
exits.push((idx - w + 1, 1.45));
|
||||||
}
|
}
|
||||||
if self.is_exit_valid(x - 1, y + 1) {
|
if self.is_exit_valid(x - 1, y + 1) {
|
||||||
exits.push((idx - w - 1, 1.45));
|
exits.push((idx + w - 1, 1.45));
|
||||||
}
|
}
|
||||||
if self.is_exit_valid(x + 1, y + 1) {
|
if self.is_exit_valid(x + 1, y + 1) {
|
||||||
exits.push((idx + w + 1, 1.45));
|
exits.push((idx + w + 1, 1.45));
|
||||||
|
|
|
||||||
|
|
@ -42,11 +42,7 @@ impl<'a> System<'a> for MonsterAI {
|
||||||
.insert(entity, WantsToMelee { target: *player_entity })
|
.insert(entity, WantsToMelee { target: *player_entity })
|
||||||
.expect("Unable to insert attack.");
|
.expect("Unable to insert attack.");
|
||||||
} else if viewshed.visible_tiles.contains(&*player_pos) {
|
} else if viewshed.visible_tiles.contains(&*player_pos) {
|
||||||
let path = rltk::a_star_search(
|
let path = rltk::a_star_search(map.xy_idx(pos.x, pos.y), map.xy_idx(player_pos.x, player_pos.y), &*map);
|
||||||
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 {
|
if path.success && path.steps.len() > 1 {
|
||||||
let mut idx = map.xy_idx(pos.x, pos.y);
|
let mut idx = map.xy_idx(pos.x, pos.y);
|
||||||
map.blocked[idx] = false;
|
map.blocked[idx] = false;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,7 @@
|
||||||
use super::{CombatStats, Map, Player, Position, RunState, State, Viewshed, WantsToMelee, MAPHEIGHT, MAPWIDTH};
|
use super::{
|
||||||
|
gamelog::GameLog, CombatStats, Item, Map, Player, Position, RunState, State, Viewshed, WantsToMelee,
|
||||||
|
WantsToPickupItem, MAPHEIGHT, MAPWIDTH,
|
||||||
|
};
|
||||||
use rltk::{Point, Rltk, VirtualKeyCode};
|
use rltk::{Point, Rltk, VirtualKeyCode};
|
||||||
use specs::prelude::*;
|
use specs::prelude::*;
|
||||||
use std::cmp::{max, min};
|
use std::cmp::{max, min};
|
||||||
|
|
@ -42,12 +45,36 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_item(ecs: &mut World) {
|
||||||
|
let player_pos = ecs.fetch::<Point>();
|
||||||
|
let player_entity = ecs.fetch::<Entity>();
|
||||||
|
let entities = ecs.entities();
|
||||||
|
let items = ecs.read_storage::<Item>();
|
||||||
|
let positions = ecs.read_storage::<Position>();
|
||||||
|
let mut gamelog = ecs.fetch_mut::<GameLog>();
|
||||||
|
|
||||||
|
let mut target_item: Option<Entity> = None;
|
||||||
|
for (item_entity, _item, position) in (&entities, &items, &positions).join() {
|
||||||
|
if position.x == player_pos.x && position.y == player_pos.y {
|
||||||
|
target_item = Some(item_entity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
match target_item {
|
||||||
|
None => gamelog.entries.push("There is nothing to pick up.".to_string()),
|
||||||
|
Some(item) => {
|
||||||
|
let mut pickup = ecs.write_storage::<WantsToPickupItem>();
|
||||||
|
pickup
|
||||||
|
.insert(*player_entity, WantsToPickupItem { collected_by: *player_entity, item })
|
||||||
|
.expect("Unable to insert want to pickup item.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState {
|
pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState {
|
||||||
// Player movement
|
// Player movement
|
||||||
match ctx.key {
|
match ctx.key {
|
||||||
None => {
|
None => return RunState::AwaitingInput,
|
||||||
return RunState::AwaitingInput;
|
|
||||||
}
|
|
||||||
Some(key) => match key {
|
Some(key) => match key {
|
||||||
// Cardinals
|
// Cardinals
|
||||||
VirtualKeyCode::Left | VirtualKeyCode::Numpad4 | VirtualKeyCode::H => {
|
VirtualKeyCode::Left | VirtualKeyCode::Numpad4 | VirtualKeyCode::H => {
|
||||||
|
|
@ -67,8 +94,11 @@ pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState {
|
||||||
VirtualKeyCode::Numpad7 | VirtualKeyCode::U => try_move_player(-1, -1, &mut gs.ecs),
|
VirtualKeyCode::Numpad7 | VirtualKeyCode::U => try_move_player(-1, -1, &mut gs.ecs),
|
||||||
VirtualKeyCode::Numpad3 | VirtualKeyCode::N => try_move_player(1, 1, &mut gs.ecs),
|
VirtualKeyCode::Numpad3 | VirtualKeyCode::N => try_move_player(1, 1, &mut gs.ecs),
|
||||||
VirtualKeyCode::Numpad1 | VirtualKeyCode::B => try_move_player(-1, 1, &mut gs.ecs),
|
VirtualKeyCode::Numpad1 | VirtualKeyCode::B => try_move_player(-1, 1, &mut gs.ecs),
|
||||||
|
// Items
|
||||||
|
VirtualKeyCode::G => get_item(&mut gs.ecs),
|
||||||
|
VirtualKeyCode::I => return RunState::ShowInventory,
|
||||||
_ => {
|
_ => {
|
||||||
return RunState::PlayerTurn;
|
return RunState::AwaitingInput;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
||||||
113
src/spawner.rs
Normal file
113
src/spawner.rs
Normal file
|
|
@ -0,0 +1,113 @@
|
||||||
|
use super::{
|
||||||
|
BlocksTile, CombatStats, Item, Monster, Name, Player, Position, Potion, Rect, Renderable, Viewshed, MAPWIDTH,
|
||||||
|
};
|
||||||
|
use rltk::{RandomNumberGenerator, RGB};
|
||||||
|
use specs::prelude::*;
|
||||||
|
|
||||||
|
/// Spawns the player and returns his/her entity object.
|
||||||
|
pub fn player(ecs: &mut World, player_x: i32, player_y: i32) -> Entity {
|
||||||
|
ecs.create_entity()
|
||||||
|
.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(Viewshed { visible_tiles: Vec::new(), range: 8, dirty: true })
|
||||||
|
.with(Name { name: "hero (you)".to_string() })
|
||||||
|
.with(CombatStats { max_hp: 30, hp: 30, defence: 2, power: 5 })
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Spawns a random monster at a given loc
|
||||||
|
pub fn random_monster(ecs: &mut World, x: i32, y: i32) {
|
||||||
|
let roll: i32;
|
||||||
|
{
|
||||||
|
let mut rng = ecs.write_resource::<RandomNumberGenerator>();
|
||||||
|
roll = rng.roll_dice(1, 2);
|
||||||
|
}
|
||||||
|
match roll {
|
||||||
|
1 => orc(ecs, x, y),
|
||||||
|
_ => goblin(ecs, x, y),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const MAX_MONSTERS: i32 = 4;
|
||||||
|
const MAX_ITEMS: i32 = 2;
|
||||||
|
|
||||||
|
fn monster<S: ToString>(ecs: &mut World, x: i32, y: i32, glyph: rltk::FontCharType, name: S) {
|
||||||
|
ecs.create_entity()
|
||||||
|
.with(Position { x, y })
|
||||||
|
.with(Renderable { glyph: 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: name.to_string() })
|
||||||
|
.with(BlocksTile {})
|
||||||
|
.with(CombatStats { max_hp: 16, hp: 16, defence: 1, power: 4 })
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn orc(ecs: &mut World, x: i32, y: i32) {
|
||||||
|
monster(ecs, x, y, rltk::to_cp437('o'), "orc");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn goblin(ecs: &mut World, x: i32, y: i32) {
|
||||||
|
monster(ecs, x, y, rltk::to_cp437('g'), "goblin");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn spawn_room(ecs: &mut World, room: &Rect) {
|
||||||
|
let mut monster_spawn_points: Vec<usize> = Vec::new();
|
||||||
|
let mut item_spawn_points: Vec<usize> = Vec::new();
|
||||||
|
|
||||||
|
// Scope for borrow checker
|
||||||
|
{
|
||||||
|
let mut rng = ecs.write_resource::<RandomNumberGenerator>();
|
||||||
|
let num_monsters = rng.roll_dice(1, MAX_MONSTERS + 2) - 3;
|
||||||
|
let num_items = rng.roll_dice(1, MAX_ITEMS + 2) - 3;
|
||||||
|
|
||||||
|
for _i in 0..num_monsters {
|
||||||
|
let mut added = false;
|
||||||
|
while !added {
|
||||||
|
let x = (room.x1 + rng.roll_dice(1, i32::abs(room.x2 - room.x1))) as usize;
|
||||||
|
let y = (room.y1 + rng.roll_dice(1, i32::abs(room.y2 - room.y1))) as usize;
|
||||||
|
let idx = (y * MAPWIDTH) + x;
|
||||||
|
if !monster_spawn_points.contains(&idx) {
|
||||||
|
monster_spawn_points.push(idx);
|
||||||
|
added = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _i in 0..num_items {
|
||||||
|
let mut added = false;
|
||||||
|
while !added {
|
||||||
|
let x = (room.x1 + rng.roll_dice(1, i32::abs(room.x2 - room.x1))) as usize;
|
||||||
|
let y = (room.y1 + rng.roll_dice(1, i32::abs(room.y2 - room.y1))) as usize;
|
||||||
|
let idx = (y * MAPWIDTH) + x;
|
||||||
|
if !item_spawn_points.contains(&idx) {
|
||||||
|
item_spawn_points.push(idx);
|
||||||
|
added = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Spawn
|
||||||
|
for idx in monster_spawn_points.iter() {
|
||||||
|
let x = *idx % MAPWIDTH;
|
||||||
|
let y = *idx / MAPWIDTH;
|
||||||
|
random_monster(ecs, x as i32, y as i32);
|
||||||
|
}
|
||||||
|
for idx in item_spawn_points.iter() {
|
||||||
|
let x = *idx % MAPWIDTH;
|
||||||
|
let y = *idx / MAPWIDTH;
|
||||||
|
health_potion(ecs, x as i32, y as i32);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn health_potion(ecs: &mut World, x: i32, y: i32) {
|
||||||
|
ecs.create_entity()
|
||||||
|
.with(Position { x, y })
|
||||||
|
.with(Renderable { glyph: rltk::to_cp437('i'), fg: RGB::named(rltk::MAGENTA), bg: RGB::named(rltk::BLACK) })
|
||||||
|
.with(Name { name: "health potion".to_string() })
|
||||||
|
.with(Item {})
|
||||||
|
.with(Potion { heal_amount: 8 })
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use super::{Map, Player, Position, Viewshed};
|
use super::{Map, Player, Position, Viewshed};
|
||||||
use rltk::{field_of_view, Point};
|
use rltk::{FieldOfViewAlg::SymmetricShadowcasting, Point};
|
||||||
use specs::prelude::*;
|
use specs::prelude::*;
|
||||||
|
|
||||||
pub struct VisibilitySystem {}
|
pub struct VisibilitySystem {}
|
||||||
|
|
@ -19,8 +19,11 @@ impl<'a> System<'a> for VisibilitySystem {
|
||||||
for (ent, viewshed, pos) in (&entities, &mut viewshed, &pos).join() {
|
for (ent, viewshed, pos) in (&entities, &mut viewshed, &pos).join() {
|
||||||
if viewshed.dirty {
|
if viewshed.dirty {
|
||||||
viewshed.dirty = false;
|
viewshed.dirty = false;
|
||||||
viewshed.visible_tiles.clear();
|
// FIXME: SymmetricShadowcasting seems to be responsible for an infrequent crash --
|
||||||
viewshed.visible_tiles = field_of_view(Point::new(pos.x, pos.y), viewshed.range, &*map);
|
// but it could be unrelated to the FieldOfViewAlg being used. Needs some more testing!
|
||||||
|
// Appeared first after switching, but can't seem to reproduce it.
|
||||||
|
viewshed.visible_tiles =
|
||||||
|
SymmetricShadowcasting.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);
|
viewshed.visible_tiles.retain(|p| p.x >= 0 && p.x < map.width && p.y >= 0 && p.y < map.height);
|
||||||
|
|
||||||
// If this is the player, reveal what they can see
|
// If this is the player, reveal what they can see
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue