Esempio n. 1
0
    public bool SetType(UnitType t)
    {
        if (this.type != UnitType.Empty && pieceInstance != null || t == UnitType.Empty)
        {
            ClearOut();
            Destroy(pieceInstance);
            if (subInstance != null)
            {
                Destroy(subInstance);
            }
        }

        if (!InitStaticType(t))
        {
            this.type = t;
            if (t == UnitType.Empty)
            {
                // reset
                _canProducing = false;
                isProducer    = false;
                return(false);
            }

            this.pieceInstance = Instantiate(piecePrefab, transform.position + new Vector3(0, 0.5f, 0), Quaternion.identity);
            this.pieceInstance.transform.SetParent(this.transform);
            this.pieceInstance.GetComponent <MeshRenderer>().material.SetColor("_Color", TerrainUnit.GetColorByType(this.type));


            this.progressFiller     = this.pieceInstance.transform.Find("Canvas").Find("Filler").GetComponent <Image>();
            this.progressBackground = this.pieceInstance.transform.Find("Canvas").Find("Back").GetComponent <Image>();
            // produce handle
            this.isProducer = unitConfig.IsProducer(this.type);
            if (this.isProducer)
            {
                this.localPeriod  = unitConfig.GetProducePeriod(this.type);
                this.subPrefab    = unitConfig.GetProducePrefab(this.type);
                this.canProducing = true;
            }
            else
            {
                this.canProducing = false;
                this.subPrefab    = unitConfig.GetProducePrefab(this.type);// for static prefabs
                if (subPrefab != null)
                {
                    this.subInstance = Instantiate(subPrefab, transform.position + new Vector3(0, 0.5f, 0), Quaternion.identity);
                }
            }
            // default Prefab
            switch (t)
            {
            case UnitType.Mine:
                subPrefab = MinePrefab;
                // static Instance
                subInstance = Instantiate(MinePrefab, transform.position + new Vector3(-0.5f, 0.5f, -0.5f), Quaternion.identity);
                //  Random Generate Factor Range
                var buffRange = worldManager.SpreadBFS(this.position,
                                                       (me, him) => (me.position - him.position).magnitude < 2.3f && Math.Abs(me.position.y - him.position.y) <= 1);
                foreach (var item in buffRange)
                {
                    worldManager.GetUnit(item).localBuff = UnitBuffType.INCREASE_FACTORY;
                }
                // flush buff mesh
                break;

            case UnitType.Storage:
                playManager.IncreaseMaxResource();    //  every storage take 1
                break;

            default:
                break;
            }
        }
        return(true);
    }