// GameObject.js
// 游戏对象基类

class GameObject {
  constructor(x = 0, y = 0, width = 0, height = 0) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.rotation = 0;
    this.scaleX = 1;
    this.scaleY = 1;
    this.alpha = 1;
    this.visible = true;
    this.children = [];
    this.parent = null;
    this.tags = [];
    this.components = new Map();
  }

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

  // 设置尺寸
  setSize(width, height) {
    this.width = width;
    this.height = height;
    return this;
  }

  // 设置旋转角度
  setRotation(rotation) {
    this.rotation = rotation;
    return this;
  }

  // 设置缩放
  setScale(scaleX, scaleY = scaleX) {
    this.scaleX = scaleX;
    this.scaleY = scaleY;
    return this;
  }

  // 设置透明度
  setAlpha(alpha) {
    this.alpha = alpha;
    return this;
  }

  // 显示/隐藏
  setVisible(visible) {
    this.visible = visible;
    return this;
  }

  // 添加子对象
  addChild(child) {
    if (child.parent) {
      child.parent.removeChild(child);
    }
    child.parent = this;
    this.children.push(child);
    return this;
  }

  // 移除子对象
  removeChild(child) {
    const index = this.children.indexOf(child);
    if (index !== -1) {
      child.parent = null;
      this.children.splice(index, 1);
    }
    return this;
  }

  // 添加标签
  addTag(tag) {
    if (!this.tags.includes(tag)) {
      this.tags.push(tag);
    }
    return this;
  }

  // 移除标签
  removeTag(tag) {
    const index = this.tags.indexOf(tag);
    if (index !== -1) {
      this.tags.splice(index, 1);
    }
    return this;
  }

  // 检查是否有标签
  hasTag(tag) {
    return this.tags.includes(tag);
  }

  // 添加组件
  addComponent(name, component) {
    this.components.set(name, component);
    if (component.onAdd) {
      component.onAdd(this);
    }
    return this;
  }

  // 获取组件
  getComponent(name) {
    return this.components.get(name);
  }

  // 移除组件
  removeComponent(name) {
    const component = this.components.get(name);
    if (component && component.onRemove) {
      component.onRemove(this);
    }
    return this.components.delete(name);
  }

  // 更新方法（子类重写）
  update(deltaTime) {
    // 更新所有组件
    for (const [name, component] of this.components) {
      if (component.update) {
        component.update(deltaTime);
      }
    }
    
    // 更新所有子对象
    this.children.forEach(child => {
      if (child.update) {
        child.update(deltaTime);
      }
    });
  }

  // 渲染方法（子类重写）
  render() {
    // 渲染所有子对象
    this.children.forEach(child => {
      if (child.render) {
        child.render();
      }
    });
  }

  // 销毁对象
  destroy() {
    // 销毁所有组件
    for (const [name, component] of this.components) {
      if (component.onDestroy) {
        component.onDestroy(this);
      }
    }
    this.components.clear();
    
    // 销毁所有子对象
    this.children.forEach(child => {
      if (child.destroy) {
        child.destroy();
      }
    });
    this.children = [];
    
    // 从父对象中移除
    if (this.parent) {
      this.parent.removeChild(this);
    }
  }
}

export default GameObject;
export { GameObject };