From 0c09f52eb66d5c55e9e38a43c40f2d592a6aa8b5 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 5 Jan 2022 09:42:36 -0500 Subject: [PATCH] Add LootTable component --- src/components.rs | 5 +++++ src/main.rs | 1 + src/raws.rs | 3 +++ src/raws/loot_structs.rs | 13 +++++++++++++ src/raws/mob_structs.rs | 1 + src/raws/rawmaster.rs | 13 +++++++++++++ src/saveload_system.rs | 2 ++ 7 files changed, 38 insertions(+) create mode 100644 src/raws/loot_structs.rs diff --git a/src/components.rs b/src/components.rs index 6f79078..38a47d8 100644 --- a/src/components.rs +++ b/src/components.rs @@ -337,6 +337,11 @@ pub struct NaturalAttackDefense { pub attacks: Vec, } +#[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. diff --git a/src/main.rs b/src/main.rs index 9c3545e..d9764d7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -539,6 +539,7 @@ fn main() -> ::rltk::BError { Skills, Pools, NaturalAttackDefense, + LootTable, ); gs.ecs.insert(SimpleMarkerAllocator::::new()); diff --git a/src/raws.rs b/src/raws.rs index 648fa7c..bd8e29b 100644 --- a/src/raws.rs +++ b/src/raws.rs @@ -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, pub props: Vec, pub spawn_table: Vec, + pub loot_tables: Vec, } embedded_resource!(RAW_FILE, "../raws/spawns.json"); diff --git a/src/raws/loot_structs.rs b/src/raws/loot_structs.rs new file mode 100644 index 0000000..655eee2 --- /dev/null +++ b/src/raws/loot_structs.rs @@ -0,0 +1,13 @@ +use serde::Deserialize; + +#[derive(Deserialize, Debug)] +pub struct LootTable { + pub name: String, + pub drops: Vec, +} + +#[derive(Deserialize, Debug)] +pub struct LootDrop { + pub name: String, + pub weight: i32, +} diff --git a/src/raws/mob_structs.rs b/src/raws/mob_structs.rs index 0d07e8d..b73a16a 100644 --- a/src/raws/mob_structs.rs +++ b/src/raws/mob_structs.rs @@ -19,6 +19,7 @@ pub struct Mob { pub mana: Option, pub equipped: Option>, pub natural: Option, + pub loot_table: Option, } #[derive(Deserialize, Debug)] diff --git a/src/raws/rawmaster.rs b/src/raws/rawmaster.rs index 59952ea..0bcf42f 100644 --- a/src/raws/rawmaster.rs +++ b/src/raws/rawmaster.rs @@ -43,6 +43,7 @@ pub struct RawMaster { item_index: HashMap, mob_index: HashMap, prop_index: HashMap, + loot_index: HashMap, } 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? diff --git a/src/saveload_system.rs b/src/saveload_system.rs index f2ce065..2b1bcd6 100644 --- a/src/saveload_system.rs +++ b/src/saveload_system.rs @@ -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, ); }