Last active
February 16, 2017 12:20
-
-
Save shakesoda/2bce19ab43317acc2b7f 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
| // Apparently, this needs to be *before* I actually import the fucking mod | |
| // wat. | |
| use player::*; | |
| mod player; | |
| fn main() { | |
| let mut stats = vec![ | |
| Stat::new("HP".to_string(), player::default_advancer_fn), | |
| Stat::new("SP".to_string(), player::default_advancer_fn), | |
| Stat::new("ATK".to_string(), player::default_advancer_fn), | |
| Stat::new("DEF".to_string(), player::default_advancer_fn) | |
| ]; | |
| stats[0].advance(1200); | |
| stats[1].advance(800); | |
| stats[2].advance(4000); | |
| stats[3].advance(250); | |
| let p = Player::new("holo".to_string(), stats); | |
| for stat in p.stats { | |
| println!("{}: {}", stat.skill, stat.level); | |
| } | |
| } |
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
| pub type AdvancerFn = fn(exp: u32) -> u32; | |
| pub struct Stat { | |
| pub skill: String, | |
| pub exp: u32, | |
| pub level: u32, | |
| advancer: AdvancerFn, | |
| } | |
| 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: u32) -> u32 { | |
| let lv = self.level; | |
| self.exp += exp; | |
| self.level = (self.advancer)(self.exp); | |
| return self.level - lv; | |
| } | |
| } | |
| // insert exp, receive current level | |
| pub fn default_advancer_fn(exp: u32) -> u32 { | |
| return exp / 100; | |
| } | |
| pub struct Player { | |
| pub name: String, | |
| pub stats: Vec<Stat> | |
| } | |
| impl Player { | |
| pub fn new(name: String, stats: Vec<Stat>) -> Player { | |
| return Player { | |
| name: name, | |
| stats: stats | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment