private void BuildRandomChamber(PathTile tile) { int chamberSize = 3, adjacentTileCount = tile.adjacentPathTiles.Count, randomIndex = Random.Range(0, adjacentTileCount); Vector2 chamberOrigin = tile.adjacentPathTiles [randomIndex]; for (int x = (int)chamberOrigin.x; x < chamberOrigin.x + chamberSize; x++) { for (int y = (int)chamberOrigin.y; y < chamberOrigin.y + chamberSize; y++) { Vector2 chamberTilePos = new Vector2(x, y); if (!gridPositions.ContainsKey(chamberTilePos) && chamberTilePos.x < maxBound && chamberTilePos.x > 0 && chamberTilePos.y < maxBound && chamberTilePos.y > 0) { if (Random.Range(0, 70) == 1) { gridPositions.Add(chamberTilePos, TileType.chest); } else { gridPositions.Add(chamberTilePos, TileType.empty); } } } } }
private void BuildEssentialPath() { int randomY = Random.Range(0, maxBound + 1); startPos = new Vector2(0, randomY); PathTile path = new PathTile(TileType.Essential, startPos, minBound, maxBound, positionGrid); int boundTracker = 0; while (boundTracker < maxBound) { //the first tile is an empty tile(entrance) positionGrid.Add(path.position, TileType.Empty); int adjacentTileCount = path.adjacentTiles.Count; if (adjacentTileCount <= 0) { break; } int randomIndex = Random.Range(0, adjacentTileCount); PathTile nextPath = new PathTile(TileType.Essential, path.adjacentTiles[randomIndex], minBound, maxBound, positionGrid); if (nextPath.position.x > path.position.x || (((int)nextPath.position.x) == maxBound - 1 && Random.Range(0, 2) == 1)) { ++boundTracker; } path = nextPath; } //the last tile is a empty tile(exit) if (!positionGrid.ContainsKey(path.position)) { positionGrid.Add(path.position, TileType.Empty); } endPos = new Vector2(path.position.x, path.position.y); }
public PlayerController FindClosestPlayer(PlayerController[] players) { if(players.Length == 0) return null; PlayerController closest = null; List<PathTile> currentPath = new List<PathTile>(); List<PathTile> shortestPath = new List<PathTile>(); for (int i = 0; i < players.Length; i++) { target = players[i].start; tileMap.FindPath(start, players[i].start, currentPath, isWalkable); if(currentPath.Count == 0) continue; if (shortestPath.Count == 0 || currentPath.Count <= shortestPath.Count) { Debug.Log("Changing targets"); //shortestPath.Clear(); tileMap.FindPath(start, players[i].start, shortestPath, isWalkable); closest = players[i]; } //currentPath.Clear(); } if(closest) pTarget = closest; else active = false; return closest; }
/// <summary> /// Creates a list of reachable tiles using the starting point and current movement points. Each PathTile in the list is the final tile in a path. /// Only the previous tile is recorded. /// </summary> /// <param name="startTile"></param> /// <param name="movementPoints"></param> /// <returns></returns> private List <PathTile> WithinWalkingDistance(Tile startTile, int movementPoints) { if (startTile == null) { //Debug.Log("Starting tile for path is null!"); return(null); } PathTile startPathTile = new PathTile(startTile, null, movementPoints, null); List <PathTile> unprocessedTiles = null; List <PathTile> processedTiles = new List <PathTile>(); unprocessedTiles = ProcessPathTile(startPathTile); while (unprocessedTiles != null && unprocessedTiles.Count > 0) { PathTile tempTile = unprocessedTiles[0]; if (tempTile != null) { List <PathTile> tempList = null; if (Upsert(processedTiles, tempTile)) { tempList = ProcessPathTile(tempTile); } unprocessedTiles.Remove(tempTile); if (tempList != null) { unprocessedTiles.AddRange(tempList); } } } return(processedTiles); }
private void BuildRandomPath() { List <PathTile> pathQueue = new List <PathTile> (); foreach (KeyValuePair <Vector2, TileType> tile in gridPositions) { Vector2 tilePos = new Vector2(tile.Key.x, tile.Key.y); pathQueue.Add(new PathTile(TileType.random, tilePos, minBound, maxBound, gridPositions)); } for (int x = 0; x < pathQueue.Count; x++) { int adjacentTileCount = pathQueue[x].adjacentPathTiles.Count; if (adjacentTileCount != 0) { if (Random.Range(0, 5) == 1) { BuildRandomChamber(pathQueue[x]); } else if (Random.Range(0, 5) == 1 || (pathQueue[x].type == TileType.random && adjacentTileCount > 1)) { int randomIndex = Random.Range(0, adjacentTileCount); Vector2 newRPathPos = pathQueue[x].adjacentPathTiles[randomIndex]; if (!gridPositions.ContainsKey(newRPathPos)) { gridPositions.Add(newRPathPos, TileType.empty); PathTile newRPath = new PathTile(TileType.random, newRPathPos, minBound, maxBound, gridPositions); pathQueue.Add(newRPath); } } } } }
void AddToPortalListLogic() { for (int i = 0; i < myPathManager.GetPortals.Count; i++) { if (myPathManager.CheckPlacement(myInputCoordinates, myPathManager.GetPortals[i].myStartTile)) { if (myValidPlacement[i] == true) { PathTile path = myBuildManager.SpawnFromPool(1, Quaternion.identity, myInputCoordinates); path.GetPathTilePosition = myInputCoordinates; //myPathManager.GetPathTileMap[myInputCoordinates.x, myInputCoordinates.z] = path; myPathManager.AddItemToPortalMap(path, i); path.CheckNeighbors(); Debug.Log("Add item to portal list"); WorldController.Instance.GetWorld.SetTileState(myInputCoordinates.x, myInputCoordinates.z, Tile.TileState.road); if (myBooleansPortal[i] == false) { myBooleansPortal[i] = true; } } //isPlaceingByPortal = true; } } }
// adds 3x3 chamber to the end of the random path private void BuildRandomChamber(PathTile tile) { int chamberSize = 3; // todo can be ramdomized int adjacentTileCount = tile.adjacentPathTiles.Count; int randomIndex = Random.Range(0, adjacentTileCount); Vector2 chamberOrigin = tile.adjacentPathTiles[randomIndex]; for (int x = (int)chamberOrigin.x; x < chamberOrigin.x + chamberSize; x++) { for (int y = (int)chamberOrigin.y; y < chamberOrigin.y + chamberSize; y++) { Vector2 chamberTilePosition = new Vector2(x, y); if (!gridPositions.ContainsKey(chamberTilePosition) && (chamberTilePosition.x < maxBound && chamberTilePosition.x > 0) && (chamberTilePosition.y < maxBound && chamberTilePosition.y > 0)) { // gridPositions.Add(chamberTilePosition, TileType.EMPTY); if (Random.Range(0, 25) == 1) { gridPositions.Add(chamberTilePosition, TileType.CHEST); } else { gridPositions.Add(chamberTilePosition, TileType.EMPTY); } } } } }
public PathTile(Vector2Int t, PathTile cf, Vector2Int dest) { Tile = t; CameFrom = cf; FromStart = CameFrom != null ? CameFrom.FromStart + 1 : 0; FromEnd = Mathf.Abs(Tile.x - dest.x) + Mathf.Abs(Tile.y - dest.y); Value = FindValue(); }
public void ClearUsedTiles(PlayerUnit player) { int length = this.PathFromTo.Count; int removeCount = 0; if (PathFrom.Tile != player.Tile) { for (int i = 0; i < length; i++) { if (PathFromTo[i].Tile == player.Tile) { removeCount = i; break; } else { PathFromTo[i].Tile.Highlight(false); } } if (length - 1 == removeCount) { Clear(); } else { PathFromTo.RemoveRange(0, removeCount); PathFrom = PathFromTo[0]; } } }
public void CalculateTurns(PlayerUnit player) { int movesPerTurn = player.MaxMovePoints; int currentMoves = player.CurrentMovePoints; PathBreak.Clear(); int lastTurn = 0; int currentTurn = 0; int distance = movesPerTurn - currentMoves; int length = this.PathFromTo.Count; for (int i = 1; i < length; i++) { PathTile current = PathFromTo[i]; distance += current.MoveCost; current.Distance = distance; currentTurn = (distance - 1) / movesPerTurn; current.Turn = currentTurn; current.isBreak = false; if (currentTurn > lastTurn) { distance = (movesPerTurn * currentTurn) + current.MoveCost; current.Distance = distance; PathFromTo[i - 1].isBreak = true; PathBreak.AddLast(PathFromTo[i - 1]); } lastTurn = currentTurn; } }
/// <summary> /// The main method for moving! Walking will not work if the default value (null) for destinationPathTile is used, as the path is supplied with the said variable. /// </summary> /// <param name="destinationTile"></param> /// <param name="method"></param> /// <param name="destinationPathTile"></param> /// <returns></returns> public bool MoveToTile(Tile destinationTile, MovementMethod method, PathTile destinationPathTile = null) { switch (method) { case MovementMethod.NotSpecified: Debug.Log("Movement method not selected!"); break; case MovementMethod.Teleport: Teleport(destinationTile); break; case MovementMethod.Walk: if (destinationTile.myType == Tile.BlockType.BlockyBlock || destinationTile.CharCurrentlyOnTile != null) { return(false); } if (destinationPathTile != null && destinationPathTile._tile == destinationTile) { List <PathTile> route = LinkListAndOrder(destinationPathTile); StartCoroutine("Walk", route); } break; default: Debug.Log("Error with movement method selection!"); //Not yet implemented? break; } CurrentTile = destinationTile; return(true); }
public static float PathCost(PathTile from, PathTile to) { var diff = to.positionTop - from.positionTop; var heightcost = diff.y * diff.y * diff.y; var cost = Mathf.Round((Mathf.Sqrt(((diff.x * diff.x) + (diff.z * diff.z))) + heightcost) * 1000.0f) / 1000.0f; return(cost > 0.01f ? cost : 0); }
public PathTile(Type type, int x, int y) { TileType = type; Parent = null; X = x; Y = y; F = G = H = 0; }
public bool Equals(PathTile tile) { if (tile == null) { return(false); } return(X == tile.X && Y == tile.Y); }
public void Clear() { HidePath(); PathFromTo.Clear(); PathBreak.Clear(); PathFrom = null; PathTo = null; }
void CreatePath(int x, int y) { world [x, y] = new WorldTile(TileType.path, x, y); GameObject path = Instantiate(pathPrefab, new Vector3(x, 0, y), Quaternion.identity); PathTile script = path.GetComponent <PathTile> (); script.SetPos(x, y); pathTiles.Add(script); }
public PathTile(PathTile copyFrom) { Tile = copyFrom.Tile; this.PathFrom = copyFrom.PathFrom; this.distance = copyFrom.Distance; this.Turn = copyFrom.Turn; this.isBreak = copyFrom.isBreak; this.moveCost = copyFrom.MoveCost; }
private void BuildRandomPath() { List <PathTile> pathQueue = new List <PathTile>(); foreach (KeyValuePair <Vector2, TileType> tile in positionGrid) { Vector2 randomPos = new Vector2(tile.Key.x, tile.Key.y); pathQueue.Add(new PathTile(TileType.Random, randomPos, minBound, maxBound, positionGrid)); } //pathQueue.ForEach(delegate (PathTile tile) //{ // int adjacent = tile.adjacentTiles.Count; // if (adjacent != 0) // { // if (Random.Range(0, 5) == 1) // { // BuildChamber(tile); // } else if (Random.Range(0, 5) == 1 || (tile.type == TileType.Random && adjacent > 1)) // { // int randomIndex = Random.Range(0, adjacent); // Vector2 pathPos = tile.adjacentTiles[randomIndex]; // positionGrid.Add(pathPos, TileType.Empty); // pathQueue.Add(new PathTile(TileType.Random, pathPos, minBound, maxBound, positionGrid)); // } // } //}); for (int i = 0; i < pathQueue.Count; i++) { PathTile tile = pathQueue[i]; int adjacent = tile.adjacentTiles.Count; if (adjacent != 0) { if (Random.Range(0, 5) == 1) { BuildChamber(tile); } else if (Random.Range(0, 5) == 1 || (tile.type == TileType.Random && adjacent > 1)) { int randomIndex = Random.Range(0, adjacent); Vector2 pathPos = tile.adjacentTiles[randomIndex]; if (!positionGrid.ContainsKey(pathPos)) { if (Random.Range(0, 30) == 1) { positionGrid.Add(pathPos, TileType.Enemy); } else { positionGrid.Add(pathPos, TileType.Empty); } } pathQueue.Add(new PathTile(TileType.Random, pathPos, minBound, maxBound, positionGrid)); } } } }
public PathTrail(PathTrail copyFrom) { PathFromTo = new List <PathTile>(copyFrom.PathFromTo); PathBreak = new LinkedList <PathTile>(copyFrom.PathBreak); if (PathFromTo.Count != 0) { PathFrom = copyFrom.PathFromTo[0]; PathTo = copyFrom.PathFromTo[copyFrom.PathFromTo.Count - 1]; } }
public void TestGetActor() { SimpleTileLayer layer = new SimpleTileLayer(LayerType.All, 7, 7); PathTile path = new PathTile(0); layer.Tiles[3][2] = path; Assert.Equal(path, layer.GetActorAt(3, 2)); Assert.Equal(path, layer.GetActorAt(new Vector2I(3, 2))); }
/// <summary> /// Returns a list of neighbouring tiles as PathTiles, that has 1 movement subtracted when compared to the given currentTile parameter. /// </summary> /// <param name="currentTile"></param> /// <returns></returns> private List <PathTile> CreatePathTileNeighbours(PathTile currentTile) { List <PathTile> pathTiles = new List <PathTile>(); foreach (var tile in currentTile._neighbours) { PathTile pathTile = new PathTile(tile, currentTile._destination, currentTile._movementPointsLeft - 1, currentTile); pathTiles.Add(pathTile); } return(pathTiles); }
public void ResetPath() { for (int i = 0; i < myPathList.Count; i++) { myPathList[i].ResetMe(); myBuildManager.ReturnToPool(myPathList[i]); myLastPlacedPathTile = null; myStartPathTile = null; } myBuildManager.ResetTiles(); }
private void BuildRandomPath() { Queue pathQueue = new Queue(); // copies essential path to the pathQueue foreach (KeyValuePair <Vector2, TileType> tile in gridPositions) { Vector2 tilePosition = new Vector2(tile.Key.x, tile.Key.y); pathQueue.Add(new PathTile(TileType.RANDOM, tilePosition, minBound, maxBound, gridPositions)); } // starts processing while (pathQueue.hasNext) { PathTile tile = pathQueue.nextTile; int adjacentTileCount = tile.adjacentPathTiles.Count; if (adjacentTileCount == 0) { return; } // 1 in 5 chance to create a chamber if (Random.Range(0, 5) == 1) { BuildRandomChamber(tile); } // 1 in 3 chance whether a random path is created // or if tile is RANDOM and more than one direction to move, then creates random path else if (Random.Range(0, 3) == 1 || (tile.type == TileType.RANDOM && adjacentTileCount > 1)) { int randomIndex = Random.Range(0, adjacentTileCount); Vector2 nextRandomPathPosition = tile.adjacentPathTiles[randomIndex]; // if it isn't already part of dungeon if (!gridPositions.ContainsKey(nextRandomPathPosition)) { if (Random.Range(0, 20) == 1) { gridPositions.Add(nextRandomPathPosition, TileType.ENEMY); } else { gridPositions.Add(nextRandomPathPosition, TileType.EMPTY); } PathTile newRandomPath = new PathTile(TileType.RANDOM, nextRandomPathPosition, minBound, maxBound, gridPositions); pathQueue.Add(newRandomPath); } } } }
/// <summary> /// Returns the route by calculating back from destination. Starting tile should have null in the variable _previousTile /// </summary> public static List <Tile> CalculateRouteBack(PathTile destinationTile) { List <PathTile> orderedRoute = LinkListAndOrder(destinationTile); List <Tile> wishIHadMeatballs = new List <Tile>(); foreach (var pathTile in orderedRoute) { wishIHadMeatballs.Add(pathTile._tile); } return(wishIHadMeatballs); }
public void resetStatus() { currentActionPoints += agility; if (currentActionPoints > maxActionPoints) { currentActionPoints = maxActionPoints; } active = true; end = null; start = findClosestTile(); listIndex = 0; }
void HighlightMoveTiles(bool playerSelected) { GameObject[] tiles = GameObject.FindGameObjectsWithTag("Tile"); Character selectedChar; if (selectedObject) { selectedChar = selectedObject.GetComponent <Character>(); } else { return; } List <PathTile> moveableTiles = new List <PathTile>(); moveableTiles.Add(selectedChar.start); for (int i = 0; i < tiles.Length; i++) { tiles[i].GetComponent <MeshRenderer>().material.color = Color.white; } if (playerSelected) { for (int i = 0; i < selectedChar.currentActionPoints + 1; i++) { List <PathTile> tempList = new List <PathTile>(); for (int j = 0; j < moveableTiles.Count; j++) { PathTile tempTile = moveableTiles[j]; if (tempTile.GetComponent <MeshRenderer>().material.color != Color.yellow) { tempTile.GetComponent <MeshRenderer>().material.color = Color.yellow; for (int k = 0; k < tempTile.connections.Count; k++) { if (tempTile.connections[k].GetComponent <MeshRenderer>().material.color != Color.yellow && isWalkable(tempTile.connections[k])) { tempList.Add(tempTile.connections[k]); } } } } for (int l = 0; l < tempList.Count; l++) { moveableTiles.Add(tempList[l]); } } } }
public void AddItemToPortalMap(PathTile aPathTileToAdd, int index) { Debug.Log(index); int x = Mathf.FloorToInt(aPathTileToAdd.GetPathTilePosition.x); int z = Mathf.FloorToInt(aPathTileToAdd.GetPathTilePosition.z); myPathTiles[x, z] = aPathTileToAdd; myPortals[index].AddVectorToMovementList(aPathTileToAdd); myPlacementEffects.transform.position = aPathTileToAdd.transform.position; myPlacementEffects.CheckPlacementIndicators(); }
//translates path encoded in PathTile type to classes linkedlist _path private static void RecreatePath(PathTile endTile) { ResetPath(); PathTile curr = endTile; _cost = endTile.Cost; while (curr.Prev != null) { Path.AddFirst(curr.Curr); curr = curr.Prev; } }
public PathTile Dequeue() { count -= 1; for (; minimum < list.Count; minimum++) { PathTile tile = list[minimum]; if (tile != null) { list[minimum] = tile.NextWithSamePriority; return(tile); } } return(null); }
private static List <Loc> GetBackreference(PathTile currentTile) { List <Loc> path = new List <Loc> { currentTile.Location }; while (currentTile.BackReference != null) { currentTile = currentTile.BackReference; path.Add(currentTile.Location); } return(path); }
public int GetMovementCost() { int cost = 0; PathTile pathbreak = getNextPathBreak(); int length = getBreakIndex(pathbreak); for (int i = 1; i <= length; i++) { cost += PathFromTo[i].MoveCost; Debug.Log(PathFromTo[i].Tile.coordinates + " " + PathFromTo[i].MoveCost); } return(cost); }
private int weight; //The weight as to whether a room is generated #endregion Fields #region Methods public bool FindPath(PathTile start, PathTile end, List<PathTile> path, Predicate<PathTile> isWalkable) { if (!isWalkable(end)) return false; closed.Clear(); source.Clear(); queue.Clear(); closed.Add(start); source.Add(start, null); if (isWalkable(start)) queue.Enqueue(start); while (queue.Count > 0) { var tile = queue.Dequeue(); if (tile == end) { path.Clear(); while (tile != null) { path.Add(tile); tile = source[tile]; } path.Reverse(); return true; } else { foreach (var connection in tile.connections) { if (!closed.Contains(connection) && isWalkable(connection)) { closed.Add(connection); source.Add(connection, tile); queue.Enqueue(connection); } } } } return false; }
public void resetStatus() { target = null; base.resetStatus(); }
protected override IEnumerator OnRequest(HttpListenerContext context) { HttpListenerRequest request = context.Request; StreamReader reader = new StreamReader(request.InputStream); string command = reader.ReadToEnd(); string dataString = ""; JSONWrapper j = new JSONWrapper(command); string givenCommand = ""; string user = ""; try { givenCommand = j["command"]; user = j["username"]; Debug.Log("Receiving command " + givenCommand + " from " + user); } catch(Exception e) { Debug.Log(e.Message); } if (PlayerRegisterRule.PlayerRegister.ContainsKey(user)) { Debug.Log(user + " found, entering command switch"); CommandPanel cp = PlayerRegisterRule.PlayerRegister[user]; cp.IsConnected = true; switch (givenCommand) { // for getting the size (and more if needed) of the map, to generate a grid at runtime case "GetMapData": if (selectedMap != null) { dataString = JsonUtility.ToJson(selectedMap.ToJSON()); } break; // for use during charcter select case "ChooseCharacter": if(GameStateManager.Instance.GameState == GameState.CharacterSelect) { string charSelected = ""; try { charSelected = j["character"]; Debug.Log(charSelected); } catch(Exception e) { Debug.Log(e.Message); } // if the character was able to be assigned if(charSelected != "" && charSelectManager != null) { charSelectManager.SelectCharForPlayer(charSelected, cp); } } break; case "ToggleReady": if (charSelectManager != null) { cp.IsReady = !cp.IsReady; List<bool> readies = new List<bool>(); foreach (CommandPanel r in PlayerRegisterRule.PlayerRegister.Values) { readies.Add(r.IsReady); } if (!readies.Contains(false) && !CharSelectManager.InCountdown) { charSelectManager.StartCoroutine("Countdown"); } else if(readies.Contains(false)) { charSelectManager.StartCoroutine(charSelectManager.StopCountdown()); } } break; // commit their movement path and execute a move case "CommitMove": if(IsYourTurn(cp)) { cp.CommitToMove(); } break; // select a tile to add to the movement path case "SelectTile": if (IsYourTurn(cp)) { if (mapGenerator != null) { try { Debug.Log("Processing tile data: {x:" + j["x"] + "," + "z:" + j["z"] + "}"); cp.PathSelection(mapGenerator.TileArray[int.Parse(j["x"]), int.Parse(j["z"])]); PathTile p = new PathTile(); List<Coordinate> queuedCoords = new List<Coordinate>(); foreach (TileBehavior tb in cp.QueuedPath) { queuedCoords.Add(tb.Coords); } p.path = queuedCoords.ToArray(); Debug.Log(JsonUtility.ToJson(p)); dataString = JsonUtility.ToJson(p); } catch (Exception e) { Debug.Log(e.Message); } } } break; // none of the above default: Debug.Log("Command either not parsed or not valid. Given command was: " + givenCommand); break; } } Debug.Log("Sending " + user + ": " + dataString); byte[] data = Encoding.ASCII.GetBytes(dataString); yield return null; HttpListenerResponse response = context.Response; response.ContentType = "text/plain"; Stream responseStream = response.OutputStream; int count = data.Length; int i = 0; while (i < count) { if (i != 0) yield return null; int writeLength = Math.Min((int)writeStaggerCount, count - i); responseStream.Write(data, i, writeLength); i += writeLength; } }
/* ******************************************* Path Finding Functions ******************************************* */ public List<PathTile> FindMovementTiles(BaseTile startingTile, int Radius) { // starting tiles BaseTile[] startingTiles = startingTile.DirectlyAdjacentTiles(); // resulting tiles to look at List<PathTile> optionalTiles = new List<PathTile>(); // tiles remaining to be checked List<PathTile> recentTiles = new List<PathTile>(); // add to optionalTiles for(int i = 0; i < startingTiles.Length; i++){ // make sure it is a tile if(startingTiles[i] != null){ // make sure enough moves are left if(Radius - startingTiles[i].MovementCost >= 0){ PathTile newPathTile = new PathTile(); newPathTile.PreviousTile = null; newPathTile.CurrentTile = startingTiles[i]; newPathTile.MovesLeft = Radius - startingTiles[i].MovementCost; recentTiles.Add(newPathTile); optionalTiles.Add(newPathTile); } } } while(recentTiles.Count != 0){ List<PathTile> newRecentTiles = new List<PathTile>(); for(int j = 0; j < recentTiles.Count; j++){ if(recentTiles[j].MovesLeft != 0){ // all tiles to be checked from the current tile BaseTile[] attatchedTiles = recentTiles[j].CurrentTile.DirectlyAdjacentTiles(); for(int k = 0; k < attatchedTiles.Length; k++){ // make sure it is a tile, and it is not the starting tile if(attatchedTiles[k] != startingTile && attatchedTiles[k] != null){ // check if enough moves are left if(recentTiles[j].MovesLeft - attatchedTiles[k].MovementCost >= 0){ // create PathTile, add to optionaltiles if within range PathTile newPathTile = new PathTile(); newPathTile.PreviousTile = recentTiles[j].CurrentTile; newPathTile.CurrentTile = attatchedTiles[k]; newPathTile.MovesLeft = recentTiles[j].MovesLeft - attatchedTiles[k].MovementCost; // if the current tile already is in the optional tiles and has more steps remaining replace the existing tile bool alreadyIn = false; for(int l = 0; l < optionalTiles.Count; l++){ if(optionalTiles[l].CurrentTile == newPathTile.CurrentTile){ if(optionalTiles[l].MovesLeft <= newPathTile.MovesLeft){ optionalTiles.Remove(optionalTiles[l]); optionalTiles.Add(newPathTile); newRecentTiles.Add(newPathTile); alreadyIn = true; break; } } } // otherwise, just add the tile if(!alreadyIn){ optionalTiles.Add(newPathTile); newRecentTiles.Add(newPathTile); } } } } } } // switch out the recently added tiles recentTiles = newRecentTiles; } return optionalTiles; }
public void resetStatus() { currentActionPoints += agility; if(currentActionPoints > maxActionPoints) currentActionPoints = maxActionPoints; active = true; end = null; start = findClosestTile(); listIndex = 0; }
protected void Move() { if(start && end && start == end) { end = null; tileList.Clear(); listIndex = 0; clearHighLights(); doneMoving = true; } else if (end && listIndex != tileList.Count && currentActionPoints > 0) { movement = (tileList[listIndex].transform.position + new Vector3(0f, 0.51f, 0f)) - transform.position; movement = movement.normalized * speed; transform.Translate(movement * Time.deltaTime); if (Vector3.Distance(transform.position, tileList[listIndex].transform.position + new Vector3(0f, 0.51f, 0f)) < 0.05f) { if (listIndex == tileList.Count - 1) { start = end; end = null; tileList.Clear(); listIndex = 0; currentActionPoints--; clearHighLights(); doneMoving = true; } else { if(listIndex >= 1) { currentActionPoints--; } listIndex++; } } } }
public PathTile(PathTile parent, Tile tile) : base(tile.X, tile.Z) { this.Parent = parent; Depth = parent.Depth + 1; }
public PathTile(Tile tile) : base(tile.X, tile.Z) { Parent = null; Depth = 0; }
/** * Returns a backwards PathTile path as a LinkedList. * * Arguments * - PathTile pathToReverse - The path to flip */ LinkedList<Tile> FlipPath(PathTile pathToReverse) { LinkedList<Tile> res = new LinkedList<Tile>(); // New list of tiles while (pathToReverse != null) { res.AddFirst(new Tile(pathToReverse.X, pathToReverse.Z)); pathToReverse = pathToReverse.Parent; } res.RemoveFirst(); // Don't need the original path position return res; }
/// <summary> /// Creating SidePath From here /// </summary> /// <param name="tiles">The size, counted in tiles</param> /// <param name="chased">Whether or not the monster is chasing th player</param> private void CreateSidePath(int tiles, bool chased) { //Pick a place to place a SidePath Point previousPosition; Point anchorPosition; do { anchorPosition = keyList[random.Next(0, keyList.Count - 1)]; } while (tileList[anchorPosition] is ExitTile); previousPosition = anchorPosition; Tile nextTile = CanCreateSidePath(anchorPosition); //If it can be placed if (nextTile != null) { tileList.Add(this.position, nextTile); keyList.Add(this.position); if (!chased) { while (tiles > 0) { List<Point> possiblePositions = GetPossiblePositions(this.position); if (possiblePositions.Count > 0) { //Choose where to place the Tile this.position = possiblePositions[random.Next(0, possiblePositions.Count - 1)]; nextTile = new PathTile(pathID); tileList.Add(this.position, nextTile); keyList.Add(this.position); } else { break; } tiles--; } } else { int x = this.position.X - previousPosition.X; int y = this.position.Y - previousPosition.Y; tiles = tiles / 4; while (tiles > 0) { if (!(tileList.ContainsKey(new Point(this.position.X + x, this.position.Y + y)))) { //Choose where to place the Tile this.position.X += x; this.position.Y += y; nextTile = new PathTile(pathID); tileList.Add(this.position, nextTile); keyList.Add(this.position); } else { break; } tiles--; } } } //Try again else { CreateSidePath(tiles, chased); } }
public bool FindPath(PathTile start, PathTile end, List<PathTile> path) { return FindPath(start, end, path, tile => true); }
PathTile Connect(PathTile tile, int x, int z, int toX, int toZ) { var index = GetIndex(toX, toZ); if (index >= 0) { var other = instances[index].GetComponent<PathTile>(); if (other != null) { tile.connections.Add(other); return other; } } return null; }
bool isWalkable(PathTile tile) { if(selectedObject) { if (selectedObject.GetComponent<Character>()) { if (selectedObject.GetComponent<Character>().start == tile) { //tile.GetComponent<MeshRenderer>().material.color = Color.green; return true; } } if (selectedObject.GetComponent<Enemy>()) { if (selectedObject.GetComponent<Enemy>().target == tile) { //tile.GetComponent<MeshRenderer>().material.color = Color.green; return true; } } foreach (PlayerController player in allPlayers) { if (player.start == tile) { //tile.GetComponent<MeshRenderer>().material.color = Color.gray; return false; } } foreach (Enemy enemy in allEnemies) { if (enemy.start == tile) { //tile.GetComponent<MeshRenderer>().material.color = Color.gray; return false; } } } //tile.GetComponent<MeshRenderer>().material.color = Color.green; return true; }