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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use ahash::AHashMap;
use bevy::prelude::*;
use bevy::reflect::TypePath;
use bevy::render::render_resource::{AsBindGroup, ShaderRef};
use de_core::cleanup::DespawnOnGameExit;
use de_core::objects::Active;
use de_core::state::AppState;

/// Width of the line that goes to the pole.
const LINE_WIDTH: f32 = 1.;
/// Offset above mean sea level of the line, stopping z-fighting with the floor.
const LINE_OFFSET: Vec3 = Vec3::new(0., 1e-3, 0.);

pub(crate) struct LinePlugin;

impl Plugin for LinePlugin {
    fn build(&self, app: &mut App) {
        app.add_plugins(MaterialPlugin::<LineMaterial>::default())
            .add_event::<UpdateLineLocationEvent>()
            .add_event::<UpdateLineEndEvent>()
            .add_event::<UpdateLineVisibilityEvent>()
            .add_systems(OnEnter(AppState::InGame), setup)
            .add_systems(OnExit(AppState::InGame), cleanup)
            .add_systems(
                PostUpdate,
                (
                    update_line_end
                        .run_if(on_event::<UpdateLineEndEvent>())
                        .in_set(LinesSet::LineEnd),
                    update_line_location
                        .run_if(on_event::<UpdateLineLocationEvent>())
                        .in_set(LinesSet::LocationEvents)
                        .after(LinesSet::LineEnd),
                    update_line_visibility
                        .run_if(on_event::<UpdateLineVisibilityEvent>())
                        .in_set(LinesSet::VisibilityEvents)
                        .after(LinesSet::LocationEvents),
                    owner_despawn
                        .in_set(LinesSet::Despawn)
                        .after(LinesSet::VisibilityEvents),
                )
                    .run_if(in_state(AppState::InGame)),
            );
    }
}

#[derive(Copy, Clone, Hash, Debug, PartialEq, Eq, SystemSet)]
enum LinesSet {
    LineEnd,
    LocationEvents,
    VisibilityEvents,
    Despawn,
}

// Passed to the `rally_point.wgsl` shader
#[derive(Asset, AsBindGroup, TypePath, Debug, Clone)]
pub struct LineMaterial {}

impl Material for LineMaterial {
    fn fragment_shader() -> ShaderRef {
        "shaders/rally_point.wgsl".into()
    }

    fn alpha_mode(&self) -> AlphaMode {
        AlphaMode::Blend
    }
}

#[derive(Clone, Copy)]
pub struct LineLocation {
    start: Vec3,
    end: Vec3,
}

impl LineLocation {
    pub fn new(start: Vec3, end: Vec3) -> Self {
        Self { start, end }
    }

    /// A transform matrix from a plane with points at `(-1, 0, -1), (1, 0,
    /// -1), (1, 0, 1), (1, 0, -1)` to the line start and end with the
    /// `LINE_WIDTH`.
    fn transform(&self) -> Transform {
        let half_dir = 0.5 * (self.end - self.start);
        let norm_perp_dir = Vec3::new(-half_dir.z, half_dir.y, half_dir.x).normalize();
        let half_perp_dir = 0.5 * LINE_WIDTH * norm_perp_dir;

        let x_axis = half_dir.extend(0.);
        let y_axis = Vec4::Y;
        let z_axis = half_perp_dir.extend(0.);
        let w_axis = (self.start + half_dir + LINE_OFFSET).extend(1.);

        Transform::from_matrix(Mat4::from_cols(x_axis, y_axis, z_axis, w_axis))
    }
}

#[derive(Event)]
pub struct UpdateLineVisibilityEvent {
    owner: Entity,
    visible: bool,
}

impl UpdateLineVisibilityEvent {
    pub fn new(owner: Entity, visible: bool) -> Self {
        Self { owner, visible }
    }
}

#[derive(Event)]
pub struct UpdateLineLocationEvent {
    owner: Entity,
    location: LineLocation,
}

impl UpdateLineLocationEvent {
    pub fn new(owner: Entity, location: LineLocation) -> Self {
        Self { owner, location }
    }
}

#[derive(Event)]
pub struct UpdateLineEndEvent {
    owner: Entity,
    end: Vec3,
}

impl UpdateLineEndEvent {
    pub fn new(owner: Entity, end: Vec3) -> Self {
        Self { owner, end }
    }
}

#[derive(Resource)]
struct LineMesh(Handle<Mesh>, Handle<LineMaterial>);

#[derive(Resource, Default)]
struct LineEntities(AHashMap<Entity, Entity>);

#[derive(Resource, Default)]
struct LineLocations(AHashMap<Entity, LineLocation>);

fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<LineMaterial>>,
) {
    commands.init_resource::<LineEntities>();
    commands.init_resource::<LineLocations>();
    let line_mesh = meshes.add(Plane3d::default());
    let line_material = materials.add(LineMaterial {});
    commands.insert_resource(LineMesh(line_mesh, line_material));
}

fn cleanup(mut commands: Commands) {
    commands.remove_resource::<LineEntities>();
    commands.remove_resource::<LineLocations>();
    commands.remove_resource::<LineMesh>();
}

fn update_line_end(
    mut events: EventReader<UpdateLineEndEvent>,
    lines: Res<LineLocations>,
    mut line_location: EventWriter<UpdateLineLocationEvent>,
) {
    for event in events.read() {
        if let Some(old_location) = lines.0.get(&event.owner) {
            let location = LineLocation::new(old_location.start, event.end);
            line_location.send(UpdateLineLocationEvent::new(event.owner, location));
        }
    }
}

fn update_line_location(
    lines: Res<LineEntities>,
    mut events: EventReader<UpdateLineLocationEvent>,
    mut transforms: Query<&mut Transform>,
    mut line_locations: ResMut<LineLocations>,
) {
    for event in events.read() {
        line_locations.0.insert(event.owner, event.location);
        if let Some(line_entity) = lines.0.get(&event.owner) {
            let mut current_transform = transforms.get_mut(*line_entity).unwrap();
            *current_transform = event.location.transform()
        }
    }
}

fn update_line_visibility(
    mut events: EventReader<UpdateLineVisibilityEvent>,
    mut lines: ResMut<LineEntities>,
    line_locations: Res<LineLocations>,
    mut commands: Commands,
    line_mesh: Res<LineMesh>,
) {
    for event in events.read() {
        if event.visible && !lines.0.contains_key(&event.owner) {
            let transform = line_locations
                .0
                .get(&event.owner)
                .map(|location| location.transform());
            let line_id = commands
                .spawn((
                    MaterialMeshBundle {
                        mesh: line_mesh.0.clone(),
                        material: line_mesh.1.clone(),
                        transform: transform.unwrap_or_default(),
                        ..default()
                    },
                    DespawnOnGameExit,
                ))
                .id();
            lines.0.insert(event.owner, line_id);
        } else if !event.visible {
            if let Some(line_entity) = lines.0.remove(&event.owner) {
                commands.entity(line_entity).despawn_recursive();
            }
        }
    }
}

fn owner_despawn(
    mut commands: Commands,
    mut lines: ResMut<LineEntities>,
    mut removed: RemovedComponents<Active>,
) {
    for owner in removed.read() {
        if let Some(line) = lines.0.remove(&owner) {
            commands.entity(line).despawn_recursive();
        }
    }
}