From 0e16a756b171e735254dcbe7a2c5d34daa31ab50 Mon Sep 17 00:00:00 2001 From: Benjamin Lloyd Date: Sat, 21 Jan 2023 23:17:13 -0500 Subject: [PATCH] fmt cleanup and added item database that loads from json file --- Cargo.lock | 1 + Cargo.toml | 1 + assets/items/comfort_items.json | 12 ++++++ config/{config.toml => worldgen.toml} | 0 docs/planning.md | 3 -- src/comfort_config.rs | 2 +- src/item_util.rs | 60 +++++++++++++++++++++++++++ src/main.rs | 3 ++ src/player.rs | 1 - 9 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 assets/items/comfort_items.json rename config/{config.toml => worldgen.toml} (100%) create mode 100644 src/item_util.rs diff --git a/Cargo.lock b/Cargo.lock index 4896907..1b7aeb9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1094,6 +1094,7 @@ dependencies = [ "iyes_loopless", "rand", "serde", + "serde_json", "toml", ] diff --git a/Cargo.toml b/Cargo.toml index 49489ea..ec56dc5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ rand = "0.8.5" bracket-noise = "0.8" toml = "0.5.10" serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" # Enable a small amount of optimization in debug mode [profile.dev] diff --git a/assets/items/comfort_items.json b/assets/items/comfort_items.json new file mode 100644 index 0000000..6d63f95 --- /dev/null +++ b/assets/items/comfort_items.json @@ -0,0 +1,12 @@ +[ + { + "id": 1, + "name": "wood", + "atlas_index": 1 + }, + { + "id": 2, + "name": "stone", + "atlas_index": 2 + } +] diff --git a/config/config.toml b/config/worldgen.toml similarity index 100% rename from config/config.toml rename to config/worldgen.toml diff --git a/docs/planning.md b/docs/planning.md index d92c454..f788781 100644 --- a/docs/planning.md +++ b/docs/planning.md @@ -35,6 +35,3 @@ - Mining ideas, multi block hit so you can hit multiple blocks - Certain terrain could require upgraded materials -## MultiTile problem -- we have a dest_tile we need to see if an interactable object exists there -- interactable objects can take from 1 to many tilepos \ No newline at end of file diff --git a/src/comfort_config.rs b/src/comfort_config.rs index a959bc0..6ed9114 100644 --- a/src/comfort_config.rs +++ b/src/comfort_config.rs @@ -17,7 +17,7 @@ pub struct FractalSettings { } pub fn load_settings(preset: &str) -> Result> { - let contents = fs::read_to_string("config/config.toml")?; + let contents = fs::read_to_string("config/worldgen.toml")?; let decoded: ComfortConfig = toml::from_str(&contents).unwrap(); match preset { "terrainperlin" => Ok(decoded.terrainperlin), diff --git a/src/item_util.rs b/src/item_util.rs new file mode 100644 index 0000000..fa142b1 --- /dev/null +++ b/src/item_util.rs @@ -0,0 +1,60 @@ +/// Item Utilities +/// +/// Includes systems to spawn items +/// Contains the database for items +use bevy::{prelude::*, utils::HashMap}; +use iyes_loopless::prelude::*; +use serde::Deserialize; +use std::{fs, error::Error}; +use crate::AppState; + +pub struct ItemUtilPlugin; + +impl Plugin for ItemUtilPlugin { + fn build(&self, app: &mut App) { + app.add_enter_system(AppState::GameLoading, init_item_database); + // .add_enter_system(AppState::GameLoading, create_item_tilestorage); + } +} + +#[derive(Resource)] +pub struct ItemDatabase { + items: HashMap, +} + +// Static information about the item that is the same across all of its kind +#[derive(Deserialize, Debug)] +#[derive(Component)] +pub struct Item { + id: u32, // unique identifier for the item + name: String, // name of item + atlas_index: u32, // sprite index for the atlas +} + +#[derive(Component)] +pub struct ItemQuantity(u32); + +fn init_item_database(mut commands: Commands) { + let items = match load_items_from_json() { + Ok(items) => items, + Err(_) => panic!("Could not load items from json"), + }; + + let mut item_db = HashMap::new(); + for item in items { + println!("{:#?}", item); + item_db.insert(item.id, item); + } + + commands.insert_resource(ItemDatabase { items: item_db }); +} + +pub fn load_items_from_json() -> Result, Box> { + let contents = fs::read_to_string("assets/items/comfort_items.json")?; + let items: Vec = serde_json::from_str(&contents)?; + Ok(items) +} + +fn create_item_tilestorage() { + todo!("Create item tilestorage here") +} diff --git a/src/main.rs b/src/main.rs index db15d49..f536783 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,8 @@ mod camera; use camera::CameraPlugin; mod player; use player::PlayerPlugin; +mod item_util; +use item_util::ItemUtilPlugin; use bevy::prelude::*; use bevy::window::PresentMode; @@ -36,6 +38,7 @@ fn main() { .add_plugin(CameraPlugin) .add_plugin(PlayerPlugin) .add_plugin(InteractPlugin) + .add_plugin(ItemUtilPlugin) .add_system(run_game.run_in_state(AppState::GameLoading)) .add_system(bevy::window::close_on_esc) .run(); diff --git a/src/player.rs b/src/player.rs index 1e65b7a..c0f6566 100644 --- a/src/player.rs +++ b/src/player.rs @@ -151,7 +151,6 @@ fn directional_input_handle( blocking_q: Query<&TilePos, With>, blocking_interact_q: Query<(Entity, &TilePos), (With, With)>, obj_tiles_q: Query<(Entity, &ObjectSize, &TilePos)>, - tile_storage_q: Query<&TileStorage>, keeb: Res>, mut ev_moveplayer: EventWriter, mut ev_interact: EventWriter,