Add LootTable component

This commit is contained in:
Timothy Warren 2022-01-05 09:42:36 -05:00
parent c9b35e2710
commit 0c09f52eb6
7 changed files with 38 additions and 0 deletions

View File

@ -337,6 +337,11 @@ pub struct NaturalAttackDefense {
pub attacks: Vec<NaturalAttack>,
}
#[derive(Component, Debug, Serialize, Deserialize, Clone)]
pub struct LootTable {
pub table: String,
}
// Serialization helper code. We need to implement ConvertSaveLoad for each type that contains an
// Entity.

View File

@ -539,6 +539,7 @@ fn main() -> ::rltk::BError {
Skills,
Pools,
NaturalAttackDefense,
LootTable,
);
gs.ecs.insert(SimpleMarkerAllocator::<SerializeMe>::new());

View File

@ -1,4 +1,5 @@
mod item_structs;
mod loot_structs;
mod mob_structs;
mod prop_structs;
mod rawmaster;
@ -9,6 +10,7 @@ use std::sync::Mutex;
use ::rltk::{embedded_resource, link_resource};
use ::serde::Deserialize;
use item_structs::*;
use loot_structs::*;
use mob_structs::*;
use prop_structs::*;
pub use rawmaster::*;
@ -20,6 +22,7 @@ pub struct Raws {
pub mobs: Vec<Mob>,
pub props: Vec<Prop>,
pub spawn_table: Vec<SpawnTableEntry>,
pub loot_tables: Vec<LootTable>,
}
embedded_resource!(RAW_FILE, "../raws/spawns.json");

13
src/raws/loot_structs.rs Normal file
View File

@ -0,0 +1,13 @@
use serde::Deserialize;
#[derive(Deserialize, Debug)]
pub struct LootTable {
pub name: String,
pub drops: Vec<LootDrop>,
}
#[derive(Deserialize, Debug)]
pub struct LootDrop {
pub name: String,
pub weight: i32,
}

View File

@ -19,6 +19,7 @@ pub struct Mob {
pub mana: Option<i32>,
pub equipped: Option<Vec<String>>,
pub natural: Option<MobNatural>,
pub loot_table: Option<String>,
}
#[derive(Deserialize, Debug)]

View File

@ -43,6 +43,7 @@ pub struct RawMaster {
item_index: HashMap<String, usize>,
mob_index: HashMap<String, usize>,
prop_index: HashMap<String, usize>,
loot_index: HashMap<String, usize>,
}
impl RawMaster {
@ -53,10 +54,12 @@ impl RawMaster {
mobs: Vec::new(),
props: Vec::new(),
spawn_table: Vec::new(),
loot_tables: Vec::new(),
},
item_index: HashMap::new(),
mob_index: HashMap::new(),
prop_index: HashMap::new(),
loot_index: HashMap::new(),
}
}
@ -103,6 +106,10 @@ impl RawMaster {
));
}
}
for (i, loot) in self.raws.loot_tables.iter().enumerate() {
self.loot_index.insert(loot.name.clone(), i);
}
}
}
@ -404,6 +411,12 @@ pub fn spawn_named_mob(
eb = eb.with(nature);
}
if let Some(loot) = &mob_template.loot_table {
eb = eb.with(LootTable {
table: loot.clone(),
});
}
let new_mob = eb.build();
// Are they weilding anything?

View File

@ -95,6 +95,7 @@ pub fn save_game(ecs: &mut World) {
Skills,
Pools,
NaturalAttackDefense,
LootTable,
);
}
@ -192,6 +193,7 @@ pub fn load_game(ecs: &mut World) {
Skills,
Pools,
NaturalAttackDefense,
LootTable,
);
}