コード例 #1
0
ファイル: UICommand.cs プロジェクト: Tracydrr/Tracydrr
        // when button clicked
        public void ButtonClicked(CommandType ct)
        {
            //Debug.Log ("ButtonClicked "+ct.ToString());
            BEAudioManager.SoundPlay(6);

            // if newly created building is selected
            // only perform create and cancel
            if (building.OnceLanded == false)
            {
                // if user clicked 'create' button
                if (ct == CommandType.Create)
                {
                    // building can land
                    if (building.Landable)
                    {
                        // hide command dialog
                        Hide();
                        // land building and unselect
                        SceneTown.instance.BuildingLandUnselect();

                        // decrease build price of the building
                        BuildingDef bd = TBDatabase.GetBuildingDef(building.Type, (building.Level == 0) ? 1 : building.Level);
                        building.PayforBuild(bd);
                        // if building level is 0(need buildtime), upgrade to level 1 start
                        // if not, check resource capacity
                        if (building.Level == 0)
                        {
                            building.Upgrade();
                        }
                        else
                        {
                            SceneTown.instance.CapacityCheck();
                        }
                    }

                    // if house is created add worker
                    if (building.Type == 1)
                    {
                        BEWorkerManager.instance.AddWorker();
                    }

                    // if wall is created,automatically next wall create for convenience
                    if (building.Type == 2)
                    {
                        // check if user has enough gold and wall count is not max count
                        BuildingDef bd        = TBDatabase.GetBuildingDef(building.Type, 1);
                        bool        Available = bd.PriceInfoCheck(null);
                        int         CountMax  = BEGround.instance.GetBuildingCountMax(building.Type);
                        int         Count     = BEGround.instance.GetBuildingCount(building.Type);
                        if (Available && (Count < CountMax))
                        {
                            //Debug.Log ("wall tilePos:"+building.tilePos.ToString ());
                            // add another wall automatically
                            Building script = BEGround.instance.BuildingAdd(2, 1);
                            if (script != null)
                            {
                                // choose whicj direction
                                Building buildingNeighbor = null;
                                Vector2  tilePos          = Vector2.zero;
                                int      NeighborX        = 0;
                                int      NeighborZ        = 0;
                                bool     bFind            = false;
                                // check prev and next tile in x,z coordination
                                for (int dir = 0; dir < 2; ++dir)
                                {
                                    for (int value = 0; value < 2; ++value)
                                    {
                                        if (dir == 0)
                                        {
                                            NeighborX = 0;
                                            NeighborZ = ((value == 0) ? -1 : 1);
                                        }
                                        else
                                        {
                                            NeighborX = ((value == 0) ? -1 : 1);
                                            NeighborZ = 0;
                                        }
                                        buildingNeighbor = BEGround.instance.GetBuilding((int)building.tilePos.x + NeighborX, (int)building.tilePos.y + NeighborZ);

                                        // if wall finded
                                        if ((buildingNeighbor != null) && (buildingNeighbor.Type == 2))
                                        {
                                            bFind = true;
                                            break;
                                        }
                                    }

                                    if (bFind)
                                    {
                                        break;
                                    }
                                }
                                //Debug.Log ("wall NeighborX:"+NeighborX.ToString ()+ "NeighborZ:"+NeighborZ.ToString ());

                                // set inverse direction
                                tilePos = building.tilePos;
                                if (NeighborX == 0)
                                {
                                    tilePos.y -= (float)NeighborZ;
                                }
                                else
                                {
                                    tilePos.x -= (float)NeighborX;
                                }

                                //Debug.Log ("wall tilePos New:"+tilePos.ToString ());
                                script.Move((int)tilePos.x, (int)tilePos.y);
                                script.CheckLandable();
                                SceneTown.instance.BuildingSelect(script);
                            }
                        }
                    }

                    SceneTown.instance.Save();
                    BEWorkerManager.instance.SetWorker(building);
                }
                // if user clicked 'cancel' button
                else if (ct == CommandType.CreateCancel)
                {
                    // hide command dialog
                    Hide();
                    // delete temporary created building
                    SceneTown.instance.BuildingDelete();
                }
                else
                {
                }
            }
            else
            {
                if (ct == CommandType.Info)
                {
                    Hide();
                    UIDialogInfo.Show(building);
                }
                else if (ct == CommandType.Upgrade)
                {
                    // check if worker available
                    if (BEWorkerManager.instance.WorkerAvailable())
                    {
                        Hide();
                        UIDialogUpgradeAsk.Show(building);
                    }
                    else
                    {
                        UIDialogMessage.Show("All workers are working now", "Ok", "No Worker Available");
                    }
                }
                else if (ct == CommandType.UpgradeCancel)
                {
                    UIDialogMessage.Show("Cancel current upgrade?", "Yes,No", "Cancel Upgrade ?", null, (result) => { MessageBoxResultUpgradeCancel(result); });
                }
                else if (ct == CommandType.UpgradeFinish)
                {
                    // if instant finish button was clicked
                    int FinishGemCount = building.GetFinishGemCount();
                    // user has enough gem to finish
                    if (SceneTown.Gem.Target() >= FinishGemCount)
                    {
                        // decrease gem
                        SceneTown.Gem.ChangeDelta(-FinishGemCount);
                        // complete upgrade
                        building.UpgradeCompleted = true;
                        Hide();
                    }
                    else
                    {
                        UIDialogMessage.Show("You need more gems to finish this work immediately", "Ok", "Need More Gems");
                    }
                }
                else if (ct == CommandType.Training)
                {
                    Hide();
                    UIDialogTraining.Show(building);
                }
                else
                {
                }
            }
        }
コード例 #2
0
        // initialize building
        public void Init(int type, int level)
        {
            Type  = type;
            Level = level;

            // delete old meshes
            if (goCenter != null)
            {
                Destroy(goCenter); goCenter = null;
            }
            if (goXInc != null)
            {
                Destroy(goXInc); goXInc = null;
            }
            if (goZInc != null)
            {
                Destroy(goZInc); goZInc = null;
            }

            bt = TBDatabase.GetBuildingType(type);

            // get mesh path type and level
            int    displayLevel = (level == 0) ? 1 : level;
            string meshPath     = "Prefabs/Building/" + bt.Name + "_" + displayLevel.ToString();
            //Debug.Log ("Loading Mesh "+meshPath);

            // instantiate mesh and set to goCenter
            GameObject prefabMesh = Resources.Load(meshPath) as GameObject;

            goCenter = (GameObject)Instantiate(prefabMesh, Vector3.zero, Quaternion.identity);
            goCenter.transform.SetParent(gameObject.transform);
            goCenter.transform.localPosition = Vector3.zero;
            goCenter.transform.localRotation = Quaternion.Euler(0, -90, 0);

            // if wall
            if (type == 2)
            {
                // create x,z side mesh
                string     meshSidePath   = meshPath + "Side";
                GameObject prefabMeshSide = Resources.Load(meshSidePath) as GameObject;

                goXInc = (GameObject)Instantiate(prefabMeshSide, Vector3.zero, Quaternion.identity);
                goXInc.transform.SetParent(gameObject.transform);
                goXInc.transform.localPosition = Vector3.zero;
                goXInc.transform.localRotation = Quaternion.Euler(0, 180, 0);               // rotate to x direction

                goZInc = (GameObject)Instantiate(prefabMeshSide, Vector3.zero, Quaternion.identity);
                goZInc.transform.SetParent(gameObject.transform);
                goZInc.transform.localPosition = Vector3.zero;
                goZInc.transform.localRotation = Quaternion.Euler(0, 90, 0);               // rotate to z direction
            }

            // set tile size
            tileSize = new Vector2(bt.TileX, bt.TileZ);
            // set proper material to gogrid
            goGrid.GetComponent <Renderer>().material = Resources.Load("Materials/Tile" + bt.TileX.ToString() + "x" + bt.TileZ.ToString()) as Material;
            goGrid.transform.localScale           = new Vector3(bt.TileX, 1, bt.TileZ);
            goArrowXMinus.transform.localPosition = new Vector3(-(0.4f + (float)bt.TileX * 0.5f), 0.01f, 0);
            goArrowXPlus.transform.localPosition  = new Vector3((0.4f + (float)bt.TileX * 0.5f), 0.01f, 0);
            goArrowZMinus.transform.localPosition = new Vector3(0, 0.01f, -(0.4f + (float)bt.TileX * 0.5f));
            goArrowZPlus.transform.localPosition  = new Vector3(0, 0.01f, (0.4f + (float)bt.TileX * 0.5f));

            goArea.transform.localScale = new Vector3(bt.TileX, 1, bt.TileZ);

            // get ui
            if (transform.Find("UIBuilding"))
            {
                uiInfo = transform.Find("UIBuilding").transform.Find("UIInfo").GetComponent <UIInfo>();
            }
            else
            {
                uiInfo = UIInGame.instance.AddInGameUI(prefUIInfo, transform, new Vector3(0, 1.5f, 0)).GetComponent <UIInfo>();
            }
            uiInfo.groupInfo.gameObject.SetActive(false);
            uiInfo.groupProgress.gameObject.SetActive(false);
            uiInfo.groupCollect.gameObject.SetActive(false);
            uiInfo.Name.text  = TBDatabase.GetBuildingName(Type);
            uiInfo.Level.text = "Level " + displayLevel.ToString();
            uiInfo.building   = this;

            // currently not used
            goRangeIn.SetActive(false);
            goRangeOut.SetActive(false);

            // initialize values
            tilePosOld = tilePos;
            CheckLandable();
            CheckNeighbor();
            def              = TBDatabase.GetBuildingDef(Type, Level);
            defNext          = bt.GetDefine(Level + 1);
            defLast          = bt.GetDefLast();
            UpgradeTimeTotal = (defNext != null) ? defNext.BuildTime : 0;
        }