Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use rand::Rng;
- use rand::prelude::{thread_rng, ThreadRng};
- use rand::distributions::Uniform;
- fn main() {
- let mut pc = Token::new("BigG0ku", "Fighter");
- pc.attributes.attribute_gen();
- pc.wearing = Armor::new("Leather Tunic", 14, "An old tunic, made of patches of mismatched leather.");
- pc.wielding = Weapon::new("Halberd", 12, "A rusty halberd. It's seen many battles, and taken many more lives.");
- pc.read_stats();
- let mut creature = Token::new("LittleGoh4n", "Monk");
- creature.attributes.attribute_gen();
- creature.wearing = Armor::new("Old Robes", 12, "Dusty brown robes, that give the impression of having once been white.");
- creature.wielding = Weapon::new("Shortsword", 6, "A shortsword, sharpened once again into a battle-ready state.");
- creature.read_stats();
- for i in 0..5 {
- println!("{} swings his {}, dealing {} damage!", pc.name, pc.wielding.name, pc.wielding.damage());
- }
- println!("\n---> Exitting program <---");
- }
- struct Dice {
- amount: i32,
- #[allow(dead_code)]
- size: i32,
- range: Uniform<i32>,
- rng: ThreadRng,
- }
- impl Dice {
- /*
- roll() - returns i32 from total summation of a group of dice
- roll_poll() - returns Vec<i32> from a group of dice, retaining individual value.
- */
- pub fn new(amount: i32, size: i32) -> Self {
- Self {
- amount,
- size,
- range: Uniform::new(1, size),
- rng: thread_rng(),
- }
- }
- fn roll(self) -> i32 {
- // Roll dice and then sum the die results together, returning one integer.
- let amount = self.amount;
- let range = self.range;
- let mut rng = self.rng;
- let throw_dice: Vec<i32> = (0..amount).map(|_| rng.sample(range)).collect();
- let result: i32 = throw_dice.iter().fold(0, |acc, elem| acc + *elem);
- // Output.
- return result;
- }
- fn roll_pool(self) -> Vec<i32> {
- // Roll dice as a "pool," ie a collection of individual dice. Return this as a vector.
- let amount: i32 = self.amount;
- let range = self.range;
- let mut rng = self.rng;
- let throw_dice: Vec<i32> = (0..amount).map(|_| rng.sample(range)).collect();
- // Output.
- return throw_dice;
- }
- }
- // The data that makes each Token numerically unique.
- struct TokenAttributes {
- strength: i32,
- dex: i32,
- wis: i32,
- hp: i32,
- }
- // Extends TokenAttributes to have the capability of generating and returning values for stats.
- impl TokenAttributes {
- fn new(strength: i32, dex: i32, wis: i32, hp: i32) -> Self {
- Self {
- strength,
- dex,
- wis,
- hp,
- }
- }
- fn attribute_gen(&mut self) -> i32 {
- let _attribute_array: Vec<i32> = vec![0, 0, 0]
- .into_iter()
- .map(|_| Dice::new(3, 6).roll().clone())
- .collect();
- self.strength = _attribute_array[0];
- self.dex = _attribute_array[1];
- self.wis = _attribute_array[2];
- self.hp = 10 + self.strength;
- // This is a hack, and isn't the proper way to do this, I think.
- return 1;
- }
- }
- // The core of this structure. Everything here will be implemented into Token in some format.
- // To be used for characters, as token is the generic term for any piece on a gameboard.
- struct Token {
- name: &'static str,
- class: &'static str,
- dead: bool,
- attributes: TokenAttributes,
- wearing: Armor,
- wielding: Weapon,
- }
- // Traits for Token.
- trait TokenFeatures {
- // Static constructor, hence 'Self'.
- fn new(name: &'static str, class: &'static str) -> Self;
- // Return strings.
- fn name(&self) -> &'static str;
- fn class(&self) -> &'static str;
- }
- impl Token {
- // Unique functions to the struct, not generic-as-from-Trait.
- fn is_dead(&self) -> bool {
- self.dead
- }
- fn health(&mut self) {
- if self.is_dead() {
- // Implementor methods can use implementor trait methods.
- println!("{} is already dead...", self.name());
- } else {
- println!("{} is alive and well!", self.name);
- }
- }
- fn assign_shout(&self) -> &'static str {
- if self.is_dead() {
- "'I'm... doomed...'"
- } else {
- "'Hrrrgh, die, monster!!'"
- }
- }
- // Default methods can be overridden.
- fn battle_shout(&self) {
- println!("{} shouts out a phrase: {}", self.name, self.assign_shout());
- }
- fn read_stats(&self) {
- println!("# {} the {} \n\
- ############ Attributes ###########\n\
- # Health Points: {} \n\
- # Armor Class: {} \n\
- # Strength: {} \n\
- # Dexterity: {} \n\
- # Wisdom: {} \n\
- ############ Equipment ############\n\
- # Wearing: {} \n\
- # Wielding: {} \n\
- ###################################\n", self.name, self.class,
- self.attributes.hp, self.wearing.value, self.attributes.strength, self.attributes.dex, self.attributes.wis,
- self.wearing.description, self.wielding.description)
- }
- }
- impl TokenFeatures for Token {
- // Self is the implementor type: 'Token'.
- fn new(name: &'static str, class: &'static str) -> Self {
- Self {
- name,
- class,
- dead: false,
- attributes: TokenAttributes::new(8, 8, 8, 8),
- wearing: Armor::new("Plain Clothes", 10, "nothing"),
- wielding: Weapon::new("Bare Hands", 1, "unarmed"),
- }
- }
- fn name(&self) -> &'static str {
- self.name
- }
- fn class(&self) -> &'static str {
- self.class
- }
- }
- // TODO Implement boolean states for broken/unbroken.
- struct Armor {
- name: &'static str,
- value: i32,
- description: &'static str,
- broken: bool,
- }
- struct Weapon {
- name: &'static str,
- value: i32,
- description: &'static str,
- broken: bool,
- }
- trait EquipmentFeatures {
- fn new(name: &'static str, value: i32, description: &'static str) -> Self;
- fn name(&self) -> &'static str;
- fn description(&self) -> &'static str;
- }
- impl EquipmentFeatures for Armor {
- fn new(name: &'static str, value: i32, description: &'static str) -> Self {
- Self {
- name,
- value,
- description,
- broken: false,
- }
- }
- fn name(&self) -> &'static str {
- self.name
- }
- fn description(&self) -> &'static str {
- self.description
- }
- }
- impl EquipmentFeatures for Weapon {
- fn new(name: &'static str, value: i32, description: &'static str) -> Self {
- Self {
- name,
- value,
- description,
- broken: false,
- }
- }
- fn name(&self) -> &'static str {
- self.name
- }
- fn description(&self) -> &'static str {
- self.description
- }
- }
- impl Weapon {
- fn is_broken(&self) -> bool {
- self.broken
- }
- fn damage(&self) -> i32 {
- if self.is_broken() {
- 1
- } else {
- Dice::new(1, self.value).roll()
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement