summaryrefslogtreecommitdiff
path: root/src/modules/engines.rs
blob: f319ee5a89342a9c9fafb60896e74c6f38960b2e (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
use crate::mass::Mass;
use crate::math::Vector;

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

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

    pub fn recv_acceleration(&mut self) -> Vector {
        let acceleration = self.acceleration.clone();
        self.acceleration = Vector::default();
        acceleration
    }

    pub fn give_client_data(&mut self, ship: &Mass, target: Option<&Mass>, data: String) {
        let mut acceleration = Vector::default();
        match data.as_bytes() {
            b"5\n" => acceleration.a += 0.1,
            b"0\n" => acceleration.a -= 0.1,
            b"8\n" => acceleration.b += 0.1,
            b"2\n" => acceleration.b -= 0.1,
            b"4\n" => acceleration.c += 0.1,
            b"6\n" => acceleration.c -= 0.1,
            b"+\n" => acceleration = ship.velocity.clone() * 0.05,
            b"-\n" => {
                acceleration = ship.velocity.clone() * -1.05;
            }
            b"s\n" => {
                acceleration = ship.velocity.clone() * -1.0;
            }
            b"c\n" => {
                if let Some(target) = target {
                    acceleration = target.velocity.clone() - ship.velocity.clone();
                }
            }
            b"t\n" => {
                if let Some(target) = target {
                    acceleration = (target.position.clone() - ship.position.clone()) * 0.01;
                }
            }
            _ => (),
        }
        self.acceleration = acceleration;
    }
}