UNPKG

2d-gaming

Version:

This is an Angular package fo developing 2d games in angular. [Please report issues/questions/any ideas to better help this package](https://github.com/CWestBlue/2d-gaming/issues).

140 lines 5.32 kB
var CrashComponent = (function () { function CrashComponent(object, barriers) { this.object = object; this.barriers = barriers; } CrashComponent.prototype.hitBarrier = function (object) { var _this = this; var right = object.game.canvas.width; var bottom = object.game.canvas.height; if (this.leavesWith(object)) { switch (this.leavesWith(object)) { case 'right': object.postion.xPos = object.game.canvas.width - object.design.width; object.speedX = 0; break; case 'left': object.postion.xPos = 0; break; case 'bottom': object.postion.yPos = object.game.canvas.height - object.design.height; object.speedY = 0; object.speedY = 0; // this.gravity = 0; break; case 'top': object.postion.yPos = 0; break; } return true; } if (this.barriers.items.length > 0) { this.barriers.items.forEach(function (obj) { if (!(_this.crashWithSide(object, obj) === 'false')) { _this.addClip(_this.crashWithSide(object, obj), object, obj); return true; } }); } return false; }; CrashComponent.prototype.addClip = function (side, object, barrier) { // console.log(side); switch (side) { case 'top': object.postion.yPos = barrier.postion.yPos - object.design.height; break; case 'bottom': object.postion.yPos = barrier.postion.yPos + barrier.design.height; break; case 'right': object.postion.xPos = barrier.postion.xPos + barrier.design.width; break; case 'left': object.postion.xPos = barrier.postion.xPos - object.design.width; break; } }; CrashComponent.prototype.crashWithSide = function (currentObj, otherobj) { var myleft = currentObj.postion.xPos; var myright = currentObj.postion.xPos + (currentObj.design.width); var mytop = currentObj.postion.yPos; var mybottom = currentObj.postion.yPos + (currentObj.design.height); var otherleft = otherobj.postion.xPos; var otherright = otherobj.postion.xPos + (otherobj.design.width); var othertop = otherobj.postion.yPos; var otherbottom = otherobj.postion.yPos + (otherobj.design.height); if ((mybottom < othertop) || (mytop > otherbottom) || (myright < otherleft) || (myleft > otherright)) { return 'false'; } else { if (mybottom > othertop && mytop < othertop) { // this.addClip('top', currentObj, otherobj) return 'top'; } if (mytop < otherbottom && mybottom > otherbottom) { // this.addClip('bottom', currentObj, otherobj) return 'bottom'; } if (myright > otherleft && myleft < otherleft) { // this.addClip('left', currentObj, otherobj) return 'left'; } if (myleft < otherright && myright > otherright) { // this.addClip('right', currentObj, otherobj) return 'right'; } } }; CrashComponent.prototype.leavesWith = function (object) { var myleft = object.postion.xPos; var myright = object.postion.xPos + (object.design.width); var mytop = object.postion.yPos; var mybottom = object.postion.yPos + (object.design.height); var otherleft = 0; var otherright = object.game.canvas.width; var othertop = 0; var otherbottom = object.game.canvas.height; if ((mytop < othertop) || (mybottom > otherbottom) || (myleft < otherleft) || (myright > otherright)) { if (mytop < othertop) { return 'top'; } if (mybottom > otherbottom) { return 'bottom'; } if (myleft < otherleft) { return 'left'; } if (myright > otherright) { return 'right'; } } else { return; } }; CrashComponent.prototype.newPos = function (barrier) { var _this = this; this.object.items.forEach(function (res) { if (barrier) { if (_this.hitBarrier(res)) { if (!(_this.leavesWith(res) === 'bottom')) { return; } else { res.speedY = 0; } } } }); }; return CrashComponent; }()); export { CrashComponent }; //# sourceMappingURL=crashLogic.component.js.map