-
Notifications
You must be signed in to change notification settings - Fork 22
/
model.ts
256 lines (214 loc) · 4.59 KB
/
model.ts
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
import {
Circle as SATCircle,
Polygon as SATPolygon,
Response,
Vector as SATVector,
} from "sat";
import { System } from "./system";
import { Box } from "./bodies/box";
import { Circle } from "./bodies/circle";
import { Ellipse } from "./bodies/ellipse";
import { Line } from "./bodies/line";
import { Point } from "./bodies/point";
import { Polygon } from "./bodies/polygon";
// version 4.0.0 1=1 copy
import RBush from "./external/rbush";
export {
Polygon as DecompPolygon,
Point as DecompPoint,
isSimple,
} from "poly-decomp-es";
export interface BBox {
minX: number;
minY: number;
maxX: number;
maxY: number;
}
export { RBush, Response, SATVector, SATPolygon, SATCircle };
export type CollisionCallback = (response: Response) => boolean | void;
/**
* types
*/
export enum BodyType {
Ellipse = "Ellipse",
Circle = "Circle",
Polygon = "Polygon",
Box = "Box",
Line = "Line",
Point = "Point",
}
/**
* for groups
*/
export enum BodyGroup {
Ellipse = 0b00100000,
Circle = 0b00010000,
Polygon = 0b00001000,
Box = 0b00000100,
Line = 0b00000010,
Point = 0b00000001,
}
/**
* body with children (rbush)
*/
export type Leaf<TBody extends Body> = TBody & {
children?: Leaf<TBody>[];
};
/**
* rbush data
*/
export interface ChildrenData<TBody extends Body> {
children: Leaf<TBody>[];
}
/**
* for use of private function of sat.js
*/
export interface Data<TBody extends Body> {
data: ChildrenData<TBody>;
}
/**
* BodyOptions for body creation
*/
export interface BodyOptions {
/**
* system.separate() doesn't move this body
*/
isStatic?: boolean;
/**
* system.separate() doesn't trigger collision of this body
*/
isTrigger?: boolean;
/**
* is body offset centered for rotation purpouses
*/
isCentered?: boolean;
/**
* body angle in radians use deg2rad to convert
*/
angle?: number;
/**
* BHV padding for bounding box, preventing costly updates
*/
padding?: number;
/**
* group for collision filtering
*/
group?: number;
}
/**
* system.raycast(from, to) result
*/
export interface RaycastHit<TBody> {
point: Vector;
body: TBody;
}
/**
* potential vector
*/
export interface PotentialVector {
x?: number;
y?: number;
}
/**
* x, y vector
*/
export interface Vector extends PotentialVector {
x: number;
y: number;
}
/**
* for use of private function of sat.js
*/
export interface GetAABBAsBox {
getAABBAsBox(): { pos: Vector; w: number; h: number };
}
/**
* generic body union type
*/
export type Body = Point | Line | Ellipse | Circle | Box | Polygon;
/**
* each body contains those regardless of type
*/
export interface BodyProps extends Required<BodyOptions> {
/**
* type of body
*/
readonly type: BodyType;
/**
* faster for comparision, inner, type of body as number
*/
readonly typeGroup: BodyGroup;
/**
* flag to show is it a convex body or non convex polygon
*/
isConvex: boolean;
/**
* bounding box cache, without padding
*/
bbox: BBox;
/**
* each body may have offset from center
*/
offset: SATVector;
/**
* collisions system reference
*/
system?: System;
/**
* was the body modified and needs update in the next checkCollision
*/
dirty: boolean;
/**
* scale getter (x)
*/
get scaleX(): number;
/**
* scale getter (y = x for Circle)
*/
get scaleY(): number;
/**
* update position BY MOVING FORWARD IN ANGLE DIRECTION
*/
move(speed: number, updateNow?: boolean): Circle | SATPolygon;
/**
* update position BY TELEPORTING
*/
setPosition(x: number, y: number, updateNow?: boolean): Circle | SATPolygon;
/**
* for setting scale
*/
setScale(x: number, y: number, updateNow?: boolean): Circle | SATPolygon;
/**
* for setting angle
*/
setAngle(angle: number, updateNow?: boolean): Circle | SATPolygon;
/**
* for setting offset from center
*/
setOffset(offset: Vector, updateNow?: boolean): Circle | SATPolygon;
/**
* draw the bounding box
*/
drawBVH(context: CanvasRenderingContext2D): void;
/**
* draw the collider
*/
draw(context: CanvasRenderingContext2D): void;
/**
* return bounding box without padding
*/
getAABBAsBBox(): BBox;
}
export type SATTest<
T extends {} = Circle | Polygon | SATPolygon,
Y extends {} = Circle | Polygon | SATPolygon,
> = (bodyA: T, bodyB: Y, response: Response) => boolean;
export type InTest<TBody extends Body = Body> = (
bodyA: TBody,
bodyB: TBody,
) => boolean;
export type TraverseFunction<TBody extends Body = Body> = (
child: Leaf<TBody>,
children: Leaf<TBody>[],
index: number,
) => boolean | void;