forked from Trouv/bevy_ecs_ldtk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
components.rs
256 lines (224 loc) · 7.56 KB
/
components.rs
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
use bevy::prelude::*;
use bevy_ecs_ldtk::{prelude::*, utils::ldtk_pixel_coords_to_translation_pivoted};
use std::collections::HashSet;
use bevy_rapier2d::prelude::*;
#[derive(Clone, Debug, Default, Bundle, LdtkIntCell)]
pub struct ColliderBundle {
pub collider: Collider,
pub rigid_body: RigidBody,
pub velocity: Velocity,
pub rotation_constraints: LockedAxes,
pub gravity_scale: GravityScale,
pub friction: Friction,
pub density: ColliderMassProperties,
}
#[derive(Clone, Debug, Default, Bundle, LdtkIntCell)]
pub struct SensorBundle {
pub collider: Collider,
pub sensor: Sensor,
pub active_events: ActiveEvents,
pub rotation_constraints: LockedAxes,
}
impl From<&EntityInstance> for ColliderBundle {
fn from(entity_instance: &EntityInstance) -> ColliderBundle {
let rotation_constraints = LockedAxes::ROTATION_LOCKED;
match entity_instance.identifier.as_ref() {
"Player" => ColliderBundle {
collider: Collider::cuboid(6., 14.),
rigid_body: RigidBody::Dynamic,
friction: Friction {
coefficient: 0.0,
combine_rule: CoefficientCombineRule::Min,
},
rotation_constraints,
..Default::default()
},
"Mob" => ColliderBundle {
collider: Collider::cuboid(5., 5.),
rigid_body: RigidBody::KinematicVelocityBased,
rotation_constraints,
..Default::default()
},
"Chest" => ColliderBundle {
collider: Collider::cuboid(8., 8.),
rigid_body: RigidBody::Dynamic,
rotation_constraints,
gravity_scale: GravityScale(1.0),
friction: Friction::new(0.5),
density: ColliderMassProperties::Density(15.0),
..Default::default()
},
_ => ColliderBundle::default(),
}
}
}
impl From<IntGridCell> for SensorBundle {
fn from(int_grid_cell: IntGridCell) -> SensorBundle {
let rotation_constraints = LockedAxes::ROTATION_LOCKED;
// ladder
if int_grid_cell.value == 2 {
SensorBundle {
collider: Collider::cuboid(8., 8.),
sensor: Sensor,
rotation_constraints,
active_events: ActiveEvents::COLLISION_EVENTS,
}
} else {
SensorBundle::default()
}
}
}
#[derive(Clone, Component, Debug, Eq, Default, PartialEq)]
pub struct Items(Vec<String>);
impl From<&EntityInstance> for Items {
fn from(entity_instance: &EntityInstance) -> Self {
let mut items: Vec<String> = vec![];
if let Some(field_instance) = entity_instance
.field_instances
.iter()
.find(|f| f.identifier == *"items")
{
// convert &String to String which returns vec![String::from("Knife"), String::from("Boot")]
items = match &field_instance.value {
FieldValue::Enums(v) => v
.iter()
.flatten()
.map(|s| s.into())
.collect::<Vec<String>>(),
_ => vec![],
};
}
Self(items)
}
}
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default, Component)]
pub struct Player;
#[derive(Clone, Eq, PartialEq, Debug, Default, Component)]
pub struct Climber {
pub climbing: bool,
pub intersecting_climbables: HashSet<Entity>,
}
#[derive(Clone, Default, Bundle, LdtkEntity)]
pub struct PlayerBundle {
#[sprite_bundle("player.png")]
#[bundle]
pub sprite_bundle: SpriteBundle,
#[from_entity_instance]
#[bundle]
pub collider_bundle: ColliderBundle,
pub player: Player,
#[worldly]
pub worldly: Worldly,
pub climber: Climber,
pub ground_detection: GroundDetection,
// Build Items Component manually by using `impl From<&EntityInstance>`
#[from_entity_instance]
items: Items,
// The whole EntityInstance can be stored directly as an EntityInstance component
#[from_entity_instance]
entity_instance: EntityInstance,
}
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default, Component)]
pub struct Wall;
#[derive(Clone, Debug, Default, Bundle, LdtkIntCell)]
pub struct WallBundle {
wall: Wall,
}
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default, Component)]
pub struct Climbable;
#[derive(Clone, Debug, Default, Bundle, LdtkIntCell)]
pub struct LadderBundle {
#[from_int_grid_cell]
#[bundle]
pub sensor_bundle: SensorBundle,
pub climbable: Climbable,
}
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default, Component)]
pub struct Enemy;
#[derive(Clone, PartialEq, Debug, Default, Component)]
pub struct Patrol {
pub points: Vec<Vec2>,
pub index: usize,
pub forward: bool,
}
impl LdtkEntity for Patrol {
fn bundle_entity(
entity_instance: &EntityInstance,
layer_instance: &LayerInstance,
_: Option<&Handle<Image>>,
_: Option<&TilesetDefinition>,
_: &AssetServer,
_: &mut Assets<TextureAtlas>,
) -> Patrol {
let mut points = Vec::new();
points.push(ldtk_pixel_coords_to_translation_pivoted(
entity_instance.px,
layer_instance.c_hei * layer_instance.grid_size,
IVec2::new(entity_instance.width, entity_instance.height),
entity_instance.pivot,
));
let ldtk_patrol = entity_instance
.field_instances
.iter()
.find(|f| f.identifier == *"patrol")
.unwrap();
if let FieldValue::Points(ldtk_points) = &ldtk_patrol.value {
for ldtk_point in ldtk_points.iter().flatten() {
// The +1 is necessary here due to the pivot of the entities in the sample
// file.
// The patrols set up in the file look flat and grounded,
// but technically they're not if you consider the pivot,
// which is at the bottom-center for the skulls.
let pixel_coords = (ldtk_point.as_vec2() + Vec2::new(0.5, 1.))
* Vec2::splat(layer_instance.grid_size as f32);
points.push(ldtk_pixel_coords_to_translation_pivoted(
pixel_coords.as_ivec2(),
layer_instance.c_hei * layer_instance.grid_size,
IVec2::new(entity_instance.width, entity_instance.height),
entity_instance.pivot,
));
}
}
Patrol {
points,
index: 1,
forward: true,
}
}
}
#[derive(Clone, Default, Bundle, LdtkEntity)]
pub struct MobBundle {
#[sprite_sheet_bundle]
#[bundle]
pub sprite_sheet_bundle: SpriteSheetBundle,
#[from_entity_instance]
#[bundle]
pub collider_bundle: ColliderBundle,
pub enemy: Enemy,
#[ldtk_entity]
pub patrol: Patrol,
}
#[derive(Clone, Default, Bundle, LdtkEntity)]
pub struct ChestBundle {
#[sprite_sheet_bundle]
#[bundle]
pub sprite_sheet_bundle: SpriteSheetBundle,
#[from_entity_instance]
#[bundle]
pub collider_bundle: ColliderBundle,
}
#[derive(Clone, Default, Bundle, LdtkEntity)]
pub struct PumpkinsBundle {
#[sprite_sheet_bundle(no_grid)]
#[bundle]
pub sprite_sheet_bundle: SpriteSheetBundle,
}
#[derive(Clone, Default, Component)]
pub struct GroundDetection {
pub on_ground: bool,
}
#[derive(Component)]
pub struct GroundSensor {
pub ground_detection_entity: Entity,
pub intersecting_ground_entities: HashSet<Entity>,
}