
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 "../game_module/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";

export class Mound extends Laya.Script3D {

    private selfobj: Sprite3D;
    private tran: Transform3D;
    private FriendSpawnPoint: Transform3D;
    private HealthText: Transform3D;
    private WallsEnd: Transform3D;
    private mat: 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.mat = (we.getChildAt(0) as Laya.MeshSprite3D).meshRenderer.material as Laya.BlinnPhongMaterial;
    }


    private tarTran: Transform3D;
    Cheack() {
        if (this.tarTran.localPositionZ - this.tran.localPositionZ >= 5) {
            console.log("回收tower");
            this.Des(); return;
        }
        this.MoveHpText();
    }
    MoveHpText() {
        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 _hp: number;
    private hpText: Laya.Text;
    private _camera: Laya.Camera;

    SetTower(data: ThingArr) {
        this._firend = 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._createFriend();
        } else {

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

    private _firend: Mate;
    /**
     * 创建friend
     */
    private _createFriend() {
        this._firend = MateManager.Instance.CreateFriend(this.FriendSpawnPoint);
    }
    /**
     * 解救friend
     */
    SaveFriend() {
        if (this._firend) {
            this._firend.TweenFall();
            this._firend = null;
        }
    }

    //被攻击
    Atked(hitpos) {
        this._hp -= PlayerManager.bulletAttack;
        if (this._hp <= 0) {
            this.SaveFriend();
            ParticleManager.Instance.CreateBoomEffect(this.tran.position);
            this.Des();
            GameManager.Instance.towerCount++;
            return;
        }
        ParticleManager.Instance.CreateHitEffect(hitpos);
        let t = Math.ceil(this._hp);
        this.hpText.text = t.toString();
        this.SetMat();
    }
    private SetMat() {
        this.mat.albedoIntensity = 4;
        Laya.timer.once(100, this, () => {
            this.mat.albedoIntensity = 1;
        });
    }
    Des() {
        Laya.timer.clearAll(this);
        if (this.hpText) {
            GamePool.Instance.SetDisableForUi(PoolType.HpText, this.hpText);
            this.hpText = null;
        }
        this.mat.albedoIntensity = 1;
        MoundManager.Instance.RemoveTower(this.selfobj);
        GamePool.Instance.SetDisableObject(this.selfobj);
        if (this._firend) {
            this._firend.Des();
            this._firend = null;
        }
    }
    onDisable() {

    }
    onDestroy() {

    }
}