1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var index_1 = require("./../index");
|
4 | var Tool_1 = require("./../Tool");
|
5 |
|
6 |
|
7 |
|
8 | var Physic = (function () {
|
9 |
|
10 | |
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | function Physic(owner, x, y, width, height, type, box, props) {
|
22 | if (props === void 0) { props = {}; }
|
23 | |
24 |
|
25 |
|
26 |
|
27 | this.enabled = false;
|
28 | this.owner = owner;
|
29 | this.offsetX = width / 2;
|
30 | this.offsetY = height / 2;
|
31 | this.shape = this.constructShape(box, width, height, props.group || Tool_1.Enum.GROUP.ALL, type === Tool_1.Enum.TYPE.GHOST);
|
32 | this.body = this.constructBody(type, x + this.offsetX, y + this.offsetY, props.gravityFactor || 1);
|
33 | this.id = this.body.id;
|
34 | this.shape.material = props.material || this.owner.context.scene.getDefaultMaterial();
|
35 | this.body.addShape(this.shape);
|
36 | if (this.owner.props.bounce) {
|
37 | this.setBounciness(this.owner.props.bounce);
|
38 | }
|
39 | }
|
40 |
|
41 | |
42 |
|
43 |
|
44 | Physic.prototype.enable = function () {
|
45 | if (this.owner) {
|
46 | this.owner.context.scene.addPhysic(this);
|
47 | this.enabled = true;
|
48 | }
|
49 | };
|
50 | |
51 |
|
52 |
|
53 | Physic.prototype.disable = function () {
|
54 | if (this.owner) {
|
55 | this.owner.context.scene.removePhysic(this);
|
56 | this.enabled = false;
|
57 | }
|
58 | };
|
59 | |
60 |
|
61 |
|
62 |
|
63 | Physic.prototype.setBounciness = function (bounce) {
|
64 | if (this.owner) {
|
65 | this.owner.context.scene.setPhysicBouncing(this, bounce);
|
66 | }
|
67 | };
|
68 | |
69 |
|
70 |
|
71 | Physic.prototype.getSize = function () {
|
72 | if (this.shape instanceof index_1.p2.Circle) {
|
73 | var diameter = this.shape.radius * 2;
|
74 | return { width: diameter, height: diameter };
|
75 | }
|
76 | var shape = this.shape;
|
77 | return { width: shape.width, height: shape.height };
|
78 | };
|
79 | |
80 |
|
81 |
|
82 |
|
83 | Physic.prototype.getPosition = function () {
|
84 | var size = this.getSize();
|
85 | return {
|
86 | x: Tool_1.Util.meterToPixel(this.body.interpolatedPosition[0]) - this.offsetX,
|
87 | y: Tool_1.Util.meterToPixel(this.body.interpolatedPosition[1]) - this.offsetY
|
88 | };
|
89 | };
|
90 | |
91 |
|
92 |
|
93 |
|
94 | Physic.prototype.getAngle = function () {
|
95 | return Tool_1.Util.toDegree(this.body.interpolatedAngle);
|
96 | };
|
97 | |
98 |
|
99 |
|
100 |
|
101 | Physic.prototype.getVelocity = function () {
|
102 | return {
|
103 | x: Tool_1.Util.meterToPixel(this.body.velocity[0]),
|
104 | y: Tool_1.Util.meterToPixel(this.body.velocity[1])
|
105 | };
|
106 | };
|
107 | |
108 |
|
109 |
|
110 |
|
111 | Physic.prototype.getAcceleration = function () {
|
112 | return {
|
113 | x: Tool_1.Util.meterToPixel(this.body.force[0]),
|
114 | y: Tool_1.Util.meterToPixel(this.body.force[1])
|
115 | };
|
116 | };
|
117 | |
118 |
|
119 |
|
120 |
|
121 | Physic.prototype.getGravityFactor = function () {
|
122 | return this.body && this.body.gravityScale;
|
123 | };
|
124 | |
125 |
|
126 |
|
127 |
|
128 | Physic.prototype.setGravityFactor = function (value) {
|
129 | this.body.gravityScale = value;
|
130 | };
|
131 | |
132 |
|
133 |
|
134 |
|
135 |
|
136 | Physic.prototype.setPosition = function (x, y) {
|
137 | if (x || x === 0) {
|
138 | this.body.interpolatedPosition[0] = this.body.position[0] = Tool_1.Util.pixelToMeter(x + this.offsetX);
|
139 | }
|
140 | if (y || y === 0) {
|
141 | this.body.interpolatedPosition[1] = this.body.position[1] = Tool_1.Util.pixelToMeter(y + this.offsetY);
|
142 | }
|
143 | };
|
144 | |
145 |
|
146 |
|
147 |
|
148 |
|
149 | Physic.prototype.setVelocity = function (x, y) {
|
150 | if (x || x === 0) {
|
151 | this.body.velocity[0] = Tool_1.Util.pixelToMeter(x);
|
152 | }
|
153 | if (y || y === 0) {
|
154 | this.body.velocity[1] = Tool_1.Util.pixelToMeter(y);
|
155 | }
|
156 | };
|
157 | |
158 |
|
159 |
|
160 |
|
161 |
|
162 | Physic.prototype.setAcceleration = function (x, y) {
|
163 | if (x || x === 0) {
|
164 | this.body.force[0] = Tool_1.Util.pixelToMeter(x);
|
165 | }
|
166 | if (y || y === 0) {
|
167 | this.body.force[1] = Tool_1.Util.pixelToMeter(y);
|
168 | }
|
169 | };
|
170 | |
171 |
|
172 |
|
173 |
|
174 | Physic.prototype.setAngle = function (angle) {
|
175 | this.body.interpolatedAngle = this.body.angle = Tool_1.Util.toRadians(angle);
|
176 | };
|
177 | |
178 |
|
179 |
|
180 |
|
181 | Physic.prototype.idle = function (stopGravity) {
|
182 | if (stopGravity === void 0) { stopGravity = false; }
|
183 | this.setVelocity(0, 0);
|
184 | this.setAcceleration(0, 0);
|
185 | if (stopGravity) {
|
186 | this.body.gravityScale = 0;
|
187 | }
|
188 | };
|
189 | |
190 |
|
191 |
|
192 |
|
193 |
|
194 | Physic.prototype.setShapeGroup = function (shape, group) {
|
195 | var toMask = function (groupNumber) { return Math.pow(2, groupNumber); };
|
196 | shape.collisionGroup = toMask(group);
|
197 | switch (group) {
|
198 | case Tool_1.Enum.GROUP.ALL:
|
199 | shape.collisionMask = -1;
|
200 | break;
|
201 | case Tool_1.Enum.GROUP.NONE:
|
202 | shape.collisionMask = 0;
|
203 | break;
|
204 | case Tool_1.Enum.GROUP.GROUND:
|
205 | shape.collisionMask = -1;
|
206 | break;
|
207 | case Tool_1.Enum.GROUP.ALLY:
|
208 | shape.collisionMask = toMask(Tool_1.Enum.GROUP.ALL) | toMask(Tool_1.Enum.GROUP.GROUND) | toMask(Tool_1.Enum.GROUP.ENEMY) | toMask(Tool_1.Enum.GROUP.ENTITIES);
|
209 | break;
|
210 | case Tool_1.Enum.GROUP.ENEMY:
|
211 | shape.collisionMask = toMask(Tool_1.Enum.GROUP.ALL) | toMask(Tool_1.Enum.GROUP.GROUND) | toMask(Tool_1.Enum.GROUP.ALLY) | toMask(Tool_1.Enum.GROUP.ENTITIES);
|
212 | break;
|
213 | case Tool_1.Enum.GROUP.NEUTRAL:
|
214 | shape.collisionMask = toMask(Tool_1.Enum.GROUP.ALL) | toMask(Tool_1.Enum.GROUP.GROUND) | toMask(Tool_1.Enum.GROUP.ENTITIES);
|
215 | break;
|
216 | case Tool_1.Enum.GROUP.ENTITIES:
|
217 | shape.collisionMask = toMask(Tool_1.Enum.GROUP.ALL) | toMask(Tool_1.Enum.GROUP.ALLY) | toMask(Tool_1.Enum.GROUP.ENEMY);
|
218 | break;
|
219 | }
|
220 | };
|
221 | |
222 |
|
223 |
|
224 |
|
225 | Physic.prototype.getBodyId = function () {
|
226 | return this.body && this.body.id;
|
227 | };
|
228 | |
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 | Physic.prototype.constructShape = function (box, width, height, group, isSensor) {
|
238 | if (isSensor === void 0) { isSensor = false; }
|
239 | var shape = null;
|
240 | switch (box) {
|
241 | case Tool_1.Enum.BOX.CIRCLE:
|
242 | shape = new index_1.p2.Circle({ radius: Tool_1.Util.pixelToMeter(width / 2) });
|
243 | break;
|
244 | default:
|
245 | shape = new index_1.p2.Box({ width: Tool_1.Util.pixelToMeter(width), height: Tool_1.Util.pixelToMeter(height) });
|
246 | break;
|
247 | }
|
248 | if (shape && group) {
|
249 | this.setShapeGroup(shape, group);
|
250 | }
|
251 | shape.sensor = isSensor;
|
252 | return shape;
|
253 | };
|
254 | |
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 | Physic.prototype.constructBody = function (type, x, y, gravityFactor) {
|
263 | if (gravityFactor === void 0) { gravityFactor = 1; }
|
264 | var settings = {
|
265 | position: [Tool_1.Util.pixelToMeter(x), Tool_1.Util.pixelToMeter(y)],
|
266 | mass: type < 0 ? 0 : type,
|
267 | gravityScale: gravityFactor,
|
268 | fixedRotation: type !== Tool_1.Enum.TYPE.WEAK
|
269 | };
|
270 | var body = new index_1.p2.Body(settings);
|
271 | return body;
|
272 | };
|
273 | return Physic;
|
274 | }());
|
275 | exports.Physic = Physic;
|