
import Sprite3D = Laya.Sprite3D;
import PlayerBehaviour from "../Behaviour/PlayerBehaviour";
import { Player } from "./player";
import { SceneController } from "./SceneController";
import Vector3 = Laya.Vector3;
import GameDefined from "./GameDefined";
export class PlayerController {
    private static instance: PlayerController;
    public static get Instance(): PlayerController {
        return PlayerController.instance || (PlayerController.instance = new PlayerController());
    }

    public player: Sprite3D;
    public playerBehave: Player;
    public Init(_p: Sprite3D) {
        if (_p) {
            this.player = _p;
            var _pb = this.player.getChildAt(0).getComponent(PlayerBehaviour);
            if (!_pb) _pb = this.player.addComponent(PlayerBehaviour);
            _pb.init();
            this.playerBehave = this.player.getComponent(Player);
            if (!this.playerBehave) this.playerBehave = this.player.addComponent(Player);
        }
    }

    public StartSliderPlayer(_y: number, _x: number) {

        this.playerBehave.SetSpeed(_y * 0.01, _x * 0.01);
        this.JundgePByCDis();
    }

    private tempPos = new Vector3(0, 0.5, 0);
    public JundgePByCDis() {
        var camera = SceneController.Instance._camera;
        this.tempPos.z=this.player.transform.position.z;
        var dis = Vector3.distance(this.tempPos, camera.transform.position);
        if(dis<=GameDefined.minDis||dis>=GameDefined.maxDis){
            this.StopSliderPlayer();
        }
    }
    public StopSliderPlayer() {
        this.playerBehave.SetSpeed();
    }
}