﻿

/*
 * @Author       : arraon
 * @Date         : 2023-07-03 15:01:25
 * @LastEditors  : arraon
 * @LastEditTime : 2023-07-13 18:16:41
 * @Description  : 
 */
@Core.Class
export default class TwoFinger extends Core.Script {
    private _twoFinger: TwoFingerToucher;
    protected onStart(): void {
        if (SystemUtil.isServer()) {
            return;
        }
        this.initTouch();
    }

    private async initTouch() {
        await Gameplay.asyncGetCurrentPlayer();
        this._twoFinger = new TwoFingerToucher(new Gameplay.TouchInput());
    }
}

export class TwoFingerToucher {
    // Touch
    protected camera: Gameplay.CameraSystem;
    protected targetArmLength: number;
    protected toucher: Gameplay.TouchInput;
    protected touchNum = 0;
    protected doubleTouchDistance = 0;
    protected isHitPet: boolean = false;

    public constructor(toucher: Gameplay.TouchInput) {
        this.toucher = toucher;
        this.camera = Gameplay.getCurrentPlayer().character.cameraSystem;
        toucher.setPlayerController();
        this.targetArmLength = this.camera.targetArmLength;
        toucher.onTouch.add((index: number, loc: Type.Vector2, touchType: Gameplay.TouchInputType) => {
            switch (touchType) {
                case Gameplay.TouchInputType.TouchBegin:
                    this.touchNum = index + 1;
                    this.isHitPet = false;
                    if (this.touchNum == 2) {
                        let touchVectorArray = this.toucher.getTouchVectorArray();
                        this.doubleTouchDistance = Vector2.distance(touchVectorArray[0], touchVectorArray[1]);
                    }
                    break;
                case Gameplay.TouchInputType.TouchEnd:
                    this.touchNum = index;
                    break;
                case Gameplay.TouchInputType.TouchMove:
                    if (this.touchNum == 2) {
                        // 限制摄像机拉近拉远范围
                        let touchVectorArray = this.toucher.getTouchVectorArray();
                        let distance = Vector2.distance(touchVectorArray[0], touchVectorArray[1]);
                        let delta = distance - this.doubleTouchDistance;
                        this.targetArmLength -= delta;
                        this.targetArmLength = MathUtil.clamp(this.targetArmLength, 100, 300);
                        this.camera.targetArmLength = this.targetArmLength;
                        this.doubleTouchDistance = distance;
                    }
            }
        });
        InputUtil.onKeyDown(Keys.MouseScrollDown, () => {
            this.targetArmLength += 10;
            this.targetArmLength = MathUtil.clamp(this.targetArmLength, 100, 300);
            this.camera.targetArmLength = this.targetArmLength;
        })
        InputUtil.onKeyDown(Keys.MouseScrollUp, () => {
            this.targetArmLength -= 10;
            this.targetArmLength = MathUtil.clamp(this.targetArmLength, 100, 300);
            this.camera.targetArmLength = this.targetArmLength;
        })
    }
}