All files / src/components/entities entity.js

100% Statements 88/88
100% Branches 34/34
92.3% Functions 24/26
100% Lines 88/88

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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162  1x 1x 1x 1x 1x     13x 1x   12x     187x 1x   186x     77x 77x 77x     5x 4x   5x 5x 4x 4x       4x       7x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x   6x 6x 6x 1x 1x 1x 1x       6x 1x   5x 5x 5x     20x 20x     1x 1x     3x 3x     8x 8x 8x 8x 8x 8x 8x 8x     64x 7x 7x 7x 7x   7x   57x 57x 57x 57x 57x 57x 57x     4x 4x 3x 3x 3x 2x     4x     1x 1x     1x 1x     1x 1x     3x 3x                                                     1x  
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Entity = void 0;
const bounds2_1 = require("../../models/bounds2");
const entityState_1 = require("./base/entityState");
const exceptions_1 = require("../../environment/exceptions");
class Entity extends entityState_1.EntityState {
    get assets() {
        if (!this._assets) {
            throw new exceptions_1.NotInitializedException("Entity");
        }
        return this._assets;
    }
    get surface() {
        if (!this._surface) {
            throw new exceptions_1.NotInitializedException("Entity");
        }
        return this._surface;
    }
    initialize(assets, surface) {
        this._assets = assets;
        this._surface = surface;
        this.onInitialized();
    }
    update(frameTime) {
        if (this.hitBox) {
            this._lastHitBox = this.setHitBox(this._hitBoxOffsetX, this._hitBoxOffsetY);
        }
        this.onUpdate(frameTime);
        if (this.hitBox) {
            const newHitBox = this.setHitBox(this._hitBoxOffsetX, this._hitBoxOffsetY);
            this.hitBox = this._lastHitBox.combine(newHitBox);
        }
    }
    collision(...colliders) {
        this.onCollision(...colliders);
    }
    draw(context2d) {
        var _a;
        if (!this.isVisible)
            return;
        const translateX = this.x + this.width * this.transformOrigin.x;
        const translateY = this.y + this.height * this.transformOrigin.y;
        context2d.save();
        context2d.globalAlpha = this.opacity;
        context2d.translate(translateX, translateY);
        context2d.scale(this.scale, this.scale);
        context2d.rotate(this.rotation * Math.PI / 180);
        context2d.translate(-translateX, -translateY);
        if ((_a = this._imageSource) === null || _a === void 0 ? void 0 : _a.image) {
            context2d.drawImage(this._imageSource.image, this.x, this.y, this.width, this.height);
        }
        this.onDraw(context2d);
        context2d.restore();
        if (this._drawHitBox) {
            context2d.save();
            context2d.strokeStyle = "red";
            context2d.strokeRect(this.hitBox.x, this.hitBox.y, this.hitBox.width, this.hitBox.height);
            context2d.restore();
        }
    }
    getCalculatedValue(frameTime, value) {
        if (!this.surface || frameTime.fps <= 0) {
            return 0;
        }
        const framerateFactor = value / frameTime.fps;
        const frameRate = this.surface.settings.frameRate;
        return framerateFactor * frameRate;
    }
    hitTest(position) {
        position = this.getLocalPositionBounds(position);
        return this.hitBox.contains(position);
    }
    destroy() {
        super.destroy();
        this.onDestroy();
    }
    initializeChild(child) {
        super.initializeChild(child);
        child.initialize(this.assets, this.surface);
    }
    setHitBox(offsetX, offsetY) {
        this._hitBoxOffsetX = offsetX;
        this._hitBoxOffsetY = offsetY;
        this.hitBox = new bounds2_1.Bounds2();
        this.hitBox.x = this.x + this._hitBoxOffsetX;
        this.hitBox.y = this.y + this._hitBoxOffsetY;
        this.hitBox.width = this.width - (this._hitBoxOffsetX * 2);
        this.hitBox.height = this.height - (this._hitBoxOffsetY * 2);
        return this.hitBox;
    }
    getLocalPositionBounds(clientPositionBounds) {
        if (Array.isArray(clientPositionBounds)) {
            const localPositionBoundsArray = new Array();
            clientPositionBounds.forEach(pos => {
                const localPositionBounds = this.getLocalPositionBounds(pos);
                localPositionBoundsArray.push(localPositionBounds);
            });
            return localPositionBoundsArray;
        }
        const clientBounds = this.surface.clientBounds;
        const scaleX = this.surface.width / clientBounds.width;
        const scaleY = this.surface.height / clientBounds.height;
        const position = new bounds2_1.Bounds2();
        position.x = (clientPositionBounds.x - clientBounds.x) * scaleX;
        position.y = (clientPositionBounds.y - clientBounds.y) * scaleY;
        return position;
    }
    setAudio(sound, volume = 1, loop = false) {
        this._audioSource = this.assets.getAudio(sound);
        if (this._audioSource) {
            this._audioSource.loop = loop;
            this._audioSource.volume = !this.assets.isAudioMuted ? volume : 0;
            if (!this._audioSource.loop || !this._audioSource.isPlaying) {
                this._audioSource.play();
            }
        }
        return this._audioSource;
    }
    setImage(image) {
        this._imageSource = this.assets.getImage(image);
        return this._imageSource;
    }
    isActiveChanged(value) {
        super.isActiveChanged(value);
        this.onIsActiveChanged(value);
    }
    isVisibleChanged(value) {
        super.isVisibleChanged(value);
        this.onVisibleChanged(this.isEnabled);
    }
    isEnabledChanged(value) {
        super.isEnabledChanged(value);
        this.onEnabledChanged(value);
    }
    onInitialized() {
        //initialize entity
    }
    onIsActiveChanged(value) {
        //handle isActive changed
    }
    onVisibleChanged(value) {
        //handle visible changed
    }
    onEnabledChanged(value) {
        //handle enabled changed
    }
    onUpdate(frameTime) {
        //update entity
    }
    onCollision(...colliders) {
        //update collision
    }
    onDraw(context2d) {
        //draw entity
    }
    onDestroy() {
        //destroy entity
    }
}
exports.Entity = Entity;