UNPKG

2.23 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = Object.setPrototypeOf ||
4 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6 return function (d, b) {
7 extendStatics(d, b);
8 function __() { this.constructor = d; }
9 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10 };
11})();
12Object.defineProperty(exports, "__esModule", { value: true });
13var Entity_1 = require("./../Entity");
14var Enum_1 = require("./../Tool/Enum");
15/**
16 * Hitbox is an helper class extended from Entity to generate hitbox contact between the owner and targets
17 */
18var Hitbox = (function (_super) {
19 __extends(Hitbox, _super);
20 /* LIFECYCLE */
21 /**
22 * @constructor
23 */
24 function Hitbox() {
25 var _this = _super.call(this) || this;
26 /**
27 * The number of hits before destruction of the Hitbox
28 * @readonly
29 */
30 _this.hit = 0;
31 _this.setProps({
32 type: Enum_1.Enum.TYPE.GHOST,
33 group: Enum_1.Enum.GROUP.ENTITIES,
34 owner: null,
35 gravityFactor: 0,
36 multipleHit: false,
37 oncePerHit: true,
38 maxHit: 1
39 });
40 _this.signals.beginCollision.add(_this.onCollision.bind(_this));
41 return _this;
42 }
43 /* EVENTS */
44 /**
45 * When entering in collision with an entity
46 * @param otherName - Name of the other entity
47 * @param other - The other entity
48 */
49 Hitbox.prototype.onCollision = function (otherName, other) {
50 var result = this.onHit(otherName, other);
51 if (result) {
52 this.hit++;
53 if (this.hit >= this.props.maxHit) {
54 this.kill();
55 }
56 }
57 };
58 /**
59 * Event fired when hitbox hit an entity
60 * @param name - Name of the entity
61 * @param target - The target
62 * @returns If true the hit will be counted
63 */
64 Hitbox.prototype.onHit = function (name, target) {
65 return true;
66 };
67 return Hitbox;
68}(Entity_1.Entity));
69exports.Hitbox = Hitbox;