summaryrefslogtreecommitdiff
path: root/src/astroid.rs
blob: 077c5bff28747588828d92cb67caa7d5c5d7f754 (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
56
57
58
59
60
61
62
63
64
use mass::{Mass, Type};
extern crate rand;
use self::rand::distributions::Range;
use astroid::rand::distributions::Sample;
extern crate serde_json;
use astroid::rand::Rng;

#[derive(Serialize, Deserialize, Debug)]
pub struct Astroid {
    name        : String,
    t           : Type,
    position    : (f64, f64, f64),
    velocity    : (f64, f64, f64),
}

impl Astroid {
    pub fn new() -> Astroid {
        let name : String = rand::thread_rng()
                            .gen_ascii_chars()
                            .take(8)
                            .collect();
        let mut pr = Range::new(-50.0, 50.0);
        let mut vr = Range::new(-5.0, 5.0);
        let mut rng = rand::thread_rng();
        let position = (pr.sample(&mut rng), pr.sample(&mut rng), pr.sample(&mut rng));
        let velocity = (vr.sample(&mut rng), vr.sample(&mut rng), vr.sample(&mut rng));
        Astroid {
            name        : name,
            t           : Type::Astroid,
            position    : position,
            velocity    : velocity,
        }
    }
}

impl Mass for Astroid {
    fn name(&self) -> &String {
        &self.name
    }

    fn position(&self) -> (f64, f64, f64) {
        self.position
    }

    fn serialize(&self) ->String {
        serde_json::to_string(self).unwrap()
    }

    fn slow(&mut self) {

    }

    fn process(&mut self) {
        self.position.0 += self.velocity.0;
        self.position.1 += self.velocity.1;
        self.position.2 += self.velocity.2;
    }

    fn give_acceleration(&mut self, acceleration : (f64, f64, f64)) {
        self.velocity.0 += acceleration.0;
        self.velocity.1 += acceleration.1;
        self.velocity.2 += acceleration.2;
    }
}