summaryrefslogtreecommitdiff
path: root/src/ship.rs
blob: 403b4ed93a9cbd5ef6b8696778e0a76ffd41acc0 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
extern crate serde_json;

use std::time::SystemTime;
//use std::collections::HashMap;

use storage::Storage;
use mass::{Mass, MassType};
//use module::{Module, ModuleType};
use module::ModuleType;
use targeting::{Targeting, TargetingStatus};

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Ship {
    mass_type   : MassType,
    position    : (f64, f64, f64),
    velocity    : (f64, f64, f64),
    range       : f64,
    modules     : Vec<ModuleType>,
    mining      : Mining,
    targeting   : Targeting,
//    modules     : HashMap<ModuleType, Box<Module>>,
    storage     : Storage,
}

impl Ship {
    pub fn new(position : (f64, f64, f64)) -> Ship {
        let mut modules = Vec::new();

        modules.push(ModuleType::Navigation);
        modules.push(ModuleType::Engines);
        modules.push(ModuleType::Dashboard);
        modules.push(ModuleType::Mining);

//        let mut modules : HashMap<ModuleType, Box<Module>> = HashMap::new();
//        modules.insert(ModuleType::Navigation, Targeting::new());
//        modules.insert(ModuleType::Mining, Mining::new());

        Ship {
            mass_type   : MassType::Ship,
            position    : position,
            velocity    : (0.0, 0.0, 0.0),
            range       : 100.0,
            modules     : modules,
            mining      : Mining::new(),
            targeting   : Targeting::new(),
            storage     : Storage::new(Vec::new()),
        }
    }

    pub fn slow(&mut self) {
        if self.velocity.0 > 0.01 {
            self.velocity.0 += -1.0 * self.velocity.0 * 0.1;
        }
        else {
            self.velocity.0 = 0.0;
        }

        if self.velocity.1 > 0.01 {
            self.velocity.1 += -1.0 * self.velocity.1 * 0.1;
        }
        else {
            self.velocity.1 = 0.0;
        }

        if self.velocity.2 > 0.01 {
            self.velocity.2 += -1.0 * self.velocity.2 * 0.1;
        }
        else {
            self.velocity.2 = 0.0;
        }
    }

    pub fn speedup(&mut self) {
        self.velocity.0 *= 1.05;
        self.velocity.1 *= 1.05;
        self.velocity.2 *= 1.05;
    }
    pub fn start_mining(&mut self) {
        self.mining.start()
    }

    pub fn stop_mining(&mut self) {
        self.mining.stop()
    }

    pub fn recv_range(&self) -> f64 {
        self.range
    }

    pub fn recv_mining_range(&self) -> f64 {
        self.mining.recv_range()
    }

    pub fn recv_mining_status(&self) -> bool {
        self.mining.recv_status()
    }

    pub fn give_target(&mut self, target : Option<String>) {
        self.targeting.give_target(target);
    }

    pub fn recv_target(&self) -> Option<String> {
        self.targeting.recv_target()
    }

    pub fn recv_targeting_status(&self) -> TargetingStatus {
        self.targeting.recv_status()
    }

    pub fn recv_modules(&self) -> String {
        serde_json::to_string(&self.modules).unwrap() + "\n"
    }
}

impl Mass for Ship {
    fn recv_mass_type(&self) -> MassType {
        self.mass_type.clone()
    }

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

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

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

    fn box_clone(&self) -> Box<Mass> {
        Box::new((*self).clone())
    }

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

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


#[derive(Serialize, Deserialize, Debug, Clone)]
struct Mining {
    range   : f64,
    status  : bool,
    time    : u64,
    start   : Option<SystemTime>,
}

impl Mining {
    pub fn new() -> Mining {
        Mining {
            range   : 10.0,
            status  : false,
            time    : 1,
            start   : None,
        }
    }

    pub fn start(&mut self) {
        self.status = true;
    }

    pub fn stop(&mut self) {
        self.status = false;
    }

    pub fn recv_range(&self) -> f64 {
        self.range
    }

    pub fn recv_status(&self) -> bool {
        self.status
    }
}