Exemple #1
0
    /*
     * private void BuildAstar()
     * {
     *  Transform tilemapTransform = tileMap.transform;
     *  Transform wall = tilemapTransform.Find("wall");
     *  Transform environment = tilemapTransform.Find("environment");
     *  Transform[] testList = new Transform[] { wall, environment };
     *  for(int t = 0; t < testList.Length; t++)
     *  {
     *      Transform testTf = testList[t];
     *      if(testTf == null)
     *      {
     *          continue;
     *      }
     *      BoxCollider[] colliders = testTf.gameObject.GetComponentsInChildren<BoxCollider>();
     *      foreach(BoxCollider box in colliders)
     *      {
     *          Transform tf = box.transform;
     *          Vector3 center =box.center;
     *          Vector3 halfExtents = box.size * 0.5f;
     *          Vector3 left_top = new Vector3(center.x - halfExtents.x, center.y, center.z + halfExtents.z);
     *          Vector3 left_bottom = new Vector3(center.x - halfExtents.x, center.y, center.z - halfExtents.z);
     *          Vector3 right_top = new Vector3(center.x + halfExtents.x, center.y, center.z + halfExtents.z);
     *          Vector3 right_bottom = new Vector3(center.x + halfExtents.x, center.y, center.z - halfExtents.z);
     *
     *          Vector3[] lists = new Vector3[] { left_top, right_top, left_bottom, right_bottom};
     *
     *          for(float x = left_top.x; x <= right_bottom.x; x += 0.1f)
     *          {
     *              for(float z = left_top.z; z > right_bottom.z; z -= 0.1f)
     *              {
     *                  Vector3 p = new Vector3(x, center.y, z);
     *                  p = tf.TransformPoint(p);
     *                  RaycastHit[] hitResults = GameUtil.Raycast(p, Vector3.down, 100, 1 << GameUtil.MapLayer);
     *                  for (int n = 0; n < hitResults.Length; n++)
     *                  {
     *                      RaycastHit hit = hitResults[n];
     *                      if (hit.collider != null && hit.collider.gameObject != null)
     *                      {
     *                          tileMap.EditorObserver(hit.point.x, hit.point.z, TileType.Block);
     *                      }
     *                  }
     *              }
     *          }
     *
     *          for(int i = 0; i < lists.Length; i++)
     *          {
     *              Vector3 p = tf.TransformPoint(lists[i]);
     *              RaycastHit[] hitResults = GameUtil.Raycast(p, Vector3.down, 100, 1 << GameUtil.MapLayer);
     *              for (int n = 0; n < hitResults.Length; n++)
     *              {
     *                  RaycastHit hit = hitResults[n];
     *                  if (hit.collider != null && hit.collider.gameObject != null)
     *                  {
     *                      tileMap.EditorObserver(hit.point.x, hit.point.z, TileType.Block);
     *                  }
     *              }
     *          }
     *
     *      }
     *  }
     * }
     */

    private void BuildAstar2()
    {
        float nodeSize = tileMap.nodeSize;
        int   xNode    = tileMap.xNode;
        int   zNode    = tileMap.zNode;
        float yPos     = 0f;
        float ySize    = 100f;

        Vector3 offset = new Vector3(-xNode * nodeSize * 0.5f, 0, -zNode * nodeSize * 0.5f);

        tileMap.InitValues();
        for (int x = 0; x < tileMap.xNode; x++)
        {
            for (int z = 0; z < tileMap.zNode; z++)
            {
                Vector3      center      = new Vector3(x * nodeSize + nodeSize * 0.5f, yPos, z * nodeSize + nodeSize * 0.5f) + offset;
                Vector3      halfExtents = new Vector3(nodeSize, ySize, nodeSize) * 0.5f;
                RaycastHit[] hitResults  = GameUtil.BoxCastAll(center, halfExtents, Vector3.up, Quaternion.identity, 10, 1 << GameUtil.MapOutWallLayer | 1 << GameUtil.ObstructLayer);
                for (int n = 0; n < hitResults.Length; n++)
                {
                    RaycastHit hit = hitResults[n];
                    if (hit.collider != null && hit.collider.gameObject != null)
                    {
                        tileMap.SetTileValue(x, z, TileType.Block);
                    }
                }
            }
        }
    }