roguelike-game/src/spawner.rs

221 lines
6.3 KiB
Rust
Raw Normal View History

2021-11-05 14:32:14 -04:00
use crate::components::{
AreaOfEffect, BlocksTile, CombatStats, Confusion, Consumable, InflictsDamage, Item, Monster,
Name, Player, Position, ProvidesHealing, Ranged, Renderable, Viewshed,
};
2021-11-05 14:32:14 -04:00
use crate::{Rect, MAP_WIDTH};
use rltk::{RandomNumberGenerator, RGB};
use specs::prelude::*;
const MAX_MONSTERS: i32 = 4;
const MAX_ITEMS: i32 = 2;
/// Spawns the player and returns their 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),
2021-11-04 09:54:38 -04:00
render_order: 0,
})
.with(Player {})
2021-11-05 10:41:47 -04:00
.with(Viewshed::default())
.with(Name::new("Player"))
.with(CombatStats {
max_hp: 30,
hp: 30,
defense: 2,
power: 5,
})
.build()
}
/// Spawns a random monster at a given location
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: rltk::FontCharType, name: S) {
ecs.create_entity()
.with(Position { x, y })
.with(Renderable {
glyph,
fg: RGB::named(rltk::RED),
bg: RGB::named(rltk::BLACK),
2021-11-04 09:54:38 -04:00
render_order: 1,
})
2021-11-05 10:41:47 -04:00
.with(Viewshed::default())
.with(Monster {})
2021-11-05 10:41:47 -04:00
.with(Name::new(name))
.with(BlocksTile {})
.with(CombatStats {
max_hp: 16,
hp: 16,
defense: 1,
power: 4,
})
.build();
}
/// fills a room with stuff!
pub fn spawn_room(ecs: &mut World, room: &Rect) {
let mut monster_spawn_points: Vec<usize> = Vec::new();
2021-11-03 15:11:19 -04:00
let mut item_spawn_points: Vec<usize> = Vec::new();
// Scope to keep the borrow checker happy
{
let mut rng = ecs.write_resource::<RandomNumberGenerator>();
let num_monsters = rng.roll_dice(1, MAX_MONSTERS + 2) - 3;
2021-11-03 15:11:19 -04:00
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;
}
}
}
2021-11-03 15:11:19 -04:00
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;
}
}
}
}
// Actually spawn the monsters
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);
}
2021-11-03 15:11:19 -04:00
2021-11-05 10:42:44 -04:00
// Actually spawn the items
2021-11-03 15:11:19 -04:00
for idx in item_spawn_points.iter() {
let x = *idx % MAP_WIDTH;
let y = *idx / MAP_WIDTH;
2021-11-05 10:42:44 -04:00
random_item(ecs, x as i32, y as i32);
2021-11-03 15:11:19 -04:00
}
}
fn health_potion(ecs: &mut World, x: i32, y: i32) {
ecs.create_entity()
.with(Position { x, y })
.with(Renderable {
glyph: rltk::to_cp437('¡'),
fg: RGB::named(rltk::MAGENTA),
bg: RGB::named(rltk::BLACK),
2021-11-04 09:54:38 -04:00
render_order: 2,
2021-11-03 15:11:19 -04:00
})
2021-11-05 10:42:44 -04:00
.with(Name::new("Health Potion"))
2021-11-03 15:11:19 -04:00
.with(Item {})
.with(Consumable {})
.with(ProvidesHealing { heal_amount: 8 })
2021-11-03 15:11:19 -04:00
.build();
}
2021-11-05 10:42:44 -04:00
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::new("Magic Missile Scroll"))
.with(Item {})
.with(Consumable {})
.with(Ranged { range: 6 })
.with(InflictsDamage { damage: 8 })
.build();
}
fn random_item(ecs: &mut World, x: i32, y: i32) {
let roll: i32;
{
let mut rng = ecs.write_resource::<RandomNumberGenerator>();
2021-11-05 14:32:14 -04:00
roll = rng.roll_dice(1, 4);
2021-11-05 10:42:44 -04:00
}
match roll {
1 => health_potion(ecs, x, y),
2021-11-05 13:12:22 -04:00
2 => fireball_scroll(ecs, x, y),
2021-11-05 14:32:14 -04:00
3 => confusion_scroll(ecs, x, y),
2021-11-05 10:42:44 -04:00
_ => magic_missile_scroll(ecs, x, y),
}
}
2021-11-05 13:12:22 -04:00
fn fireball_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::ORANGE),
bg: RGB::named(rltk::BLACK),
render_order: 2,
})
.with(Name::new("Fireball Scroll"))
.with(Item {})
.with(Consumable {})
.with(Ranged { range: 6 })
.with(InflictsDamage { damage: 20 })
.with(AreaOfEffect { radius: 3 })
.build();
}
2021-11-05 14:32:14 -04:00
fn confusion_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::PINK),
bg: RGB::named(rltk::BLACK),
render_order: 2,
})
.with(Name::new("Confusion Scroll"))
.with(Item {})
.with(Consumable {})
.with(Ranged { range: 6 })
.with(Confusion { turns: 4 })
.build();
}