From 70494d8122e9506eef12b1c1797c70d8f49518b9 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Thu, 23 Dec 2021 11:00:37 -0500 Subject: [PATCH] Start of data-driven design (Section 4.2) --- raws/spawns.json | 30 ++++++++++++++++++++++++++++++ src/main.rs | 3 +++ src/raws.rs | 18 ++++++++++++++++++ src/raws/item_structs.rs | 28 ++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 raws/spawns.json create mode 100644 src/raws.rs create mode 100644 src/raws/item_structs.rs diff --git a/raws/spawns.json b/raws/spawns.json new file mode 100644 index 0000000..9b6beeb --- /dev/null +++ b/raws/spawns.json @@ -0,0 +1,30 @@ +{ + "items": [{ + "name": "Health Potion", + "renderable": { + "glpyh": "!", + "fg": "#FF00FF", + "bg": "#000000", + "order": 2 + }, + "consumable": { + "effects": { + "provides_healing": "8" + } + } + }, { + "name": "Magic Missile Scroll", + "renderable": { + "glyph": ")", + "fg": "#00FFFF", + "bg": "#000000", + "order": 2 + }, + "consumable": { + "effects": { + "ranged": "6", + "damage": "20" + } + } + }] +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 2b151f1..67427a3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,7 @@ mod monster_ai_system; mod particle_system; mod player; pub mod random_table; +pub mod raws; mod rect; mod rex_assets; pub mod saveload_system; @@ -535,6 +536,8 @@ fn main() -> rltk::BError { gs.ecs.insert(SimpleMarkerAllocator::::new()); + raws::load_raws(); + gs.ecs.insert(Map::new(1, 64, 64)); gs.ecs.insert(Point::zero()); gs.ecs.insert(rltk::RandomNumberGenerator::new()); diff --git a/src/raws.rs b/src/raws.rs new file mode 100644 index 0000000..49ee9d1 --- /dev/null +++ b/src/raws.rs @@ -0,0 +1,18 @@ +mod item_structs; + +use item_structs::Raws; + +rltk::embedded_resource!(RAW_FILE, "../raws/spawns.json"); + +pub fn load_raws() { + let raw_data = rltk::embedding::EMBED + .lock() + .get_resource("../raws/spawns.json".to_string()) + .unwrap(); + + let raw_string = + std::str::from_utf8(&raw_data).expect("Unable to convert to a valid UTF-8 string."); + let decoder: Raws = serde_json::from_str(&raw_string).expect("Unable to parse JSON"); + + rltk::console::log(format!("{:?}", decoder)); +} diff --git a/src/raws/item_structs.rs b/src/raws/item_structs.rs new file mode 100644 index 0000000..2a414a3 --- /dev/null +++ b/src/raws/item_structs.rs @@ -0,0 +1,28 @@ +use std::collections::HashMap; + +use serde::Deserialize; + +#[derive(Deserialize, Debug)] +pub struct Raws { + pub items: Vec, +} + +#[derive(Deserialize, Debug)] +pub struct Item { + pub name: String, + pub renderable: Option, + pub consumable: Option, +} + +#[derive(Deserialize, Debug)] +pub struct Renderable { + pub glyph: String, + pub fg: String, + pub bg: String, + pub order: i32, +} + +#[derive(Deserialize, Debug)] +pub struct Consumable { + pub effects: HashMap, +}