Add chapter 2.9
This commit is contained in:
parent
1fe967250e
commit
bda34abfa3
8
.idea/.gitignore
vendored
Normal file
8
.idea/.gitignore
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/roguelike.iml" filepath="$PROJECT_DIR$/.idea/roguelike.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
11
.idea/roguelike.iml
Normal file
11
.idea/roguelike.iml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="CPP_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
@ -13,6 +13,7 @@ pub struct Renderable {
|
|||||||
pub glyph: rltk::FontCharType,
|
pub glyph: rltk::FontCharType,
|
||||||
pub fg: RGB,
|
pub fg: RGB,
|
||||||
pub bg: RGB,
|
pub bg: RGB,
|
||||||
|
pub render_order: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component, Debug)]
|
#[derive(Component, Debug)]
|
||||||
@ -66,3 +67,34 @@ impl SufferDamage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Debug)]
|
||||||
|
pub struct Item {}
|
||||||
|
|
||||||
|
#[derive(Component, Debug)]
|
||||||
|
pub struct Potion {
|
||||||
|
pub heal_amount: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Debug)]
|
||||||
|
pub struct InBackpack {
|
||||||
|
pub owner: Entity,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Debug, Clone)]
|
||||||
|
pub struct WantsToPickupItem {
|
||||||
|
pub collected_by: Entity,
|
||||||
|
pub item: Entity,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Debug, Clone)]
|
||||||
|
pub struct WantsToDrinkPotion {
|
||||||
|
pub potion: Entity,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Debug, Clone)]
|
||||||
|
pub struct WantsToDropItem {
|
||||||
|
pub item: Entity,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
148
src/gui.rs
148
src/gui.rs
@ -1,13 +1,14 @@
|
|||||||
|
use rltk::{BTerm, Point, VirtualKeyCode};
|
||||||
|
use rltk::RGB;
|
||||||
|
use rltk::Rltk;
|
||||||
|
use specs::prelude::*;
|
||||||
|
|
||||||
|
use crate::{CombatStats, InBackpack, State};
|
||||||
use crate::gamelog::GameLog;
|
use crate::gamelog::GameLog;
|
||||||
use crate::CombatStats;
|
|
||||||
use crate::Map;
|
use crate::Map;
|
||||||
use crate::Name;
|
use crate::Name;
|
||||||
use crate::Player;
|
use crate::Player;
|
||||||
use crate::Position;
|
use crate::Position;
|
||||||
use rltk::Point;
|
|
||||||
use rltk::Rltk;
|
|
||||||
use rltk::RGB;
|
|
||||||
use specs::prelude::*;
|
|
||||||
|
|
||||||
pub fn draw_ui(ecs: &World, ctx: &mut Rltk) {
|
pub fn draw_ui(ecs: &World, ctx: &mut Rltk) {
|
||||||
ctx.draw_box(
|
ctx.draw_box(
|
||||||
@ -150,3 +151,140 @@ 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),
|
||||||
|
"ESCAPE 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn drop_item_menu(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), "Drop Which Item?");
|
||||||
|
ctx.print_color(18, y + count as i32 + 1, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "ESCAPE 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
116
src/inventory_system.rs
Normal file
116
src/inventory_system.rs
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
use rltk::Rltk;
|
||||||
|
use specs::prelude::*;
|
||||||
|
|
||||||
|
use crate::{CombatStats, InBackpack, Name, Position, Potion, State, WantsToDrinkPotion, WantsToDropItem, WantsToPickupItem};
|
||||||
|
use crate::gamelog::GameLog;
|
||||||
|
use crate::gui::ItemMenuResult;
|
||||||
|
|
||||||
|
pub struct ItemCollectionSystem {}
|
||||||
|
|
||||||
|
impl<'a> System<'a> for ItemCollectionSystem {
|
||||||
|
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 insert backpack entry");
|
||||||
|
|
||||||
|
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 {
|
||||||
|
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 drink the {}, healing {} hp.", names.get(drink.potion).unwrap().name, potion.heal_amount));
|
||||||
|
}
|
||||||
|
entities.delete(drink.potion).expect("Delete failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
wants_drink.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ItemDropSystem {}
|
||||||
|
|
||||||
|
impl<'a> System<'a> for ItemDropSystem {
|
||||||
|
type SystemData = (
|
||||||
|
ReadExpect<'a, Entity>,
|
||||||
|
WriteExpect<'a, GameLog>,
|
||||||
|
Entities<'a>,
|
||||||
|
WriteStorage<'a, WantsToDropItem>,
|
||||||
|
ReadStorage<'a, Name>,
|
||||||
|
WriteStorage<'a, Position>,
|
||||||
|
WriteStorage<'a, InBackpack>
|
||||||
|
);
|
||||||
|
|
||||||
|
fn run(&mut self, data: Self::SystemData) {
|
||||||
|
let (player_entity, mut gamelog, entities, mut wants_drop, names, mut positions, mut backpack) = data;
|
||||||
|
|
||||||
|
for (entity, to_drop) in (&entities, &wants_drop).join() {
|
||||||
|
let mut dropper_pos: Position = Position { x: 0, y: 0 };
|
||||||
|
{
|
||||||
|
let dropped_pos = positions.get(entity).unwrap();
|
||||||
|
dropper_pos.x = dropped_pos.x;
|
||||||
|
dropper_pos.y = dropped_pos.y;
|
||||||
|
}
|
||||||
|
positions.insert(to_drop.item, Position { x: dropper_pos.x, y: dropper_pos.y }).expect("Unable to insert position");
|
||||||
|
backpack.remove(to_drop.item);
|
||||||
|
|
||||||
|
if entity == *player_entity {
|
||||||
|
gamelog.entries.push(format!("You drop the {}.", names.get(to_drop.item).unwrap().name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
wants_drop.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
164
src/main.rs
164
src/main.rs
@ -1,3 +1,8 @@
|
|||||||
|
use std::thread::spawn;
|
||||||
|
|
||||||
|
use rltk::{GameState, Point, RGB};
|
||||||
|
use specs::prelude::*;
|
||||||
|
|
||||||
use components::*;
|
use components::*;
|
||||||
use damage_system::DamageSystem;
|
use damage_system::DamageSystem;
|
||||||
use map::*;
|
use map::*;
|
||||||
@ -5,21 +10,22 @@ use map_indexing_system::MapIndexingSystem;
|
|||||||
use melee_combat_system::MeleeCombatSystem;
|
use melee_combat_system::MeleeCombatSystem;
|
||||||
use monster_ai_system::*;
|
use monster_ai_system::*;
|
||||||
use player::*;
|
use player::*;
|
||||||
use rltk::{GameState, Point, RGB};
|
|
||||||
use visibility_system::*;
|
use visibility_system::*;
|
||||||
|
|
||||||
use specs::prelude::*;
|
use crate::inventory_system::{ItemCollectionSystem, ItemDropSystem, PotionUseSystem};
|
||||||
|
|
||||||
mod components;
|
mod components;
|
||||||
mod damage_system;
|
mod damage_system;
|
||||||
mod gamelog;
|
mod gamelog;
|
||||||
mod gui;
|
mod gui;
|
||||||
|
mod inventory_system;
|
||||||
mod map;
|
mod map;
|
||||||
mod map_indexing_system;
|
mod map_indexing_system;
|
||||||
mod melee_combat_system;
|
mod melee_combat_system;
|
||||||
mod monster_ai_system;
|
mod monster_ai_system;
|
||||||
mod player;
|
mod player;
|
||||||
mod rect;
|
mod rect;
|
||||||
|
mod spawner;
|
||||||
mod visibility_system;
|
mod visibility_system;
|
||||||
|
|
||||||
#[derive(PartialEq, Copy, Clone)]
|
#[derive(PartialEq, Copy, Clone)]
|
||||||
@ -28,6 +34,8 @@ pub enum RunState {
|
|||||||
PreRun,
|
PreRun,
|
||||||
PlayerTurn,
|
PlayerTurn,
|
||||||
MonsterTurn,
|
MonsterTurn,
|
||||||
|
ShowInventory,
|
||||||
|
ShowDropItem,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct State {
|
pub struct State {
|
||||||
@ -51,6 +59,15 @@ impl State {
|
|||||||
let mut damagesystem = DamageSystem {};
|
let mut damagesystem = DamageSystem {};
|
||||||
damagesystem.run_now(&self.ecs);
|
damagesystem.run_now(&self.ecs);
|
||||||
|
|
||||||
|
let mut inventory = ItemCollectionSystem {};
|
||||||
|
inventory.run_now(&self.ecs);
|
||||||
|
|
||||||
|
let mut potions = PotionUseSystem {};
|
||||||
|
potions.run_now(&self.ecs);
|
||||||
|
|
||||||
|
let mut drop_items = ItemDropSystem {};
|
||||||
|
drop_items.run_now(&self.ecs);
|
||||||
|
|
||||||
self.ecs.maintain();
|
self.ecs.maintain();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -59,6 +76,25 @@ impl GameState for State {
|
|||||||
fn tick(&mut self, ctx: &mut rltk::Rltk) {
|
fn tick(&mut self, ctx: &mut rltk::Rltk) {
|
||||||
ctx.cls();
|
ctx.cls();
|
||||||
|
|
||||||
|
{
|
||||||
|
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>();
|
||||||
|
|
||||||
|
let mut data = (&positions, &renderables).join().collect::<Vec<_>>();
|
||||||
|
data.sort_by(|&a, &b| b.1.render_order.cmp(&a.1.render_order));
|
||||||
|
for (pos, render) in data.iter() {
|
||||||
|
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 newrunstate;
|
let mut newrunstate;
|
||||||
{
|
{
|
||||||
let runstate = self.ecs.fetch::<RunState>();
|
let runstate = self.ecs.fetch::<RunState>();
|
||||||
@ -79,6 +115,32 @@ impl GameState for State {
|
|||||||
self.run_systems();
|
self.run_systems();
|
||||||
newrunstate = RunState::AwaitingInput;
|
newrunstate = RunState::AwaitingInput;
|
||||||
}
|
}
|
||||||
|
RunState::ShowInventory => {
|
||||||
|
let result = gui::show_inventory(self, ctx);
|
||||||
|
match result.0 {
|
||||||
|
gui::ItemMenuResult::Cancel => newrunstate = 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");
|
||||||
|
newrunstate = RunState::PlayerTurn;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
RunState::ShowDropItem => {
|
||||||
|
let result = gui::drop_item_menu(self, ctx);
|
||||||
|
match result.0 {
|
||||||
|
gui::ItemMenuResult::Cancel => newrunstate = RunState::AwaitingInput,
|
||||||
|
gui::ItemMenuResult::NoResponse => {}
|
||||||
|
gui::ItemMenuResult::Selected => {
|
||||||
|
let item_entity = result.1.unwrap();
|
||||||
|
let mut intent = self.ecs.write_storage::<WantsToDropItem>();
|
||||||
|
intent.insert(*self.ecs.fetch::<Entity>(), WantsToDropItem { item: item_entity }).expect("Unable to insert intent");
|
||||||
|
newrunstate = RunState::PlayerTurn;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -86,21 +148,6 @@ impl GameState for State {
|
|||||||
*runwriter = newrunstate;
|
*runwriter = newrunstate;
|
||||||
}
|
}
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,6 +162,7 @@ fn main() -> rltk::BError {
|
|||||||
let mut gs = State { ecs: World::new() };
|
let mut gs = State { ecs: World::new() };
|
||||||
|
|
||||||
gs.ecs.insert(RunState::PreRun);
|
gs.ecs.insert(RunState::PreRun);
|
||||||
|
gs.ecs.insert(rltk::RandomNumberGenerator::new());
|
||||||
gs.ecs.insert(gamelog::GameLog {
|
gs.ecs.insert(gamelog::GameLog {
|
||||||
entries: vec!["Welcome to Rusty Roguelike".to_string()],
|
entries: vec!["Welcome to Rusty Roguelike".to_string()],
|
||||||
});
|
});
|
||||||
@ -129,83 +177,21 @@ 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::<Potion>();
|
||||||
|
gs.ecs.register::<Item>();
|
||||||
|
gs.ecs.register::<InBackpack>();
|
||||||
|
gs.ecs.register::<WantsToPickupItem>();
|
||||||
|
gs.ecs.register::<WantsToDrinkPotion>();
|
||||||
|
gs.ecs.register::<WantsToDropItem>();
|
||||||
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.insert(Point::new(player_x, player_y));
|
||||||
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,
|
|
||||||
defense: 2,
|
|
||||||
power: 5,
|
|
||||||
})
|
|
||||||
.build();
|
|
||||||
|
|
||||||
gs.ecs.insert(player_entity);
|
gs.ecs.insert(player_entity);
|
||||||
|
|
||||||
let mut rng = rltk::RandomNumberGenerator::new();
|
for room in map.rooms.iter().skip(1) {
|
||||||
for (i, room) in map.rooms.iter().skip(1).enumerate() {
|
spawner::spawn_room(&mut gs.ecs, room);
|
||||||
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);
|
gs.ecs.insert(map);
|
||||||
|
@ -11,9 +11,11 @@ pub enum TileType {
|
|||||||
Floor,
|
Floor,
|
||||||
}
|
}
|
||||||
|
|
||||||
const MAP_WIDTH: usize = 80;
|
pub const MAP_WIDTH: usize = 80;
|
||||||
const MAP_HEIGHT: usize = 43;
|
pub const MAP_HEIGHT: usize = 43;
|
||||||
const MAP_COUNT: usize = MAP_HEIGHT * MAP_WIDTH;
|
pub const MAP_COUNT: usize = MAP_HEIGHT * MAP_WIDTH;
|
||||||
|
pub const MAX_MONSTER: i32 = 4;
|
||||||
|
pub const MAX_ITEMS: i32 = 2;
|
||||||
|
|
||||||
pub struct Map {
|
pub struct Map {
|
||||||
pub tiles: Vec<TileType>,
|
pub tiles: Vec<TileType>,
|
||||||
|
@ -1,11 +1,14 @@
|
|||||||
use crate::{
|
use std::cmp::{max, min};
|
||||||
components::{CombatStats, Player, Position, Viewshed, WantsToMelee},
|
|
||||||
map::{Map, TileType},
|
|
||||||
RunState, State,
|
|
||||||
};
|
|
||||||
use rltk::{console, Point, Rltk, VirtualKeyCode};
|
use rltk::{console, Point, Rltk, VirtualKeyCode};
|
||||||
use specs::prelude::*;
|
use specs::prelude::*;
|
||||||
use std::cmp::{max, min};
|
|
||||||
|
use crate::{
|
||||||
|
components::{CombatStats, Player, Position, Viewshed, WantsToMelee},
|
||||||
|
Item,
|
||||||
|
map::{Map, TileType}, RunState, State, WantsToPickupItem,
|
||||||
|
};
|
||||||
|
use crate::gamelog::GameLog;
|
||||||
|
|
||||||
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>();
|
||||||
@ -17,7 +20,7 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
|
|||||||
let map = ecs.fetch::<Map>();
|
let map = ecs.fetch::<Map>();
|
||||||
|
|
||||||
for (entity, _player, pos, viewshed) in
|
for (entity, _player, pos, viewshed) in
|
||||||
(&entities, &mut players, &mut positions, &mut viewsheds).join()
|
(&entities, &mut players, &mut positions, &mut viewsheds).join()
|
||||||
{
|
{
|
||||||
let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y);
|
let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y);
|
||||||
|
|
||||||
@ -47,6 +50,40 @@ 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 here 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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState {
|
pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState {
|
||||||
match ctx.key {
|
match ctx.key {
|
||||||
None => return RunState::AwaitingInput,
|
None => return RunState::AwaitingInput,
|
||||||
@ -59,6 +96,9 @@ pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState {
|
|||||||
VirtualKeyCode::U => 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::N => try_move_player(1, 1, &mut gs.ecs),
|
||||||
VirtualKeyCode::B => try_move_player(-1, 1, &mut gs.ecs),
|
VirtualKeyCode::B => try_move_player(-1, 1, &mut gs.ecs),
|
||||||
|
VirtualKeyCode::G => get_item(&mut gs.ecs),
|
||||||
|
VirtualKeyCode::I => return RunState::ShowInventory,
|
||||||
|
VirtualKeyCode::D => return RunState::ShowDropItem,
|
||||||
_ => return RunState::AwaitingInput,
|
_ => return RunState::AwaitingInput,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
152
src/spawner.rs
Normal file
152
src/spawner.rs
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
use rltk::{FontCharType, RandomNumberGenerator, RGB};
|
||||||
|
use specs::prelude::*;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
BlocksTile, CombatStats, Item, MAP_WIDTH, MAX_ITEMS, MAX_MONSTER, Monster, Name, Player, Position,
|
||||||
|
Potion, Renderable, Viewshed,
|
||||||
|
};
|
||||||
|
use crate::rect::Rect;
|
||||||
|
|
||||||
|
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),
|
||||||
|
render_order: 0,
|
||||||
|
})
|
||||||
|
.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()
|
||||||
|
}
|
||||||
|
|
||||||
|
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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn monster<S: ToString>(ecs: &mut World, x: i32, y: i32, glyph: FontCharType, name: S) {
|
||||||
|
ecs.create_entity()
|
||||||
|
.with(Position { x, y })
|
||||||
|
.with(Renderable {
|
||||||
|
glyph,
|
||||||
|
fg: RGB::named(rltk::RED),
|
||||||
|
bg: RGB::named(rltk::BLACK),
|
||||||
|
render_order: 1,
|
||||||
|
})
|
||||||
|
.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,
|
||||||
|
defense: 1,
|
||||||
|
power: 4,
|
||||||
|
})
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut rng = ecs.write_resource::<RandomNumberGenerator>();
|
||||||
|
let num_monsters = rng.roll_dice(1, MAX_MONSTER + 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 * MAP_WIDTH) + 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 * MAP_WIDTH) + x;
|
||||||
|
if !item_spawn_points.contains(&idx) {
|
||||||
|
item_spawn_points.push(idx);
|
||||||
|
added = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for idx in monster_spawn_points.iter() {
|
||||||
|
let x = *idx % MAP_WIDTH;
|
||||||
|
let y = *idx / MAP_WIDTH;
|
||||||
|
random_monster(ecs, x as i32, y as i32);
|
||||||
|
}
|
||||||
|
|
||||||
|
for idx in item_spawn_points.iter() {
|
||||||
|
let x = *idx % MAP_WIDTH;
|
||||||
|
let y = *idx / MAP_WIDTH;
|
||||||
|
health_potion(ecs, x as i32, y as i32);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub 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),
|
||||||
|
render_order: 2,
|
||||||
|
})
|
||||||
|
.with(Name {
|
||||||
|
name: "Health Potion".to_string(),
|
||||||
|
})
|
||||||
|
.with(Item {})
|
||||||
|
.with(Potion { heal_amount: 8 })
|
||||||
|
.build();
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user