summaryrefslogtreecommitdiffstats
path: root/src/ui/contact.rs
blob: e525541da2e7533fb8ac0951716167055860b356 (plain) (blame)
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
use crate::body::BodyId;
use crate::fleet::{Fleet, FleetId, FleetsManager};
use crate::solar_system::{Kilometers, SolarSystem};
use crate::solar_system::body::OrbitalBody;
use crate::timeman::Second;

#[derive(Copy, Clone, Eq, PartialEq)]
pub enum ContactType
{
    Body,
    Fleet
}

#[derive(Copy, Clone, Eq, PartialEq)]
pub struct MapContact
{
    obj_type: ContactType,
    id: usize
}

impl MapContact
{
    pub fn from_body(
        id: BodyId)
    -> Self {
        Self {
            obj_type: ContactType::Body,
            id: id
        }
    }

    pub fn from_fleet(
        id: FleetId)
    -> Self {
        Self {
            obj_type: ContactType::Fleet,
            id: id
        }
    }

    pub fn body(&self)
    -> Option<BodyId>
    {
        match self.obj_type {
            ContactType::Body => Some(self.id),
            _ => None
        }
    }

    pub fn fleet(&self)
    -> Option<FleetId>
    {
        match self.obj_type {
            ContactType::Fleet => Some(self.id),
            _ => None
        }
    }

    pub fn name<'ss>(
        &self,
        star_system: &'ss SolarSystem,
        fleets_man: &'ss FleetsManager)
    -> &'ss String
    {
        match self.obj_type {
            ContactType::Body => star_system.body(self.id).name(),
            ContactType::Fleet => fleets_man.fleet(self.id).unwrap().name()
        }
    }

    pub fn origin_position<'ss>(
        &self,
        star_system: &'ss SolarSystem,
        fleets_man: &'ss FleetsManager,
        time: Second)
    -> cgmath::Vector3<Kilometers>
    {
        match self.obj_type {
            ContactType::Body => 
                star_system.body(self.id).origin_position(star_system, time),
            ContactType::Fleet =>
                *fleets_man.fleet(self.id).unwrap().origin_position()
        }
    }

    pub fn offset_position<'ss>(
        &self,
        star_system: &'ss SolarSystem,
        fleets_man: &'ss FleetsManager,
        time: Second)
    -> cgmath::Vector3<Kilometers>
    {
        match self.obj_type {
            ContactType::Body => 
                star_system.body(self.id).offset_position(time),
            ContactType::Fleet =>
                *fleets_man.fleet(self.id).unwrap().offset_position()
        }
    }
}