@spibo-studio/spibo-studio
Version:
Spibo Studio is an HTML Canvas Library for creating adventure games
142 lines • 6.59 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var CanvasImage_1 = __importDefault(require("./Canvas/CanvasImage"));
var CanvasPosition_1 = __importDefault(require("./Canvas/CanvasPosition"));
var Character = /** @class */ (function (_super) {
__extends(Character, _super);
function Character(imageSrc, width, height, speed) {
var _this = _super.call(this, imageSrc, width, height) || this;
_this.speed = 5;
_this.activeSprite = null;
if (speed) {
_this.speed = speed;
}
return _this;
}
Character.prototype.moveUp = function (canvas) {
var background = canvas.background, framerate = canvas.framerate, boundaries = canvas.boundaries;
var _a = this, activeSprite = _a.activeSprite, currentPos = _a.pos, speed = _a.speed;
if (activeSprite && currentPos) {
var newPos = new CanvasPosition_1.default(currentPos.x, currentPos.y - speed / framerate);
if (!boundaries.yIsWithinBoundaries(newPos.y)) {
newPos.y = boundaries.top;
if (background)
background.moveUp(speed, framerate);
}
if (!this.areFeetRestricted(background, newPos)) {
this.pos = newPos;
if (background)
this.verifyGateways(background, newPos);
}
}
};
Character.prototype.moveDown = function (canvas) {
var background = canvas.background, framerate = canvas.framerate, boundaries = canvas.boundaries, canvasHeight = canvas.height;
var _a = this, activeSprite = _a.activeSprite, currentPos = _a.pos, speed = _a.speed;
if (activeSprite && currentPos) {
var newPos = new CanvasPosition_1.default(currentPos.x, currentPos.y + this.speed / framerate);
if (!boundaries.yIsWithinBoundaries(newPos.y + activeSprite.height)) {
newPos.y = boundaries.bottom - activeSprite.height;
if (background)
background.moveDown(speed, framerate, canvasHeight);
}
if (!this.areFeetRestricted(background, newPos)) {
this.pos = newPos;
if (background)
this.verifyGateways(background, newPos);
}
}
};
Character.prototype.moveLeft = function (canvas) {
var background = canvas.background, framerate = canvas.framerate, boundaries = canvas.boundaries;
var _a = this, activeSprite = _a.activeSprite, currentPos = _a.pos, speed = _a.speed;
if (activeSprite && currentPos) {
var newPos = new CanvasPosition_1.default(currentPos.x - speed / framerate, currentPos.y);
if (!boundaries.xIsWithinBoundaries(newPos.x)) {
newPos.x = boundaries.left;
if (background)
background.moveLeft(speed, framerate);
}
if (!this.areFeetRestricted(background, newPos)) {
this.pos = newPos;
if (background)
this.verifyGateways(background, newPos);
}
}
};
Character.prototype.moveRight = function (canvas) {
var background = canvas.background, framerate = canvas.framerate, boundaries = canvas.boundaries, canvasWidth = canvas.width;
var _a = this, activeSprite = _a.activeSprite, currentPos = _a.pos, speed = _a.speed;
if (activeSprite && currentPos) {
var newPos = new CanvasPosition_1.default(currentPos.x + this.speed / framerate, currentPos.y);
if (!boundaries.xIsWithinBoundaries(newPos.x + activeSprite.width)) {
newPos.x = boundaries.right - activeSprite.width;
if (background)
background.moveRight(speed, framerate, canvasWidth);
}
if (!this.areFeetRestricted(background, newPos)) {
this.pos = newPos;
if (background)
this.verifyGateways(background, newPos);
}
}
};
Character.prototype.setActiveSprite = function (name) {
var found = false;
var newSprite = this.sprites.find(function (sprite) { return sprite.name === name; });
if (newSprite) {
this.activeSprite = newSprite;
found = true;
}
return found;
};
Character.prototype.areFeetRestricted = function (background, pos) {
if (background) {
var feetPositions = this.getFeetPosition(pos);
if (feetPositions) {
return background.isRestricted(feetPositions[0]) || background.isRestricted(feetPositions[1]);
}
}
return false;
};
Character.prototype.verifyGateways = function (background, pos) {
if (background) {
var feetPositions = this.getFeetPosition(pos);
if (feetPositions) {
background.checkGateways(feetPositions[0]);
background.checkGateways(feetPositions[1]);
}
}
return false;
};
Character.prototype.getFeetPosition = function (pos) {
var activeSprite = this.activeSprite;
if (activeSprite) {
var outerLeftFoot = new CanvasPosition_1.default(pos.x, pos.y + activeSprite.height);
var outerRightFoot = new CanvasPosition_1.default(pos.x + activeSprite.width, pos.y + activeSprite.height);
return [outerLeftFoot, outerRightFoot];
}
return null;
};
return Character;
}(CanvasImage_1.default));
exports.default = Character;
//# sourceMappingURL=Character.js.map