Skip to content

Commit

Permalink
look we have items
Browse files Browse the repository at this point in the history
  • Loading branch information
jossse69 committed Sep 1, 2023
1 parent bbd29e0 commit 0d9fcd2
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 3 deletions.
14 changes: 14 additions & 0 deletions data/items/armor/Prisoner_Mini_Skirt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "Prisoner Mini Skirt",
"type": "armor",
"value": 15,
"description": "A tattered and uncomfortable mini skirt.",
"visuals": {
"glyph": "A",
"color": [139, 69, 19]
},
"onuse": {
"type": "protection",
"protection_value": 10
}
}
15 changes: 15 additions & 0 deletions data/items/books/The_Bible.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "The Bible",
"type": "book",
"value": 10,
"description": "A holy book filled with ancient wisdom.",
"visuals": {
"glyph": "B",
"color": [255, 255, 0]
},
"onuse": {
"type": "spell",
"spell_name": "Blessing",
"effect": "Heals the user and provides protection."
}
}
14 changes: 14 additions & 0 deletions data/items/drink/Milk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "Milk",
"type": "drink",
"value": 5,
"description": "Fresh and creamy milk.",
"visuals": {
"glyph": "D",
"color": [255, 255, 255]
},
"onuse": {
"type": "thirst",
"thirst_quenching": 15
}
}
14 changes: 14 additions & 0 deletions data/items/food/Red_Bean_Paste.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "Red Bean Paste",
"type": "food",
"value": 5,
"description": "A sweet and nutritious red bean paste.",
"visuals": {
"glyph": "F",
"color": [255, 0, 0]
},
"onuse": {
"type": "hunger",
"hunger_restoration": 20
}
}
14 changes: 14 additions & 0 deletions data/items/mellee/Rusty_Sword.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "Rusty Sword",
"type": "melee",
"value": 20,
"description": "A worn and rusty sword, but it can still cut.",
"visuals": {
"glyph": "S",
"color": [128, 128, 128]
},
"onuse": {
"type": "damage",
"damage": 15
}
}
15 changes: 15 additions & 0 deletions data/items/ranged/Wooden_Bow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "Wooden Bow",
"type": "ranged",
"value": 30,
"description": "A simple wooden bow for firing arrows.",
"visuals": {
"glyph": "B",
"color": [139, 69, 19]
},
"onuse": {
"type": "shoot",
"ammo_type": "arrow",
"damage": 20
}
}
52 changes: 52 additions & 0 deletions src/item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// item.rs

use bracket_lib::prelude::*;
use serde::Deserialize;
use std::error::Error;
use std::fs;
use crate::entity::Entity;
#[derive(Debug, Deserialize)]
pub struct ItemData {
pub name: String,
pub value: i32,
pub description: String,
pub visuals: Visuals,
pub onuse: Option<OnUse>,
}

#[derive(Debug, Deserialize)]
pub struct Visuals {
pub glyph: char,
pub color: (u8, u8, u8),
}

#[derive(Debug, Deserialize)]
pub struct OnUse {
pub r#type: String,
#[serde(default)]
pub hunger_restoration: i32,
#[serde(default)]
pub thirst_quenching: i32,
// Add other possible effects here
}

pub struct Item {
pub entity: Entity,
pub data: ItemData,
}

impl Item {
pub fn new(x: i32, y: i32, filename: &str) -> Result<Self, Box<dyn Error>> {
let contents = fs::read_to_string(filename)?;
let data: ItemData = serde_json::from_str(&contents)?;
let entity = Entity::new(x, y, data.visuals.glyph as FontCharType, data.visuals.color, BLACK);

Ok(Item { entity, data })
}

pub fn draw(&self, ctx: &mut BTerm) {
self.entity.draw(ctx);
}

// Implement item-specific methods here
}
25 changes: 22 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
// main.rs

mod entity;
mod player;
mod ui;
mod mobs;
mod map;
mod progen; // Import the progen module
mod progen;
mod item; // Import the item module

use bracket_lib::{prelude::*, color};
use entity::Entity;
use mobs::Mob;
use player::Player;
use ui::UI;
use map::{Map, TileType, SCREEN_WIDTH};
use item::Item; // Import the Item struct

struct State {
player: Player,
ui: UI,
mobs: Vec<Mob>,
map: Map,
items: Vec<Item>, // Add items vector
}

impl State {
Expand All @@ -32,7 +37,7 @@ impl State {
// Generate the dungeon map
progen::generate_dungeon(&mut map);

// fint the up stairs to place player
// Find the up stairs to place the player
let mut player_loc = Point::new(0, 0);
for i in 0..map.tiles.len() {
if map.tiles[i] == TileType::UpStairs {
Expand All @@ -42,11 +47,17 @@ impl State {
}
}

// Create the Milk item and add it to the items vector
let milk = Item::new(player_loc.x, player_loc.y, "data/items/drink/Milk.json").unwrap();
let mut items = Vec::new();
items.push(milk);

State {
player: Player::new(player_loc.x, player_loc.y),
ui: UI::new(),
mobs,
map,
items, // Initialize the items vector
}
}
}
Expand All @@ -62,12 +73,20 @@ impl GameState for State {
self.ui.add_message("Hello world!");

self.map.render(ctx);


// Draw items
for item in &self.items {
item.draw(ctx);
}

// Draw mobs
for mob in &self.mobs {
mob.draw(ctx);
}
self.player.draw(ctx);



self.ui.draw(ctx, &self.player);
}
}
Expand Down

0 comments on commit 0d9fcd2

Please sign in to comment.