Skip to content

Commit

Permalink
add character creation; add Race and Class types to Player
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaechus committed Mar 17, 2020
1 parent 5af4799 commit dc22a95
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 35 deletions.
34 changes: 5 additions & 29 deletions Cargo.lock

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

27 changes: 25 additions & 2 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
entity::{Element, Enemy, Entity, Item},
input::{read_line, CmdTokens, Lexer, Parser},
player::Player,
types::{Action, CmdResult},
types::{Action, Class, CmdResult, Race},
world::World,
};

Expand Down Expand Up @@ -109,8 +109,11 @@ Some available commands:

/// Start a typical game for the command line
pub fn start(&self) {
if !self.running.get() {
self.create_character();
}

println!("Type \"help\" if you are unfamiliar with text-based games.\n");
println!("Use \"increase\" to use your initial stat points.\n");
println!("{}", self.ask("l"));

self.running.set(true);
Expand All @@ -119,6 +122,26 @@ Some available commands:
}
}

pub fn create_character(&self) {
self.player
.borrow_mut()
.set_race(Race::select_race(&self.prompt(
"Choose a race:\n \
1) Human (default)\n \
2) Dwarf\n \
3) Elf\n\n",
)));

self.player
.borrow_mut()
.set_class(Class::select_class(&self.prompt(
"Choose a class:\n \
1) Fighter (default)\n \
2) Rogue\n \
3) Wizard\n\n",
)));
}

/// Handle user input and return the results of commands and events
pub fn ask(&self, input: &str) -> String {
let command = Lexer::lex(input);
Expand Down
22 changes: 18 additions & 4 deletions src/player/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ use crate::{
Item::{self, Armor, Weapon},
},
inventory::Inventory,
types::{Action, CmdResult, CombatStatus, Items, Stats},
types::{Action, Class, CmdResult, CombatStatus, Items, Race, Stats},
};

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Player {
lvl: u32,
race: Race,
class: Class,
hp: (i32, u32),
xp: (u32, u32),
in_combat: CombatStatus,
lvl: u32,
stats: Stats,
main_hand: Option<Box<Item>>,
armor: Option<Box<Item>>,
Expand All @@ -27,10 +29,12 @@ pub struct Player {
impl Default for Player {
fn default() -> Self {
Self {
lvl: 1,
race: Race::Human,
class: Class::Fighter,
hp: (13, 13),
xp: (0, 1000),
in_combat: CombatStatus::Resting,
lvl: 1,
stats: Stats::new(),
main_hand: None,
armor: None,
Expand All @@ -40,6 +44,14 @@ impl Default for Player {
}

impl Player {
pub fn set_race(&mut self, race: Race) {
self.race = race;
}

pub fn set_class(&mut self, class: Class) {
self.class = class;
}

fn deal_damage(&self, weapon_damage: u32) -> u32 {
(weapon_damage as i32 + self.stats.strngth_mod()) as u32
}
Expand Down Expand Up @@ -192,12 +204,14 @@ impl Player {
CmdResult::new(
Action::Passive,
format!(
"Level: {}\
"Level {} {} {}\
\nHP: ({} / {})\
\nAC: {}\
\nXP: ({} / {})\
\n{}",
self.lvl,
self.race.to_string(),
self.class.to_string(),
self.hp(),
self.hp_cap(),
self.ac(),
Expand Down
26 changes: 26 additions & 0 deletions src/types/class.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum Class {
Fighter,
Wizard,
Rogue,
}

impl Class {
pub fn to_string(&self) -> String {
match self {
Self::Fighter => "Fighter".to_string(),
Self::Wizard => "Wizard".to_string(),
Self::Rogue => "Rogue".to_string(),
}
}

pub fn select_class(input: &str) -> Self {
match input.chars().next() {
Some('2') => Class::Rogue,
Some('3') => Class::Wizard,
_ => Class::Fighter,
}
}
}
4 changes: 4 additions & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
mod aliases;
mod class;
mod race;
mod results;
mod stats;
mod status;

pub use aliases::{Allies, Elements, Enemies, Items, Paths, Rooms};
pub use class::Class;
pub use race::Race;
pub use results::{Action, CmdResult};
pub use stats::Stats;
pub use status::{CombatStatus, EnemyStatus};
26 changes: 26 additions & 0 deletions src/types/race.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum Race {
Human,
Dwarf,
Elf,
}

impl Race {
pub fn to_string(&self) -> String {
match self {
Self::Human => "Human".to_string(),
Self::Dwarf => "Dwarf".to_string(),
Self::Elf => "Elf".to_string(),
}
}

pub fn select_race(input: &str) -> Self {
match input.chars().next() {
Some('2') => Race::Dwarf,
Some('3') => Race::Elf,
_ => Race::Human,
}
}
}

0 comments on commit dc22a95

Please sign in to comment.