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
//! Debug rendering for collision boxes, etc.

use crate::prelude::*;
use rapier2d::prelude as rapier;

/// Install this module.
pub fn plugin(session: &mut Session) {
    session
        .stages
        .add_system_to_stage(CoreStage::Last, debug_render_colliders)
        .add_system_to_stage(CoreStage::Last, debug_render_damage_regions)
        .add_system_to_stage(CoreStage::Last, debug_render_emote_regions);
}

/// Resource configuring various debugging settings.
#[derive(Copy, Clone, HasSchema, Default)]
pub struct DebugSettings {
    /// Whether or not to render kinematic collider shapes.
    pub show_kinematic_colliders: bool,
    /// Whether or not to render damage region collider shapes.
    pub show_damage_regions: bool,
    /// Whether or not to show the pathfinding lines.
    pub show_pathfinding_lines: bool,
}

/// Resource containing the physics debug line entity.
#[derive(HasSchema)]
#[schema(no_default)]
pub struct RapierDebugContext {
    path_entity: Entity,
    debug_pipeline: rapier::DebugRenderPipeline,
}

/// An implementation of the rapier `DebugRenderingBackend` that we use to create bones `Path2d`
/// entities with.
struct RapierDebugBackend<'a> {
    points: &'a mut Vec<Vec2>,
    line_breaks: &'a mut Vec<usize>,
}

impl<'a> rapier::DebugRenderBackend for RapierDebugBackend<'a> {
    fn draw_line(
        &mut self,
        object: rapier::DebugRenderObject,
        a: rapier::Point<rapier::Real>,
        b: rapier::Point<rapier::Real>,
        // TODO: implement multi-colored rendering
        _color: [f32; 4],
    ) {
        let render = match object {
            rapier::DebugRenderObject::RigidBody(_, body) => body.is_enabled(),
            rapier::DebugRenderObject::Collider(_, collider) => collider.is_enabled(),
            rapier::DebugRenderObject::ImpulseJoint(_, _) => true,
            rapier::DebugRenderObject::MultibodyJoint(_, _, _) => true,
            rapier::DebugRenderObject::ColliderAabb(_, _, _) => true,
            rapier::DebugRenderObject::ContactPair(_, _, _) => true,
        };
        if render {
            self.points.push(vec2(a.x, a.y));
            self.points.push(vec2(b.x, b.y));
            self.line_breaks.push(self.points.len());
        }
    }
}

impl Clone for RapierDebugContext {
    fn clone(&self) -> Self {
        Self {
            path_entity: self.path_entity,
            debug_pipeline: default(),
        }
    }
}

impl FromWorld for RapierDebugContext {
    fn from_world(world: &World) -> Self {
        let path_entity = world.resource_mut::<Entities>().create();

        let transforms = world.components.get::<Transform>();
        let mut transforms = transforms.borrow_mut();
        transforms.insert(
            path_entity,
            Transform::from_translation(vec3(0.0, 0.0, -1.0)),
        );

        Self {
            path_entity,
            debug_pipeline: default(),
        }
    }
}

/// Renders debug lines for rapier colliders.
fn debug_render_colliders(
    settings: ResInit<DebugSettings>,
    mut collision_world: CollisionWorld,
    transforms: Comp<Transform>,
    mut dynamic_bodies: CompMut<DynamicBody>,
    mut paths: CompMut<Path2d>,
    mut debug_context: ResMutInit<RapierDebugContext>,
) {
    if settings.show_kinematic_colliders {
        // TODO: It's unfortunate that we are doing an extra sync here, just for debug rendering. We
        // should try find a way to avoid this. Without this, the collider body positions will be
        // out of sync when they are rendered.
        collision_world.sync_bodies(&transforms, &mut dynamic_bodies);

        let mut points = Vec::new();
        let mut line_breaks = Vec::new();

        debug_context.debug_pipeline.render_colliders(
            &mut RapierDebugBackend {
                points: &mut points,
                line_breaks: &mut line_breaks,
            },
            &collision_world.ctx.rigid_body_set,
            &collision_world.ctx.collider_set,
        );

        // TODO: Provide a way to change the collider colors
        paths.insert(
            debug_context.path_entity,
            Path2d {
                // An orange-y color
                color: Color::from([205.0 / 255.0, 94.0 / 255.0, 15.0 / 255.0, 1.0]),
                points,
                line_breaks,
                ..default()
            },
        );
    } else {
        paths.remove(debug_context.path_entity);
    }
}

/// Renders debug lines for damage regions.
fn debug_render_damage_regions(
    settings: ResInit<DebugSettings>,
    entities: Res<Entities>,
    regions: Comp<DamageRegion>,
    transforms: Comp<Transform>,
    mut paths: CompMut<Path2d>,
) {
    let path_for_region = |rotation: f32, region: &DamageRegion| {
        let rect = Rect::new(0.0, 0.0, region.size.x, region.size.y);

        // The collision boxes don't rotate, so apply the opposite rotation of the object to the
        // debug lines to keep it upright.
        let angle = Vec2::from_angle(-rotation);

        Path2d {
            color: Color::RED,
            points: vec![
                angle.rotate(rect.top_left()),
                angle.rotate(rect.top_right()),
                angle.rotate(rect.bottom_right()),
                angle.rotate(rect.bottom_left()),
                angle.rotate(rect.top_left()),
            ],
            thickness: 1.0,
            ..default()
        }
    };

    if settings.show_damage_regions {
        for (ent, (region, transform)) in entities.iter_with((&regions, &transforms)) {
            paths.insert(
                ent,
                path_for_region(transform.rotation.to_euler(EulerRot::XYZ).2, region),
            );
        }
    } else {
        for ent in entities.iter_with_bitset(regions.bitset()) {
            paths.remove(ent);
        }
    }
}

/// Renders debug lines for emote regions.
fn debug_render_emote_regions(
    settings: ResInit<DebugSettings>,
    entities: Res<Entities>,
    regions: Comp<EmoteRegion>,
    transforms: Comp<Transform>,
    mut paths: CompMut<Path2d>,
) {
    let path_for_region = |rotation: f32, region: &EmoteRegion| {
        let rect = Rect::new(0.0, 0.0, region.size.x, region.size.y);

        // The collision boxes don't rotate, so apply the opposite rotation of the object to the
        // debug lines to keep it upright.
        let angle = Vec2::from_angle(-rotation);

        Path2d {
            // Green color
            color: Color::from([39.0 / 255.0, 191.0 / 255.0, 68.0 / 255.0, 1.0]),
            points: vec![
                angle.rotate(rect.top_left()),
                angle.rotate(rect.top_right()),
                angle.rotate(rect.bottom_right()),
                angle.rotate(rect.bottom_left()),
                angle.rotate(rect.top_left()),
            ],
            thickness: 1.0,
            ..default()
        }
    };

    if settings.show_damage_regions {
        for (ent, (region, transform)) in entities.iter_with((&regions, &transforms)) {
            paths.insert(
                ent,
                path_for_region(transform.rotation.to_euler(EulerRot::XYZ).2, region),
            );
        }
    } else {
        for ent in entities.iter_with_bitset(regions.bitset()) {
            paths.remove(ent);
        }
    }
}