diff options
| author | troido <troido@protonmail.com> | 2020-04-17 11:36:58 +0200 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-04-17 11:36:58 +0200 |
| commit | 334fc118b0eaf14c4d68129050c4837cb64cebae (patch) | |
| tree | 66d5f12aa2842cea39ddf5af3ef3d9230c256c45 | |
| parent | 13e9ab2c859b9a6a1935acbb93d34a7f10b7e122 (diff) | |
drop loot a step after dying
| -rw-r--r-- | content/encyclopediae/default_encyclopedia.json | 14 | ||||
| -rw-r--r-- | src/components/mod.rs | 8 | ||||
| -rw-r--r-- | src/componentwrapper.rs | 6 | ||||
| -rw-r--r-- | src/room.rs | 2 | ||||
| -rw-r--r-- | src/systems/droploot.rs | 30 |
5 files changed, 38 insertions, 22 deletions
diff --git a/content/encyclopediae/default_encyclopedia.json b/content/encyclopediae/default_encyclopedia.json index 5f127cd..204b104 100644 --- a/content/encyclopediae/default_encyclopedia.json +++ b/content/encyclopediae/default_encyclopedia.json @@ -101,7 +101,7 @@ "arguments": [["health", "int", 100]], "components": [ ["Health", {"health": ["arg", "health"], "maxhealth": 100}], - ["Loot", {"spread": false, "loot": ["list", [{"type": "stone"}]]}] + ["Loot", {"loot": ["list", [{"type": "stone"}]]}] ], "sprite": "builtwall", "height": 2, @@ -140,7 +140,7 @@ ["Fighter", {"damage": 2, "cooldown": 6}], ["Movable", {"cooldown": 3}], ["Faction", {"faction": "evil"}], - ["Loot", {"spread": true, "loot": ["list", [ + ["Loot", {"loot": ["list", [ ["list", [{"type": "radishseed"}, 1.0]] ]]}] ] @@ -158,7 +158,7 @@ ["Fighter", {"damage": 5, "cooldown": 8}], ["Movable", {"cooldown": 4}], ["Faction", {"faction": "evil"}], - ["Loot", {"spread": true, "loot": ["list", [ + ["Loot", {"loot": ["list", [ ["list", [{"type": "sword"}, 0.05]], ["list", [{"type": "club"}, 0.1]], ["list", [{"type": "radish"}, 0.25]] @@ -178,7 +178,7 @@ ["Fighter", {"damage": 15, "cooldown": 10}], ["Movable", {"cooldown": 5}], ["Faction", {"faction": "evil"}], - ["Loot", {"spread": true, "loot": ["list", [ + ["Loot", {"loot": ["list", [ ["list", [{"type": "stone"}, 1.0]], ["list", [{"type": "stone"}, 0.3]], ["list", [{"type": "pebble"}, 0.5]], @@ -231,7 +231,7 @@ "height": 0.5, "components": [ ["Interactable", {"action": ["interaction", ["trigger", "die"]]}], - ["Loot", {"spread": true, "loot": ["list", [ + ["Loot", {"loot": ["list", [ ["list", [{"type": "radishseed"}, 0.92]], ["list", [{"type": "radishseed"}, 0.20]], ["list", [{"type": "radish"}, 0.8]], @@ -407,7 +407,7 @@ "height": 1.0, "components": [ ["Interactable", {"action": ["interaction", ["trigger", "die"]]}], - ["Loot", {"spread": true, "loot": ["list", [ + ["Loot", {"loot": ["list", [ ["list", [{"type": "carrotseed"}, 1.0]], ["list", [{"type": "carrot"}, 1.0]] ]]}] @@ -420,7 +420,7 @@ "components": [ ["Interactable", {"action": ["interaction", ["mine", "mining"]]}], ["Minable", {"total": 20, "trigger": "loot"}], - ["Loot", {"spread": true, "loot": ["list", [ + ["Loot", {"loot": ["list", [ ["list", [{"type": "stone"}, 1.0]] ]]}] ] diff --git a/src/components/mod.rs b/src/components/mod.rs index 8ffe30d..394284a 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -197,10 +197,16 @@ pub struct Clan { #[derive(Component, Debug, Clone)] #[storage(HashMapStorage)] pub struct Loot { - pub spread: bool, pub loot: Vec<(Template, f64)> } +#[derive(Component, Debug, Clone)] +#[storage(HashMapStorage)] +pub struct LootHolder { + pub loot: Vec<(Template, f64)> +} + + #[derive(Component, Debug, Clone)] #[storage(HashMapStorage)] diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs index 0eca576..392c0dc 100644 --- a/src/componentwrapper.rs +++ b/src/componentwrapper.rs @@ -182,8 +182,8 @@ components!(all: Home (home: Pos); Faction (faction: String) {Faction::from_str(faction.as_str()).ok_or(aerr!("invalid faction name"))?}; Interactable (action: Interaction) {action}; - Loot (loot: List, spread: Bool) { - Loot {spread, loot: + Loot (loot: List) { + Loot {loot: loot .iter() .map(|param| {match param { @@ -291,6 +291,8 @@ components!(all: total } }; + Removed; + LootHolder () {panic!("LootHolder from parameters not implemented")}; ); diff --git a/src/room.rs b/src/room.rs index 3020241..9583a93 100644 --- a/src/room.rs +++ b/src/room.rs @@ -120,7 +120,7 @@ impl <'a, 'b>Room<'a, 'b> { world.insert(NewEntities::new(encyclopedia)); register_insert!( world, - (Position, Visible, Controller, Movable, New, Removed, Moved, Player, Inventory, Health, Serialise, RoomExit, Entered, TriggerBox, Trap, Fighter, Healing, ControlCooldown, Autofight, MonsterAI, Home, AttackInbox, Item, Spawner, Clan, Faction, Interactable, Loot, Timer, Equipment, TimeOffset, Flags, Ear, Build, Whitelist, Dedup, Minable), + (Position, Visible, Controller, Movable, New, Removed, Moved, Player, Inventory, Health, Serialise, RoomExit, Entered, TriggerBox, Trap, Fighter, Healing, ControlCooldown, Autofight, MonsterAI, Home, AttackInbox, Item, Spawner, Clan, Faction, Interactable, Loot, Timer, Equipment, TimeOffset, Flags, Ear, Build, Whitelist, Dedup, Minable, LootHolder), (Ground, Input, Output, Size, Spawn, Players, Emigration, Time) ); diff --git a/src/systems/droploot.rs b/src/systems/droploot.rs index 604f29f..d9d79d2 100644 --- a/src/systems/droploot.rs +++ b/src/systems/droploot.rs @@ -13,12 +13,15 @@ use crate::{ components::{ Position, Loot, + LootHolder, Trigger, TriggerBox, Flags, - Flag + Flag, + Removed }, resources::{NewEntities, Ground}, + componentwrapper::ComponentWrapper, Pos }; @@ -31,20 +34,25 @@ impl <'a> System<'a> for DropLoot{ ReadStorage<'a, TriggerBox>, ReadStorage<'a, Loot>, Read<'a, Ground>, - ReadStorage<'a, Flags> + ReadStorage<'a, Flags>, + ReadStorage<'a, LootHolder> ); - fn run(&mut self, (positions, mut new, triggerboxes, loots, ground, flags): Self::SystemData) { + fn run(&mut self, (positions, mut new, triggerboxes, loots, ground, flags, lootholders): Self::SystemData) { for (position, triggerbox, loot) in (&positions, &triggerboxes, &loots).join(){ if triggerbox.has_message(&[Trigger::Die, Trigger::Loot]) { - for (template, chance) in &loot.loot { - if *chance > rand::thread_rng().gen_range(0.0, 1.0) { - let pos = if loot.spread { - pick_position(position.pos, &ground, &flags) - } else {position.pos}; - // todo: better error handling - new.create(pos, &template).unwrap(); - } + new.to_build.push((position.pos, vec![ + ComponentWrapper::LootHolder(LootHolder{loot: loot.loot.clone()}), + ComponentWrapper::Removed(Removed) + ])); + } + } + for (position, loot) in (&positions, &lootholders).join() { + for (template, chance) in &loot.loot { + if *chance > rand::thread_rng().gen_range(0.0, 1.0) { + let pos = pick_position(position.pos, &ground, &flags); + // todo: better error handling + new.create(pos, &template).unwrap(); } } } |
