Skip to content

Commit

Permalink
Merge pull request #75 from Lamby777/pchar-enum
Browse files Browse the repository at this point in the history
Convert `PChar` to an enum instead of const strings
  • Loading branch information
Lamby777 committed Jun 16, 2024
2 parents db39de3 + c5187e1 commit 906bb3a
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 126 deletions.
9 changes: 5 additions & 4 deletions pets-lib/src/battle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,18 @@ impl BattleEngine {

pub fn swap_party_member(&mut self, new_index: usize) {
self.current_party_member = new_index;
let id = self.battlers.good_guys[new_index].id();
godot_print!("Swapped to party member `{}`", id);
let pchar = self.battlers.good_guys[new_index].id();
let pchar = PChar::from_godot(pchar.into());
godot_print!("Swapped to party member `{}`", pchar);

// set battle icon sprite
self.icon.bind_mut().set_icon(&id);
self.icon.bind_mut().set_icon(pchar);

// set battle portrait texture
let mut portrait =
self.base().get_node_as::<TextureRect>("%PortraitTexture");

let path = format!("res://assets/textures/portraits/{}.png", id);
let path = format!("res://assets/textures/portraits/{}.png", pchar);
let texture = load::<Texture2D>(path);
portrait.set_texture(texture);
}
Expand Down
10 changes: 5 additions & 5 deletions pets-lib/src/battle/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ pub struct BattleIcon {

#[godot_api]
impl BattleIcon {
fn pchar_to_frame(pchar_id: &str) -> i32 {
match pchar_id {
fn pchar_to_frame(pchar: PChar) -> i32 {
match pchar {
PChar::ETHAN => 0,
PChar::TERRA => 1,
PChar::SIVA => 2,
Expand All @@ -57,15 +57,15 @@ impl BattleIcon {
PChar::FUZZY => 8,

_ => {
godot_warn!("PChar {} doesn't have a battle icon (yet). Defaulting to Ethan's icon.", pchar_id);
godot_warn!("PChar {} doesn't have a battle icon (yet). Defaulting to Ethan's icon.", pchar);
0
}
}
}

pub fn set_icon(&mut self, pchar_id: &str) {
pub fn set_icon(&mut self, pchar: PChar) {
let mut sprite = self.base().get_node_as::<Sprite2D>("Sprite2D");
sprite.set_frame(Self::pchar_to_frame(pchar_id));
sprite.set_frame(Self::pchar_to_frame(pchar));
}

fn process_movement(&mut self, delta: f64) {
Expand Down
4 changes: 2 additions & 2 deletions pets-lib/src/battle/skills/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::fmt;

use godot::prelude::*;
use godot::tools::tr;
use strum::{EnumIter, IntoEnumIterator};
use strum::{EnumIter, IntoEnumIterator as _};

mod status_effects;
use status_effects::*;
Expand All @@ -22,7 +22,7 @@ pub(crate) use other::{PSIFluxSkill, PSIRewireSkill};
pub(crate) use recovery::RecoverySkill;
pub(crate) use shields::ShieldSkill;

type BattlerPtr = Rc<RefCell<dyn Battler>>;
pub(crate) type BattlerPtr = Rc<RefCell<dyn Battler>>;

#[typetag::serde(tag = "type")]
pub trait SkillFamily {
Expand Down
2 changes: 1 addition & 1 deletion pets-lib/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fn debug_skill(_args: GArgs) -> GReturn {
fn debug_battle(_args: GArgs) -> GReturn {
end_interaction();

World::start_battle(EnemyID::A_NONNY_MOUSE.into());
World::start_battle(&EnemyID::A_NONNY_MOUSE);
Ok(Variant::nil())
}

Expand Down
10 changes: 5 additions & 5 deletions pets-lib/src/stats/autoload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl StatsInterface {
}

// #[func]
pub fn get_character(&self, ch: &str) -> CharData {
pub fn get_character(&self, ch: &PChar) -> CharData {
self.save
.chars
.get(ch)
Expand All @@ -46,7 +46,7 @@ impl StatsInterface {
}

/// Get the list of stat calculation functions for a given character
pub fn get_statcalc(&self, ch: &str) -> Rc<StatCalcList> {
pub fn get_statcalc(&self, ch: &PChar) -> Rc<StatCalcList> {
self.statcalcs
.get(ch)
.expect("key should be a valid PChar name")
Expand All @@ -63,13 +63,13 @@ macro_rules! impl_stat_getters_on_si {
concat_idents::concat_idents!(fn_name = natural_, $stat, _of {
/// Get the stat of a given character at a level,
/// not including equips or consumables
pub fn fn_name(&self, pchar: &str) -> IntegralStat {
pub fn fn_name(&self, pchar: PChar) -> IntegralStat {
// get character level
let ch = self.get_character(pchar);
let ch = self.get_character(&pchar);
let lvl = ch.level;

// get calculation fn for character
let calcs = self.get_statcalc(pchar);
let calcs = self.get_statcalc(&pchar);

// calculate the stat
(calcs.speed)(lvl)
Expand Down
4 changes: 3 additions & 1 deletion pets-lib/src/stats/battler.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::battle::skills::BattlerPtr;

use super::*;

/// Trait for stuff that both party members and enemies
Expand Down Expand Up @@ -84,5 +86,5 @@ pub trait Battler {

pub struct Battlers {
pub good_guys: Vec<Box<dyn Battler>>,
pub _bad_guys: Vec<Box<dyn Battler>>,
pub _bad_guys: Vec<BattlerPtr>,
}
28 changes: 14 additions & 14 deletions pets-lib/src/stats/charmap.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use strum::IntoEnumIterator as _;

use crate::prelude::*;

pub type CharMap = HashMap<String, RefCell<CharData>>;
pub type CharMap = HashMap<PChar, RefCell<CharData>>;

/// CharMap with all characters having the same exact stats
pub fn uniform_charmap() -> CharMap {
PChar::ALL.iter().fold(CharMap::new(), |mut map, chname| {
PChar::iter().fold(CharMap::new(), |mut map, chname| {
map.insert(
chname.to_string(),
chname,
RefCell::new(CharData {
id: (*chname).to_owned(),
id: chname,
..Default::default()
}),
);
Expand All @@ -18,12 +20,10 @@ pub fn uniform_charmap() -> CharMap {

/// The default stat calculation functions for all characters
pub fn uniform_statcalcmap() -> CharStatCalcs {
PChar::ALL
.iter()
.fold(CharStatCalcs::new(), |mut calcs, chname| {
calcs.insert(chname.to_string(), Rc::new(StatCalcList::default()));
calcs
})
PChar::iter().fold(CharStatCalcs::new(), |mut calcs, chname| {
calcs.insert(chname, Rc::new(StatCalcList::default()));
calcs
})
}

/// "Jat Chippity goes hard"
Expand All @@ -36,14 +36,14 @@ macro_rules! ch_unique_registry {
}),* $(,)?) => {
$(
let character = PChar::$character;
$map.entry(character.to_owned()).and_modify(|pchar| {
$map.entry(character).and_modify(|pchar| {
let mut pchar = pchar.borrow_mut();
$(
pchar.$field$(.$property)? = $value;
)*
});

$calcs.insert(character.to_owned(), {
$calcs.insert(character, {
let calcs = StatCalcList {
$(
$base: StatCalcFn::from($base_fn as fn(_) -> _),
Expand Down Expand Up @@ -103,7 +103,7 @@ mod tests {
#[test]
fn ch_unique_charmap_works() {
let (charmap, _) = default_charmap();
let ethan = charmap.get(PChar::ETHAN).unwrap();
let ethan = charmap.get(&PChar::ETHAN).unwrap();
let ethan = ethan.borrow();
assert_eq!(ethan.display_name, "Ethan");
// assert_eq!(ethan.base_stats.max_hp, 12);
Expand All @@ -129,7 +129,7 @@ mod tests {
},
}

let calcs = calcs.get(PChar::ETHAN).unwrap();
let calcs = calcs.get(&PChar::ETHAN).unwrap();
assert_eq!((calcs.max_hp)(20), 14);
assert_eq!((calcs.max_mana)(40), Some(41));
}
Expand Down
5 changes: 1 addition & 4 deletions pets-lib/src/stats/enemy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub struct ItemDrops {
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct EnemyData {
pub id: String,

pub inherent_stats: InherentStats,
pub battle_stats: BattleStats,

Expand All @@ -20,11 +19,9 @@ pub struct EnemyData {
}

impl EnemyData {
pub fn from_id(id: &str) -> Self {
pub fn from_id(id: EnemyID) -> Self {
Self {
id: id.to_string(),

// TODO
inherent_stats: InherentStats::default(),
battle_stats: BattleStats::default(),
status_effects: HashSet::new(),
Expand Down
6 changes: 3 additions & 3 deletions pets-lib/src/stats/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct BattleStats {
/// All the information the game needs to know about a character
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CharData {
pub id: String,
pub id: PChar,

/// Name of the character, as picked by the user
/// ⚠️⚠️⚠️ See <https://github.com/Lamby777/PETS-G/issues/23>
Expand All @@ -63,7 +63,7 @@ pub struct CharData {

impl Battler for CharData {
fn id(&self) -> String {
self.id.clone()
self.id.to_string()
}

fn hp_mut(&mut self) -> &mut IntegralStat {
Expand Down Expand Up @@ -195,7 +195,7 @@ impl Display for StatusEffect {
impl Default for CharData {
fn default() -> Self {
CharData {
id: PChar::DEVON.to_owned(),
id: PChar::DEVON,
display_name: "Chicken Nugget".to_owned(),
level: 1,

Expand Down
Loading

0 comments on commit 906bb3a

Please sign in to comment.