Skip to content

Commit

Permalink
made equipment slots struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Lamby777 committed Aug 19, 2024
1 parent da4a708 commit 8541ace
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 37 deletions.
165 changes: 150 additions & 15 deletions pets-gd/assets/charmap.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@
"lambda": null,
"max_mana": null
},
"equipment": []
"equipment": {
"head": null,
"body": null,
"weapon": null,
"accessories": [
null,
null,
null
]
}
}
},
{
Expand Down Expand Up @@ -52,7 +61,16 @@
"lambda": null,
"max_mana": null
},
"equipment": []
"equipment": {
"head": null,
"body": null,
"weapon": null,
"accessories": [
null,
null,
null
]
}
}
},
{
Expand Down Expand Up @@ -80,7 +98,16 @@
"lambda": null,
"max_mana": null
},
"equipment": []
"equipment": {
"head": null,
"body": null,
"weapon": "cool_stick",
"accessories": [
null,
null,
null
]
}
}
},
{
Expand Down Expand Up @@ -108,7 +135,16 @@
"lambda": null,
"max_mana": null
},
"equipment": []
"equipment": {
"head": null,
"body": null,
"weapon": null,
"accessories": [
null,
null,
null
]
}
}
},
{
Expand Down Expand Up @@ -136,7 +172,16 @@
"lambda": null,
"max_mana": null
},
"equipment": []
"equipment": {
"head": null,
"body": null,
"weapon": null,
"accessories": [
null,
null,
null
]
}
}
},
{
Expand Down Expand Up @@ -164,7 +209,16 @@
"lambda": null,
"max_mana": null
},
"equipment": []
"equipment": {
"head": null,
"body": null,
"weapon": null,
"accessories": [
null,
null,
null
]
}
}
},
{
Expand Down Expand Up @@ -192,7 +246,16 @@
"lambda": null,
"max_mana": null
},
"equipment": []
"equipment": {
"head": null,
"body": null,
"weapon": null,
"accessories": [
null,
null,
null
]
}
}
},
{
Expand Down Expand Up @@ -220,7 +283,16 @@
"lambda": null,
"max_mana": null
},
"equipment": []
"equipment": {
"head": null,
"body": null,
"weapon": null,
"accessories": [
null,
null,
null
]
}
}
},
{
Expand Down Expand Up @@ -248,7 +320,16 @@
"lambda": null,
"max_mana": null
},
"equipment": []
"equipment": {
"head": null,
"body": null,
"weapon": null,
"accessories": [
null,
null,
null
]
}
}
},
{
Expand Down Expand Up @@ -276,7 +357,16 @@
"lambda": null,
"max_mana": null
},
"equipment": []
"equipment": {
"head": null,
"body": null,
"weapon": null,
"accessories": [
null,
null,
null
]
}
}
},
{
Expand Down Expand Up @@ -304,7 +394,16 @@
"lambda": null,
"max_mana": null
},
"equipment": []
"equipment": {
"head": null,
"body": null,
"weapon": null,
"accessories": [
null,
null,
null
]
}
}
},
{
Expand Down Expand Up @@ -332,7 +431,16 @@
"lambda": null,
"max_mana": null
},
"equipment": []
"equipment": {
"head": null,
"body": null,
"weapon": null,
"accessories": [
null,
null,
null
]
}
}
},
{
Expand Down Expand Up @@ -360,7 +468,16 @@
"lambda": null,
"max_mana": null
},
"equipment": []
"equipment": {
"head": null,
"body": null,
"weapon": null,
"accessories": [
null,
null,
null
]
}
}
},
{
Expand Down Expand Up @@ -388,7 +505,16 @@
"lambda": null,
"max_mana": null
},
"equipment": []
"equipment": {
"head": null,
"body": null,
"weapon": null,
"accessories": [
null,
null,
null
]
}
}
},
{
Expand Down Expand Up @@ -416,7 +542,16 @@
"lambda": null,
"max_mana": null
},
"equipment": []
"equipment": {
"head": null,
"body": null,
"weapon": null,
"accessories": [
null,
null,
null
]
}
}
}
]
3 changes: 3 additions & 0 deletions pets-lib/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ pub mod battle {
/// are the ones that you get to use in battle.
pub const BATTLE_PARTY_SIZE: usize = 4;

/// Number of accessory slots per character
pub const ACCESSORY_SLOTS: usize = 3;

pub const INTRO_FADE_PREDELAY: f64 = 0.5;
pub const INTRO_COUNTDOWN_SEC: f64 = 3.0;

Expand Down
34 changes: 25 additions & 9 deletions pets-lib/src/items/inv.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
use super::*;
use crate::consts::battle::ACCESSORY_SLOTS;

pub trait ItemList {
/// Every item that can be equipped
fn offsets(&self) -> impl Iterator<Item = &InherentStats>;
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct Equipment {
head: Option<String>,
body: Option<String>,
weapon: Option<String>,
accessories: [Option<String>; ACCESSORY_SLOTS],
}

impl ItemList for Vec<Item> {
fn offsets(&self) -> impl Iterator<Item = &InherentStats> {
use ItemCat::*;
impl Equipment {
pub fn iter(&self) -> impl Iterator<Item = &str> {
self.head
.iter()
.chain(self.body.iter())
.chain(self.weapon.iter())
.chain(self.accessories.iter().filter_map(|s| s.as_ref()))
.map(|s| s.as_str())
}

pub fn offsets(&self) -> InherentStats {
self.iter().fold(InherentStats::default(), |acc, item| {
let ItemCat::Equipment { ref offsets, .. } =
Item::from_registry(item).category
else {
panic!("item {} not equippable", item)
};

self.iter().filter_map(|i| match &i.category {
Equipment { offsets, .. } => Some(offsets),
_ => None,
acc + offsets.clone()
})
}
}
Expand Down
10 changes: 7 additions & 3 deletions pets-lib/src/items/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::util::registry::*;
use std::sync::OnceLock;

mod inv;
pub use inv::{Inventory, ItemList};
pub use inv::{Equipment, Inventory};

pub static ITEM_REGISTRY: OnceLock<HashMap<String, Item>> = OnceLock::new();

Expand Down Expand Up @@ -78,8 +78,12 @@ pub enum AmmoCat {
}

impl Item {
pub fn is_equipment(&self) -> bool {
matches!(self.category, ItemCat::Equipment { .. })
pub fn from_registry(id: &str) -> &Item {
unwrap_fmt!(
ITEM_REGISTRY.get().unwrap().get(id),
"Item ID not found: {}",
id
)
}
}

Expand Down
15 changes: 5 additions & 10 deletions pets-lib/src/stats/battler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ pub struct Battler {
pub buffs_list: Vec<InherentStats>,

pub inherent_stats: InherentStats,
pub equipment: Vec<Item>,

/// The IDs of all equipped items
pub equipment: Equipment,
}

impl Default for Battler {
Expand All @@ -18,7 +20,7 @@ impl Default for Battler {
buffs_list: Vec::new(),

inherent_stats: InherentStats::default(),
equipment: Vec::new(),
equipment: Equipment::default(),
}
}
}
Expand All @@ -45,14 +47,7 @@ impl Battler {
/// This should take armor, weapons, etc. into account for players.
/// It should NOT consider in-battle buffs/debuffs.
fn armored_stats(&self) -> InherentStats {
let inherent = self.inherent_stats.clone();

// get all offsets from each item that has one
let equips = &self.equipment;
let offsets = equips.offsets();

// ... and sum them up
inherent + offsets.cloned().sum()
self.inherent_stats.clone() + self.equipment.offsets()
}

/// The final "in practice" stats of the character.
Expand Down

0 comments on commit 8541ace

Please sign in to comment.