Skip to content

Commit

Permalink
fmt cleanup and added item database that loads from json file
Browse files Browse the repository at this point in the history
  • Loading branch information
benlloyd50 committed Jan 22, 2023
1 parent 36d14fd commit 0e16a75
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
12 changes: 12 additions & 0 deletions assets/items/comfort_items.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"id": 1,
"name": "wood",
"atlas_index": 1
},
{
"id": 2,
"name": "stone",
"atlas_index": 2
}
]
File renamed without changes.
3 changes: 0 additions & 3 deletions docs/planning.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/comfort_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct FractalSettings {
}

pub fn load_settings(preset: &str) -> Result<FractalSettings, Box<dyn Error>> {
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),
Expand Down
60 changes: 60 additions & 0 deletions src/item_util.rs
Original file line number Diff line number Diff line change
@@ -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<u32, Item>,
}

// 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<Vec<Item>, Box<dyn Error>> {
let contents = fs::read_to_string("assets/items/comfort_items.json")?;
let items: Vec<Item> = serde_json::from_str(&contents)?;
Ok(items)
}

fn create_item_tilestorage() {
todo!("Create item tilestorage here")
}
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
1 change: 0 additions & 1 deletion src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ fn directional_input_handle(
blocking_q: Query<&TilePos, With<Blocking>>,
blocking_interact_q: Query<(Entity, &TilePos), (With<Interact>, With<Blocking>)>,
obj_tiles_q: Query<(Entity, &ObjectSize, &TilePos)>,
tile_storage_q: Query<&TileStorage>,
keeb: Res<Input<KeyCode>>,
mut ev_moveplayer: EventWriter<MoveEvent>,
mut ev_interact: EventWriter<Interaction>,
Expand Down

0 comments on commit 0e16a75

Please sign in to comment.