コード例 #1
0
    public static CubePathData GetNextCube(CubeManager cm, CubePathData cube)
    {
        if (cube.index == cm.end_cube)
        {
            return(null);
        }

        var near = cm.GetNearPathData(cube);

        float min_F = float.MaxValue;

        int n_index = -1;

        for (int i = 0; i < near.Count; ++i)
        {
            if (near[i].F < min_F && near[i].cost != 65535f)
            {
                n_index = i;
                min_F   = near [i].F;
            }
        }

        if (n_index != -1)
        {
            return(near [n_index]);
        }
        else
        {
            return(null);
        }
    }
コード例 #2
0
    public List <CubePathData> GetNearPathData(CubePathData end_cube)
    {
        List <CubePathData> res = new List <CubePathData> ();

        for (int i = 0; i < 8; ++i)
        {
            if (end_cube.x + direction [i].x_move < x && end_cube.x + direction [i].x_move >= 0 &&
                end_cube.z + direction [i].z_move < z && end_cube.z + direction [i].z_move >= 0)
            {
                int index = end_cube.index + direction [i].x_move + direction [i].z_move * x;

                if (index >= 0 && index < path_datas.Count)
                {
                    res.Add(path_datas [index]);
                }
                else
                {
                    Debug.LogError(string.Format("Index Out: {0} - {1}", (end_cube.x + direction [i].x_move), (end_cube.z + direction [i].z_move)));
                }
            }
            else
            {
                res.Add(null);
            }
        }

        return(res);
    }
コード例 #3
0
    private void CheckF(CubePathData end_cube, CubeManager cm)
    {
        var nears = cm.GetNearPathData(end_cube);

        for (int i = 0; i < nears.Count; ++i)
        {
            var n_cube = nears [i];
            if (n_cube == null)
            {
                continue;
            }
            float F = n_cube.cost + end_cube.F;

            if (n_cube.F > F)
            {
                n_cube.F = F;
                if (F > max_F)
                {
                    max_F = F;
                }
                if (!open_cube.Contains(n_cube))
                {
                    open_cube.Add(n_cube);
                }
            }
        }

        open_cube.Remove(end_cube);
        close_cube.Add(end_cube);
    }
コード例 #4
0
//	void OnGUI()
//	{
//		if (GUI.Button(new Rect(10, 10, 100, 30), "Generate Cube Map"))
//		{
//			GenerateCubeMap ();
//		}
//
//		input_x = GUI.TextField(new Rect(120,10, 50, 30), "X");
//		input_z = GUI.TextField (new Rect (180, 10, 50, 30), "Y");
//		input_obstacle = GUI.TextField(new Rect(240,10,50,30), "Obstacle");
//	}

    private void GenerateCubeMap()
    {
        for (int i = 0; i < path_datas.Count; ++i)
        {
            Destroy(path_datas [i].cube);
        }

        path_datas.Clear();

        for (int i = 0; i < obstacle_datas.Count; ++i)
        {
            Destroy(obstacle_datas [i].cube);
        }

        obstacle_datas.Clear();

        end_cube = -1;
        state    = State.None;

        x = int.Parse(string.IsNullOrEmpty(input_x.text) ? "32" : input_x.text);
        z = int.Parse(string.IsNullOrEmpty(input_z.text) ? "20" : input_z.text);
        int ob = int.Parse(string.IsNullOrEmpty(input_obstacle.text) ? "50" : input_obstacle.text);

        for (int m = 0; m < z; ++m)
        {
            for (int n = 0; n < x; ++n)
            {
                var go = GameObject.CreatePrimitive(PrimitiveType.Cube);
                go.transform.SetParent(this.transform);
                go.transform.localPosition = new Vector3(n, 0, m);
                go.transform.localScale    = new Vector3(0.95f, 1f, 0.95f);
                go.tag  = "Path_Cube";
                go.name = "Paht: " + n.ToString() + "-" + m.ToString();
                MeshRenderer mr = go.GetComponent <MeshRenderer> ();
                mr.sharedMaterial = base_mat;
                path_datas.Add(new CubePathData(n + m * x, n, m, go, go.GetComponent <MeshRenderer>()));
            }
        }

        while (ob > 0)
        {
            int ox = Random.Range(0, x);
            int oz = Random.Range(0, z);

            int ob_index = ox + oz * x;
            var go       = path_datas [ob_index].cube;
            go.name = go.name + " obstacle";
            MeshRenderer mr = go.GetComponent <MeshRenderer> ();
            mr.sharedMaterial = obstacle_mat;
            //mr.material.color = Color.red;
            CubePathData data = new CubePathData(ox + oz * x, ox, oz, go, mr);
            data.cost = 65535f;
            obstacle_datas.Add(data);
            path_datas [ob_index] = data;
            --ob;
        }
    }
コード例 #5
0
    public MoveCube(CubePathData stand, GameObject cube)
    {
        this._stand_cube = stand;
        this.self_cube   = cube;

        this.self_cube.transform.position =
            new Vector3(_stand_cube.cube.transform.position.x,
                        _stand_cube.cube.transform.position.y + 1,
                        _stand_cube.cube.transform.position.z);
    }
コード例 #6
0
    void Update()
    {
        if (EventSystem.current.IsPointerOverGameObject(0) == false && state != State.None && Input.GetMouseButtonUp(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            RaycastHit hit_info;

            if (Physics.Raycast(ray, out hit_info))
            {
                if (hit_info.transform.tag == "Path_Cube")
                {
                    Debug.Log("Click On Path: " + hit_info.transform.name);
                    Debug.Log("Click On Path_index: " + hit_info.transform.GetSiblingIndex());

                    CubePathData cube = path_datas[hit_info.transform.GetSiblingIndex()];

                    if (state == State.Set_Start)
                    {
                        if (cube.cost != 65535)
                        {
                            for (int i = 0; i < 10; ++i)
                            {
                                var move_cube = new MoveCube(cube, GameObject.CreatePrimitive(PrimitiveType.Cube));
                                move_cube.self_cube.GetComponent <MeshRenderer> ().sharedMaterial = mover_mat;
                                move_cube.self_cube.transform.localScale = new Vector3(0.2f, .2f, .2f);
                                move_cube.self_cube.transform.position  += new Vector3(Random.Range(-0.4f, 0.4f), 0, Random.Range(-0.4f, 0.4f));
                                goer.Add(move_cube);
                            }
                        }
                        else
                        {
                            Debug.LogError("Obstacle is there!");
                        }
                    }
                    else if (state == State.Set_End)
                    {
                        if (end_cube == cube.index)
                        {
//							end_cube = -1;
//							cube.mesh.material = null;
//							cube.mesh.sharedMaterial = base_mat;
                        }
                        else
                        {
                            end_cube = cube.index;
                            cube.mesh.material.color = Color.blue;
                            GenerateFlowField();
                        }
                    }
                }
            }
        }
    }