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
use crate::{
    comp,
    link::{Is, Link, LinkHandle, Role},
    mounting::{Rider, VolumeRider},
    uid::{IdMaps, Uid},
};
use serde::{Deserialize, Serialize};
use specs::{Entities, Read, ReadStorage, WriteStorage};
use vek::*;

#[derive(Serialize, Deserialize, Debug)]
pub struct Leader;

impl Role for Leader {
    type Link = Tethered;
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Follower;

impl Role for Follower {
    type Link = Tethered;
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Tethered {
    pub leader: Uid,
    pub follower: Uid,
    pub tether_length: f32,
}

#[derive(Debug)]
pub enum TetherError {
    NoSuchEntity,
    NotTetherable,
}

impl Link for Tethered {
    type CreateData<'a> = (
        Read<'a, IdMaps>,
        WriteStorage<'a, Is<Leader>>,
        WriteStorage<'a, Is<Follower>>,
        ReadStorage<'a, Is<Rider>>,
        ReadStorage<'a, Is<VolumeRider>>,
    );
    type DeleteData<'a> = (
        Read<'a, IdMaps>,
        WriteStorage<'a, Is<Leader>>,
        WriteStorage<'a, Is<Follower>>,
    );
    type Error = TetherError;
    type PersistData<'a> = (
        Read<'a, IdMaps>,
        Entities<'a>,
        ReadStorage<'a, comp::Health>,
        ReadStorage<'a, Is<Leader>>,
        ReadStorage<'a, Is<Follower>>,
    );

    fn create(
        this: &LinkHandle<Self>,
        (id_maps, is_leaders, is_followers, is_riders, is_volume_rider): &mut Self::CreateData<'_>,
    ) -> Result<(), Self::Error> {
        let entity = |uid: Uid| id_maps.uid_entity(uid);

        if this.leader == this.follower {
            // Forbid self-tethering
            Err(TetherError::NotTetherable)
        } else if let Some((leader, follower)) = entity(this.leader).zip(entity(this.follower)) {
            // Ensure that neither leader or follower are already part of a conflicting
            // relationship
            if !is_riders.contains(follower)
                && !is_volume_rider.contains(follower)
                && !is_followers.contains(follower)
                // TODO: Does this definitely prevent tether cycles?
                && (!is_leaders.contains(follower) || !is_followers.contains(leader))
            {
                let _ = is_leaders.insert(leader, this.make_role());
                let _ = is_followers.insert(follower, this.make_role());
                Ok(())
            } else {
                Err(TetherError::NotTetherable)
            }
        } else {
            Err(TetherError::NoSuchEntity)
        }
    }

    fn persist(
        this: &LinkHandle<Self>,
        (id_maps, entities, healths, is_leaders, is_followers): &mut Self::PersistData<'_>,
    ) -> bool {
        let entity = |uid: Uid| id_maps.uid_entity(uid);

        if let Some((leader, follower)) = entity(this.leader).zip(entity(this.follower)) {
            let is_alive = |entity| {
                entities.is_alive(entity) && healths.get(entity).map_or(true, |h| !h.is_dead)
            };

            // Ensure that both entities are alive and that they continue to be linked
            is_alive(leader)
                && is_alive(follower)
                && is_leaders.get(leader).is_some()
                && is_followers.get(follower).is_some()
        } else {
            false
        }
    }

    fn delete(
        this: &LinkHandle<Self>,
        (id_maps, is_leaders, is_followers): &mut Self::DeleteData<'_>,
    ) {
        let entity = |uid: Uid| id_maps.uid_entity(uid);

        let leader = entity(this.leader);
        let follower = entity(this.follower);

        // Delete link components
        leader.map(|leader| is_leaders.remove(leader));
        follower.map(|follower| is_followers.remove(follower));
    }
}