Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![allow(unused)]
- struct Stat {
- value: i32,
- min: i32,
- max: i32
- }
- impl Stat {
- fn new() -> Stat {
- Stat {
- value: 100,
- min: 0,
- max: 100
- }
- }
- fn with_min(mut self, min: i32) -> Stat {
- self.min = min;
- self
- }
- fn with_max(mut self, max: i32) -> Stat {
- self.max = max;
- self
- }
- fn with_value(mut self, value: i32) -> Stat {
- self.value = value;
- self
- }
- fn with_value_min_max(mut self, value: i32, min: i32, max: i32) -> Stat {
- self.value = value;
- self.min = min;
- self.max = max;
- self
- }
- fn increase(&mut self, amount: i32) -> Result<(),()> {
- if self.value + amount > self.max { return Err(()); }
- self.value += amount;
- Ok(())
- }
- fn decrease(&mut self, amount: i32) -> Result<(),()> {
- if self.value - amount < self.min { return Err(()); }
- self.value -= amount;
- Ok(())
- }
- }
- struct Player {
- name: String,
- xp: u64,
- health: Stat,
- stamina: Stat,
- water: Stat,
- food: Stat,
- sleep: Stat
- }
- impl Player {
- fn new() -> Player {
- Player {
- name : String::from("Player"),
- xp : 0,
- health : Stat::new(),
- stamina: Stat::new(),
- water : Stat::new(),
- food : Stat::new(),
- sleep : Stat::new(),
- }
- }
- fn with_name(mut self, name: &str) -> Player {
- self.name = String::from(name);
- self
- }
- fn with_sleep(mut self, stat: Stat) -> Player {
- self.sleep = stat;
- self
- }
- fn display(&self) {
- println!("{}", self.name);
- println!("\tXP: {}", self.xp);
- println!("\tHEALTH: {}\tSTAMINA: {}", self.health.value, self.stamina.value);
- println!("\tWATER : {}\tFOOD : {}", self.water.value, self.food.value);
- println!("\tSLEEP : {}", self.sleep.value);
- }
- }
- fn main() {
- let player = Player::new()
- .with_name("Jim")
- .with_sleep(Stat::new().with_value(95));
- player.display();
- }
Add Comment
Please, Sign In to add comment