import { world, system } from "@minecraft/server";

const PARTICLE_ID = "pa:rayos_de_luz";

// límites del área (XZ)
const MIN = 20000;
const MAX = 30000;

// distancia mínima y máxima respecto al jugador
const MIN_DISTANCE = 8;
const MAX_DISTANCE = 22;

// altura relativa
const MIN_HEIGHT = 6;
const MAX_HEIGHT = 14;

// cantidad por ciclo
const MIN_PARTICLES = 6;
const MAX_PARTICLES = 9;

function isInsideZone(x, z) {
  return (
    x >= MIN && x <= MAX &&
    z >= MIN && z <= MAX
  );
}

function randomBetween(min, max) {
  return min + Math.random() * (max - min);
}

system.runInterval(() => {
  for (const player of world.getPlayers()) {

    // solo The End (ajusta si hace falta)
    if (player.dimension.id !== "minecraft:the_end") continue;

    const { x, y, z } = player.location;

    if (!isInsideZone(x, z)) continue;

    const count = Math.floor(randomBetween(MIN_PARTICLES, MAX_PARTICLES + 1));

    for (let i = 0; i < count; i++) {

      // ángulo y distancia aleatoria
      const angle = Math.random() * Math.PI * 2;
      const distance = randomBetween(MIN_DISTANCE, MAX_DISTANCE);

      const px = x + Math.cos(angle) * distance;
      const pz = z + Math.sin(angle) * distance;
      const py = y + randomBetween(MIN_HEIGHT, MAX_HEIGHT);

      player.dimension.spawnParticle(
        PARTICLE_ID,
        { x: px, y: py, z: pz }
      );
    }
  }
}, 20); // cada segundo