public Vector3 GetRandomTilePosition(bool canConsume) { TVec2 <int> selectedTile = GetRandomTile(canConsume); Polygon poly = GetPolygonAt(selectedTile.x, selectedTile.y); return(poly.GetPosition()); }
public Tile(TVec2 ID, TVec3 scale, TVec3 position, TMesh cube, TMesh ball, bool walkable) { this.ID = ID; this.cube = LE.CopyEntity(cube); this.ball = LE.CopyEntity(ball); this.walkable = walkable; LE.ShowEntity(this.cube); //set position and scale LE.ScaleEntity(this.cube, scale); LE.PositionEntity(this.cube, position); LE.ScaleEntity(this.ball, new TVec3(scale.X / 2, scale.Y / 2, scale.Z / 2)); LE.PositionEntity(this.ball, new TVec3(position.X, position.Y + 1, position.Z)); //Set a different Color when the tile is non walkable if (!this.walkable) { LE.EntityColor(this.cube, Tile.BLACK); } else { LE.EntityColor(this.cube, Tile.PINK); LE.EntityColor(this.ball, Tile.YELLOW); } }
private void FillTheSpace(int sizeX, int sizeY, TVec2 <int> indexVec, bool canConsume) { for (int indexX = 0; indexX < sizeX; indexX++) { for (int indexY = 0; indexY < sizeY; indexY++) { UpdateMappedData(indexVec.x + indexX, indexVec.y + indexY, canConsume); } } }
public Vector3 GetRandomPositionWithSize(int sizeX, int sizeY, TVec2 <int> alignment, bool canConsume) { //if space available then try to get a space for the object if (IsSpaceAvailable(sizeX, sizeY)) { //try 3 times with random tile to get the required space int count = 3; while (count > 0) { TVec2 <int> ret = GetRandomTile(alignment, false); if (HasSpaceForSize(sizeX, sizeY, ret)) { //fill the space FillTheSpace(sizeX, sizeY, ret, canConsume); //return the position Polygon poly1 = GetPolygonAt(ret.x, ret.y); Polygon poly2 = GetPolygonAt(ret.x + sizeX - 1, ret.y + sizeY - 1); return(CenterOf(poly1, poly2)); } else { count--; } } //else brute force available space Debug.Log("[ Runnin Brute Force !!! ]"); { TVec2 <int> temp = new TVec2 <int>(-1, -1); for (int x = 0; x < row - 1; x++) { temp.x = x; for (int y = 0; y < col - 1; y++) { temp.y = y; if (HasSpaceForSize(sizeX, sizeY, temp)) { //fill the space FillTheSpace(sizeX, sizeY, temp, canConsume); //return the position Polygon poly1 = GetPolygonAt(temp.x, temp.y); Polygon poly2 = GetPolygonAt(temp.x + sizeX - 1, temp.y + sizeY - 1); return(CenterOf(poly1, poly2)); } } } } } //else log error Debug.LogError("No Space available for given size"); return(Vector3.zero); }
void PopulateShop(List <int> roomIds) { //int roomId = Utility.GetRandomNumber(roomIds); foreach (var roomId in roomIds) { Room room = dungeon.GetRoomById(roomId); TVec2 <int> alignment = null; if (room.roomData.exitConfig[0] == ExitConfig.LEFT) { alignment = new TVec2 <int>(0, 1); } else if (room.roomData.exitConfig[0] == ExitConfig.RIGHT) { alignment = new TVec2 <int>(1, 0); } else if (room.roomData.exitConfig[0] == ExitConfig.TOP) { alignment = new TVec2 <int>(0, 0); } else if (room.roomData.exitConfig[0] == ExitConfig.BOTTOM) { alignment = new TVec2 <int>(1, 0); } Vector3 pos = dungeon.GetRandomPositionInRoomWithSize(roomId, 1, 1, alignment, true); { GameObject shop = assetReference.GetGameObjectInstance("Shop"); shop.name = "Shop"; shop.transform.SetParent(room.gameObject.transform); room.roomData.dungeonElements.AddShop(shop); pos = pos + room.gameObject.transform.position; pos.y = shop.transform.position.y; shop.transform.position = pos; if (room.roomData.exitConfig[0] == ExitConfig.LEFT) { Vector3 rot = shop.transform.rotation.eulerAngles; rot.y = 230.0f; //rotate 230 degree shop.transform.rotation = Quaternion.Euler(rot); } else if (room.roomData.exitConfig[0] == ExitConfig.TOP) { Vector3 rot = shop.transform.rotation.eulerAngles; rot.y = 45.0f; //rotate 45 degree shop.transform.rotation = Quaternion.Euler(rot); } shop.GetComponent <InGameShop>().ShowTextMesh(false); } } }
//Get the tile with the lowest total value public TVec2 GetTileWithLowestTotal(List <TVec2> openList) { //temp variables TVec2 tileWithLowestTotal = new TVec2(-1, -1); int lowestTotal = int.MaxValue; //search all the open tiles and get the tile with the lowest total cost foreach (TVec2 openTile in openList) { if (grid[(int)openTile.X, (int)openTile.Y].total <= lowestTotal) { lowestTotal = grid[(int)openTile.X, (int)openTile.Y].total; tileWithLowestTotal = new TVec2((int)openTile.X, (int)openTile.Y); } } return(tileWithLowestTotal); }
private bool HasSpaceForSize(int sizeX, int sizeY, TVec2 <int> indexVec) { for (int index = 0; index < sizeX; index++) { if (IsTileOccupied(indexVec.x + index, indexVec.y)) { return(false); } } for (int index = 0; index < sizeY; index++) { if (IsTileOccupied(indexVec.x, indexVec.y + index)) { return(false); } } return(true); }
void GenerateFogOfWar(GameEvent e) { if (e.type == GameEvent.GENERATE_FOW) { Events.instance.RemoveListener <GameEvent>(GenerateFogOfWar); _plane = new CustomPlane(e.width * e.gridSize, e.height * e.gridSize, (e.width * 8 / 512)); _mesh.vertices = _plane.getVertices(); _mesh.triangles = _plane.getTriangles(); _mesh.uv = _plane.getUVs(); _mesh.colors32 = _plane.getColors(); gameObject.transform.Translate(new Vector3(-_plane.width / 2, 0, -_plane.height / 2)); MeshCollider collider = gameObject.AddComponent <MeshCollider>(); collider.sharedMesh = _mesh; _lastExploredIndex = new TVec2 <int>(-1, -1); } }
public void GenerateFOW(int width, int height, int gridSize, float yPos) { _mesh = GetComponent <MeshFilter>().mesh; _mesh.Clear(); _plane = new CustomPlane(width * gridSize, height * gridSize, gridSize, startAlpha, endAlpha); _mesh.vertices = _plane.getVertices(); _mesh.triangles = _plane.getTriangles(); _mesh.uv = _plane.getUVs(); _mesh.colors32 = _plane.getColors(); transform.position = new Vector3(0.0f, yPos, 0.0f); // MeshCollider collider = gameObject.AddComponent<MeshCollider>(); // collider.sharedMesh = _mesh; _lastExploredIndex = new TVec2 <int>(-1, -1); this.rayHeight = yPos * 2; canUpdate = true; }
public TVec2 <int> GetRandomTile(bool canConsume) { TVec2 <int> ret = new TVec2 <int>(-1, -1); temp.Clear(); for (int index = 0; index < mappedObjectsList.Count; index++) { if (!mappedObjectsList[index].isOccupied) { temp.Add(index); } } temp.Shuffle(); int selectedIndex = Utility.GetRandomNumber(temp); ret.x = mappedObjectsList[selectedIndex].x; ret.y = mappedObjectsList[selectedIndex].y; UpdateMappedData(mappedObjectsList[selectedIndex].x, mappedObjectsList[selectedIndex].y, canConsume); return(ret); }
public void ShowPath() { bool startFound = false; TVec2 currentTile = endTile; List <TVec2> pathTiles = new List <TVec2>(); while (startFound == false) { List <TVec2> adjacentTiles = GetAdjacentTiles(currentTile); //check to see what newest current tile foreach (TVec2 adjacentTile in adjacentTiles) { //Check if it is the start tile if (adjacentTile.X == startTile.X && adjacentTile.Y == startTile.Y) { startFound = true; } //it has to be inside the closed as well as in the open list if (closedList.Contains(adjacentTile) || openList.Contains(adjacentTile)) { if (grid[(int)adjacentTile.X, (int)adjacentTile.Y].cost <= grid[(int)currentTile.X, (int)currentTile.Y].cost && grid[(int)adjacentTile.X, (int)adjacentTile.Y].cost > 0) { //Change the current Tile currentTile = adjacentTile; //Add this adjacent tile to the path list pathTiles.Add(adjacentTile); //Show the ball LE.ShowEntity(grid[(int)adjacentTile.X, (int)adjacentTile.Y].ball); break; } } } } }
//Check if it is in the boundry and if it walkable public List <TVec2> GetAdjacentTiles(TVec2 currentTile) { List <TVec2> adjacentTiles = new List <TVec2>(); TVec2 adjacentTile; //Tile above adjacentTile = new TVec2(currentTile.X, currentTile.Y + 1); if (adjacentTile.Y < AStartTest.gridHeight && grid[(int)adjacentTile.X, (int)adjacentTile.Y].walkable) { adjacentTiles.Add(adjacentTile); } //Tile underneath adjacentTile = new TVec2(currentTile.X, currentTile.Y - 1); if (adjacentTile.Y >= 0 && grid[(int)adjacentTile.X, (int)adjacentTile.Y].walkable) { adjacentTiles.Add(adjacentTile); } //Tile to the right adjacentTile = new TVec2(currentTile.X + 1, currentTile.Y); if (adjacentTile.X < AStartTest.gridWidth && grid[(int)adjacentTile.X, (int)adjacentTile.Y].walkable) { adjacentTiles.Add(adjacentTile); } //Tile to the left adjacentTile = new TVec2(currentTile.X - 1, currentTile.Y); if (adjacentTile.X >= 0 && grid[(int)adjacentTile.X, (int)adjacentTile.Y].walkable) { adjacentTiles.Add(adjacentTile); } //OPTIONAL DIAGONAL return(adjacentTiles); }
public TVec2 <int> GetRandomTile(TVec2 <int> alignment, bool canConsume) { TVec2 <int> ret = new TVec2 <int>(-1, -1); int selectedIndex = -1; for (int index = 0; index < mappedObjectsList.Count; index++) { if (alignment.x == 1 && alignment.y == 0) { if (mappedObjectsList[index].x == borderSize && mappedObjectsList[index].y == col - (2 * borderSize) && !mappedObjectsList[index].isOccupied) { selectedIndex = index; break; } } else if (alignment.x == 0 && alignment.y == 1) { if (mappedObjectsList[index].x == row - (2 * borderSize) && mappedObjectsList[index].y == col - (2 * borderSize) && !mappedObjectsList[index].isOccupied) { selectedIndex = index; break; } } else if (alignment.x == 0 && alignment.y == 0) { if (mappedObjectsList[index].x == borderSize && mappedObjectsList[index].y == borderSize && !mappedObjectsList[index].isOccupied) { selectedIndex = index; break; } } } ret.x = mappedObjectsList[selectedIndex].x; ret.y = mappedObjectsList[selectedIndex].y; UpdateMappedData(mappedObjectsList[selectedIndex].x, mappedObjectsList[selectedIndex].y, canConsume); return(ret); }
public Vector3 GetRandomPositionInRoomWithSize(int roomId, int xSize, int ySize, TVec2 <int> alignment, bool consume = true) { Room room = GetRoomById(roomId); return(room.meshData.floorMesh.GetRandomPositionWithSize(xSize, ySize, alignment, true)); }
/////////////////////// A* /////////////////////// ///add the starting node to the open list ///while the open list is not empty /// { /// current node = node from open list with the lowest cost /// if currentnode = goal /// path complete /// else /// move current node to the closed list /// for each adjacent node /// if it lies with the field /// and it isn't an obstacle /// and it isn't on the open list /// and isn't on the closed list /// move it to the open list and calculate cost ////////////////////////////////////////////////// #endregion public void SearchPath(TVec2 startTile, TVec2 endTile) { this.startTile = startTile; this.endTile = endTile; //Reset all the values for (int i = 0; i < AStartTest.gridWidth; i++) { for (int j = 0; j < AStartTest.gridHeight; j++) { grid[i, j].cost = 0; grid[i, j].heuristic = 0; } } #region Path validation bool canSearch = true; if (grid[(int)startTile.X, (int)startTile.Y].walkable == false) { Console.WriteLine("The start tile is non walkable. Choose a different value than: " + startTile.ToString()); canSearch = false; } if (grid[(int)endTile.X, (int)endTile.Y].walkable == false) { Console.WriteLine("The end tile is non walkable. Choose a different value than: " + endTile.ToString()); canSearch = false; } #endregion //Start the A* algorithm if (canSearch) { //add the starting tile to the open list openList.Add(startTile); currentTile = new TVec2(-1, -1); //while Openlist is not empty while (openList.Count != 0) { //current node = node from open list with the lowest cost currentTile = GetTileWithLowestTotal(openList); //If the currentTile is the endtile, then we can stop searching if (currentTile.X == endTile.X && currentTile.Y == endTile.Y) { Console.WriteLine("YEHA, We found the end tile!!!! :D"); break; } else { //move the current tile to the closed list and remove it from the open list openList.Remove(currentTile); closedList.Add(currentTile); //Get all the adjacent Tiles List <TVec2> adjacentTiles = GetAdjacentTiles(currentTile); foreach (TVec2 adjacentTile in adjacentTiles) { //adjacent tile can not be in the open list if (!openList.Contains(adjacentTile)) { //adjacent tile can not be in the closed list if (!closedList.Contains(adjacentTile)) { //move it to the open list and calculate cost openList.Add(adjacentTile); Tile tile = grid[(int)adjacentTile.X, (int)adjacentTile.Y]; //Calculate the cost tile.cost = grid[(int)currentTile.X, (int)currentTile.Y].cost + 1; //Calculate the manhattan distance tile.heuristic = ManhattanDistance(adjacentTile); //calculate the total amount tile.total = tile.cost + tile.heuristic; //make this tile green LE.EntityColor(tile.cube, Tile.GREEN); } } } } } } //Pain the start and end tile red LE.EntityColor(grid[(int)startTile.X, (int)startTile.Y].cube, Tile.RED); LE.EntityColor(grid[(int)endTile.X, (int)endTile.Y].cube, Tile.RED); //Show the path ShowPath(); }
//Calculate the manhattan distance public int ManhattanDistance(TVec2 adjacentTile) { int manhattan = Math.Abs((int)(endTile.X - adjacentTile.X)) + Math.Abs((int)(endTile.Y - adjacentTile.Y)); return(manhattan); }
public Polygon GetRandomPolygon(bool canConsume) { TVec2 <int> selectedTile = GetRandomTile(canConsume); return(GetPolygonAt(selectedTile.x, selectedTile.y)); }