All files / src/components scene.js

100% Statements 70/70
100% Branches 46/46
100% Functions 26/26
100% Lines 67/67

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127  1x 1x 1x 1x 1x     36x     3x 1x 2x     41x 1x   40x     52x 2x   50x     3x 1x   2x     57x 57x 57x     54x 1x   54x 54x 54x 54x     1x 1x       7x 1x   6x 1x   6x 6x     30x 29x 1x       3x           40x 40x     2x 2x     6x       5x 1x 4x     8x     10x 10x 9x 9x 4x 4x 4x 4x       9x   5x 5x 5x 4x   5x       4x 8x 4x   8x       1x  
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Scene = void 0;
const surfaceController_1 = require("../controller/surface/surfaceController");
const exceptions_1 = require("../environment/exceptions");
const entityBase_1 = require("./entities/base/entityBase");
class Scene extends entityBase_1.EntityBase {
    get isLoop() {
        return this._isLoop;
    }
    set isLoop(value) {
        if (this._isLoop === value)
            return;
        this._isLoop = value;
    }
    get assets() {
        if (!this._assets) {
            throw new exceptions_1.NotInitializedException("Scene");
        }
        return this._assets;
    }
    get surface() {
        if (!this._surface) {
            throw new exceptions_1.NotInitializedException("Scene");
        }
        return this._surface;
    }
    get input() {
        if (!this._input) {
            throw new exceptions_1.NotInitializedException("Scene");
        }
        return this._input;
    }
    constructor(assets) {
        super();
        this._isLoop = true;
        this._assets = assets;
    }
    initialize(surface, input) {
        if (this._surface) {
            this._surface.removeEventListener(surfaceController_1.SurfaceController.surfaceChangeEvent, this.refresh);
        }
        this._input = input;
        this._surface = surface;
        this._surface.addEventListener(surfaceController_1.SurfaceController.surfaceChangeEvent, this.refresh);
        this.onInitialized();
    }
    destroy() {
        super.destroy();
        this.surface.removeEventListener(surfaceController_1.SurfaceController.surfaceChangeEvent, this.refresh);
    }
    update(frameTime) {
        var _a;
        if (!this.isActive || !this.isVisible) {
            return;
        }
        if (!this.isLoop) {
            this.isActive = false;
        }
        (_a = this._surface) === null || _a === void 0 ? void 0 : _a.clear();
        this.updateEntities(this.children, frameTime);
    }
    refresh() {
        if (this.isLoop)
            return;
        this.isActive = true;
    }
    invalidate() {
        var _a;
        (_a = this._input) === null || _a === void 0 ? void 0 : _a.refresh();
    }
    onInitialized() {
        //initialize scene
    }
    initializeChild(child) {
        super.initializeChild(child);
        child.initialize(this.assets, this.surface);
    }
    changeCursor(style) {
        const pointer = this.input.get("Pointer");
        pointer === null || pointer === void 0 ? void 0 : pointer.setStyle(style);
    }
    isActiveChanged(value) {
        this.children.forEach(child => child.isActive = value);
    }
    isVisibleChanged(value) {
        var _a;
        if (value)
            return;
        (_a = this._surface) === null || _a === void 0 ? void 0 : _a.clear();
    }
    isEnabledChanged(value) {
        this.children.forEach(child => child.isEnabled = value);
    }
    updateEntities(entities, frameTime) {
        const colliders = new Array();
        const context2d = this.surface.context2d;
        const zGroups = this.getGroups(entities, colliders);
        zGroups.forEach(zGroup => zGroup.forEach(entity => {
            entity.update(frameTime);
            entity.collision(...this.getCollisions(entity, colliders));
            entity.draw(context2d);
            this.updateEntities(entity.children, frameTime);
        }));
    }
    getGroups(entities, colliders) {
        return entities.reduce((group, entity) => {
            var _a;
            group[entity.zIndex] = (_a = group[entity.zIndex]) !== null && _a !== void 0 ? _a : [];
            group[entity.zIndex].push(entity);
            if (entity.hitBox) {
                colliders.push(entity);
            }
            return group;
        }, []);
    }
    getCollisions(entity, colliders) {
        return colliders.reduce((array, sibling) => {
            if (sibling !== entity && entity.hitBox.intersectsWith(sibling.hitBox)) {
                array.push(sibling);
            }
            return array;
        }, []);
    }
}
exports.Scene = Scene;