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
224use bevy::{math::Vec3A, prelude::*, render::primitives::Aabb, utils::HashSet};
use bevy_ecs_ldtk::{assets::LdtkProject, GridCoords, Respawn};
use bevy_mod_picking::{
events::{Click, Out, Over, Pointer},
prelude::On,
PickableBundle,
};
use bevy_rapier2d::plugin::RapierConfiguration;
use crate::{
components::Wall, FontHandle, GameMode, HOVERED_BUTTON, NORMAL_BUTTON, PRESSED_BUTTON,
TEXT_COLOR,
};
pub struct EditPlugin;
impl Plugin for EditPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
Update,
(
spawn_wall_aabb,
set_color_based_on_enabled,
update_collider_count,
button_system,
crate::play::camera_fit_inside_current_level,
)
.run_if(in_state(GameMode::Edit)),
)
.add_systems(OnEnter(GameMode::Edit), setup_edit_mode)
.add_systems(OnExit(GameMode::Edit), exit_mode);
}
}
fn exit_mode(mut commands: Commands, query: Query<Entity, With<OnEditMode>>) {
for entity in &mut query.iter() {
commands.entity(entity).despawn_recursive();
}
}
#[derive(Component)]
struct OnEditMode;
fn setup_edit_mode(
mut commands: Commands,
world_query: Query<Entity, With<Handle<LdtkProject>>>,
mut rapier_config: ResMut<RapierConfiguration>,
colliders: Res<EnabledColliders>,
font: Res<FontHandle>,
) {
let button_style = Style {
width: Val::Px(150.0),
height: Val::Px(50.0),
margin: UiRect::bottom(Val::Px(10.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
border: UiRect::all(Val::Px(5.0)),
..default()
};
let button_text_style = TextStyle {
font_size: 20.0,
color: TEXT_COLOR,
font: font.0.clone(),
};
for world_entity in &world_query {
commands.entity(world_entity).insert(Respawn);
}
rapier_config.gravity = Vec2::new(0.0, 0.0);
commands
.spawn(NodeBundle {
style: Style {
position_type: PositionType::Absolute,
top: Val::Px(12.0),
right: Val::Px(12.0),
flex_direction: FlexDirection::Column,
align_items: AlignItems::FlexEnd,
..default()
},
..default()
})
.insert(OnEditMode)
.with_children(|parent| {
parent
.spawn((
ButtonBundle {
style: button_style.clone(),
background_color: NORMAL_BUTTON.into(),
border_color: BorderColor(HOVERED_BUTTON),
..default()
},
ButtonAction::Play,
))
.with_children(|parent| {
parent.spawn(TextBundle::from_section("Play", button_text_style.clone()));
});
parent.spawn(TextBundle::from_sections([
TextSection {
value: colliders.coords.len().to_string(),
style: TextStyle {
font_size: 20.,
color: TEXT_COLOR,
font: font.0.clone(),
},
},
TextSection {
value: " colliders".to_string(),
style: TextStyle {
font_size: 20.,
color: TEXT_COLOR,
font: font.0.clone(),
},
},
]));
});
}
#[derive(Component)]
enum ButtonAction {
Play,
}
#[derive(Component)]
struct ColliderStatus {
enabled: bool,
}
#[derive(Resource, Default)]
pub struct EnabledColliders {
pub coords: HashSet<GridCoords>,
}
fn spawn_wall_aabb(
mut commands: Commands,
wall_query: Query<(Entity, &GridCoords), Added<Wall>>,
enabled: Res<EnabledColliders>,
) {
wall_query.for_each(|(entity, gridcoords)| {
commands.entity(entity).insert((
Aabb {
center: Vec3A::ZERO,
half_extents: Vec3A::new(8., 8., 0.) * 0.95,
},
AabbGizmo {
color: Some(if enabled.coords.contains(gridcoords) {
Color::GREEN
} else {
Color::GRAY
}),
},
PickableBundle::default(),
ColliderStatus {
enabled: enabled.coords.contains(gridcoords),
},
On::<Pointer<Out>>::target_component_mut::<AabbGizmo>(|_, gizmo| {
let color = gizmo.color.unwrap();
let mut color = color.as_hsla();
color.set_l(0.5);
gizmo.color = Some(color.as_rgba());
}),
On::<Pointer<Over>>::target_component_mut::<AabbGizmo>(|_, gizmo| {
let color = gizmo.color.unwrap();
let mut color = color.as_hsla();
color.set_l(0.9);
gizmo.color = Some(color.as_rgba());
}),
On::<Pointer<Click>>::target_component_mut::<ColliderStatus>(|_, collider| {
collider.enabled = !collider.enabled;
}),
));
});
}
fn set_color_based_on_enabled(
mut query: Query<(Ref<ColliderStatus>, &mut AabbGizmo, &GridCoords)>,
mut enabled: ResMut<EnabledColliders>,
) {
for (collider_status, mut gizmo, gridcoords) in &mut query {
if collider_status.is_changed() && !collider_status.is_added() {
if collider_status.enabled {
gizmo.color = Some(Color::GREEN);
enabled.coords.insert(*gridcoords);
} else {
gizmo.color = Some(Color::GRAY);
enabled.coords.remove(gridcoords);
}
}
}
}
fn update_collider_count(colliders: Res<EnabledColliders>, mut text: Query<&mut Text>) {
if colliders.is_changed() {
for mut text in &mut text {
if text.sections.len() == 2 {
text.sections[0].value = colliders.coords.len().to_string();
}
}
}
}
#[allow(clippy::type_complexity)]
fn button_system(
mut interaction_query: Query<
(&Interaction, &mut BackgroundColor, &ButtonAction),
(Changed<Interaction>, With<Button>),
>,
mut next_state: ResMut<NextState<GameMode>>,
) {
for (interaction, mut color, button) in &mut interaction_query {
*color = match *interaction {
Interaction::Pressed => {
match button {
ButtonAction::Play => next_state.set(GameMode::Play),
}
PRESSED_BUTTON.into()
}
Interaction::Hovered => HOVERED_BUTTON.into(),
Interaction::None => NORMAL_BUTTON.into(),
}
}
}