Spawn all entities via raws, complete Section 5.2

This commit is contained in:
Timothy Warren 2021-12-23 12:48:09 -05:00
parent b80c64febb
commit aaf3fdcc42
5 changed files with 127 additions and 40 deletions

View File

@ -175,5 +175,36 @@
},
"vision_range": 8
}
],
"props": [
{
"name": "Bear Trap",
"renderable": {
"glyph": "^",
"fg": "#FF0000",
"bg": "#000000",
"order": 2
},
"hidden": true,
"entry_trigger": {
"effects": {
"damage": "6",
"single_activation": "1"
}
}
},
{
"name": "Door",
"renderable": {
"glyph": "+",
"fg": "#805A46",
"bg": "#000000",
"order": 2
},
"hidden": false,
"blocks_tile": true,
"blocks_visibility": true,
"door_open": true
}
]
}

View File

@ -1,11 +1,13 @@
mod item_structs;
mod mob_structs;
mod prop_structs;
mod rawmaster;
use std::sync::Mutex;
use item_structs::*;
use mob_structs::*;
use prop_structs::*;
pub use rawmaster::*;
use serde::Deserialize;
@ -13,6 +15,7 @@ use serde::Deserialize;
pub struct Raws {
pub items: Vec<Item>,
pub mobs: Vec<Mob>,
pub props: Vec<Prop>,
}
rltk::embedded_resource!(RAW_FILE, "../raws/spawns.json");

21
src/raws/prop_structs.rs Normal file
View File

@ -0,0 +1,21 @@
use std::collections::HashMap;
use serde::Deserialize;
use super::Renderable;
#[derive(Deserialize, Debug)]
pub struct Prop {
pub name: String,
pub renderable: Option<Renderable>,
pub hidden: Option<bool>,
pub blocks_tile: Option<bool>,
pub blocks_visibility: Option<bool>,
pub door_open: Option<bool>,
pub entry_trigger: Option<EntryTrigger>,
}
#[derive(Deserialize, Debug)]
pub struct EntryTrigger {
pub effects: HashMap<String, String>,
}

View File

@ -13,6 +13,7 @@ pub struct RawMaster {
raws: Raws,
item_index: HashMap<String, usize>,
mob_index: HashMap<String, usize>,
prop_index: HashMap<String, usize>,
}
impl RawMaster {
@ -21,9 +22,11 @@ impl RawMaster {
raws: Raws {
items: Vec::new(),
mobs: Vec::new(),
props: Vec::new(),
},
item_index: HashMap::new(),
mob_index: HashMap::new(),
prop_index: HashMap::new(),
}
}
@ -37,6 +40,9 @@ impl RawMaster {
for (i, mob) in self.raws.mobs.iter().enumerate() {
self.mob_index.insert(mob.name.clone(), i);
}
for (i, prop) in self.raws.props.iter().enumerate() {
self.prop_index.insert(prop.name.clone(), i);
}
}
}
@ -198,6 +204,66 @@ pub fn spawn_named_mob(
None
}
pub fn spawn_named_prop(
raws: &RawMaster,
new_entity: EntityBuilder,
key: &str,
pos: SpawnType,
) -> Option<Entity> {
if raws.prop_index.contains_key(key) {
let prop_template = &raws.raws.props[raws.prop_index[key]];
let mut eb = new_entity;
// Spawn in the specified location
eb = spawn_position(pos, eb);
// Renderable
if let Some(renderable) = &prop_template.renderable {
eb = eb.with(get_renderable_component(renderable));
}
eb = eb.with(Name::from(&prop_template.name));
if let Some(hidden) = prop_template.hidden {
if hidden {
eb = eb.with(Hidden {})
}
}
if let Some(blocks_tile) = prop_template.blocks_tile {
if blocks_tile {
eb = eb.with(BlocksTile {})
}
}
if let Some(blocks_visibility) = prop_template.blocks_visibility {
if blocks_visibility {
eb = eb.with(BlocksVisibility {})
}
}
if let Some(door_open) = prop_template.door_open {
eb = eb.with(Door { open: door_open });
}
if let Some(entry_trigger) = &prop_template.entry_trigger {
eb = eb.with(EntryTrigger {});
for effect in entry_trigger.effects.iter() {
match effect.0.as_str() {
"damage" => {
eb = eb.with(InflictsDamage {
damage: effect.1.parse::<i32>().unwrap(),
});
}
"single_activation" => eb = eb.with(SingleActivation {}),
_ => {}
}
}
}
return Some(eb.build());
}
None
}
pub fn spawn_named_entity(
raws: &RawMaster,
new_entity: EntityBuilder,
@ -208,6 +274,8 @@ pub fn spawn_named_entity(
return spawn_named_item(raws, new_entity, key, pos);
} else if raws.mob_index.contains_key(key) {
return spawn_named_mob(raws, new_entity, key, pos);
} else if raws.prop_index.contains_key(key) {
return spawn_named_prop(raws, new_entity, key, pos);
}
None

View File

@ -143,44 +143,8 @@ pub fn spawn_entity(ecs: &mut World, spawn: &(&usize, &String)) {
return;
}
match spawn.1.as_ref() {
"Bear Trap" => bear_trap(ecs, x, y),
"Door" => door(ecs, x, y),
_ => {}
}
}
fn bear_trap(ecs: &mut World, x: i32, y: i32) {
ecs.create_entity()
.with(Position { x, y })
.with(Renderable {
glyph: rltk::to_cp437('^'),
fg: RGB::named(rltk::RED),
bg: RGB::named(rltk::BLACK),
render_order: 2,
})
.with(Name::from("Bear Trap"))
.with(Hidden {})
.with(EntryTrigger {})
.with(InflictsDamage { damage: 6 })
.with(SingleActivation {})
.marked::<SimpleMarker<SerializeMe>>()
.build();
}
fn door(ecs: &mut World, x: i32, y: i32) {
ecs.create_entity()
.with(Position { x, y })
.with(Renderable {
glyph: rltk::to_cp437('+'),
fg: RGB::named(rltk::CHOCOLATE),
bg: RGB::named(rltk::BLACK),
render_order: 2,
})
.with(Name::from("Door"))
.with(BlocksTile {})
.with(BlocksVisibility {})
.with(Door { open: false })
.marked::<SimpleMarker<SerializeMe>>()
.build();
rltk::console::log(format!(
"WARNING: We don't know how to spawn [{}]!",
spawn.1
));
}