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
use core::ffi::c_float;

use sys::ffi::CollisionPoint;
use sys::ffi::CollisionVector;
use sys::ffi::PDRect;
use sys::ffi::SpriteCollisionInfo;
use sys::ffi::SpriteCollisionResponseType;
use sys::ffi::SpriteQueryInfo;

use crate::SpriteRef;


pub trait SpriteQueryInfoExt {
	/// The sprite being intersected by the segment
	fn sprite(&self) -> SpriteRef;

	/// Entry point
	///
	/// `ti1` and `ti2` are numbers between 0 and 1
	/// which indicate how far from the starting point of the line segment the collision happened
	fn ti1(&self) -> c_float;

	/// Exit point
	///
	/// `ti1` and `ti2` are numbers between 0 and 1
	/// which indicate how far from the starting point of the line segment the collision happened
	fn ti2(&self) -> c_float;

	/// The coordinates of the first intersection between sprite and the line segment
	fn entry_point(&self) -> &CollisionPoint;
	fn entry_point_mut(&mut self) -> &mut CollisionPoint;

	/// The coordinates of the second intersection between sprite and the line segment
	fn exit_point(&self) -> &CollisionPoint;
	fn exit_point_mut(&mut self) -> &mut CollisionPoint;
}

impl SpriteQueryInfoExt for SpriteQueryInfo {
	fn sprite(&self) -> SpriteRef { self.sprite.into() }

	fn ti1(&self) -> c_float { self.ti1 }

	fn ti2(&self) -> c_float { self.ti2 }

	fn entry_point(&self) -> &CollisionPoint { &self.entryPoint }
	fn entry_point_mut(&mut self) -> &mut CollisionPoint { &mut self.entryPoint }

	fn exit_point(&self) -> &CollisionPoint { &self.exitPoint }
	fn exit_point_mut(&mut self) -> &mut CollisionPoint { &mut self.exitPoint }
}


trait SpriteCollisionInfoExt {
	/// The sprite being moved
	fn sprite(&self) -> SpriteRef;

	/// The sprite colliding with the sprite being moved
	fn other(&self) -> SpriteRef;

	/// The result of collisionResponse
	fn response_type(&self) -> SpriteCollisionResponseType;

	/// True if the sprite was overlapping other when the collision started.
	///
	/// False if it didn’t overlap but tunneled through other
	fn overlaps(&self) -> bool;

	/// A number between `0` and `1` indicating how far along the movement to the goal the collision occurred
	fn ti(&self) -> c_float;

	/// The difference between the original coordinates and the actual ones when the collision happened
	fn diff(&self) -> &CollisionPoint;

	/// The collision normal;
	/// usually `-1`, `0`, or `1` in `x` and `y`.
	///
	/// Use this value to determine things like if your character is touching the ground
	fn normal(&self) -> &CollisionVector;

	/// The coordinates where the sprite started touching other
	fn touch(&self) -> &CollisionPoint;

	/// The rectangle the sprite occupied when the touch happened
	fn sprite_rect(&self) -> &PDRect;

	/// The rectangle the sprite being collided with occupied when the touch happened
	fn other_rect(&self) -> &PDRect;
}

impl SpriteCollisionInfoExt for SpriteCollisionInfo {
	fn sprite(&self) -> SpriteRef { self.sprite.into() }
	fn other(&self) -> SpriteRef { self.other.into() }
	fn response_type(&self) -> SpriteCollisionResponseType { self.responseType }
	fn overlaps(&self) -> bool { self.overlaps != 0 }
	fn ti(&self) -> c_float { self.ti }
	fn diff(&self) -> &CollisionPoint { &self.move_ }
	fn normal(&self) -> &CollisionVector { &self.normal }
	fn touch(&self) -> &CollisionPoint { &self.touch }
	fn sprite_rect(&self) -> &PDRect { &self.spriteRect }
	fn other_rect(&self) -> &PDRect { &self.otherRect }
}


pub trait SpriteCollisionResponseTypeExt {
	#![allow(non_upper_case_globals)]
	const Slide: SpriteCollisionResponseType = SpriteCollisionResponseType::kCollisionTypeSlide;
	const Freeze: SpriteCollisionResponseType = SpriteCollisionResponseType::kCollisionTypeFreeze;
	const Overlap: SpriteCollisionResponseType = SpriteCollisionResponseType::kCollisionTypeOverlap;
	const Bounce: SpriteCollisionResponseType = SpriteCollisionResponseType::kCollisionTypeBounce;
}

impl SpriteCollisionResponseTypeExt for SpriteCollisionResponseType {}