﻿
@Core.Class
export default class JumpSetting extends Core.Script {
    /**UI面板 */
    private _jumpSettingPanel: UI.UserWidget
    /**UI面板的根画布 */
    private _rootCanvas: UI.Canvas

    /**地面最大速度设置组 */
    private _MaxSpeedGroup: ControlGroup

    /**最大加速度设置组 */
    private _AddSpeedGroup: ControlGroup

    /**最大跳跃高度设置组 */
    private _JumpHeightGroup: ControlGroup

    /**下落控制设置组 */
    private _FallControlGroup: ControlGroup

    /**最大下落速度设置组 */
    private _FallSpeedGroup: ControlGroup

    /**重力倍率设置组 */
    private _GravityGroup: ControlGroup

    /**下落制动速率设置组 */
    private _FallBrakingGroup: ControlGroup

    /**摄像机距离 */
    private _CameraLength: ControlGroup

    /**角色 */
    char: Gameplay.Character

    protected async onStart(): Promise<void> {
        if (SystemUtil.isClient()) {
            this.char = (await Gameplay.asyncGetCurrentPlayer()).character

            // *****************设置默认值*********************
            // 地面最大速度
            this.char.maxWalkSpeed = 450
            // 最大加速度
            this.char.maxAcceleration = 2048
            // 最大跳跃高度
            this.char.maxJumpHeight = 50
            // 下落控制
            this.char.airControl = 0.2
            // 最大下落速度
            this.char.maxFallingSpeed = 2048
            // 重力倍率
            this.char.gravityScale = 1
            // 下落制动速率
            this.char.brakingDecelerationFalling = 0


            this.createControlGroup()
        }
    }

    /**创建控制组 */
    private createControlGroup() {
        // 创建UI，并添加到屏幕上
        this._jumpSettingPanel = UI.UserWidget.newObject();
        this._jumpSettingPanel.addToViewport(1)

        // 创建一个Canvas，并把这个Canvas设置为rootCanvas
        this._rootCanvas = UI.Canvas.newObject()
        this._rootCanvas.size = new Type.Vector2(1920, 1080);
        this._rootCanvas.position = Type.Vector2.zero
        this._jumpSettingPanel.rootContent = this._rootCanvas

        this._MaxSpeedGroup = new ControlGroup(this._rootCanvas, new Type.Vector2(1200, 150), "地面最大速度", 450, 10000)
        this._AddSpeedGroup = new ControlGroup(this._rootCanvas, new Type.Vector2(1200, 200), "最大加速度", 2048, 10000)
        this._JumpHeightGroup = new ControlGroup(this._rootCanvas, new Type.Vector2(1200, 350), "最大跳跃高度", 50, 2000)
        this._FallControlGroup = new ControlGroup(this._rootCanvas, new Type.Vector2(1200, 400), "下落控制", 0.2, 1)
        this._FallSpeedGroup = new ControlGroup(this._rootCanvas, new Type.Vector2(1200, 450), "最大下落速度", 2048, 10000)
        this._GravityGroup = new ControlGroup(this._rootCanvas, new Type.Vector2(1200, 500), "重力倍率", 1, 10)
        this._FallBrakingGroup = new ControlGroup(this._rootCanvas, new Type.Vector2(1200, 550), "下落制动速率", 450, 10000)
        this._CameraLength = new ControlGroup(this._rootCanvas, new Type.Vector2(1200, 650), "摄像机距离", 180, 2000)

        this.bindEvent()
    }

    /**绑定事件 */
    private bindEvent() {
        this._MaxSpeedGroup.slider.onSliderValueChanged.add((v) => {
            this.char.maxWalkSpeed = v
            this._MaxSpeedGroup.valueTxt.text = v.toFixed(1)
        })

        this._AddSpeedGroup.slider.onSliderValueChanged.add((v) => {
            this.char.maxAcceleration = v
            this._AddSpeedGroup.valueTxt.text = v.toFixed(1)
        })

        this._JumpHeightGroup.slider.onSliderValueChanged.add((v) => {
            this.char.maxJumpHeight = v
            this._JumpHeightGroup.valueTxt.text = v.toFixed(1)
        })

        this._FallControlGroup.slider.onSliderValueChanged.add((v) => {
            this.char.airControl = v
            this._FallControlGroup.valueTxt.text = v.toFixed(1)
        })

        this._FallSpeedGroup.slider.onSliderValueChanged.add((v) => {
            this.char.maxFallingSpeed = v
            this._FallSpeedGroup.valueTxt.text = v.toFixed(1)
        })

        this._GravityGroup.slider.onSliderValueChanged.add((v) => {
            this.char.gravityScale = v
            this._GravityGroup.valueTxt.text = v.toFixed(1)
        })

        this._FallBrakingGroup.slider.onSliderValueChanged.add((v) => {
            this.char.brakingDecelerationFalling = v
            this._FallBrakingGroup.valueTxt.text = v.toFixed(1)
        })

        this._CameraLength.slider.onSliderValueChanged.add((v) => {
            this.char.cameraSystem.targetArmLength = v
            this._CameraLength.valueTxt.text = v.toFixed(1)
        })
    }

}

class ControlGroup {

    /**画布UI */
    private _canvas: UI.Canvas

    /**标题UI */
    private _titleTxt: UI.TextBlock

    /**进度条UI */
    public slider: UI.ProgressBar

    /**数值UI */
    public valueTxt: UI.TextBlock

    constructor(rootCanvas: UI.Canvas, groupLoc: Type.Vector2, titleName: string, defaultValue: number, maxValue: number) {
        // 创建一个Canvas并添加到rootCanvas上
        this._canvas = UI.Canvas.newObject(rootCanvas)
        this._canvas.size = new Type.Vector2(700, 40);
        this._canvas.position = groupLoc

        // 创建一个TextBlock并添加到this._canvas上，作用是显示标题
        this._titleTxt = UI.TextBlock.newObject(this._canvas)
        this._titleTxt.size = new Type.Vector2(220, 40);
        this._titleTxt.position = Type.Vector2.zero
        this._titleTxt.text = titleName
        this._titleTxt.fontSize = 24

        // 创建一个ProgressBar并添加到this._canvas上，作用是修改数值
        this.slider = UI.ProgressBar.newObject(this._canvas)
        this.slider.size = new Type.Vector2(400, 40);
        this.slider.position = new Type.Vector2(230, 0);
        this.slider.currentValue = defaultValue
        this.slider.sliderMaxValue = maxValue

        // 创建一个TextBlock并添加到this._canvas上，作用是显示数值
        this.valueTxt = UI.TextBlock.newObject(this._canvas)
        this.valueTxt.size = new Type.Vector2(50, 40);
        this.valueTxt.position = new Type.Vector2(640, 0);
        this.valueTxt.fontSize = 12
        this.valueTxt.text = defaultValue.toFixed(1)
    }
}