private void Awake()//Ran once the program starts { instance = this; GameObject player2 = GameObject.Find("Lumberjack2"); player2.SetActive(false); GameObject player3 = GameObject.Find("Lumberjack3"); player3.SetActive(false); fNodeDiameter = fNodeRadius * 2; //Double the radius to get diameter iGridSizeX = Mathf.RoundToInt(vGridWorldSize.x / fNodeDiameter); //Divide the grids world co-ordinates by the diameter to get the size of the graph in array units. iGridSizeY = Mathf.RoundToInt(vGridWorldSize.y / fNodeDiameter); //Divide the grids world co-ordinates by the diameter to get the size of the graph in array units. CreateGrid(); //Draw the grid if (scenario == Scenario.Known && algorithm == Algorithm.AStar) { print("Scenario : " + scenario + ", Algorithm = " + algorithm); PathfindingA PAscript = this.gameObject.AddComponent(typeof(PathfindingA)) as PathfindingA; GameObject player = GameObject.Find("Agent"); Unit unitScript = player.gameObject.AddComponent(typeof(Unit)) as Unit; unitScript.enabled = true; PAscript.enabled = true; } else if (scenario == Scenario.Known && algorithm == Algorithm.Dijkstra) { print("Scenario : " + scenario + ", Algorithm = " + algorithm); Dijkstra DIJscript = this.gameObject.AddComponent(typeof(Dijkstra)) as Dijkstra; GameObject player = GameObject.Find("Agent"); UnitDijkstra unitDijkstraScript = player.gameObject.AddComponent(typeof(UnitDijkstra)) as UnitDijkstra; unitDijkstraScript.enabled = true; DIJscript.enabled = true; } else if (scenario == Scenario.Unknown && searchMode == SearchMode.CellByCell) { print("Scenario : " + scenario + ", Search Mode : " + searchMode + " , Algorithm = " + algorithm); GameObject player = GameObject.Find("Agent"); UnitSearchUnkCell UnitSearchscript = player.gameObject.AddComponent(typeof(UnitSearchUnkCell)) as UnitSearchUnkCell; UnitSearchscript.enabled = true; } else if (scenario == Scenario.Unknown && searchMode == SearchMode.Randomly) { print("Scenario : " + scenario + ", Search Mode : " + searchMode + " , Algorithm = " + algorithm); GameObject player = GameObject.Find("Agent"); UnitRandom UnitRandomscript = player.gameObject.AddComponent(typeof(UnitRandom)) as UnitRandom; UnitRandomscript.enabled = true; } else if (scenario == Scenario.ThreeAgents) { player2.SetActive(true); player3.SetActive(true); print("Scenario : " + scenario + ", Algorithm = " + algorithm); ThreeAgents CompThreeAgents = this.gameObject.AddComponent(typeof(ThreeAgents)) as ThreeAgents; CompThreeAgents.enabled = true; } }
public void groupLumber(GameObject lumberjack1, GameObject lumberjack2, GameObject tree) { if (firstCall) { firstCall = false; if (lumberjack1.GetComponent <UnitRandom>() != null) { Destroy(lumberjack1.GetComponent <UnitRandom>()); //remove } else { Destroy(lumberjack1.GetComponent <UnitSearchUnkCell>()); } if (lumberjack2.GetComponent <UnitRandom>() != null) { Destroy(lumberjack2.GetComponent <UnitRandom>()); //remove } else { Destroy(lumberjack2.GetComponent <UnitSearchUnkCell>()); } if (GridWorld.instance.algorithm == GridWorld.Algorithm.AStar) { PathfindingA Pscript = this.gameObject.AddComponent(typeof(PathfindingA)) as PathfindingA; Pscript.enabled = true; UnitThree unitScript = lumberjack1.gameObject.AddComponent(typeof(UnitThree)) as UnitThree; unitScript.enabled = true; UnitThree unitScript2 = lumberjack2.gameObject.AddComponent(typeof(UnitThree)) as UnitThree; unitScript2.enabled = true; } if (GridWorld.instance.algorithm == GridWorld.Algorithm.Dijkstra) { Dijkstra DJscript = this.gameObject.AddComponent(typeof(Dijkstra)) as Dijkstra; DJscript.enabled = true; UnitThreeDJ unitScript = lumberjack1.gameObject.AddComponent(typeof(UnitThreeDJ)) as UnitThreeDJ; unitScript.enabled = true; UnitThreeDJ unitScript2 = lumberjack2.gameObject.AddComponent(typeof(UnitThreeDJ)) as UnitThreeDJ; unitScript2.enabled = true; } } }
private void Awake()//When the program starts { instance = this; GridReference = GetComponent <GridWorld>();//Get a reference to the game manager }
// Use this for initialization void Start() { selected_units = new List <Unit>(); pathfinding = new PathfindingA(grid.CountInX, grid.CountInZ, grid.GetGrid()); spotAlgorithm = new Spot(); }
public List <UnitPoint> MakeSpot(Vector3 StartPos, Vector3 Center, Grid grid, int unitCount, Item item, PathfindingA pathfindingA) { Vector3 direction = (Center - StartPos).normalized; this.grid = grid; Strategy.InitStrategy(); List <Vector3> EndCells = Strategy.GetMoveCells(direction, Center, unitCount, grid, item); spotsArray.Clear(); activeSpots.Clear(); pointsToCover.Clear(); CoveredPoints.Clear(); foreach (var Cell in EndCells) { AddToCover(new UnitPoint(grid, Cell)); } Vector2 centerPos = grid.GetPointByPosition(Center); Vector2 startPosInGrid = grid.GetPointByPosition(StartPos); foreach (var startPos in Strategy.GetStartPoints(centerPos, pointsToCover)) { bool walkable = grid.GetGrid()[(int)startPos.x, (int)startPos.y] == 0; if (walkable && pathfindingA.HasPath(startPos, startPosInGrid) || !walkable) { AddToSpot(new SpotPoint(startPos, walkable)); } } while (CoveredPoints.Count < unitCount) { while (pointsToCover.Count != 0 && CoveredPoints.Count < unitCount && HasActiveSpot()) { SpotPoint point = GetCurrentActiveSpot(); List <SpotPoint> Neighbours = GetNeighbours(point, grid); AddToSpot(Neighbours, unitCount); } if (CoveredPoints.Count < unitCount) { List <Vector3> NewCells = Strategy.ExpandPoints(direction, Center, unitCount, grid, item); foreach (var cell in NewCells) { AddToCover(new UnitPoint(grid, cell)); } CheckNewPointsToCover(unitCount); } if (!HasActiveSpot()) { /*foreach (var line in spotsArray.Values) * { * foreach (SpotPoint point in line.Values) * { * grid.PlaceSpot(point.X, point.Z, point.Walkable); * } * } */ return(new List <UnitPoint>()); } } /*foreach (var line in spotsArray.Values) * { * foreach (SpotPoint point in line.Values) * { * grid.PlaceSpot(point.X, point.Z, point.Walkable); * } * }*/ return(CoveredPoints); }
private void Update() { if ((foundSawmill && foundTree) && (GridWorld.instance.scenario != GridWorld.Scenario.ThreeAgents)) { print("Found Both!!"); if (GridReference.algorithm == GridWorld.Algorithm.AStar) { GameObject GameManager = GameObject.Find("GameManager"); PathfindingA PAscript = GameManager.gameObject.AddComponent(typeof(PathfindingA)) as PathfindingA; PAscript.enabled = true; Unit unitScript = this.gameObject.AddComponent(typeof(Unit)) as Unit; unitScript.enabled = true; Destroy(this.GetComponent <UnitRandom>()); //remove return; } if (GridReference.algorithm == GridWorld.Algorithm.Dijkstra) { GameObject GameManager = GameObject.Find("GameManager"); Dijkstra DJscript = GameManager.gameObject.AddComponent(typeof(Dijkstra)) as Dijkstra; DJscript.enabled = true; UnitDijkstra UnitDijkstraScript = this.gameObject.AddComponent(typeof(UnitDijkstra)) as UnitDijkstra; UnitDijkstraScript.enabled = true; Destroy(this.GetComponent <UnitRandom>()); //remove return; } } if (TargetNode.vPosition != transform.position) { Vector3 forward = TargetNode.vPosition - transform.position; targetRotation = Quaternion.LookRotation(forward); transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime); } if (TargetNode.iGridX == 0) { //we are the left edge randomMove = Random.Range(1, 4); switch (randomMove) { case 1: moveX = 1; moveY = 0; break; case 2: moveX = 1; moveY = 1; break; case 3: moveX = 1; moveY = -1; break; default: break; } } if (TargetNode.iGridX >= GridReference.vGridWorldSize.x - 1) { //we are on right edge randomMove = Random.Range(1, 4); switch (randomMove) { case 1: moveX = -1; moveY = 0; break; case 2: moveX = -1; moveY = 1; break; case 3: moveX = -1; moveY = -1; break; default: break; } } if (TargetNode.iGridY == 0) { //we are the Bottom edge randomMove = Random.Range(1, 4); switch (randomMove) { case 1: moveX = -1; moveY = 1; break; case 2: moveX = 1; moveY = 1; break; case 3: moveX = 0; moveY = 1; break; default: break; } } if (TargetNode.iGridY >= GridReference.vGridWorldSize.y - 1) { //we are on Top edge randomMove = Random.Range(1, 4); switch (randomMove) { case 1: moveX = 0; moveY = -1; break; case 2: moveX = 1; moveY = -1; break; case 3: moveX = -1; moveY = -1; break; default: break; } } if ((TargetNode.iGridX == 0) && (TargetNode.iGridY == 0)) { //we are the left bottom corner randomMove = Random.Range(1, 5); switch (randomMove) { case 1: moveX = 1; moveY = 0; break; case 2: moveX = 0; moveY = 1; break; case 3: moveX = 1; moveY = 1; break; case 4: moveX = 1; moveY = 1; break; default: break; } } if ((TargetNode.iGridX == 0) && (TargetNode.iGridY >= GridReference.vGridWorldSize.y - 1)) { //we are the left top corner randomMove = Random.Range(1, 5); switch (randomMove) { case 1: moveX = 1; moveY = 0; break; case 2: moveX = 0; moveY = -1; break; case 3: moveX = 1; moveY = -1; break; case 4: moveX = 1; moveY = -1; break; default: break; } } if ((TargetNode.iGridX >= GridReference.vGridWorldSize.x - 1) && ((TargetNode.iGridY >= GridReference.vGridWorldSize.y - 1))) { //we are on right top corner randomMove = Random.Range(1, 5); switch (randomMove) { case 1: moveX = -1; moveY = 0; break; case 2: moveX = 0; moveY = -1; break; case 3: moveX = -1; moveY = -1; break; case 4: moveX = -1; moveY = -1; break; default: break; } } if ((TargetNode.iGridX >= GridReference.vGridWorldSize.x - 1) && (TargetNode.iGridY == 0)) { //we are on right bottom corner randomMove = Random.Range(1, 5); switch (randomMove) { case 1: moveX = 0; moveY = 1; break; case 2: moveX = -1; moveY = 0; break; case 3: moveX = -1; moveY = 1; break; case 4: moveX = -1; moveY = 1; break; default: break; } } if ((TargetNode.iGridX < GridReference.vGridWorldSize.x - 1) && (TargetNode.iGridY < GridReference.vGridWorldSize.y - 1) && (TargetNode.iGridX > 0) && (TargetNode.iGridY > 0)) { Left = GridReference.NodeFromGridPos(TargetNode.iGridX - 1, TargetNode.iGridY); Bottom = GridReference.NodeFromGridPos(TargetNode.iGridX, TargetNode.iGridY - 1); Up = GridReference.NodeFromGridPos(TargetNode.iGridX, TargetNode.iGridY + 1); Right = GridReference.NodeFromGridPos(TargetNode.iGridX + 1, TargetNode.iGridY); if (!Left.isWalkable) { randomMove = Random.Range(1, 4); switch (randomMove) { case 1: moveX = 1; moveY = 0; break; case 2: moveX = 1; moveY = 1; break; case 3: moveX = 1; moveY = -1; break; default: break; } } else if (!Right.isWalkable) { randomMove = Random.Range(1, 4); switch (randomMove) { case 1: moveX = -1; moveY = 0; break; case 2: moveX = -1; moveY = 1; break; case 3: moveX = -1; moveY = -1; break; default: break; } } else if (!Up.isWalkable) { randomMove = Random.Range(1, 4); switch (randomMove) { case 1: moveX = 0; moveY = -1; break; case 2: moveX = 1; moveY = -1; break; case 3: moveX = -1; moveY = -1; break; default: break; } } else if (!Bottom.isWalkable) { randomMove = Random.Range(1, 4); switch (randomMove) { case 1: moveX = -1; moveY = 1; break; case 2: moveX = 1; moveY = 1; break; case 3: moveX = 0; moveY = 1; break; default: break; } } } if ((moveX == -1) && (moveY == 0)) { movingLeft = true; movingBottom = false; movingRight = false; movingTop = false; movingTopLeft = false; movingTopRight = false; movingBottomLeft = false; movingBottomRight = false; } else if ((moveX == 1) && (moveY == 0)) { movingLeft = false; movingBottom = false; movingRight = true; movingTop = false; movingTopLeft = false; movingTopRight = false; movingBottomLeft = false; movingBottomRight = false; } else if ((moveX == 0) && (moveY == -1)) { movingLeft = false; movingBottom = true; movingRight = false; movingTop = false; movingTopLeft = false; movingTopRight = false; movingBottomLeft = false; movingBottomRight = false; } else if ((moveX == 0) && (moveY == 1)) { movingLeft = false; movingBottom = false; movingRight = false; movingTop = true; movingTopLeft = false; movingTopRight = false; movingBottomLeft = false; movingBottomRight = false; } else if ((moveX == 1) && (moveY == 1)) { movingLeft = false; movingBottom = false; movingRight = false; movingTop = false; movingTopLeft = false; movingTopRight = true; movingBottomLeft = false; movingBottomRight = false; } else if ((moveX == -1) && (moveY == 1)) { movingLeft = false; movingBottom = false; movingRight = false; movingTop = false; movingTopLeft = true; movingTopRight = false; movingBottomLeft = false; movingBottomRight = false; } else if ((moveX == 1) && (moveY == -1)) { movingLeft = false; movingBottom = false; movingRight = false; movingTop = false; movingTopLeft = false; movingTopRight = false; movingBottomLeft = false; movingBottomRight = true; } else if ((moveX == -1) && (moveY == -1)) { movingLeft = false; movingBottom = false; movingRight = false; movingTop = false; movingTopLeft = false; movingTopRight = false; movingBottomLeft = true; movingBottomRight = false; } if (Vector3.Distance(transform.position, TargetNode.vPosition) < 0.1f) { if (movingLeft) { TargetNode = GridReference.NodeFromGridPos(TargetNode.iGridX - 1, TargetNode.iGridY); } else if (movingBottom) { TargetNode = GridReference.NodeFromGridPos(TargetNode.iGridX, TargetNode.iGridY - 1); } else if (movingRight) { TargetNode = GridReference.NodeFromGridPos(TargetNode.iGridX + 1, TargetNode.iGridY); } else if (movingTop) { TargetNode = GridReference.NodeFromGridPos(TargetNode.iGridX, TargetNode.iGridY + 1); } else if (movingTopLeft) { TargetNode = GridReference.NodeFromGridPos(TargetNode.iGridX - 1, TargetNode.iGridY + 1); } else if (movingTopRight) { TargetNode = GridReference.NodeFromGridPos(TargetNode.iGridX + 1, TargetNode.iGridY + 1); } else if (movingBottomLeft) { TargetNode = GridReference.NodeFromGridPos(TargetNode.iGridX - 1, TargetNode.iGridY - 1); } else if (movingBottomRight) { TargetNode = GridReference.NodeFromGridPos(TargetNode.iGridX + 1, TargetNode.iGridY - 1); } } //print("Position X = " + TargetNode.iGridX + " and Y = " + TargetNode.iGridY); transform.position = Vector3.Lerp(transform.position, TargetNode.vPosition, speed * Time.deltaTime); TargetNode.isDiscovered = true; }
private void Update() { if ((foundSawmill && foundTree) && (GridWorld.instance.scenario != GridWorld.Scenario.ThreeAgents)) { print("Found Both!!"); if (GridReference.algorithm == GridWorld.Algorithm.AStar) { GameObject GameManager = GameObject.Find("GameManager"); PathfindingA PAscript = GameManager.gameObject.AddComponent(typeof(PathfindingA)) as PathfindingA; PAscript.enabled = true; Unit unitScript = this.gameObject.AddComponent(typeof(Unit)) as Unit; unitScript.enabled = true; Destroy(this.GetComponent <UnitSearchUnkCell>()); //remove return; } if (GridReference.algorithm == GridWorld.Algorithm.Dijkstra) { GameObject GameManager = GameObject.Find("GameManager"); Dijkstra DJscript = GameManager.gameObject.AddComponent(typeof(Dijkstra)) as Dijkstra; DJscript.enabled = true; UnitDijkstra UnitDijkstraScript = this.gameObject.AddComponent(typeof(UnitDijkstra)) as UnitDijkstra; UnitDijkstraScript.enabled = true; Destroy(this.GetComponent <UnitSearchUnkCell>()); //remove return; } } if (TargetNode.vPosition != transform.position) { Vector3 forward = TargetNode.vPosition - transform.position; targetRotation = Quaternion.LookRotation(forward); transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime); } if (TargetNode.iGridX == 0) { //we are the left edge, move up TargetNode = GridReference.NodeFromGridPos(1, TargetNode.iGridY + 1); movingLeft = false; } if (TargetNode.iGridX >= GridReference.vGridWorldSize.x - 1) { //we are on right edge, move up TargetNode = GridReference.NodeFromGridPos(TargetNode.iGridX - 1, TargetNode.iGridY + 1); movingLeft = true; } if ((TargetNode.iGridX <= GridReference.vGridWorldSize.x - 1) && (TargetNode.iGridY <= GridReference.vGridWorldSize.y - 1)) { if (avoidWall) { Up = GridReference.NodeFromGridPos(TargetNode.iGridX, TargetNode.iGridY + 1); if ((Up.isWalkable) && (Up.isDiscovered)) { TargetNode = GridReference.NodeFromGridPos(TargetNode.iGridX, TargetNode.iGridY + 1); } if ((Up.isWalkable) && (!Up.isDiscovered)) { TargetNode = GridReference.NodeFromGridPos(TargetNode.iGridX, TargetNode.iGridY + 1); avoidWall = false; } } Left = GridReference.NodeFromGridPos(TargetNode.iGridX - 1, TargetNode.iGridY); if ((!Left.isWalkable) && (Left.iGridX >= 1)) { TargetNode = GridReference.NodeFromGridPos(TargetNode.iGridX, TargetNode.iGridY - 1); avoidWall = true; } Right = GridReference.NodeFromGridPos(TargetNode.iGridX + 1, TargetNode.iGridY); if ((!Right.isWalkable) && (!(Right.iGridX >= GridReference.vGridWorldSize.x - 1))) { TargetNode = GridReference.NodeFromGridPos(TargetNode.iGridX, TargetNode.iGridY - 1); avoidWall = true; } } if (Vector3.Distance(transform.position, TargetNode.vPosition) < 0.1f) { TargetNode = GridReference.NodeFromGridPos((movingLeft == false)?TargetNode.iGridX + 1: TargetNode.iGridX - 1, TargetNode.iGridY); } transform.position = Vector3.Lerp(transform.position, TargetNode.vPosition, speed * Time.deltaTime); TargetNode.isDiscovered = true; }