Syncies
This commit is contained in:
parent
8eccea69f6
commit
95101b2c30
@ -38,7 +38,7 @@ pub enum RunState {
|
|||||||
AwaitingInput,
|
AwaitingInput,
|
||||||
PreRun,
|
PreRun,
|
||||||
PlayerTurn,
|
PlayerTurn,
|
||||||
MonsterTurn
|
MonsterTurn,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component, Debug)]
|
#[derive(Component, Debug)]
|
||||||
@ -49,17 +49,17 @@ pub struct CombatStats {
|
|||||||
pub max_hp: i32,
|
pub max_hp: i32,
|
||||||
pub hp: i32,
|
pub hp: i32,
|
||||||
pub defence: i32,
|
pub defence: i32,
|
||||||
pub power : i32
|
pub power: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component, Debug, Clone)]
|
#[derive(Component, Debug, Clone)]
|
||||||
pub struct WantsToMelee {
|
pub struct WantsToMelee {
|
||||||
pub target: Entity
|
pub target: Entity,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component, Debug)]
|
#[derive(Component, Debug)]
|
||||||
pub struct SufferDamage {
|
pub struct SufferDamage {
|
||||||
pub amount : Vec<i32>
|
pub amount: Vec<i32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SufferDamage {
|
impl SufferDamage {
|
||||||
@ -67,8 +67,32 @@ impl SufferDamage {
|
|||||||
if let Some(sufferring) = store.get_mut(victim) {
|
if let Some(sufferring) = store.get_mut(victim) {
|
||||||
sufferring.amount.push(amount);
|
sufferring.amount.push(amount);
|
||||||
} else {
|
} else {
|
||||||
let dmg = SufferDamage { amount : vec![amount] };
|
let dmg = SufferDamage {
|
||||||
|
amount: vec![amount],
|
||||||
|
};
|
||||||
store.insert(victim, dmg).expect("Unable to insert damage");
|
store.insert(victim, dmg).expect("Unable to insert damage");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[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,
|
||||||
|
}
|
||||||
|
|
||||||
|
// UPTO https://bfnightly.bracketproductions.com/chapter_9.html
|
||||||
|
// / Next, we'll put together a system to process WantsToPickupItem notices. We'll make a new file
|
||||||
|
|||||||
19
src/main.rs
19
src/main.rs
@ -38,7 +38,7 @@ mod spawner;
|
|||||||
use spawner::*;
|
use spawner::*;
|
||||||
|
|
||||||
pub struct State {
|
pub struct State {
|
||||||
pub ecs: World
|
pub ecs: World,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
@ -112,9 +112,7 @@ fn main() -> rltk::BError {
|
|||||||
.with_title("Rougelike Tutorial")
|
.with_title("Rougelike Tutorial")
|
||||||
.build()?;
|
.build()?;
|
||||||
context.with_post_scanlines(true);
|
context.with_post_scanlines(true);
|
||||||
let mut gs = State {
|
let mut gs = State { ecs: World::new() };
|
||||||
ecs: World::new()
|
|
||||||
};
|
|
||||||
gs.ecs.register::<Position>();
|
gs.ecs.register::<Position>();
|
||||||
gs.ecs.register::<Renderable>();
|
gs.ecs.register::<Renderable>();
|
||||||
gs.ecs.register::<Player>();
|
gs.ecs.register::<Player>();
|
||||||
@ -125,7 +123,8 @@ 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>();
|
||||||
|
|
||||||
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();
|
||||||
@ -136,17 +135,15 @@ fn main() -> rltk::BError {
|
|||||||
gs.ecs.insert(rltk::RandomNumberGenerator::new());
|
gs.ecs.insert(rltk::RandomNumberGenerator::new());
|
||||||
|
|
||||||
for room in map.rooms.iter().skip(1) {
|
for room in map.rooms.iter().skip(1) {
|
||||||
let(x,y) = room.center();
|
spawner::spawn_room(&mut gs.ecs, room);
|
||||||
spawner::random_monster(&mut gs.ecs, x, y);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gs.ecs.insert(map);
|
gs.ecs.insert(map);
|
||||||
gs.ecs.insert(player_entity);
|
gs.ecs.insert(player_entity);
|
||||||
gs.ecs.insert(RunState::PreRun);
|
gs.ecs.insert(RunState::PreRun);
|
||||||
gs.ecs.insert(gamelog::GameLog{ entries : vec!["Welcome to my rougelike".to_string()]});
|
gs.ecs.insert(gamelog::GameLog {
|
||||||
|
entries: vec!["Welcome to my rougelike".to_string()],
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
// Main loop runner
|
// Main loop runner
|
||||||
rltk::main_loop(context, gs)
|
rltk::main_loop(context, gs)
|
||||||
|
|||||||
130
src/spawner.rs
130
src/spawner.rs
@ -1,24 +1,42 @@
|
|||||||
use rltk::{ RGB, RandomNumberGenerator };
|
use crate::MAPWIDTH;
|
||||||
|
|
||||||
|
use super::{
|
||||||
|
BlocksTile, CombatStats, Item, Monster, Name, Player, Position, Potion, Rect, Renderable,
|
||||||
|
Viewshed,
|
||||||
|
};
|
||||||
|
use rltk::{RandomNumberGenerator, RGB};
|
||||||
use specs::prelude::*;
|
use specs::prelude::*;
|
||||||
use super::{CombatStats, Player, Renderable, Name, Position, Viewshed, Monster, BlocksTile};
|
|
||||||
|
|
||||||
const MAX_MONSTERS: i32 = 4;
|
const MAX_MONSTERS: i32 = 4;
|
||||||
const MAX_ITEMS: i32 = 2;
|
const MAX_ITEMS: i32 = 2;
|
||||||
|
|
||||||
// Spawns the player and returns his/her entity object.
|
// Spawns the player and returns his/her entity object.
|
||||||
pub fn player(ecs: &mut World, player_x: i32, player_y: i32) -> Entity {
|
pub fn player(ecs: &mut World, player_x: i32, player_y: i32) -> Entity {
|
||||||
ecs
|
ecs.create_entity()
|
||||||
.create_entity()
|
.with(Position {
|
||||||
.with(Position{x: player_x, y: player_y})
|
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(Viewshed {
|
||||||
.with(Name{name: "Player".to_string()})
|
visible_tiles: Vec::new(),
|
||||||
.with(CombatStats{ max_hp: 30, hp: 30, defence: 2, power: 5})
|
range: 8,
|
||||||
|
dirty: true,
|
||||||
|
})
|
||||||
|
.with(Name {
|
||||||
|
name: "Player".to_string(),
|
||||||
|
})
|
||||||
|
.with(CombatStats {
|
||||||
|
max_hp: 30,
|
||||||
|
hp: 30,
|
||||||
|
defence: 2,
|
||||||
|
power: 5,
|
||||||
|
})
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,13 +47,17 @@ pub fn random_monster(ecs: &mut World, x: i32, y: i32) {
|
|||||||
roll = rng.roll_dice(1, 2);
|
roll = rng.roll_dice(1, 2);
|
||||||
}
|
}
|
||||||
match roll {
|
match roll {
|
||||||
1 => { orc(ecs, x, y) }
|
1 => orc(ecs, x, y),
|
||||||
_ => { goblin(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 orc(ecs: &mut World, x: i32, y: i32) {
|
||||||
fn goblin(ecs: &mut World, x: i32, y: i32) { monster(ecs, x, y, rltk::to_cp437('g'), "Goblin"); }
|
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) {
|
fn monster<S: ToString>(ecs: &mut World, x: i32, y: i32, glyph: rltk::FontCharType, name: S) {
|
||||||
ecs.create_entity()
|
ecs.create_entity()
|
||||||
@ -45,14 +67,86 @@ fn monster<S : ToString>(ecs: &mut World, x: i32, y: i32, glyph : rltk::FontChar
|
|||||||
fg: RGB::named(rltk::RED),
|
fg: RGB::named(rltk::RED),
|
||||||
bg: RGB::named(rltk::BLACK),
|
bg: RGB::named(rltk::BLACK),
|
||||||
})
|
})
|
||||||
.with(Viewshed{ visible_tiles : Vec::new(), range: 8, dirty: true})
|
.with(Viewshed {
|
||||||
|
visible_tiles: Vec::new(),
|
||||||
|
range: 8,
|
||||||
|
dirty: true,
|
||||||
|
})
|
||||||
.with(Monster {})
|
.with(Monster {})
|
||||||
.with(Name{ name: name.to_string()})
|
.with(Name {
|
||||||
|
name: name.to_string(),
|
||||||
|
})
|
||||||
.with(BlocksTile {})
|
.with(BlocksTile {})
|
||||||
.with(CombatStats{ max_hp: 16, hp: 16, defence: 1, power: 4})
|
.with(CombatStats {
|
||||||
|
max_hp: 16,
|
||||||
|
hp: 16,
|
||||||
|
defence: 1,
|
||||||
|
power: 4,
|
||||||
|
})
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://bfnightly.bracketproductions.com/chapter_9.html
|
pub fn spawn_room(ecs: &mut World, room: &Rect) {
|
||||||
/// Still in spawner.rs, we create a new function - spawn_room that uses these constants
|
let mut monster_spawn_points: Vec<usize> = Vec::new();
|
||||||
pub fn spawn_room()
|
let mut item_spawn_points: Vec<usize> = Vec::new();
|
||||||
|
|
||||||
|
// Scope to keep the borow checker happy
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Actually spawn the monsters
|
||||||
|
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(';'),
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user