Skip to content

Commit

Permalink
feat: add DisplayInformation shared trait
Browse files Browse the repository at this point in the history
  • Loading branch information
nwesterhausen committed Mar 6, 2024
1 parent 7b82e19 commit c098402
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion game_library/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "game_library"
version = "2.4.0"
version = "2.5.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
19 changes: 11 additions & 8 deletions game_library/src/data_loader/spells.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use super::{
DataFile,
};

/// System to load spell data into the spells vault for usage in the game.
///
/// This responds to the `LoadedSpellData` event and loads the spell data into the `GameData` resource.
pub(super) fn load_spells(
mut events: EventReader<LoadedSpellData>,
mut game_data: ResMut<GameData>,
Expand All @@ -17,20 +20,19 @@ pub(super) fn load_spells(
return;
}

tracing::info!("Load spells event with {} spells", events.len());
debug!("Load spells event with {} spells", events.len());
for event in events.read() {
let unique_id = &event.spell_data.header.unique_id;
let spell = event.spell_data.data.clone().with_unique_id(unique_id);

game_data.spells.insert(unique_id.clone(), spell);
tracing::debug!(
"load_spells: loaded spell {} as {}",
event.spell_data.data.name(),
unique_id
);
info!("Loaded {}", event.spell_data.data);
}
}

/// System to parse the spell data files and load them into the game.
///
/// This responds to the `DataFileFound` event and reads the spell data from the file, and upon success, sends a `LoadedSpellData` event.
pub(super) fn parse_spell_file(
mut er_df_found: EventReader<DataFileFound>,
mut ew_spell_df: EventWriter<LoadedSpellData>,
Expand All @@ -41,8 +43,9 @@ pub(super) fn parse_spell_file(
d
} else {
warn!(
"load_data_file_dir: failed to read spell data from {}",
event.header.unique_id
"parse_spell_file: failed to read spell data from {}; {}",
event.header.unique_id,
event.filepath.display()
);
continue;
};
Expand Down
4 changes: 2 additions & 2 deletions game_library/src/enums/cast_slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ impl std::fmt::Display for CastSlot {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Cantrip => write!(f, "Cantrip"),
Self::Slot => write!(f, "Slot"),
Self::Ultimate => write!(f, "Ultimate"),
Self::Slot => write!(f, "Spell"),
Self::Ultimate => write!(f, "Ultimate Spell"),
}
}
}
1 change: 1 addition & 0 deletions game_library/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub use noise::NoisePlugin;
pub use physics::PhysicsPlugin;
pub use realm_data::Realm;
pub use schedule::*;
pub use shared_traits::DisplayInformation;
pub use shared_traits::InternalId;
pub use simple_object::SimpleObject;
pub use skill::Skills;
Expand Down
17 changes: 17 additions & 0 deletions game_library/src/shared_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,20 @@ pub trait KnownCastSlot {
/// Get the cast slot of the spell.
fn cast_slot(&self) -> CastSlot;
}

/// A trait to supply display various display information about an object.
///
/// This is useful for objects that need to be displayed in a user interface, and since
/// all object in the game are dynamically loaded, being able to use the functions in this
/// trait will allow simplifying the process of displaying the information about loaded
/// data to the user.
pub trait DisplayInformation {
/// A tooltip that summarizes the object. Can be multiline and describes the object in brief.
fn tooltip(&self) -> String;
/// A longer description of the object. Can be multiline and describes the object in detail. This
/// is used for a more detailed description of the object.
fn more_info(&self) -> String;
/// A one-line summary of the object. This puts it in a single line for lists and other places
/// where a single line is needed.
fn summarized_one_liner(&self) -> String;
}
37 changes: 36 additions & 1 deletion game_library/src/spells/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{talisman::SpellTalisman, SpellParticles};
use crate::{
data_loader::DataFile,
enums::{CastSlot, CastType, GameSystem, Skill},
InternalId, StatEffect,
DisplayInformation, InternalId, StatEffect,
};
use bevy::prelude::*;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -473,6 +473,41 @@ impl Spell {
}
}

impl DisplayInformation for Spell {
fn tooltip(&self) -> String {
format!(
"{}\n{} {} {}\n{}",
self.name,
self.talisman.tier(),
self.skill,
self.cast_slot,
self.description
)
}

fn more_info(&self) -> String {
format!("{}\n{}", self.description(), self.lore())
}

fn summarized_one_liner(&self) -> String {
format!("{self}")
}
}

impl std::fmt::Display for Spell {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// Include name, tier, magic type, and cast slot
write!(
f,
"{}: {} {} {}",
self.name,
self.talisman.tier(),
self.skill,
self.cast_slot
)
}
}

/// #### DEFAULTS FOR SERDE ####
mod spell_defaults {
use super::SpellTalisman;
Expand Down
17 changes: 17 additions & 0 deletions game_library/src/spells/talisman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,23 @@ pub enum Tier {
Unique,
}

impl std::fmt::Display for Tier {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Mundane => write!(f, "Mundane"),
Self::Common => write!(f, "Common"),
Self::Uncommon => write!(f, "Uncommon"),
Self::Rare => write!(f, "Rare"),
Self::Epic => write!(f, "Epic"),
Self::Legendary => write!(f, "Legendary"),
Self::Mythic => write!(f, "Mythic"),
Self::Divine => write!(f, "Divine"),
Self::Astral => write!(f, "Astral"),
Self::Unique => write!(f, "Unique"),
}
}
}

impl Default for SpellTalisman {
fn default() -> Self {
Self {
Expand Down

0 comments on commit c098402

Please sign in to comment.