summaryrefslogtreecommitdiff
path: root/src/modules/engines.rs
blob: fa16bf96050943fcc4f169d22b3c9ee0850ed63f (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
71
72
73
74
75
76
77
78
79
80
81
use crate::constants;
use crate::mass::Mass;
use crate::math::Vector;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum EnginesStatus {
    None,
    ApproachingTargetVelocity,
}

impl Default for EnginesStatus {
    fn default() -> Self {
        EnginesStatus::None
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Engines {
    acceleration: Vector,
    target_velocity: Option<Vector>,
    pub fuel: f64,
}

impl Engines {
    pub fn new() -> Engines {
        Engines {
            acceleration: Vector::default(),
            target_velocity: None,
            fuel: constants::SHIP_ENGINES_FUEL_START,
        }
    }

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

        if self.fuel - acceleration.magnitude() >= 0.0 {
            self.fuel -= acceleration.magnitude();
            acceleration
        } else {
            Vector::default()
        }
    }

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