import { _decorator, Component, Node, input, Input, EventTouch, Animation } from 'cc';
import { gameManager } from './gameManager';
const { ccclass, property } = _decorator;

@ccclass('UIMain')
export class UIMain extends Component {

    @property(gameManager)
    public gm:gameManager = null;

    @property(Node)
    public bg1:Node = null;

    @property(Node)
    public bg2:Node = null;

    @property(Node)
    public plane:Node = null;

    //背景滚动速度
    public bg_sd:number = 1;

    //背景滚动最大距离
    public bg_max:number = 10;

    //plane移动速度
    @property
    public p_speed:number = 1;

    //plane移动动画
    @property(Animation)
    public p_an:Animation = null;

    start() {

        //背景
        this.bg1.setPosition(0,0,0);
        this.bg2.setPosition(0,0,-this.bg_max);

        //plane
        this.node.on(Input.EventType.TOUCH_MOVE,this.onmove,this);
        this.node.on(Input.EventType.TOUCH_START,this.onmoves,this);
        this.node.on(Input.EventType.TOUCH_END,this.onmovee,this);


    }

    update(deltaTime: number) {

        //背景
        this.bg1.setPosition(0,0,this.bg1.position.z+deltaTime*this.bg_sd);
        this.bg2.setPosition(0,0,this.bg2.position.z+deltaTime*this.bg_sd);
        if(this.bg1.position.z>this.bg_max) this.bg1.setPosition(0,0,this.bg2.position.z-this.bg_max);
        else if(this.bg2.position.z>this.bg_max) this.bg2.setPosition(0,0,this.bg1.position.z-this.bg_max);

        
    }

    onmove(event:EventTouch){
        const delat = event.touch.getDelta();
        const pos = this.plane.position;
        this.plane.setPosition(pos.x + 0.01 * this.p_speed * delat.x, pos.y,  pos.z - 0.01 * this.p_speed * delat.y);
    }

    onmoves(event:EventTouch){
        this.p_an.crossFade('p_an', .5);
        this.gm.isTouch(true);
        //this.plane.setPosition(event.touch.getLocationX(),this.plane.position.y,event.touch.getLocationY());
    }

    onmovee(event:EventTouch){
        this.p_an.stop();
        this.gm.isTouch(false);
    }

}

