Created
July 12, 2014 22:05
-
-
Save shakesoda/d1ba64bdbb8df72ad62b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| mod player { | |
| type AdvancerFn = fn(exp: uint) -> uint; | |
| pub struct Stat { | |
| pub skill: String, | |
| pub exp: uint, | |
| pub level: uint, | |
| advancer: AdvancerFn, | |
| } | |
| // insert exp, receive current level | |
| pub fn default_advancer_fn(exp: uint) -> uint { | |
| return exp / 100u; | |
| } | |
| impl Stat { | |
| pub fn new(name: String, advancer: AdvancerFn) -> Stat { | |
| let exp = 0; | |
| return Stat { | |
| skill: name, | |
| exp: exp, | |
| level: advancer(exp), | |
| advancer: advancer | |
| } | |
| } | |
| pub fn advance(&mut self, exp: uint) -> uint { | |
| let lv = self.level; | |
| self.exp += exp; | |
| self.level = (self.advancer)(self.exp); | |
| return self.level - lv; | |
| } | |
| } | |
| /* | |
| pub struct Player { | |
| stats: Vec<Stat> | |
| } | |
| impl Player { | |
| pub fn new(name: String, stats: Vec<Stat>) -> Player { | |
| return Player { | |
| stats: stats | |
| } | |
| } | |
| } | |
| */ | |
| } | |
| fn main() { | |
| let mut hp = player::Stat::new(String::from_str("HP"), player::default_advancer_fn); | |
| let mut sp = player::Stat::new(String::from_str("SP"), player::default_advancer_fn); | |
| hp.advance(1200); | |
| sp.advance(800); | |
| println!("level: {}, {}", hp.level, sp.level); | |
| //let mut stats = vec![hp, sp]; | |
| //let p = player::Player::new(String::from_str("holo"), stats); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment