summaryrefslogtreecommitdiff
path: root/src/mass.rs
blob: f09685fc5e9ff93a8265953d224a7de29e2158ea (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
extern crate rand;

use self::rand::distributions::Range;
use self::rand::distributions::Sample;

use item::Item;
use storage::Storage;
use modules::mining::Mining;
use modules::engines::Engines;
use modules::types::ModuleType;
use modules::refinery::Refinery;
use modules::dashboard::Dashboard;
use modules::navigation::Navigation;
use modules::construction::Construction;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Mass {
    pub mass_type   : MassType,
    pub position    : (f64, f64, f64),
    pub velocity    : (f64, f64, f64),
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum MassType {
    Ship {
        storage     : Storage,
        mining      : Option<Mining>,
        engines     : Option<Engines>,
        refinery    : Option<Refinery>,
        dashboard   : Option<Dashboard>,
        navigation  : Option<Navigation>,
        construction: Option<Construction>,
    },
    Astroid {
        resources   : Storage,
    },
    Item {
        item : Item,
    },
    Station {
        module_type : ModuleType,
    },
}

impl Mass {
    pub fn new_astroid() -> Mass {
        let mut rng = rand::thread_rng();

        let mut pr = Range::new(-50.0, 50.0);
        let position = (pr.sample(&mut rng), pr.sample(&mut rng), pr.sample(&mut rng));

        let mut vr = Range::new(-0.5, 0.5);
        let velocity = (vr.sample(&mut rng), vr.sample(&mut rng), vr.sample(&mut rng));

        let mut rr = Range::new(0, 20);
        let mut resources = Vec::new();
        for _ in 0..rr.sample(&mut rng) {
            resources.push(Item::new("Mineral", 1));
        }

        let astroid = MassType::Astroid {
            resources  : Storage::new(resources),
        };

        Mass {
            mass_type   : astroid,
            position    : position,
            velocity    : velocity,
        }
    }

    pub fn new_ship() -> Mass {
        let ship = MassType::Ship {
            mining      : Some(Mining::new()),
            engines     : Some(Engines::new()),
            refinery    : Some(Refinery::new()),
            dashboard   : Some(Dashboard::new()),
            navigation  : Some(Navigation::new()),
            construction: Some(Construction::new()),
            storage     : Storage::new(Vec::new()),
        };

        Mass {
            mass_type   : ship,
            position    : (0.0, 0.0, 0.0),
            velocity    : (0.0, 0.0, 0.0),
        }
    }

    pub fn new_item(item : Item, position : (f64, f64, f64), velocity : (f64, f64, f64)) -> Mass {
        Mass {
            mass_type   : MassType::Item{item : item},
            position    : position,
            velocity    : velocity,
        }
    }

    pub fn new_station(module_type : ModuleType, position : (f64, f64, f64), velocity : (f64, f64, f64)) -> Mass {
        let mass_type = MassType::Station {
            module_type : module_type
        };

        Mass {
            mass_type   : mass_type,
            position    : position,
            velocity    : velocity,
        }
    }

    pub fn get_modules(&self) -> Vec<ModuleType> {
        let mut modules = Vec::new();
        modules.push(ModuleType::Mining);
        modules.push(ModuleType::Engines);
        modules.push(ModuleType::Refinery);
        modules.push(ModuleType::Dashboard);
        modules.push(ModuleType::Navigation);
        modules.push(ModuleType::Construction);
        modules
    }

    pub fn process(&mut self) {
        let mut acceleration = (0.0, 0.0, 0.0);
        match self.mass_type {
            MassType::Ship{ref mut navigation, ref mut engines, ref mut mining, ref mut refinery, ref mut construction, ..} => {
                mining.as_mut().unwrap().process();
                refinery.as_mut().unwrap().process();
                navigation.as_mut().unwrap().process();
                construction.as_mut().unwrap().process();
                acceleration = engines.as_mut().unwrap().recv_acceleration();
            },
            _ => (),
        }
        self.accelerate(acceleration);
        self.position.0 += self.velocity.0;
        self.position.1 += self.velocity.1;
        self.position.2 += self.velocity.2;
    }

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

    pub fn has_minerals(&self) -> bool {
        match self.mass_type {
            MassType::Ship{ref storage, ..} => storage.has_minerals(),
            MassType::Astroid{ref resources, ..} => resources.has_minerals(),
            _ => false,
        }
    }

    pub fn refined_count(&self) -> usize {
        match self.mass_type {
            MassType::Ship{ref storage, ..} => storage.refined_count(),
            _ => 0,
        }
    }

    pub fn take(&mut self, name : &str) -> Option<Item> {
        match self.mass_type {
            MassType::Ship{ref mut storage, ..} => storage.take(name),
            MassType::Astroid{ref mut resources, ..} => resources.take(name),
            _ => None,
        }
    }

    pub fn give(&mut self, item : Item) -> bool {
        match self.mass_type {
            MassType::Ship{ref mut storage, ..} => storage.give(item),
            MassType::Astroid{ref mut resources, ..} => resources.give(item),
            _ => false,
        }
    }
}