Guest User

Untitled

a guest
May 21st, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #![allow(unused)]
  2.  
  3. struct Stat {
  4. value: i32,
  5. min: i32,
  6. max: i32
  7. }
  8. impl Stat {
  9. fn new() -> Stat {
  10. Stat {
  11. value: 100,
  12. min: 0,
  13. max: 100
  14. }
  15. }
  16. fn with_min(mut self, min: i32) -> Stat {
  17. self.min = min;
  18. self
  19. }
  20. fn with_max(mut self, max: i32) -> Stat {
  21. self.max = max;
  22. self
  23. }
  24. fn with_value(mut self, value: i32) -> Stat {
  25. self.value = value;
  26. self
  27. }
  28. fn with_value_min_max(mut self, value: i32, min: i32, max: i32) -> Stat {
  29. self.value = value;
  30. self.min = min;
  31. self.max = max;
  32. self
  33. }
  34.  
  35. fn increase(&mut self, amount: i32) -> Result<(),()> {
  36. if self.value + amount > self.max { return Err(()); }
  37.  
  38. self.value += amount;
  39. Ok(())
  40. }
  41. fn decrease(&mut self, amount: i32) -> Result<(),()> {
  42. if self.value - amount < self.min { return Err(()); }
  43.  
  44. self.value -= amount;
  45. Ok(())
  46. }
  47. }
  48.  
  49. struct Player {
  50. name: String,
  51. xp: u64,
  52. health: Stat,
  53. stamina: Stat,
  54. water: Stat,
  55. food: Stat,
  56. sleep: Stat
  57. }
  58. impl Player {
  59. fn new() -> Player {
  60. Player {
  61. name : String::from("Player"),
  62. xp : 0,
  63. health : Stat::new(),
  64. stamina: Stat::new(),
  65. water : Stat::new(),
  66. food : Stat::new(),
  67. sleep : Stat::new(),
  68. }
  69. }
  70. fn with_name(mut self, name: &str) -> Player {
  71. self.name = String::from(name);
  72. self
  73. }
  74. fn with_sleep(mut self, stat: Stat) -> Player {
  75. self.sleep = stat;
  76. self
  77. }
  78.  
  79. fn display(&self) {
  80. println!("{}", self.name);
  81. println!("\tXP: {}", self.xp);
  82. println!("\tHEALTH: {}\tSTAMINA: {}", self.health.value, self.stamina.value);
  83. println!("\tWATER : {}\tFOOD : {}", self.water.value, self.food.value);
  84. println!("\tSLEEP : {}", self.sleep.value);
  85. }
  86. }
  87.  
  88. fn main() {
  89. let player = Player::new()
  90. .with_name("Jim")
  91. .with_sleep(Stat::new().with_value(95));
  92.  
  93. player.display();
  94. }
Add Comment
Please, Sign In to add comment