1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
use rand::Rng;
use super::components::{Visible, Blocking, Played};
use super::assemblage;
use super::assemblage::Assemblage;
assemblage!(Wall {}; Visible{sprite: "wall".to_string(), height: 2.0}, Blocking);
assemblage!(Grass { sprite : String}; Visible{sprite: sprite.to_string(), height: 0.1});
impl Grass {
pub fn new() -> Grass {
Grass {
sprite: ["grass1", "grass2", "grass3", "grass1", "grass2", "grass3", "ground"][rand::thread_rng().gen_range(0,7)].to_string()
}
}
}
assemblage!(Player {name: String}; Visible{sprite: "player".to_string(), height: 1.0}, Played::new(name.to_string()));
impl Player {
pub fn new(name: &str) -> Player {
Player { name: name.to_string()}
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
use super::super::hashmap;
#[test]
fn test_assemblage_from_json() {
let mut p = Player::new("Joe");
assert_eq!(p.name, "Joe");
p.init_from_json(vec![json!("Bob"), json!("Mike")], hashmap!());
assert_eq!(p.name, "Bob");
p.init_from_json(vec![], hashmap!("sprite" => json!("stone")));
assert_eq!(p.name, "Bob");
p.init_from_json(vec![], hashmap!("name" => json!("Teddy")));
assert_eq!(p.name, "Teddy");
p.init_from_json(vec![json!("Bill")], hashmap!("name" => json!("Stan")));
assert_eq!(p.name, "Stan");
}
}
|