2022-01-26 15:42:42 +01:00
|
|
|
use rltk::{FontCharType, RandomNumberGenerator, RGB};
|
|
|
|
use specs::prelude::*;
|
2022-01-27 18:28:40 +01:00
|
|
|
use specs::saveload::{MarkedBuilder, SimpleMarker};
|
2022-01-26 15:42:42 +01:00
|
|
|
|
|
|
|
use crate::rect::Rect;
|
2022-01-27 22:38:44 +01:00
|
|
|
use crate::{
|
|
|
|
AreaOfEffect, BlocksTile, CombatStats, Confusion, Consumable, InflictsDamage, Item, Monster,
|
|
|
|
Name, Player, Position, ProvidesHealing, Ranged, Renderable, SerializeMe, Viewshed, MAP_WIDTH,
|
|
|
|
MAX_ITEMS, MAX_MONSTER,
|
|
|
|
};
|
2022-01-26 15:42:42 +01:00
|
|
|
|
|
|
|
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,
|
|
|
|
})
|
2022-01-27 18:28:40 +01:00
|
|
|
.marked::<SimpleMarker<SerializeMe>>()
|
2022-01-26 15:42:42 +01:00
|
|
|
.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,
|
|
|
|
})
|
2022-01-27 18:28:40 +01:00
|
|
|
.marked::<SimpleMarker<SerializeMe>>()
|
2022-01-26 15:42:42 +01:00
|
|
|
.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;
|
2022-01-27 14:31:31 +01:00
|
|
|
random_item(ecs, x as i32, y as i32);
|
2022-01-26 15:42:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {})
|
2022-01-27 14:31:31 +01:00
|
|
|
.with(Consumable {})
|
|
|
|
.with(ProvidesHealing { heal_amount: 8 })
|
2022-01-27 18:28:40 +01:00
|
|
|
.marked::<SimpleMarker<SerializeMe>>()
|
2022-01-26 15:42:42 +01:00
|
|
|
.build();
|
|
|
|
}
|
2022-01-27 14:31:31 +01:00
|
|
|
|
|
|
|
pub fn magic_missile_scroll(ecs: &mut World, x: i32, y: i32) {
|
|
|
|
ecs.create_entity()
|
|
|
|
.with(Position { x, y })
|
|
|
|
.with(Renderable {
|
|
|
|
glyph: rltk::to_cp437(')'),
|
|
|
|
fg: RGB::named(rltk::CYAN),
|
|
|
|
bg: RGB::named(rltk::BLACK),
|
|
|
|
render_order: 2,
|
|
|
|
})
|
|
|
|
.with(Name {
|
|
|
|
name: "Magic Missile Scroll".to_string(),
|
|
|
|
})
|
|
|
|
.with(Item {})
|
|
|
|
.with(Consumable {})
|
|
|
|
.with(Ranged { range: 6 })
|
2022-01-27 19:42:51 +01:00
|
|
|
.with(InflictsDamage { damage: 20 })
|
2022-01-27 18:28:40 +01:00
|
|
|
.marked::<SimpleMarker<SerializeMe>>()
|
2022-01-27 14:31:31 +01:00
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fireball_scroll(ecs: &mut World, x: i32, y: i32) {
|
|
|
|
ecs.create_entity()
|
|
|
|
.with(Position { x, y })
|
|
|
|
.with(Renderable {
|
|
|
|
glyph: rltk::to_cp437(')'),
|
2022-01-27 19:42:51 +01:00
|
|
|
fg: RGB::named(rltk::ORANGE),
|
2022-01-27 14:31:31 +01:00
|
|
|
bg: RGB::named(rltk::BLACK),
|
|
|
|
render_order: 2,
|
|
|
|
})
|
|
|
|
.with(Name {
|
|
|
|
name: "Fireball Scroll".to_string(),
|
|
|
|
})
|
|
|
|
.with(Item {})
|
|
|
|
.with(Consumable {})
|
|
|
|
.with(Ranged { range: 6 })
|
|
|
|
.with(InflictsDamage { damage: 20 })
|
|
|
|
.with(AreaOfEffect { radius: 3 })
|
2022-01-27 18:28:40 +01:00
|
|
|
.marked::<SimpleMarker<SerializeMe>>()
|
2022-01-27 14:31:31 +01:00
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn confusion_scroll(ecs: &mut World, x: i32, y: i32) {
|
|
|
|
ecs.create_entity()
|
|
|
|
.with(Position { x, y })
|
|
|
|
.with(Renderable {
|
|
|
|
glyph: rltk::to_cp437(')'),
|
2022-01-27 19:42:51 +01:00
|
|
|
fg: RGB::named(rltk::PINK),
|
2022-01-27 14:31:31 +01:00
|
|
|
bg: RGB::named(rltk::BLACK),
|
|
|
|
render_order: 2,
|
|
|
|
})
|
|
|
|
.with(Name {
|
|
|
|
name: "Confusion Scroll".to_string(),
|
|
|
|
})
|
|
|
|
.with(Item {})
|
|
|
|
.with(Consumable {})
|
|
|
|
.with(Ranged { range: 6 })
|
|
|
|
.with(Confusion { turns: 4 })
|
2022-01-27 18:28:40 +01:00
|
|
|
.marked::<SimpleMarker<SerializeMe>>()
|
2022-01-27 14:31:31 +01:00
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn random_item(ecs: &mut World, x: i32, y: i32) {
|
|
|
|
let roll: i32;
|
|
|
|
{
|
|
|
|
let mut rng = ecs.write_resource::<RandomNumberGenerator>();
|
|
|
|
roll = rng.roll_dice(1, 4);
|
|
|
|
}
|
|
|
|
match roll {
|
|
|
|
1 => health_potion(ecs, x, y),
|
|
|
|
2 => fireball_scroll(ecs, x, y),
|
|
|
|
3 => confusion_scroll(ecs, x, y),
|
|
|
|
_ => magic_missile_scroll(ecs, x, y),
|
|
|
|
}
|
|
|
|
}
|