import { EPhysics2DDrawFlags } from 'cc';
import { _decorator, Camera, Component, math, Node, PhysicsSystem2D, screen, Sprite, UITransform, view } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('GameScene1')
export class GameScene1 extends Component {
  @property({
    type: Camera
  })
  mainCamera: Camera

  @property({
    type: Camera
  })
  uiCamera: Camera

  @property({
    type: Sprite
  })
  player: Sprite

  maxY: number = 0;
  minY: number = 0;

  start() {
    // 屏幕可视区域的大小
    const { width, height } = screen.windowSize;
    const { width: dW, height: dH} = view.getDesignResolutionSize();
    const { width: vW, height: vH } = view.getVisibleSize();
    console.log(`屏幕分辨率 ${width} x ${height}`);
    console.log(`设计稿分辨率 ${dW} x ${dH}`)
    console.log(`可视区域 ${vW} x ${vH}`)
    const space = (dH - vH) * 0.5;
    this.maxY = space;
    this.minY = -space;

    PhysicsSystem2D.instance.debugDrawFlags = EPhysics2DDrawFlags.Shape
  }

  update(deltaTime: number) {
    const cameraPos = this.mainCamera.node.getPosition();
    const playerY = this.player.node.position.y;
    if (playerY < 0) {
      cameraPos.y = Math.max(playerY, this.minY);
    } else {
      cameraPos.y = Math.min(playerY, this.maxY);
    }
    this.mainCamera.node.setPosition(cameraPos);
  }
}

