// ParticleSystem.js
// 粒子系统

class Particle {
  constructor(x, y) {
    this.x = x;
    this.y = y;
    this.velocityX = 0;
    this.velocityY = 0;
    this.accelerationX = 0;
    this.accelerationY = 0;
    this.life = 1.0;
    this.decay = 0.01;
    this.size = 1;
    this.color = '#ffffff';
    this.element = null;
  }

  update(deltaTime) {
    // 更新速度
    this.velocityX += this.accelerationX * (deltaTime / 1000);
    this.velocityY += this.accelerationY * (deltaTime / 1000);
    
    // 更新位置
    this.x += this.velocityX * (deltaTime / 1000);
    this.y += this.velocityY * (deltaTime / 1000);
    
    // 更新生命值
    this.life -= this.decay * (deltaTime / 16); // 标准化到60fps
    
    return this.life > 0;
  }

  render(parentElement) {
    if (!this.element) {
      this.element = document.createElement('div');
      this.element.style.position = 'absolute';
      this.element.style.pointerEvents = 'none';
      parentElement.appendChild(this.element);
    }
    
    this.element.style.left = `${this.x}px`;
    this.element.style.top = `${this.y}px`;
    this.element.style.width = `${this.size}px`;
    this.element.style.height = `${this.size}px`;
    this.element.style.backgroundColor = this.color;
    this.element.style.opacity = this.life;
  }

  destroy() {
    if (this.element && this.element.parentNode) {
      this.element.parentNode.removeChild(this.element);
    }
    this.element = null;
  }
}

class ParticleEmitter {
  constructor(options = {}) {
    this.x = options.x || 0;
    this.y = options.y || 0;
    this.particles = [];
    this.emitRate = options.emitRate || 10; // 每秒发射粒子数
    this.emitCount = 0;
    this.maxParticles = options.maxParticles || 100;
    this.active = true;
    
    // 粒子属性范围
    this.velocityXRange = options.velocityXRange || { min: -50, max: 50 };
    this.velocityYRange = options.velocityYRange || { min: -50, max: 50 };
    this.accelerationXRange = options.accelerationXRange || { min: 0, max: 0 };
    this.accelerationYRange = options.accelerationYRange || { min: 0, max: 0 };
    this.lifeRange = options.lifeRange || { min: 0.5, max: 2.0 };
    this.decayRange = options.decayRange || { min: 0.01, max: 0.05 };
    this.sizeRange = options.sizeRange || { min: 1, max: 5 };
    this.colorRange = options.colorRange || ['#ffffff'];
  }

  // 随机数生成器
  randomRange(min, max) {
    return Math.random() * (max - min) + min;
  }

  // 随机颜色选择
  randomColor() {
    return this.colorRange[Math.floor(Math.random() * this.colorRange.length)];
  }

  // 发射粒子
  emit() {
    if (!this.active || this.particles.length >= this.maxParticles) return;
    
    const particle = new Particle(this.x, this.y);
    
    // 设置随机属性
    particle.velocityX = this.randomRange(this.velocityXRange.min, this.velocityXRange.max);
    particle.velocityY = this.randomRange(this.velocityYRange.min, this.velocityYRange.max);
    particle.accelerationX = this.randomRange(this.accelerationXRange.min, this.accelerationXRange.max);
    particle.accelerationY = this.randomRange(this.accelerationYRange.min, this.accelerationYRange.max);
    particle.life = this.randomRange(this.lifeRange.min, this.lifeRange.max);
    particle.decay = this.randomRange(this.decayRange.min, this.decayRange.max);
    particle.size = this.randomRange(this.sizeRange.min, this.sizeRange.max);
    particle.color = this.randomColor();
    
    this.particles.push(particle);
  }

  // 更新发射器
  update(deltaTime, parentElement) {
    if (!this.active) return;
    
    // 发射新粒子
    this.emitCount += this.emitRate * (deltaTime / 1000);
    while (this.emitCount >= 1 && this.particles.length < this.maxParticles) {
      this.emit();
      this.emitCount--;
    }
    
    // 更新所有粒子
    this.particles = this.particles.filter(particle => {
      const alive = particle.update(deltaTime);
      if (alive) {
        particle.render(parentElement);
      } else {
        particle.destroy();
      }
      return alive;
    });
  }

  // 设置位置
  setPosition(x, y) {
    this.x = x;
    this.y = y;
  }

  // 启用/禁用发射器
  setActive(active) {
    this.active = active;
  }

  // 销毁发射器
  destroy() {
    this.particles.forEach(particle => particle.destroy());
    this.particles = [];
  }
}

class ParticleSystem {
  constructor(engine) {
    this.engine = engine;
    this.emitters = [];
  }

  // 创建发射器
  createEmitter(options = {}) {
    const emitter = new ParticleEmitter(options);
    this.emitters.push(emitter);
    return emitter;
  }

  // 更新所有发射器
  update(deltaTime) {
    this.emitters.forEach(emitter => {
      emitter.update(deltaTime, this.engine.stage);
    });
  }

  // 销毁所有发射器
  destroy() {
    this.emitters.forEach(emitter => emitter.destroy());
    this.emitters = [];
  }
}

export { Particle, ParticleEmitter, ParticleSystem };