UNPKG

9.96 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var index_1 = require("./../index");
4var Tool_1 = require("./../Tool");
5/**
6 * Physic class for collision and gravity
7 */
8var Physic = (function () {
9 /* LIFECYCLE */
10 /**
11 * @constructor
12 * @param owner - The owner of the physic
13 * @param x - Position in x axis
14 * @param y - Position in y axis
15 * @param width - Width of the body
16 * @param height - Height of the body
17 * @param type - Type of body properties (see Enum.TYPE)
18 * @param box - Shape of the body (see Enum.BOX)
19 * @param props - Optional properties to pass to the body
20 */
21 function Physic(owner, x, y, width, height, type, box, props) {
22 if (props === void 0) { props = {}; }
23 /**
24 * Know if the physic engine is enabled
25 * @readonly
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 /* METHODS */
41 /**
42 * Enable the physic engine to the world
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 * Disable the physic engine to the world
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 * Set a new factor of bounciness
61 * @param bounce - The next factor of bounciness
62 */
63 Physic.prototype.setBounciness = function (bounce) {
64 if (this.owner) {
65 this.owner.context.scene.setPhysicBouncing(this, bounce);
66 }
67 };
68 /**
69 * Get the real size of the body
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 * Get the position of the body
81 * @returns The current position
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 * Get the angle of the body (in Degree)
92 * @returns The current angle
93 */
94 Physic.prototype.getAngle = function () {
95 return Tool_1.Util.toDegree(this.body.interpolatedAngle);
96 };
97 /**
98 * Get the velocity of the body
99 * @returns The current velocity
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 * Get the acceleration of the body
109 * @returns The current acceleration
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 * Get the factor of gravity for the body
119 * @returns The factor of gravity
120 */
121 Physic.prototype.getGravityFactor = function () {
122 return this.body && this.body.gravityScale;
123 };
124 /**
125 * Set a new factor of gravity for the body
126 * @param value - The new factor of gravity
127 */
128 Physic.prototype.setGravityFactor = function (value) {
129 this.body.gravityScale = value;
130 };
131 /**
132 * Set a new position for the body
133 * @param x - The position in x axis
134 * @param y - The position in y axis
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 * Set a new velocity for the body
146 * @param x - Velocity in x axis
147 * @param y - Velocity in y axis
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 * Set a new acceleration for the body
159 * @param x - Acceleration in x axis
160 * @param y - Acceleration in y axis
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 * Set a new angle for the body
172 * @param angle - The angle (in Degree)
173 */
174 Physic.prototype.setAngle = function (angle) {
175 this.body.interpolatedAngle = this.body.angle = Tool_1.Util.toRadians(angle);
176 };
177 /**
178 * Stop all movement of the physic body
179 * @param stopGravity - If true, the body will be no longer moved by gravity
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 * Set a Group of collision to a shape
191 * @param shape - The Shape
192 * @param group - The group enumeration (see Enum.GROUP)
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 * Get the id of the p2.Body
223 * @returns The id of the current body
224 */
225 Physic.prototype.getBodyId = function () {
226 return this.body && this.body.id;
227 };
228 /**
229 * Construct a p2.Shape by Enum.BOX
230 * @param box - Box enumeration (see Enum.BOX)
231 * @param width - The width of the shape
232 * @param height - The height of the shape (not used for circle
233 * @param group - Group enumeration (see Enum.GROUP)
234 * @param isSensor - If true, the shape will be a reporter of collision but will not resolve collisions
235 * @returns {Shape} A p2.Shape
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 * Construct a p2.Body by Enum.TYPE
256 * @param type - Type enumeration (see Enum.TYPE)
257 * @param x - Position in x axis of the body
258 * @param y - Position in y axis of the body
259 * @param gravityFactor - The factor of gravity (provided by the owner)
260 * @returns A p2.Body
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}());
275exports.Physic = Physic;