Created
January 19, 2015 19:54
-
-
Save jmcnevin/8b2840ea76c2698eaf1e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use actors::Race::*; | |
pub enum Race { | |
Human, | |
Robot, | |
Dog, | |
Wolf, | |
Bear | |
} | |
impl Race { | |
pub fn max_health(&self) -> usize { | |
match *self { | |
Human => 10, | |
Robot => 30, | |
Dog => 5, | |
Wolf => 8, | |
Bear => 15 | |
} | |
} | |
pub fn damage(&self) -> usize { | |
match *self { | |
Human => 3, | |
Robot => 5, | |
Dog => 1, | |
Wolf => 2, | |
Bear => 3 | |
} | |
} | |
pub fn greeting(&self) -> &str { | |
match *self { | |
Human => "Hey", | |
Robot => "Beep", | |
Dog => "Woof", | |
Wolf => "Growl", | |
Bear => "ROAR" | |
} | |
} | |
} | |
pub struct Monster { | |
pub race: Race, | |
pub health: usize | |
} | |
impl Monster { | |
pub fn new(race: Race) -> Monster { | |
Monster { | |
race: race, | |
health: race.max_health() | |
} | |
} | |
pub fn say_hello(&self) -> &str { | |
self.race.greeting() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment