Esempio n. 1
0
    void createObject(GameObject g2, Vector3 pos)
    {
        BuildingScript b = LevelController.me.getBuildingPosIsIn(pos);

        if (b == null)
        {
            //Debug.LogError("No building found for object position");
        }
        else
        {
            CommonObjectsStore c             = FindObjectOfType <CommonObjectsStore>();
            List <GameObject>  tilesForPoint = new List <GameObject>();
            b.getPoints();
            foreach (WorldTile wt in b.tilesInBuilding)
            {
                if (wt.walkable == false)
                {
                    continue;
                }

                float d = Vector2.Distance(wt.transform.position, pos);
                if (d > 12 || d < 5)
                {
                    continue;
                }

                tilesForPoint.Add(wt.gameObject);
            }

            if (tilesForPoint.Count == 0)
            {
                //Debug.LogError("No tiles were found in building");
                return;
            }
            else
            {
                GameObject pointToSpawn = null;
                float      d            = 999999.0f;
                bool       posValid     = true;
                foreach (GameObject g in tilesForPoint)
                {
                    List <Vector3> dir = new List <Vector3>();
                    dir.Add(new Vector3(-1, 0, 0));
                    dir.Add(new Vector3(1, 0, 0));
                    dir.Add(new Vector3(0, -1, 0));
                    dir.Add(new Vector3(0, 1, 0));

                    foreach (Vector3 direction in dir)
                    {
                        RaycastHit2D ray = Physics2D.Raycast(g.transform.position, direction, 0.5f, c.maskForMelee);
                        //Debug.DrawRay(g.transform.position, direction * 1.0f, Color.green, 20.0f);
                        if (ray.collider == null)
                        {
                        }
                        else
                        {
                            //if (ray.collider.tag == "Walls" || ray.collider.tag == "CivilianObject" || ray.collider.tag == "Door") {
                            //	posValid = false;
                            //}
                        }
                    }
                    if (posValid == true)
                    {
                        float d2 = Vector2.Distance(g.transform.position, pos);
                        if (d2 < d)
                        {
                            pointToSpawn = g;
                            d            = d2;
                        }
                    }
                }
                if (pointToSpawn == null)
                {
                }
                else
                {
                    Instantiate(g2, new Vector3(pointToSpawn.transform.position.x, pointToSpawn.transform.position.y, 0), Quaternion.Euler(0, 0, 0));
                }
            }
        }
    }