import { Hex } from "../../Util/index.js";
import { SimProjectile } from "../index.js";
export class SimTower {
    _lastProjectileStep = -1;
    _lastAoeStep = -1;
    _gdProjectileEffect;
    _projectileRange = -1;
    _projectileRate = -1;
    _projectileSize = -1;
    _gdAoeEffect;
    _aoeRange = -1;
    _aoeRate = -1;
    _gdTower;
    _position;
    _currentProjectileTarget = -1;
    _simLevel;
    _index;
    _gdRoot;
    constructor(gdRoot, level, index, hex) {
        this._gdRoot = gdRoot;
        this._simLevel = level;
        this._position = Hex.toWorld(hex);
        this._index = index;
        const data = this._gdRoot.towers[index];
        this._gdProjectileEffect = data.projectileEffect;
        this._projectileRange = data.projectileRange;
        this._projectileRate = data.projectileRate;
        this._projectileSize = data.projectileSize;
        this._gdAoeEffect = data.aoeEffect;
        this._aoeRange = data.aoeRange;
        this._aoeRate = data.aoeRate;
        this._gdTower = data;
    }
    get index() {
        return this._index;
    }
    fireIfAble() {
        const level = this._simLevel;
        const currentStep = this._simLevel.currentStep;
        if (this.canFireProjectile() && this._gdProjectileEffect != null) {
            level.simProjectiles.push(new SimProjectile(level, this._gdProjectileEffect, this._position, this.pickProjectileTarget(), this._projectileSize));
            this._lastProjectileStep = currentStep;
        }
        if (this.canFireAoe()) {
            // TODO
        }
    }
    canFireProjectile() {
        if (this._gdProjectileEffect == null)
            return false;
        const currentStep = this._simLevel.currentStep;
        if (this._lastProjectileStep == -1 || this._lastProjectileStep + this._projectileRate < currentStep) {
            return this.isEnemyInRange(this._projectileRange);
        }
        return false;
    }
    canFireAoe() {
        const level = this._simLevel;
        const currentStep = level.currentStep;
        if (this._gdAoeEffect != null) {
            if (this._lastAoeStep == -1 || this._lastAoeStep + this._aoeRate < currentStep) {
                return this.isEnemyInRange(this._aoeRange);
            }
        }
        return false;
    }
    pickProjectileTarget() {
        if (this._currentProjectileTarget != -1) {
            return this._currentProjectileTarget;
        }
        let minLength = -1;
        let candidate = -1;
        const level = this._simLevel;
        for (let idx = 0; idx < level.simEnemies.length; idx++) {
            const enemy = level.simEnemies[idx];
            const delta = enemy.currentPathPosition.subtract(this._position);
            const length = delta.magnitude();
            if (enemy.path != null && length < this._projectileRange) {
                const pathLength = enemy.path.length - enemy.currentPathIndex;
                if (minLength == -1 || pathLength < minLength) {
                    candidate = idx;
                    minLength = pathLength;
                }
            }
        }
        return candidate;
    }
    isEnemyInRange(range) {
        const level = this._simLevel;
        for (const idx in level.simEnemies) {
            const enemy = level.simEnemies[idx];
            const delta = enemy.currentPathPosition.subtract(this._position);
            const length = delta.magnitude();
            if (length < range) {
                return true;
            }
        }
    }
}
//# sourceMappingURL=SimTower.js.map