
import Transform3D = Laya.Transform3D;
import Sprite3D = Laya.Sprite3D;
import Vector3 = Laya.Vector3;
import {MoundManager} from "../Manager/MoundManager";
import {CameraManager} from "../Manager/CameraManager";
import {ViewManager} from "../Net/ViewManager";
import { MatrixTransform } from "../Util/MatrixTransform";
import { GamePool, PoolType } from "./GamePool";
import {MainRoleManager} from "../Manager/MainRoleManager";
import {MateManager} from "../Manager/MateManager";
import {Mate} from "./Mate";
import { PlayerManager } from "./PlayerManager";
import { ThingArr } from "../Manager/StreetManager";
import { ParticleManager } from "./ParticleManager";
import {GameManager} from "./GameManager";

import Sprite = Laya.Sprite;
import Stage = Laya.Stage;
import HitArea = Laya.HitArea;
import WebGL = Laya.WebGL;

export class Mound extends Laya.Script3D {

    private selfobj: Sprite3D;
    private tran: Transform3D;
    private FriendSpawnPoint: Transform3D;
    private HealthText: Transform3D;
    private WallsEnd: Transform3D;
    private _reflect: Laya.BlinnPhongMaterial;
    onAwake() {
        this.selfobj = this.owner as Sprite3D;
        this.tran = this.selfobj.transform;
        this.FriendSpawnPoint = (this.selfobj.getChildByName("FriendSpawnPoint") as Sprite3D).transform;
        this.HealthText = (this.selfobj.getChildByName("HealthText") as Sprite3D).transform;
        let we = (this.selfobj.getChildByName("WallsEnd") as Sprite3D);
        this.WallsEnd = we.transform;
        this._reflect = (we.getChildAt(0) as Laya.MeshSprite3D).meshRenderer.material as Laya.BlinnPhongMaterial;
    }

    HpUi() {
        let p = MatrixTransform.WorldToScreen2(this._camera, this.HealthText.position.clone());
        if (p.x <= 0 || p.x >= Laya.stage.width || p.y <= 0 || p.y >= Laya.stage.height) this.hpText.visible = false;
        else {
            if (!this.hpText.visible) this.hpText.visible = true;
            this.hpText.x = p.x;
            this.hpText.y = p.y;
        }
    }
    private tarTran: Transform3D;
    CheckHp() {
        if (this.tarTran.localPositionZ - this.tran.localPositionZ >= 5) {
            this.Recovery(); return;
        }
        this.HpUi();
    }

    /************************************ */
    private red:Sprite;
    private guideContainer:Sprite;
    private tipContainer:Sprite;
    private guideSteps:Array<any> = 
    [
        { x: 151, y: 575, radius:150, tip:"res/guide/help6.png", tipx:200, tipy:250 },
        { x: 883, y: 620, radius:100, tip:"res/guide/help4.png", tipx:730, tipy:380 },
        { x: 1128, y: 583, radius:110, tip:"res/guide/help3.png", tipx:900, tipy:300 }
    ];
    private guideStep:number = 0;
    private hitArea:HitArea;
    private interactionArea:Sprite;
    private nextStep():void
    {
        if (this.guideStep == this.guideSteps.length)
        {
            Laya.stage.removeChild(this.guideContainer);
            Laya.stage.removeChild(this.tipContainer);
        }
        else
        {
            var step:any = this.guideSteps[this.guideStep++];
            
            this.hitArea.unHit.clear();
            this.hitArea.unHit.drawCircle(step.x, step.y, step.radius, "#000000");
            
            this.interactionArea.graphics.clear();
            this.interactionArea.graphics.drawCircle(step.x, step.y, step.radius, "#000000");
            
            this.tipContainer.graphics.clear();
            this.tipContainer.loadImage(step.tip);
            this.tipContainer.pos(step.tipx, step.tipy);
        }
    }
    /**************************************************************************** */
   
    private _mate: Mate;
    /**
     * 创建friend
     */
    private MakeMate() {
        this._mate = MateManager.Instance.MadeMate(this.FriendSpawnPoint);
    }
    /**
     * 解救friend
     */
    Rescue() {
        if (this._mate) {
            this._mate.FallDown();
            this._mate = null;
        }
    }

    private ReflectSet() {
        this._reflect.albedoIntensity = 4;
        Laya.timer.once(100, this, () => {
            this._reflect.albedoIntensity = 1;
        });
    }
    Recovery() {
        Laya.timer.clearAll(this);
        if (this.hpText) {
            GamePool.Instance.RecoverUi(PoolType.HpText, this.hpText);
            this.hpText = null;
        }
        this._reflect.albedoIntensity = 1;
        MoundManager.Instance.RecoverMound(this.selfobj);
        GamePool.Instance.RecoverModel(this.selfobj);
        if (this._mate) {
            this._mate.Recover();
            this._mate = null;
        }
    }

    //被攻击
    Injured(hitpos) {
        this._hp -= PlayerManager.bulletAttack;
        if (this._hp <= 0) {
            this.Rescue();
            ParticleManager.Instance.ExplodeEffect(this.tran.position);
            this.Recovery();
            GameManager.Instance.towerCount++;
            return;
        }
        ParticleManager.Instance.AttackEffect(hitpos);
        let t = Math.ceil(this._hp);
        this.hpText.text = t.toString();
        this.ReflectSet();
    }


    private _hp: number;
    private hpText: Laya.Text;
    private _camera: Laya.Camera;
    SetMound(data: ThingArr) {
        this._mate = null;
        this.tarTran = MainRoleManager.Instance.player.transform;
        this.tran.localPositionX = data.x;
        this.tran.localPositionY = 0;
        this.tran.localPositionZ = data.z;
        if (data.towerR)
            this.WallsEnd.localRotationEulerY = 30;
        else
            this.WallsEnd.localRotationEulerY = 0;
        if (data.type == 1) {
            this.MakeMate();
        } else {

        }
        this._hp = data.hp;
        if (!this.hpText) {
            this.hpText = MoundManager.Instance.HpUi();
            this.hpText.text = this._hp.toString();
            if (!this._camera) this._camera = CameraManager.Instance._maincamera;
            ViewManager.Instance.popNode.addChild(this.hpText);
        }
        this.HpUi();
        Laya.timer.frameLoop(1, this, this.CheckHp);
    }
}