Пример #1
0
 // Start is called before the first frame update
 void Start()
 {
     GeneralPool.resetPool();
     gateCount = gateList.Count;
     foreach (GameObject gate in gateList)
     {
         GeneralPool.addToTowerList(gate);
     }
     uiHelper        = GetComponent <UIHelper>();
     towerController = GetComponent <TowerController>();
     enemyController = GetComponent <EnemyController>();
     enemySpawner    = GetComponent <EnemySpawner>();
     currentRound    = 0;
     toStep0();
 }
Пример #2
0
    // Update is called once per frame
    void Update()
    {
        if (loadedflag_inner)
        {
            //Debug.Log("gamestep情况:"+gameStep);
            if (gameStep == 0)
            {
                if (towerMode)
                {
                    foreach (GameObject tower in GeneralPool.get_towerList())
                    {
                        if (tower.tag == "Tower")
                        {
                            Collider towerColli = tower.GetComponent <Collider>();
                            if (towerColli != null)
                            {
                                tower.GetComponent <Collider>().enabled = false;
                            }
                        }
                    }
                    //创建一条从摄像机到触摸位置的射线
                    Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition); // 定义射线
                    RaycastHit rayHit;
                    if (Physics.Raycast(ray, out rayHit, 200))                          // 参数1 射线,参数2 碰撞信息, 参数3 碰撞层
                    {
                        //Debug.Log(rayHit.collider.gameObject.name);
                        if (rayHit.collider.gameObject.tag == "BuildablePlane")
                        {
                            Vector3 hitpoint          = rayHit.point;
                            float   hitpoint_x        = hitpoint.x;
                            float   hitpoint_y        = hitpoint.y;
                            float   hitpoint_z        = hitpoint.z;
                            float   hitpoint_x_center = Mathf.Floor(hitpoint_x) + 0.5f;
                            float   hitpoint_z_center = Mathf.Floor(hitpoint_z) + 0.5f;
                            tempTower.transform.position = new Vector3(hitpoint_x_center, hitpoint_y, hitpoint_z_center);
                            if (!tempTower.activeSelf)
                            {
                                tempTower.SetActive(true);
                            }

                            if (CheckGuiRaycastObjects())
                            {
                                return;
                            }
                            if (Input.GetMouseButtonDown(0))//建塔
                            {
                                if (tempTower.activeSelf)
                                {
                                    if (GeneralPool.coins >= tempTowerComp.towerPrice)
                                    {
                                        uiHelper.updateCoinCountText(GeneralPool.coins);
                                        GameObject realTower = Instantiate(towerPrefab, tempTower.transform.position, new Quaternion());

                                        realTower.transform.parent = TowerParent;
                                        Tower realTowerComp = realTower.GetComponent <Tower>();
                                        realTowerComp.setRoundWhenCreated(currentRound);
                                        GameObject realTowerScopePlane = realTowerComp.getScopePlane();
                                        realTowerScopePlane.SetActive(false);
                                        List <Vector3> realTowerScopePlanePos = realTowerComp.getScopePlanePosList();
                                        displayScopePlane.DisplayScopeOfTower(realTowerScopePlanePos, realTowerScopePlane.transform);
                                        GeneralPool.addToTowerList(realTower);
                                        GeneralPool.addToTowerAndBPDict(realTower, rayHit.collider.gameObject);
                                        rayHit.collider.gameObject.SetActive(false);
                                        GeneralPool.coins -= realTowerComp.towerPrice;
                                        uiHelper.updateCoinCountText(GeneralPool.coins);
                                        //Debug.Log(rayHit.collider.gameObject.name);
                                    }
                                    else
                                    {
                                        Debug.Log("金币不足");
                                        uiHelper.ShowAlarm("金币不足");
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (tempTower.activeSelf)
                            {
                                //Debug.Log("nothing");
                                tempTower.SetActive(false);
                            }
                        }
                    }
                }
                else//非TowerMode
                {
                    //非TowerMode中所有塔都可以被点击,要先全设置为碰撞体
                    foreach (GameObject tower in GeneralPool.get_towerList())
                    {
                        if (tower.tag == "Tower")
                        {
                            Collider towerColli = tower.GetComponent <Collider>();
                            if (towerColli != null)
                            {
                                tower.GetComponent <Collider>().enabled = true;
                            }
                        }
                    }

                    if (CheckGuiRaycastObjects())
                    {
                        return;
                    }
                    if (Input.GetMouseButtonDown(0))
                    {
                        Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition); // 定义射线
                        RaycastHit rayHit;
                        if (Physics.Raycast(ray, out rayHit, 200))                          // 参数1 射线,参数2 碰撞信息, 参数3 碰撞层
                        {
                            GameObject rayHitObject = rayHit.collider.gameObject;
                            if (rayHitObject.tag == "Tower")//点击塔会显示塔的外轮廓,塔的检测范围,以及塔的选项
                            {
                                if (focusTower == rayHitObject)
                                {
                                    QuickOutline towerOutline = rayHitObject.GetComponent <QuickOutline>();
                                    towerOutline.enabled = false;
                                    focusTower           = null;
                                    //towerOperatePanel_Prepare.transform.localScale = Vector3.zero;
                                }
                                else
                                {
                                    // foreach (GameObject tower in GeneralPool.get_towerList())
                                    // {
                                    //     if (tower.tag == "Tower")
                                    //     {
                                    //         tower.GetComponent<QuickOutline>().enabled = false;
                                    //         Transform OneScopePlaneTransform = tower.transform.Find("scopePlane");
                                    //         OneScopePlaneTransform.gameObject.SetActive(false);
                                    //     }
                                    // }

                                    if (focusTower == null)
                                    {
                                    }
                                    else
                                    {
                                        focusTower.GetComponent <QuickOutline>().enabled = false;
                                        Transform OneScopePlaneTransform = focusTower.transform.Find("scopePlane");
                                        OneScopePlaneTransform.gameObject.SetActive(false);
                                        towerOperatePanel_Prepare.transform.localScale = Vector3.zero;
                                    }
                                    focusTower = rayHitObject;
                                    focusTower.GetComponent <QuickOutline>().enabled = true;
                                }
                                GameObject TowerScopePlane = rayHitObject.transform.Find("scopePlane").gameObject;
                                TowerScopePlane.SetActive(!TowerScopePlane.activeSelf);
                                switchTowerOperatePanel_Prepare(rayHitObject);
                            }
                            else if (rayHitObject.name == "Road1" || rayHitObject.name == "Road2")
                            {
                                Debug.Log("its road");
                            }
                            else
                            {
                                Debug.Log(rayHitObject.name);
                                // foreach (GameObject tower in GeneralPool.get_towerList())
                                // {
                                //     if (tower.tag == "Tower")
                                //     {
                                //         tower.GetComponent<QuickOutline>().enabled = false;
                                //         Transform OneScopePlaneTransform = tower.transform.Find("scopePlane");
                                //         OneScopePlaneTransform.gameObject.SetActive(false);
                                //     }
                                // }
                                if (focusTower != null)
                                {
                                    focusTower.GetComponent <QuickOutline>().enabled = false;
                                    Transform OneScopePlaneTransform = focusTower.transform.Find("scopePlane");
                                    OneScopePlaneTransform.gameObject.SetActive(false);
                                }
                                focusTower = null;

                                towerOperatePanel_Prepare.transform.localScale = Vector3.zero;
                            }
                        }
                    }

                    if (focusTower != null)
                    {
                        towerOperatePanel_Prepare.transform.position = Camera.main.WorldToScreenPoint(focusTower.transform.position);
                    }
                }
            }
            else//在出兵阶段和交战阶段,点击塔可以显示塔的攻击范围,塔名和属性,紧急修复按钮,及技能按钮
            {
                //此时所有塔都可以被点击,要先全设置为碰撞体
                foreach (GameObject tower in GeneralPool.get_towerList())
                {
                    if (tower.tag == "Tower")
                    {
                        Collider towerColli = tower.GetComponent <Collider>();
                        if (towerColli != null)
                        {
                            tower.GetComponent <Collider>().enabled = true;
                        }
                    }
                }
                if (CheckGuiRaycastObjects())
                {
                    return;
                }
                if (Input.GetMouseButtonDown(0))
                {
                    Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition); // 定义射线
                    RaycastHit rayHit;
                    if (Physics.Raycast(ray, out rayHit, 200))                          // 参数1 射线,参数2 碰撞信息, 参数3 碰撞层
                    {
                        GameObject rayHitObject = rayHit.collider.gameObject;
                        if (rayHitObject.tag == "Tower")//点击塔会显示塔的外轮廓,塔的检测范围,以及塔的选项
                        {
                            if (focusTower == rayHitObject)
                            {
                                QuickOutline towerOutline = rayHitObject.GetComponent <QuickOutline>();
                                towerOutline.enabled = false;
                                focusTower           = null;
                                //towerOperatePanel_Prepare.transform.localScale = Vector3.zero;
                            }
                            else
                            {
                                // foreach (GameObject tower in GeneralPool.get_towerList())
                                // {
                                //     if (tower.tag == "Tower")
                                //     {
                                //         tower.GetComponent<QuickOutline>().enabled = false;
                                //         Transform OneScopePlaneTransform = tower.transform.Find("scopePlane");
                                //         OneScopePlaneTransform.gameObject.SetActive(false);
                                //     }
                                // }

                                if (focusTower == null)
                                {
                                }
                                else
                                {
                                    focusTower.GetComponent <QuickOutline>().enabled = false;
                                    Transform OneScopePlaneTransform = focusTower.transform.Find("scopePlane");
                                    OneScopePlaneTransform.gameObject.SetActive(false);
                                    towerOperatePanel_Battle.transform.localScale = Vector3.zero;
                                }
                                focusTower = rayHitObject;
                                focusTower.GetComponent <QuickOutline>().enabled = true;
                            }
                            GameObject TowerScopePlane = rayHitObject.transform.Find("scopePlane").gameObject;
                            TowerScopePlane.SetActive(!TowerScopePlane.activeSelf);
                            switchTowerOperatePanel_Battle(rayHitObject);
                        }
                        else if (rayHitObject.name == "Road1" || rayHitObject.name == "Road2")
                        {
                            Debug.Log("its road");
                        }
                        else
                        {
                            Debug.Log(rayHitObject.name);
                            // foreach (GameObject tower in GeneralPool.get_towerList())
                            // {
                            //     if (tower.tag == "Tower")
                            //     {
                            //         tower.GetComponent<QuickOutline>().enabled = false;
                            //         Transform OneScopePlaneTransform = tower.transform.Find("scopePlane");
                            //         OneScopePlaneTransform.gameObject.SetActive(false);
                            //     }
                            // }
                            if (focusTower != null)
                            {
                                focusTower.GetComponent <QuickOutline>().enabled = false;
                                Transform OneScopePlaneTransform = focusTower.transform.Find("scopePlane");
                                OneScopePlaneTransform.gameObject.SetActive(false);
                            }
                            focusTower = null;

                            towerOperatePanel_Battle.transform.localScale = Vector3.zero;
                        }
                    }
                }

                if (focusTower != null)
                {
                    towerOperatePanel_Battle.transform.position = Camera.main.WorldToScreenPoint(focusTower.transform.position);
                }
                else
                {
                    towerOperatePanel_Battle.transform.localScale = Vector3.zero;
                }
            }
        }
        else//loadedflag_inner为false,说明资源还未全部加载完成
        {
        }
    }