summaryrefslogtreecommitdiff
path: root/src/world.rs
blob: 3542471cdc310ff63e8dace8036e8af5a157c93c (plain)
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
51
52
53
54
55
use inhabitant;

pub struct World {
    pub name: String,
    pub width: i32,
    pub height: i32,
    pub inhabitants: Vec<inhabitant::Inhabitant>,
}

impl World {
    pub fn new(name: &str, width: i32, height: i32) -> World {
        World {
            name: name.to_string(),
            width: width,
            height: height,
            inhabitants: vec![inhabitant::Inhabitant::Nothing; (width * height) as usize],
        }
    }

    pub fn new_rand(name: &str, width: i32, height: i32) -> World {
        use rand;
        use rand::Rng;

        let mut world = World {
            name: name.to_string(),
            width: width,
            height: height,
            inhabitants: vec![inhabitant::Inhabitant::Nothing; (width * height) as usize],
        };
        let mut rng = rand::thread_rng();
        for i in 0..(width*height) {
            if rng.gen_weighted_bool(5) {
                world.inhabitants[i as usize] = inhabitant::Inhabitant::Rock;
            }
            else if rng.gen_weighted_bool(7) {
                world.inhabitants[i as usize] = inhabitant::Inhabitant::Food { amount: rng.gen_range(1, 200) };
            }
        }

        world
    }

    pub fn print(&self) {
        for row in (0..self.height).rev() {
            for col in (1..self.width).step_by(2) {
                print!("    {:?}", self.inhabitants[( col * self.height + row ) as usize]);
            }
            print!("\n ");
            for col in (0..self.width).step_by(2) {
                print!("{:?}    ", self.inhabitants[( col * self.height + row ) as usize]);
            }
            print!("\n");
        }
    }
}