import GameObject from "./GameObject";
import Leg, { LegToward } from "./Leg";
import GameMgr from "../Mgr/GameMgr";
import { ControllerDirection } from "./PlayerController";
import DebugMgr from "../Mgr/DebugMgr";
import GameStage from "./GameStage";
// import { MConfigs } from "../MConfigs";
const MConfigs: any = window;

export default class Legs {
    public get toward(): LegToward {
        return this.left.toward;
    }
    private left: Leg = null;
    private right: Leg = null;
    /**是否测量过步长 */
    private stepLength = null;

    private initPosX: number = null;

    constructor(gameStage: GameStage, rect: egret.Rectangle, texture: egret.Texture, toward: LegToward, colliderOffset: number) {
        this.left = new Leg(gameStage, rect, texture, toward, colliderOffset);
        gameStage.legLayer.addChild(this.left);

        this.right = new Leg(gameStage, rect, texture, toward, colliderOffset);
        gameStage.legLayer.addChild(this.right);


        this.left.updateCallback = () => {
            if (this.toward == LegToward.Right) {
                if (this.posX > GameMgr.instance.stage.stageWidth + MConfigs.distanceOverStageWhlieLegDestroy) {
                    this.left.destroy();
                    this.right.destroy();
                }
            } else {
                if (this.posX < -  MConfigs.distanceOverStageWhlieLegDestroy) {
                    this.left.destroy();
                    this.right.destroy();
                }
            }
        }

        //两腿交替行动
        this.left.act();
        this.left.onActed = () => {
            if (!this.stepLength) {
                this.stepLength = Math.abs(this.left.x - this.initPosX);
                this.right.land(this.initPosX + this.right.towardCoefficient * (this.stepLength / 2), -5);
            }

            this.right.act();
        }
        this.right.onActed = () => {
            this.left.act();
        }
    }

    public setInitPosX(x: number) {
        this.initPosX = x;
        this.left.land(x, -5);
        this.right.land(x, -5);

        this.left.shadow.update();
        this.right.shadow.update();

    }
    public get posX() {
        if (this.toward == LegToward.Right) {
            return Math.min(this.right.x, this.left.x);
        } else {
            return Math.max(this.right.x, this.left.x);
        }
    }
}