// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import ConfigManager from "../../../engines/configs/ConfigManager";
import MathUtils from "../../../engines/utils/MathUtils";
import ConfigKeys from "../../models/ConfigKeys";
import GameModel from "../../models/GameModel";
import GameController from "./GameController";

const {ccclass, property} = cc._decorator;

@ccclass
export default class PlayerController extends cc.Component {
    
    width:number=0;
    height:number=0;


    isLeft:boolean=false;
    isRight:boolean=false;
    private __isSprint:boolean=false;

    speed:cc.Vec3;

    
    leftSpeed:cc.Vec3;

   
    rightSpeed:cc.Vec3;

    
    dropSpeed:cc.Vec3;
    
    sprintSpeed:cc.Vec3;

    private helfWidth:number;
    private helfHeight:number;
    /**
     * 起始点
     */
    private startPos:cc.Vec3;

    /**
     * 喷气粒子
     */
    particle:cc.Node;
    onLoad () {
        
    }

    set isSprint(value:boolean){
        this.__isSprint=value;
        if(this.particle!=null){
            this.particle.active=this.__isSprint;
        }
    }

    get isSprint():boolean{
        return this.__isSprint;
    }
    
    start () {
        this.width=GameController.single.carConfig.width;
        this.height=GameController.single.carConfig.height;

        this.startPos=new cc.Vec3();
        this.startPos.x=5;
        this.startPos.y=3;
        this.startPos.z=GameController.single.levelConfig.startPos;

        this.helfWidth=this.width*0.5;
        this.helfHeight=this.height*0,5;

        this.speed=new cc.Vec3();
        this.speed.z=GameController.single.carConfig.speed;

        this.leftSpeed=new cc.Vec3();
        this.leftSpeed.x=-GameController.single.carConfig.turnSpeed;

        this.rightSpeed=new cc.Vec3();
        this.rightSpeed.x=GameController.single.carConfig.turnSpeed;
        
        this.sprintSpeed=new cc.Vec3();
        this.sprintSpeed.z=GameController.single.carConfig.sprintSpeed;

        this.dropSpeed=new cc.Vec3(0,-60,0);
    }

    update (dt:number) {
        let currentPos:cc.Vec3=this.node.position;
        currentPos.x=this.__logicPosition.x;
        currentPos.y=this.__logicPosition.y;
        currentPos.z=-this.__logicPosition.z;
        this.node.position=currentPos;
    }

    private __logicPosition:cc.Vec3=new cc.Vec3();
    get logicPosition():cc.Vec3{
        return this.__logicPosition;
    }

    set logicPosition(value:cc.Vec3){
        this.__logicPosition=value;
    }

    private __rect:cc.Rect=new cc.Rect();
    /**
     * 矩形
     */
    get rect():cc.Rect{
        this.__rect.x=this.logicPosition.x-this.helfWidth;
        this.__rect.y=this.logicPosition.z-this.helfHeight;
        this.__rect.width=this.width;
        this.__rect.height=this.height;
        return this.__rect;
    }

    private __gridView:cc.Vec3[];
    /**
     * 将碰撞盒子化为九宫格九个点
     */
    get gridView():cc.Vec3[]{
        let point:cc.Vec3;
        if(this.__gridView==null){
            this.__gridView=[];
            for (let index = 0; index < 9; index++) {
                point=new cc.Vec3();
                this.__gridView.push(point);
            }
        }
        let rt:cc.Rect=this.rect;
        let w1:number=rt.width/3;
        let h1:number=rt.height/3;
        
        let index:number=0;
        for (let xIndex = 0; xIndex < 3; xIndex++) {
            for (let yIndex = 0; yIndex < 3; yIndex++) {
                index=xIndex*3+yIndex;
                point=this.__gridView[index];
                point.x=rt.x+w1*xIndex;
                point.y=0;
                point.z=rt.y+h1*yIndex;
            }
        }
        return this.__gridView;
    }

    Reset():void{
        this.logicPosition.x=this.startPos.x;
        this.logicPosition.y=this.startPos.y;
        this.logicPosition.z=this.startPos.z;
        this.isLeft=false;
        this.isRight=false;
        this.isSprint=false;
    }
}
