UNPKG

@fuwu-yuan/bgew

Version:

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

225 lines (224 loc) 8.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Container = void 0; const entity_1 = require("../entity"); class Container extends entity_1.Entity { constructor(x, y, width, height, entities = []) { super(x, y, width, height); this._clicked = false; this._entities = []; this._entities = entities; this.onMouseEvent("mousedown", this.onMouseDown.bind(this)); this.onMouseEvent("mouseup", this.onMouseUp.bind(this)); this.onMouseEvent("mouseenter", this.onMouseEnter.bind(this)); this.onMouseEvent("mouseleave", this.onMouseLeave.bind(this)); } init(board) { super.init(board); for (const entity of this.entities) { entity.init(board); } } onMouseDown(event) { this._clicked = true; } onMouseUp(event) { this._clicked = false; } onMouseEnter(event) { //this.board?.changeCursor("pointer"); } onMouseLeave(event) { //this.board?.changeCursor("default"); } intersect(x, y, event = null, depth = 1) { var _a, _b, _c, _d, _e, _f, _g, _h, _j; // Rotate context from entity center (_a = this.board) === null || _a === void 0 ? void 0 : _a.ctx.translate(this.x + this.width / 2, this.y + this.height / 2); (_b = this.board) === null || _b === void 0 ? void 0 : _b.ctx.rotate(this.rotate); (_c = this.board) === null || _c === void 0 ? void 0 : _c.ctx.translate((this.x + this.width / 2) * -1, (this.y + this.height / 2) * -1); let result = ((_d = this.board) === null || _d === void 0 ? void 0 : _d.ctx.isPointInPath(this.getPath2D(), x, y)) || false; if (result) { (_e = this.board) === null || _e === void 0 ? void 0 : _e.ctx.translate(this.x, this.y); this.entities.forEach((entity) => { if (entity.disabled) return; if (entity.intersect(x, y, event, depth + 1)) { if (event && event.type === "mousemove") { if (!entity.hovered) { entity.hovered = true; entity.dispatcher.dispatch("mouseenter", new MouseEvent("mouseenter", event), x, y); } } if (event && event.type === "click") { if (!entity.focus) { entity.focus = true; } } if (event) { entity.dispatcher.dispatch(event.type, event, x, y); } } else { if (event && event.type === "mousemove") { if (entity.hovered) { entity.hovered = false; entity.dispatcher.dispatch("mouseleave", new MouseEvent("mouseleave", event), x, y); } } if (event && event.type === "click") { if (entity.focus) { entity.focus = false; } } } }); (_f = this.board) === null || _f === void 0 ? void 0 : _f.ctx.translate(this.x * -1, this.y * -1); } // UNDO Rotate context from entity center (_g = this.board) === null || _g === void 0 ? void 0 : _g.ctx.translate(this.x + this.width / 2, this.y + this.height / 2); (_h = this.board) === null || _h === void 0 ? void 0 : _h.ctx.rotate(this.rotate * -1); (_j = this.board) === null || _j === void 0 ? void 0 : _j.ctx.translate((this.x + this.width / 2) * -1, (this.y + this.height / 2) * -1); return result; } addEntity(entity) { if (this.board && !entity.board) { entity.init(this.board); } entity.parent = this; this._entities.push(entity); } addEntities(entities) { for (const entity of entities) { this.addEntity(entity); } } removeEntity(entity) { var _a, _b; let index = this._entities.indexOf(entity); if (index > -1) { if (this.entities[index] instanceof Container) { this.entities[index].removeEntities(this.entities[index].entities); } this._entities.splice(index, 1); } if ((_a = this.board) === null || _a === void 0 ? void 0 : _a.collisionSystem) { (_b = this.board) === null || _b === void 0 ? void 0 : _b.collisionSystem.remove(entity.body); } } removeEntities(entities) { for (const entity of entities) { this.removeEntity(entity); } } getEntitiesIn(x1, y1, x2, y2) { let entities = []; for (const entity of this.entities) { if (entity instanceof Container) { entities = entities.concat(entity.getEntitiesIn(x1, y1, x2, y2)); } else if (entity.absX < x2 && entity.absX + entity.width > x1 && entity.absY < y2 && entity.height + entity.absY > y1) { entities.push(entity); } } return entities; } /** * Find an entity by id * @param id * @param recursive search recursively in entity that can contains other entities (like Entities.Container) * @return Entity found entity with the given id or undefined if not found */ findEntity(id, recursive = false) { let entity = this.entities.find((entity) => { return entity.id === id; }); if (typeof entity === "undefined" && recursive) { for (const e of this.entities) { if (e instanceof Container) { entity = e.findEntity(id, recursive); if (typeof entity !== 'undefined') break; } } } return entity; } clear() { this._entities = []; } get clicked() { return this._clicked; } set clicked(clicked) { this._clicked = clicked; } draw(ctx) { if (this.board) { ctx.translate(this.x * this.board.scale, this.y * this.board.scale); } this.entities.forEach((entity) => { if (entity.visible && this.board) { // Reset style this.board.resetStyles(); // Save context ctx.save(); // Rotate context from entity center if (entity.rotate !== 0) { ctx.translate(entity.x * this.board.scale + entity.width * this.board.scale / 2, entity.y * this.board.scale + entity.height * this.board.scale / 2); ctx.rotate(entity.rotate); 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(ctx); // Restore context ctx.restore(); } }); } countEntities() { let count = 0; count = this.entities.length; for (const entity of this.entities) { if (entity instanceof Container) { count += entity.countEntities(); } } return count; } set board(value) { super.board = value; for (const entity of this.entities) { if (this.board && !entity.board) { entity.board = this.board; if (this.board.collisionSystem) { this.board.collisionSystem.insert(entity.body); } } } } get board() { return super.board; } update(delta) { super.update(delta); for (const entity of this.entities) { entity.update(delta); } } checkCollisions() { super.checkCollisions(); for (const entity of this.entities) { entity.checkCollisions(); } } /********************* * Getters & Setters * *********************/ get entities() { return this._entities; } set entities(value) { this._entities = value; } } exports.Container = Container;