import { _decorator, Component, Node, Vec3, UITransform, math } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('CameraFollow2D')
export class CameraFollow2D extends Component {
  @property(Node)
  target: Node = null!;   // 要跟随的角色

  @property(Node)
  map: Node = null!;      // 地图节点（需要有 UITransform 才能获取大小）

  @property
  offset: Vec3 = new Vec3(0, 0, 1000);  // 相机与角色的偏移（Z 轴拉远）

  @property
  smoothSpeed: number = 5; // 平滑跟随速度，越大越快

  private _currentPos: Vec3 = new Vec3();
  private _mapSize: Vec3 = new Vec3();

  onLoad() {
    if (this.map) {
      const ui = this.map.getComponent(UITransform);
      if (ui) {
        this._mapSize.set(ui.width, ui.height, 0);
      }
    }
  }

  update(deltaTime: number) {
    if (!this.target) return;

    // 目标位置
    const targetPos = this.target.getWorldPosition();
    const desiredPos = new Vec3(
      targetPos.x + this.offset.x,
      targetPos.y + this.offset.y,
      this.offset.z   // 相机保持固定 Z
    );

    // 平滑跟随
    Vec3.lerp(this._currentPos, this.node.getWorldPosition(), desiredPos, deltaTime * this.smoothSpeed);

    // 限制相机不超出地图
    if (this.map) {
      const halfW = this._mapSize.x / 2;
      const halfH = this._mapSize.y / 2;

      // 相机视口一半宽高（取 UITransform 尺寸的一半）
      const camUI = this.getComponent(UITransform);
      let viewW = camUI ? camUI.width / 2 : 0;
      let viewH = camUI ? camUI.height / 2 : 0;

      this._currentPos.x = math.clamp(this._currentPos.x, -halfW + viewW, halfW - viewW);
      this._currentPos.y = math.clamp(this._currentPos.y, -halfH + viewH, halfH - viewH);
    }

    // 更新相机位置
    this.node.setWorldPosition(this._currentPos);
  }
}
