コード例 #1
0
    /// <summary>
    /// checks for selection commands (unit and buildidng selection)
    /// </summary>
    void selection()
    {
        if (Input.GetKey(KeyCode.LeftShift))
        {
            isShift = true;
        }
        else
        {
            isShift = false;
        }

        if (Input.GetMouseButtonDown(0) && !buildingGUI && !buildingBlocked)
        {
            selectionBoxStart  = CalcUtil.getMousePos();
            isDrawSelectionBox = true;
            isClickSelection   = true;
        }

        if (Input.GetMouseButtonDown(1) && buildingGUI)
        {
            buildingGUI = false;
            Destroy(buildingManager.getCurrentGhostBuilding().gameObject);
            buildingManager.addCurrentGhostBuilding(null);
            buildingManager.SetCurrentGhostBuildingName(null);
        }

        if (Input.GetMouseButtonDown(0) && buildingGUI)
        {
            GhostBuilding gh = buildingManager.getCurrentGhostBuilding().GetComponent <GhostBuilding>();
            if (!buildingBlocked && gh.isBuildable())
            {
                GameObject  b     = spawner.spawnBuildingForGhost(gh.transform.position);
                List <Grid> grids = gh.GetComponent <GhostBuilding>().blockFieldOn(b);
                b.GetComponent <Building>().setGridsOn(grids);
                if (gh.GetComponent <GhostBuilding>().isStorage())
                {
                    b.GetComponent <Building>().setStorageGrids(gh.GetComponent <GhostBuilding>().getStoragesOn());
                }
                ressourceManager.removeRessources(gh.GetComponent <GhostBuilding>().getRessourceRequirements());
                buildingGUI = false;
                Destroy(buildingManager.getCurrentGhostBuilding().gameObject);
                if (isShift)
                {
                    spawner.spawnGhostForGhost(gh.transform.position);
                    buildingGUI = true;
                }
                else
                {
                    buildingManager.addCurrentGhostBuilding(null);
                    buildingManager.SetCurrentGhostBuildingName(null);
                }
            }
        }


        if (Input.GetMouseButtonUp(0))
        {
            selectionBoxEnd    = CalcUtil.getMousePos();
            isDrawSelectionBox = false;

            if (!isClickSelection)
            {
                if (!isShift)
                {
                    foreach (GameObject go in unitManager.getUnits())
                    {
                        go.GetComponent <Unit>().setSelected(false);
                    }
                }
                if (selectionBoxStart.x >= selectionBoxEnd.x)
                {
                    float f = selectionBoxStart.x;
                    selectionBoxStart.x = selectionBoxEnd.x;
                    selectionBoxEnd.x   = f;
                }
                if (selectionBoxStart.y <= selectionBoxEnd.y)
                {
                    float f = selectionBoxStart.y;
                    selectionBoxStart.y = selectionBoxEnd.y;
                    selectionBoxEnd.y   = f;
                }
                foreach (GameObject go in unitManager.getUnits())
                {
                    if (go.transform.position.x >= selectionBoxStart.x && go.transform.position.x <= selectionBoxEnd.x && go.transform.position.y <= selectionBoxStart.y && go.transform.position.y >= selectionBoxEnd.y)
                    {
                        go.GetComponent <Unit>().setSelected(true);
                    }
                }
            }
            else
            {
                if (!isShift)
                {
                    foreach (GameObject go in unitManager.getUnits())
                    {
                        go.GetComponent <Unit>().setSelected(false);
                    }
                    foreach (GameObject go in buildingManager.getBuildings())
                    {
                        go.GetComponent <Building>().setSelected(false);
                    }
                }

                // foreach (GameObject go in unitManager.getUnits())
                // {
                //    if(selectionBoxEnd.x <= go.transform.position.x + go.GetComponent<Unit>().getTriggerCollider().size.x &&
                //        selectionBoxEnd.x >= go.transform.position.x - go.GetComponent<Unit>().getTriggerCollider().size.x &&
                //        selectionBoxEnd.y <= go.transform.position.y + go.GetComponent<Unit>().getTriggerCollider().size.y &&
                //        selectionBoxEnd.y >= go.transform.position.y - go.GetComponent<Unit>().getTriggerCollider().size.y)
                //     {
                //         go.GetComponent<Unit>().setSelected(true);
                //     }
                // }

                //Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                //RaycastHit2D hit = Physics2D.Raycast(ray.origin,ray.direction,Mathf.Infinity);

                Vector2  mousePos = CalcUtil.getMousePos();
                Grid     grid     = mapGrid.getGridOn(mousePos);
                Building bo       = null;
                try
                {
                    bo = grid.getBlockingObject().GetComponent <Building>();
                }
                catch
                {
                }
                if (bo != null)
                {
                    bo.setSelected(true);
                }

                //if (hit)
                // {
                //   foreach (GameObject go in unitManager.getUnits())
                //   {
                //        if (hit.collider.gameObject == go)
                //        {
                //            hit.collider.transform.gameObject.GetComponent<Unit>().setSelected(true);
                //         }
                //     }
                //     foreach (GameObject go in buildingManager.getBuildings())
                //     {
                //         if (hit.collider.gameObject == go)
                //          {
                //              hit.collider.transform.gameObject.GetComponent<Building>().setSelected(true);
                //          }
                //      }
                //  }

                // foreach (GameObject go in buildingManager.getBuildings())
                // {

                // if (selectionBoxEnd.x <= go.transform.position.x + go.GetComponent<Building>().getTriggerCollider().size.x &&
                //    selectionBoxEnd.x >= go.transform.position.x - go.GetComponent<Building>().getTriggerCollider().size.x &&
                //    selectionBoxEnd.y <= go.transform.position.y + go.GetComponent<Building>().getTriggerCollider().size.y &&
                //    selectionBoxEnd.y >= go.transform.position.y - go.GetComponent<Building>().getTriggerCollider().size.y)
                // {
                //     go.GetComponent<Building>().setSelected(true);
                // }
                // }

                // isClickSelection = false;
            }
        }
    }