pub enum CollisionShape {
Circle {
center: (i32, i32),
radius: u32,
},
Rect {
center: (i32, i32),
size: (u32, u32),
},
Polygon {
center: (i32, i32),
points: Vec<(i32, i32)>,
},
}Expand description
Colliders that attach to GameObjects. Support Circle and Rect colliders
Variants§
Implementations§
Source§impl CollisionShape
impl CollisionShape
Sourcepub fn collides_with(&self, other: &CollisionShape) -> bool
pub fn collides_with(&self, other: &CollisionShape) -> bool
Examples found in repository?
examples/platformer/game.rs (line 299)
228 fn update(
229 &mut self, delta: f64,
230 _ctl_objs: &Vec<Box<dyn ControlObjectBehavior<Img, Snd, Fnt, Spr, Rm, Data>>>,
231 others: &Vec<Box<dyn GameObjectBehavior<Img, Snd, Fnt, Spr, Rm, Data>>>) -> (
232 Option<Rm>, Vec<Box<dyn GameObjectBehavior<Img, Snd, Fnt, Spr, Rm, Data>>>
233 ) {
234 if self.should_reset {
235 // We can destroy bricks, so when we reset, we want to respawn the bricks that have
236 // been destroyed. It should be all of them, but technically we don't know, so we have
237 // to formulaically figure it out.
238 let mut existing_bricks = Vec::new();
239 for obj in others.iter() {
240 if let Data::Brick(id) = obj.state().custom {
241 existing_bricks.push(id);
242 }
243 }
244 let to_respawn = (0..=5).into_iter()
245 .map(|id| if !existing_bricks.contains(&id) {
246 Some(
247 Box::new(Brick::new(BRICK_POS[id], id))
248 as Box<dyn GameObjectBehavior<Img, Snd, Fnt, Spr, Rm, Data>>
249 )
250 } else {
251 None
252 }).filter(|brick| brick.is_some())
253 .map(|brick| brick.unwrap())
254 .collect::<Vec<Box<_>>>();
255 return (Some(Rm::Room0), to_respawn);
256 }
257
258 self.state.pos.0 += self.vel.0 * delta;
259 self.state.pos.1 += self.vel.1 * delta;
260
261 // Animate
262 if let Some(spr) = self.state.sprs.get_mut(&self.state.cur_spr) {
263 spr.scale = (0.5, 0.5);
264 spr.flip = (!self.facing_right, false);
265 }
266 if !self.grounded && self.state.cur_spr != Spr::Idle {
267 self.state.cur_spr = Spr::Idle;
268 } else if self.grounded {
269 if self.vel.0.abs() < MOVE_SPD * 0.8 && self.state.cur_spr != Spr::Idle {
270 self.state.cur_spr = Spr::Idle;
271 } else if self.vel.0.abs() >= MOVE_SPD * 0.8 && self.state.cur_spr != Spr::Walk {
272 self.state.cur_spr = Spr::Walk;
273 self.facing_right = self.vel.0 > 0.1;
274 }
275 }
276
277 // Update vel at the end, so collisions can affect it!
278 let hor = (if self.right { 1.0 } else { 0.0 }) + (if self.left { -1.0 } else { 0.0 });
279 self.vel.0 = ycraft::util::lerp(self.vel.0, hor * MOVE_SPD, ACC * delta);
280
281 if let CollisionShape::Rect { center, size } = self.state.collider {
282 let gnd_check = CollisionShape::Rect {
283 center: (
284 center.0 + self.state.pos.0 as i32,
285 center.1 + (self.state.pos.1 + self.vel.1 * delta) as i32 + 1
286 ), size
287 };
288 self.grounded = false;
289 for other in others.iter() {
290 if self.state.pos.1 > other.state().pos.1 - size.1 as f64 * 0.8 {
291 continue;
292 }
293 if let Data::Brick(_) = other.state().custom {
294 let mut other_col = other.state().collider.clone();
295 if let CollisionShape::Rect { center: ref mut other_center, .. } = other_col {
296 other_center.0 += other.state().pos.0 as i32;
297 other_center.1 += other.state().pos.1 as i32;
298 };
299 if gnd_check.collides_with(&other_col) {
300 if let CollisionShape::Rect { center, size } = other_col {
301 self.grounded = true;
302 self.vel.1 = 0.0;
303 self.state.pos.1 = (center.1 - size.1 as i32 - 1) as f64;
304 }
305 }
306 }
307 }
308 }
309 if self.grounded || self.vel.1 > 0.1 {
310 self.extra_grav = false;
311 }
312 if self.extra_grav {
313 self.vel.1 += EXTRA_GRAV * delta;
314 } else {
315 self.vel.1 += GRAVITY * delta;
316 }
317
318 (None, vec![])
319 }Trait Implementations§
Source§impl Clone for CollisionShape
impl Clone for CollisionShape
Source§fn clone(&self) -> CollisionShape
fn clone(&self) -> CollisionShape
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for CollisionShape
impl Debug for CollisionShape
Source§impl Ord for CollisionShape
impl Ord for CollisionShape
Source§fn cmp(&self, other: &CollisionShape) -> Ordering
fn cmp(&self, other: &CollisionShape) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl PartialEq for CollisionShape
impl PartialEq for CollisionShape
Source§impl PartialOrd for CollisionShape
impl PartialOrd for CollisionShape
impl Eq for CollisionShape
impl StructuralPartialEq for CollisionShape
Auto Trait Implementations§
impl Freeze for CollisionShape
impl RefUnwindSafe for CollisionShape
impl Send for CollisionShape
impl Sync for CollisionShape
impl Unpin for CollisionShape
impl UnwindSafe for CollisionShape
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more