summaryrefslogtreecommitdiff
path: root/src/modules/engines.rs
blob: 10e4c859fa903e410c72c3dcdb74f948e53fc9a8 (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
65
66
67
68
69
70
use mass::Mass;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Engines {
    acceleration : (f64, f64, f64),
}

impl Engines {
    pub fn new() -> Engines {
        Engines {
            acceleration : (0.0, 0.0, 0.0)
        }
    }

    pub fn recv_acceleration(&mut self) -> (f64, f64, f64) {
        let acceleration = self.acceleration;
        self.acceleration = (0.0, 0.0, 0.0);
        acceleration
    }

    pub fn give_client_data(&mut self, ship : &Mass, target : Option<&Mass>, data : String) {
        let mut acceleration = (0.0, 0.0, 0.0);
        match data.as_bytes() {
            b"5\n" => acceleration.0 += 0.1,
            b"0\n" => acceleration.0 -= 0.1,
            b"8\n" => acceleration.1 += 0.1,
            b"2\n" => acceleration.1 -= 0.1,
            b"4\n" => acceleration.2 += 0.1,
            b"6\n" => acceleration.2 -= 0.1,
            b"+\n" => {
                let m_v = ship.velocity;
                acceleration = (m_v.0 * 0.05,
                                m_v.1 * 0.05,
                                m_v.2 * 0.05);
            },
            b"-\n" => {
                let m_v = ship.velocity;
                acceleration = (-1.0 * m_v.0 * 0.05,
                                -1.0 * m_v.1 * 0.05,
                                -1.0 * m_v.2 * 0.05);
            },
            b"c\n" => {
                match target {
                    Some(target) => {
                        let d_v = target.velocity;
                        let m_v = ship.velocity;
                        acceleration = (d_v.0 - m_v.0,
                                        d_v.1 - m_v.1,
                                        d_v.2 - m_v.2);
                    },
                    None => (),
                }
            },
            b"t\n" => {
                match target {
                    Some(target) => {
                        let d_p = target.position;
                        let m_p = ship.position;
                        acceleration = ((d_p.0 - m_p.0) * 0.01,
                                        (d_p.1 - m_p.1) * 0.01,
                                        (d_p.2 - m_p.2) * 0.01);
                    },
                    None => (),
                }
            },
            _ => (),
        }
        self.acceleration = acceleration;
    }
}