CollisionShape

Enum CollisionShape 

Source
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§

§

Circle

Fields

§center: (i32, i32)
§radius: u32
§

Rect

Fields

§center: (i32, i32)
§size: (u32, u32)
§

Polygon

Fields

§center: (i32, i32)
§points: Vec<(i32, i32)>

Implementations§

Source§

impl CollisionShape

Source

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

Source§

fn clone(&self) -> CollisionShape

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CollisionShape

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Ord for CollisionShape

Source§

fn cmp(&self, other: &CollisionShape) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for CollisionShape

Source§

fn eq(&self, other: &CollisionShape) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for CollisionShape

Source§

fn partial_cmp(&self, other: &CollisionShape) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for CollisionShape

Source§

impl StructuralPartialEq for CollisionShape

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.