-
Notifications
You must be signed in to change notification settings - Fork 241
/
CollisionHandler.cs
341 lines (284 loc) · 12.5 KB
/
CollisionHandler.cs
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
* Copyright (c) 2023 Codefoco (codefoco@codefoco.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using System;
using System.ComponentModel;
using System.Diagnostics;
using cpArbiter = System.IntPtr;
using cpBool = System.Byte;
using cpCollisionHandlerPointer = System.IntPtr;
using cpSpace = System.IntPtr;
using voidptr_t = System.IntPtr;
#if __IOS__ || __TVOS__ || __WATCHOS__ || __MACCATALYST__
using ObjCRuntime;
#endif
namespace Tizen.NUI.Physics2D.Chipmunk
{
/// <summary>
/// A collision handler is a set of 4 function callbacks for the different collision events that
/// Chipmunk recognizes. Collision callbacks are closely associated with <see cref="Arbiter"/>
/// objects. You should familiarize yourself with those as well. Note #1: Shapes tagged as
/// sensors (<see cref="Shape.Sensor"/> == true) never generate collisions that get processed,
/// so collisions between sensor shapes and other shapes will never call the post_solve()
/// callback. They still generate begin() and separate() callbacks, and the pre_solve() callback
/// is also called every frame even though there is no collision response. Note #2: pre_solve()
/// callbacks are called before the sleeping algorithm runs. If an object falls asleep, its
/// post_solve() callback won’t be called until it’s re-awoken.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class CollisionHandler
{
private readonly cpCollisionHandlerPointer handle;
private static CollisionBeginFunction beginCallback = CollisionBeginFunctionCallback;
private static CollisionPreSolveFunction preSolveCallback = CollisionPreSolveFunctionCallback;
private static CollisionPostSolveFunction postSolveCallback = CollisionPostSolveFunctionCallback;
private static CollisionSeparateFunction separeteCallback = CollisionSeparateFunctionCallback;
private static IntPtr DefaultBeginFunction;
private static IntPtr DefaultPreSolveFunction;
private static IntPtr DefaultPostSolveFunction;
private static IntPtr DefaultSeparateFunction;
private CollisionHandler(cpCollisionHandlerPointer collisionHandle, ref cpCollisionHandler handler)
{
handle = collisionHandle;
IntPtr data = NativeInterop.RegisterHandle(this);
handler.userData = data;
long typeA = (long)handler.typeA.ToUInt64();
long typeB = (long)handler.typeB.ToUInt64();
TypeA = unchecked((int)typeA);
TypeB = unchecked((int)typeB);
cpCollisionHandler.ToPointer(handler, handle);
}
internal static CollisionHandler GetOrCreate(cpCollisionHandlerPointer collisionHandle)
{
Debug.Assert(collisionHandle != IntPtr.Zero, "CollisionHandle cannot be zero");
var handler = cpCollisionHandler.FromHandle(collisionHandle);
if (handler.userData != IntPtr.Zero)
{
return NativeInterop.FromIntPtr<CollisionHandler>(handler.userData);
}
EnsureDefaultCallbackValues(handler);
return new CollisionHandler(collisionHandle, ref handler);
}
private static void EnsureDefaultCallbackValues(cpCollisionHandler handler)
{
if (DefaultBeginFunction != IntPtr.Zero)
return;
DefaultBeginFunction = handler.beginFunction;
DefaultPreSolveFunction = handler.preSolveFunction;
DefaultPostSolveFunction = handler.postSolveFunction;
DefaultSeparateFunction = handler.separateFunction;
}
private Action<Arbiter, Space, object> begin;
/// <summary>
/// This function is called when two shapes with types that match this collision handler begin colliding
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public Action<Arbiter, Space, object> Begin
{
set
{
begin = value;
var handler = cpCollisionHandler.FromHandle(handle);
IntPtr callbackPointer;
if (value == null)
{
callbackPointer = DefaultBeginFunction;
}
else
{
callbackPointer = beginCallback.ToFunctionPointer();
}
handler.beginFunction = callbackPointer;
cpCollisionHandler.ToPointer(handler, handle);
}
get => begin;
}
private Func<Arbiter, Space, object, bool> preSolve;
/// <summary>
/// This function is called each step when two shapes with types that match this collision
/// handler are colliding. It's called before the collision solver runs so that you can
/// affect a collision's outcome.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public Func<Arbiter, Space, object, bool> PreSolve
{
set
{
preSolve = value;
var handler = cpCollisionHandler.FromHandle(handle);
IntPtr callbackPointer;
if (value == null)
{
callbackPointer = DefaultPreSolveFunction;
}
else
{
callbackPointer = preSolveCallback.ToFunctionPointer();
}
handler.preSolveFunction = callbackPointer;
cpCollisionHandler.ToPointer(handler, handle);
}
get => preSolve;
}
private Action<Arbiter, Space, object> postSolve;
/// <summary>
/// This function is called each step when two shapes with types that match this collision
/// handler are colliding. It's called after the collision solver runs so that you can read
/// back information about the collision to trigger events in your game.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public Action<Arbiter, Space, object> PostSolve
{
set
{
postSolve = value;
var handler = cpCollisionHandler.FromHandle(handle);
IntPtr callbackPointer;
if (value == null)
{
callbackPointer = DefaultPostSolveFunction;
}
else
{
callbackPointer = postSolveCallback.ToFunctionPointer();
}
handler.postSolveFunction = callbackPointer;
cpCollisionHandler.ToPointer(handler, handle);
}
get => postSolve;
}
private Action<Arbiter, Space, object> separate;
/// <summary>
/// This function is called when two shapes with types that match this collision handler
/// stop colliding.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public Action<Arbiter, Space, object> Separate
{
set
{
separate = value;
var handler = cpCollisionHandler.FromHandle(handle);
IntPtr callbackPointer;
if (value == null)
{
callbackPointer = DefaultSeparateFunction;
}
else
{
callbackPointer = separeteCallback.ToFunctionPointer();
}
handler.separateFunction = callbackPointer;
cpCollisionHandler.ToPointer(handler, handle);
}
get => separate;
}
/// <summary>
/// User definable context pointer that is passed to all of the collision handler functions.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public object Data { get; set; }
/// <summary>
/// In the collision handler callback, the shape with this type will be the first argument.
/// Read only.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public int TypeA { get; }
/// <summary>
/// In the collision handler callback, the shape with this type will be the second argument.
/// Read only.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public int TypeB { get; }
#if __IOS__ || __TVOS__ || __WATCHOS__ || __MACCATALYST__
#pragma warning disable CA1416 // Validate platform compatibility
[MonoPInvokeCallback(typeof(CollisionBeginFunction))]
#pragma warning restore CA1416 // Validate platform compatibility
#endif
private static void CollisionBeginFunctionCallback(cpArbiter arbiterHandle, cpSpace spaceHandle, voidptr_t userData)
{
var arbiter = new Arbiter(arbiterHandle);
var space = Space.FromHandle(spaceHandle);
var handler = NativeInterop.FromIntPtr<CollisionHandler>(userData);
var begin = handler.Begin;
if (begin == null)
{
return;
}
begin(arbiter, space, handler.Data);
}
#if __IOS__ || __TVOS__ || __WATCHOS__ || __MACCATALYST__
#pragma warning disable CA1416 // Validate platform compatibility
[MonoPInvokeCallback(typeof(CollisionPreSolveFunction))]
#pragma warning restore CA1416 // Validate platform compatibility
#endif
private static cpBool CollisionPreSolveFunctionCallback(cpArbiter arbiterHandle, cpSpace spaceHandle, voidptr_t userData)
{
var arbiter = new Arbiter(arbiterHandle);
var space = Space.FromHandle(spaceHandle);
var handler = NativeInterop.FromIntPtr<CollisionHandler>(userData);
var preSolve = handler.PreSolve;
if (preSolve == null)
{
return 1;
}
if (preSolve(arbiter, space, handler.Data))
{
return 1;
}
return 0;
}
#if __IOS__ || __TVOS__ || __WATCHOS__ || __MACCATALYST__
#pragma warning disable CA1416 // Validate platform compatibility
[MonoPInvokeCallback(typeof(CollisionPostSolveFunction))]
#pragma warning restore CA1416 // Validate platform compatibility
#endif
private static void CollisionPostSolveFunctionCallback(cpArbiter arbiterHandle, cpSpace spaceHandle, voidptr_t userData)
{
var arbiter = new Arbiter(arbiterHandle);
var space = Space.FromHandle(spaceHandle);
var handler = NativeInterop.FromIntPtr<CollisionHandler>(userData);
var postSolve = handler.PostSolve;
if (postSolve == null)
{
return;
}
postSolve(arbiter, space, handler.Data);
}
#if __IOS__ || __TVOS__ || __WATCHOS__ || __MACCATALYST__
#pragma warning disable CA1416 // Validate platform compatibility
[MonoPInvokeCallback(typeof(CollisionSeparateFunction))]
#pragma warning restore CA1416 // Validate platform compatibility
#endif
private static void CollisionSeparateFunctionCallback(cpArbiter arbiterHandle, cpSpace spaceHandle, voidptr_t userData)
{
var arbiter = new Arbiter(arbiterHandle);
var space = Space.FromHandle(spaceHandle);
var handler = NativeInterop.FromIntPtr<CollisionHandler>(userData);
var separate = handler.Separate;
if (separate == null)
{
return;
}
separate(arbiter, space, handler.Data);
}
}
}