UNPKG

@fuwu-yuan/bgew

Version:

Baguette Game Engine Web is a french 2D Web game engine

101 lines (100 loc) 3.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GameStep = void 0; const camera_1 = require("./camera"); const timer_1 = require("./timer"); class GameStep { constructor(board) { this._timers = []; this._board = board; this._camera = new camera_1.Camera(); } get board() { return this._board; } onNetworkMessage(msg) { } onPlayerJoin(msg) { } onPlayerLeave(msg) { } onConnectionClosed() { } /** * Game update loop */ update(delta) { if (this.board.canvas && this.board.ctx) { this.board.entities.forEach(function (entity) { entity.update(delta); }); } for (const timer of this._timers) { timer.update(delta); } } checkCollisions() { if (this.board.canvas && this.board.ctx) { this.board.entities.forEach(function (entity) { entity.checkCollisions(); }); } } /** * Game draw loop */ draw() { var _a, _b, _c; if (this.board.canvas) { // Clear canvas this.board.clear(); if (this.camera) { this.board.ctx.translate(-this.camera.x, -this.camera.y); } else { console.error("Missing camera"); } this.board.entities.forEach((entity) => { if (entity.visible) { // Reset style this.board.resetStyles(); // Save context this.board.ctx.save(); // Rotate context from entity center if (entity.rotate !== 0) { this.board.ctx.translate(entity.x * this.board.scale + entity.width * this.board.scale / 2, entity.y * this.board.scale + entity.height * this.board.scale / 2); this.board.ctx.rotate(entity.rotate); this.board.ctx.translate((entity.x * this.board.scale + entity.width * this.board.scale / 2) * -1, (entity.y * this.board.scale + entity.height * this.board.scale / 2) * -1); } // Draw entity entity.draw(this.board.ctx); // Restore context this.board.ctx.restore(); } }); if (((_a = this.board) === null || _a === void 0 ? void 0 : _a.debug.collision) && ((_b = this.board) === null || _b === void 0 ? void 0 : _b.collisionSystem)) { this.board.ctx.strokeStyle = '#FF0000'; this.board.ctx.beginPath(); (_c = this.board) === null || _c === void 0 ? void 0 : _c.collisionSystem.draw(this.board.ctx); this.board.ctx.stroke(); } } } get camera() { return this._camera; } set camera(value) { this._camera = value; } addTimer(time, end, repeat = true) { let timer = new timer_1.Timer(time, end, () => { this.removeTimer(timer); }); timer.repeat = repeat; this._timers.push(timer); return timer; } removeTimer(timer) { const index = this._timers.indexOf(timer, 0); if (index > -1) { this._timers.splice(index, 1); } } } exports.GameStep = GameStep;