﻿/*
 * @Author: ao001.wu ao001.wu@appshahe.com
 * @Date: 2023-05-23 10:10:59
 * @LastEditors: ao001.wu ao001.wu@appshahe.com
 * @LastEditTime: 2023-05-23 10:52:13
 * @FilePath: \WorldUUUI\JavaScripts\MeasureTool.ts
 * @Description: 
 */

@Core.Class
export default class Shoot extends Core.Script {

    /**展示是否射到物体的文本 */
    private text1: UI.TextBlock
    /**展示两点间距离的文本 */
    private text2: UI.TextBlock

    /**点1 */
    private pos1: Vector = null
    /**点2 */
    private pos2: Vector = null

    /** 当脚本被实例后，会在第一帧更新前调用此函数 */
    protected onStart(): void {
        if (SystemUtil.isClient()) {

            this.createUI()

            let input = new Gameplay.TouchInput();
            input.setPlayerController();

            input.onTouchEnd.add((index, location, touchtype) => {
                // 转换点击的屏幕坐标为3D世界坐标
                let pos = InputUtil.convertScreenLocationToWorldSpace(location.x, location.y)
                // 获得点击位置前方方向
                let forv = pos.worldDirection;
                // 计算点击位置朝向一定距离的终点位置
                let endpos = pos.worldLocation.clone().add(forv.multiply(20000));

                let hitInfo = Gameplay.lineTrace(pos.worldLocation, endpos, false, true);

                if (hitInfo.length > 0) {
                    // result 就是射线射到的第一个点
                    let result = hitInfo[0].impactPoint

                    this.text1.text = "碰到的物体：" + hitInfo[0].gameObject.name

                    if (this.pos1 == null) {
                        this.pos1 = result
                        this.text2.text = "已选取1/2"
                    } else if (this.pos2 == null) {
                        this.pos2 = result
                        this.checkDis()
                    }
                    EffectService.getInstance().playEffectAtLocation("4390", result, 1)
                } else {
                    this.text1.text = "没碰到物体"
                }
            })
        }
    }


    /**
     * 检查两点间距离
     */
    private checkDis() {
        let dis = Vector.distance(this.pos1, this.pos2)
        this.text2.text = "两点之间距离：" + dis.toFixed(2)
        this.pos1 = null
        this.pos2 = null
    }


    /**
     * 动态创建UI
     */
    private createUI() {
        // 创建一个UI对象
        let ui = UI.UserWidget.newObject();
        // 将UI添加到屏幕上
        ui.addToViewport(1)
        // 创建一个画布组件
        let rootCanvas = UI.Canvas.newObject()
        rootCanvas.size = new Type.Vector2(1920, 1080);
        rootCanvas.position = Type.Vector2.zero
        // 将Ui的根画布设置为rootCanvas
        ui.rootContent = rootCanvas

        // 是否碰到物体
        this.text1 = UI.TextBlock.newObject(rootCanvas)
        this.text1.position = new Type.Vector2(1200, 190)
        this.text1.size = new Type.Vector2(700, 100)
        this.text1.text = "未触碰到物体"

        // 两点间距离
        this.text2 = UI.TextBlock.newObject(rootCanvas)
        this.text2.position = new Type.Vector2(1200, 300)
        this.text2.size = new Type.Vector2(700, 100)
        this.text2.text = "已选取0/2"
    }


}

