private void SetSelected(Gridmap map)
 {
     RaycastHit hitinfo;
     if (Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition), out hitinfo))
     {
         selected = map.GetCellFromPoint(hitinfo.point);
         if (selected.state != GridmapCell.CellState.Buildable) selected = null;
     }
 }
        // Use this for initialization
        void Start()
        {
            if (map == null) map = GameObject.FindObjectOfType<Gridmap>();

            if (SetPosition(gameObject.transform.position))
            {
                gameObject.transform.position = new Vector3(center.x, center.y + gameObject.transform.renderer.bounds.size.y / 2, center.z);
            }
        }
Beispiel #3
0
            public static List<Vector3> FindPath(GridmapCell start, GridmapCell end, Gridmap map, bool check)
            {
                List<Vector3> path = new List<Vector3>();
                if (start == null || end == null) return path;

                openList.Clear();
                closedList.Clear();
                checkValiditiy = check;
                AStarRecord.finalTarget = end.center;

                openList.Add(new AStarRecord(start, null, 0));

                while (openList.Count > 0)
                {
                    AStarRecord current = openList[0];
                    if (current.cell.Equals(end)) break;

                    bool up_ok =    PlaceInCorrectList(map[current.cell.x, current.cell.z + 1], current, 1.0f);
                    bool left_ok =  PlaceInCorrectList(map[current.cell.x - 1, current.cell.z], current, 1.0f);
                    bool right_ok = PlaceInCorrectList(map[current.cell.x + 1, current.cell.z], current, 1.0f);
                    bool down_ok =  PlaceInCorrectList(map[current.cell.x, current.cell.z - 1], current, 1.0f);

                    if (up_ok && left_ok) PlaceInCorrectList(map[current.cell.x - 1, current.cell.z + 1], current, 1.4f);
                    if (up_ok && right_ok) PlaceInCorrectList(map[current.cell.x + 1, current.cell.z + 1], current, 1.4f);
                    if (down_ok && left_ok) PlaceInCorrectList(map[current.cell.x - 1, current.cell.z - 1], current, 1.4f);
                    if (down_ok && right_ok) PlaceInCorrectList(map[current.cell.x + 1, current.cell.z - 1], current, 1.4f);

                    closedList.Add(current);
                    openList.RemoveAt(0);

                    // TODO: Don't sort, just cache the index of the lowest cost (n instead of nlogn)
                    openList.Sort((l1, l2) => l1.estimatedFinalCost.CompareTo(l2.estimatedFinalCost));
                }

                if (openList.Count == 0) return path;   // no path found.

                AStarRecord r = openList[0];
                do
                {
                    path.Add(r.cell.center);
                    r = r.parent;
                } while (r != null && !r.cell.Equals(start));
                path.Reverse();
                return path;
            }
        void Start()
        {
            activemap = FindObjectOfType<Gridmap>();

            Messenger.RegisterListener("GridmapCellValidityChanged", Repath);
        }
 // Use this for initialization
 void Start()
 {
     cCntrlr = gameObject.GetComponent<CharacterController>();
     activemap = FindObjectOfType<Gridmap>();
     nextPosition = transform.position;
 }