import { system, world } from "@minecraft/server"
import { GeneralUtils } from "./utils.js"

// ----- Misc -----
import { clearPlayerCameraTick, addCustomCameras } from "camera.js"
// import "missile.js"

// ----- Vehicles -----
import { helicopterEntityTick } from "vehicle/helicopter.js"
import { groundEntityTick, groundHandleEntityLoad } from "vehicle/ground.js"
import { planeEntityTick } from "vehicle/plane.js"
import { turretEntityTick, turretHandleEntitySpawn } from "vehicle/turret.js"
import { colliderEntityTick, colliderSpawner } from "vehicle/naval.js"

system.runInterval(() => {
    for (const dimensionName of GeneralUtils.world.dimensions) {
        const dimension = world.getDimension(dimensionName);

        for (const entity of dimension.getEntities()) {
            const rideable = entity.getComponent("rideable");
            if (rideable) {
                addCustomCameras(entity, rideable);
                planeEntityTick(entity, rideable);
                groundEntityTick(entity, rideable, dimension);
                turretEntityTick(entity, rideable);
                helicopterEntityTick(entity, rideable);
            }

            colliderEntityTick(entity);
        }
    }

    for (const player of world.getAllPlayers()) {
        clearPlayerCameraTick(player);
    }
}, 1)

world.afterEvents.entitySpawn.subscribe(turretHandleEntitySpawn);
world.afterEvents.entitySpawn.subscribe(colliderSpawner);
world.afterEvents.entityLoad.subscribe(groundHandleEntityLoad);