var _a, _b;
import * as mc from "@minecraft/server";
import * as utils from "../utils/index";
const DURATION = 5 * mc.TicksPerSecond;
const SHOT_COUNT = 3;
const SHOT_COOLDOWN = 0.25 * mc.TicksPerSecond;
export async function iglooAbility(player) {
    utils.mclog(`igloo`);
    player.dimension.spawnEntity("ldz:igloo_canon", player.location.center());
}
class IglooCanon extends utils.Actor {
    constructor() {
        super(...arguments);
        this.despawnTime = 0;
    }
    onConstructed() {
        IglooCanon.registry.set(this.id, this);
        utils.despawnOnReload(this);
        this.updateDespawnTime();
        this.playSoundAt("ability.igloo.spawn");
        this.addActorComponent(ParticleRangedAttackComponent, {
            shoot_animation: "animation.igloo_canon.shoot",
            particle_name: "ldz:ability_igloo",
            shoot_sound: "ability.igloo.shoot",
            cooldown: 20,
            damage: 15,
        });
        utils.waitForLoopEvent(20, () => {
            if (this.isValid()) {
                if (mc.system.currentTick >= this.despawnTime) {
                    try {
                        this.dimension.spawnParticle("ldz:spawn_effect_small", this.location);
                        this.playSoundAt("random.fizz");
                    }
                    catch (error) { }
                    this.remove();
                    return true;
                }
                return false;
            }
            return true;
        });
    }
    destructor() {
        super.destructor();
        IglooCanon.registry.delete(this.id);
    }
    updateDespawnTime() {
        this.despawnTime = mc.system.currentTick + DURATION;
    }
}
_a = IglooCanon;
IglooCanon.registry = new Map();
(() => {
    utils.afterEvents.scriptEventRecieve.subscribe("ldz:igloo_has_target", event => {
        var _c, _d, _e;
        (_e = _a.registry.get((_d = (_c = event.sourceEntity) === null || _c === void 0 ? void 0 : _c.id) !== null && _d !== void 0 ? _d : "")) === null || _e === void 0 ? void 0 : _e.updateDespawnTime();
    });
})();
class ParticleRangedAttackComponent extends utils.ActorComponent {
    constructor() {
        super(...arguments);
        this.lastShot = mc.system.currentTick;
    }
    get InCooldown() {
        return this.lastShot + this.options.cooldown > mc.system.currentTick;
    }
    onComponentAdded() {
        ParticleRangedAttackComponent.registry.set(this.actor.id, this);
    }
    onComponentRemoved() {
        var _c;
        ParticleRangedAttackComponent.registry.delete(this.actor.id);
        if (((_c = ParticleRangedAttackComponent.listener) === null || _c === void 0 ? void 0 : _c.id) === this.actor.id) {
            ParticleRangedAttackComponent.listener = undefined;
        }
    }
    async attack(target) {
        if (this.InCooldown)
            return;
        for (let index = 0; index < SHOT_COUNT; index++) {
            this.fireShot(target);
            await utils.waitForTicks(SHOT_COOLDOWN);
        }
    }
    fireShot(target) {
        if (!target.isValid() || !this.actor.isValid())
            return;
        this.lastShot = mc.system.currentTick;
        const travelTime = target.location.distance(this.actor.location) / 20;
        this.actor.teleport(this.actor.location, { facingLocation: target.location });
        this.actor.playSoundAt(this.options.shoot_sound, { offset: utils.Vector3.UP });
        this.actor.playAnimation(this.options.shoot_animation, { controller: "ctrl.shoot" });
        const direction = target.getHeadLocation().subtract(this.actor.location.up(1)).normalize();
        const molang = new mc.MolangVariableMap();
        molang.setFloat("variable.lifetime", travelTime);
        molang.setSpeedAndDirection("variable.motion", 20, direction);
        this.actor.dimension.spawnParticle(this.options.particle_name, this.actor.location.up(1), molang);
        utils.waitForEvent(travelTime, () => {
            try {
                target.applyDamage(this.options.damage, { cause: mc.EntityDamageCause.entityAttack, damagingEntity: this.actor.UnsafeEntity });
            }
            catch (_error) {
                // Do nothing
            }
        });
    }
}
_b = ParticleRangedAttackComponent;
ParticleRangedAttackComponent.registry = new Map();
(() => {
    utils.afterEvents.scriptEventRecieve.subscribe("ldz:igloo_attack", event => {
        var _c;
        if (event.data === "attacker" && event.sourceEntity) {
            const component = _b.registry.get(event.sourceEntity.id);
            if (component) {
                _b.listener = component;
            }
        }
        else if (event.data === "victim" && event.sourceEntity) {
            if (!((_c = _b.listener) === null || _c === void 0 ? void 0 : _c.actor.isValid())) {
                utils.mcerror(`No listener found for victim`, "ParticleRangedAttackComponent");
                return;
            }
            _b.listener.attack(event.sourceEntity);
        }
    });
})();
utils.Actor.makeActors({ type: "ldz:igloo_canon" }, IglooCanon);
