playdate_sprite/
ext.rs

1use core::ffi::c_float;
2
3use sys::ffi::CollisionPoint;
4use sys::ffi::CollisionVector;
5use sys::ffi::PDRect;
6use sys::ffi::SpriteCollisionInfo;
7use sys::ffi::SpriteCollisionResponseType;
8use sys::ffi::SpriteQueryInfo;
9
10use crate::SpriteRef;
11
12
13pub trait SpriteQueryInfoExt {
14	/// The sprite being intersected by the segment
15	fn sprite(&self) -> SpriteRef;
16
17	/// Entry point
18	///
19	/// `ti1` and `ti2` are numbers between 0 and 1
20	/// which indicate how far from the starting point of the line segment the collision happened
21	fn ti1(&self) -> c_float;
22
23	/// Exit point
24	///
25	/// `ti1` and `ti2` are numbers between 0 and 1
26	/// which indicate how far from the starting point of the line segment the collision happened
27	fn ti2(&self) -> c_float;
28
29	/// The coordinates of the first intersection between sprite and the line segment
30	fn entry_point(&self) -> &CollisionPoint;
31	fn entry_point_mut(&mut self) -> &mut CollisionPoint;
32
33	/// The coordinates of the second intersection between sprite and the line segment
34	fn exit_point(&self) -> &CollisionPoint;
35	fn exit_point_mut(&mut self) -> &mut CollisionPoint;
36}
37
38impl SpriteQueryInfoExt for SpriteQueryInfo {
39	fn sprite(&self) -> SpriteRef { self.sprite.into() }
40
41	fn ti1(&self) -> c_float { self.ti1 }
42
43	fn ti2(&self) -> c_float { self.ti2 }
44
45	fn entry_point(&self) -> &CollisionPoint { &self.entryPoint }
46	fn entry_point_mut(&mut self) -> &mut CollisionPoint { &mut self.entryPoint }
47
48	fn exit_point(&self) -> &CollisionPoint { &self.exitPoint }
49	fn exit_point_mut(&mut self) -> &mut CollisionPoint { &mut self.exitPoint }
50}
51
52
53trait SpriteCollisionInfoExt {
54	/// The sprite being moved
55	fn sprite(&self) -> SpriteRef;
56
57	/// The sprite colliding with the sprite being moved
58	fn other(&self) -> SpriteRef;
59
60	/// The result of collisionResponse
61	fn response_type(&self) -> SpriteCollisionResponseType;
62
63	/// True if the sprite was overlapping other when the collision started.
64	///
65	/// False if it didn’t overlap but tunneled through other
66	fn overlaps(&self) -> bool;
67
68	/// A number between `0` and `1` indicating how far along the movement to the goal the collision occurred
69	fn ti(&self) -> c_float;
70
71	/// The difference between the original coordinates and the actual ones when the collision happened
72	fn diff(&self) -> &CollisionPoint;
73
74	/// The collision normal;
75	/// usually `-1`, `0`, or `1` in `x` and `y`.
76	///
77	/// Use this value to determine things like if your character is touching the ground
78	fn normal(&self) -> &CollisionVector;
79
80	/// The coordinates where the sprite started touching other
81	fn touch(&self) -> &CollisionPoint;
82
83	/// The rectangle the sprite occupied when the touch happened
84	fn sprite_rect(&self) -> &PDRect;
85
86	/// The rectangle the sprite being collided with occupied when the touch happened
87	fn other_rect(&self) -> &PDRect;
88}
89
90impl SpriteCollisionInfoExt for SpriteCollisionInfo {
91	fn sprite(&self) -> SpriteRef { self.sprite.into() }
92	fn other(&self) -> SpriteRef { self.other.into() }
93	fn response_type(&self) -> SpriteCollisionResponseType { self.responseType }
94	fn overlaps(&self) -> bool { self.overlaps != 0 }
95	fn ti(&self) -> c_float { self.ti }
96	fn diff(&self) -> &CollisionPoint { &self.move_ }
97	fn normal(&self) -> &CollisionVector { &self.normal }
98	fn touch(&self) -> &CollisionPoint { &self.touch }
99	fn sprite_rect(&self) -> &PDRect { &self.spriteRect }
100	fn other_rect(&self) -> &PDRect { &self.otherRect }
101}
102
103
104pub trait SpriteCollisionResponseTypeExt {
105	#![allow(non_upper_case_globals)]
106	const Slide: SpriteCollisionResponseType = SpriteCollisionResponseType::kCollisionTypeSlide;
107	const Freeze: SpriteCollisionResponseType = SpriteCollisionResponseType::kCollisionTypeFreeze;
108	const Overlap: SpriteCollisionResponseType = SpriteCollisionResponseType::kCollisionTypeOverlap;
109	const Bounce: SpriteCollisionResponseType = SpriteCollisionResponseType::kCollisionTypeBounce;
110}
111
112impl SpriteCollisionResponseTypeExt for SpriteCollisionResponseType {}