public DirectionManager.Direction IsLocatedOn_AccordingToAddress(Address a) { int diff_X = _X - a.x; int diff_Y = _Y - a.y; if(diff_X > 0) { if(diff_Y > 0) return DirectionManager.Direction.NorthEast; else if(diff_Y < 0) return DirectionManager.Direction.SouthEast; else if(diff_Y == 0) return DirectionManager.Direction.East; } else if(diff_X < 0) { if(diff_Y > 0) return DirectionManager.Direction.NorthWest; else if(diff_Y < 0) return DirectionManager.Direction.SouthWest; else if(diff_Y == 0) return DirectionManager.Direction.West; } else if(diff_X == 0) { if(diff_Y > 0) return DirectionManager.Direction.North; else if(diff_Y < 0) return DirectionManager.Direction.South; else if(diff_Y == 0) return DirectionManager.Direction.NONE; } return DirectionManager.Direction.NONE; }
public override void SetupPlayer(WorldMap newWorld, Address mapAdress, Address spawnPoint) { if (newWorld.maps == null || newWorld.size_X <= 0 || newWorld.size_Y <= 0) return; currentWorld = newWorld; //Resize player to cell size //if (ProDManager.Instance.topREPLACEDown) //{ // transform.localScale = new Vector3(ProDManager.Instance.tileSpacingX, 1, ProDManager.Instance.tileSpacingY); //} //else //{ // transform.localScale = new Vector3(ProDManager.Instance.tileSpacingX, ProDManager.Instance.tileSpacingY, 1); //} if (currentWorld == null || mapAdress.x < 0 || mapAdress.x >= currentWorld.size_X || mapAdress.y < 0 || mapAdress.y >= currentWorld.size_Y) return; currentMap = currentWorld.maps[mapAdress.x, mapAdress.y]; if (spawnPoint == null) { List<Cell> placementList = new List<Cell>(); foreach (string walkableType in walkableCellTypes) placementList.AddRange(MethodLibrary.GetListOfCellType(walkableType, currentMap)); MoveToCell(placementList[Random.Range(0, placementList.Count - 1)]); } else MoveToCell(currentMap.GetCell(spawnPoint.x, spawnPoint.y)); }
public bool Equals(Address a) { bool condition_0 = false; if(_X == a.x) condition_0 = true; bool condition_1 = false; if(_Y == a.y) condition_1 = true; if(condition_0 && condition_1) return true; else return false; }
/// <summary> /// Gets the world position of an address on a specified map. /// </summary> /// <returns> /// The world position. /// </returns> /// <param name='map'> /// The map that the address is on. /// </param> /// <param name='mapAddress'> /// Address on the map you want the world position of. /// </param> public Address GetAddressWorldPosition(Map map, Address mapAddress) { Address result = mapAddress; if (_Maps == null) return result; else { Address tempAddress = map.addressOnWorldMap; int tempX = 0; for (int i = 0; i < tempAddress.x; i++) tempX += _Maps[i, 0].size_X; int tempY = 0; for (int j = 0; j < tempAddress.y; j++) tempY += _Maps[0, j].size_Y; result = new Address(tempX + mapAddress.x, tempY + mapAddress.y); } return result; }
public bool Contains(Address address) { return Contains(address.x, address.y); }
private List<Address> findVisibleCellsRaySquare(Map map, Address position, int range) { List<Address> result = new List<Address>(); for (int i = position.x - range; i <= position.x + range; i++) result.AddRange(CastRay(position.x, position.y, i, position.y + range, map, opaqueCells)); for (int i = position.y + range; i >= position.y - range; i--) result.AddRange(CastRay(position.x, position.y, position.x + range, i, map, opaqueCells)); for (int i = position.x + range; i >= position.x - range; i--) result.AddRange(CastRay(position.x, position.y, i, position.y - range, map, opaqueCells)); for (int i = position.y - range; i <= position.y + range; i++) result.AddRange(CastRay(position.x, position.y, position.x - range, i, map, opaqueCells)); return result; }
private static Address TranslateOctant(Address a, int octant) { switch (octant) { default: return a; case 1: return new Address(a.y, a.x); case 2: return new Address(-a.y, a.x); case 3: return new Address(-a.x, a.y); case 4: return new Address(-a.x, -a.y); case 5: return new Address(-a.y, -a.x); case 6: return new Address(a.y, -a.x); case 7: return new Address(a.x, -a.y); } }
public void UpdatePathfinding(Address playerPosition) { if (pathPlane == null) return; foreach (Cell c in path) pathTexture.SetPixel(c.x, c.y, emptyColor); if (path.Count > 0 && path.Peek().address.Equals(playerPosition)) path.Pop(); else { try { //Debug.Log("done a pathfinding"); path = algorithm.GetFastestPath(map.cellsOnMap[playerPosition.x, playerPosition.y], map.cellsOnMap[targetX, targetY]); } catch (System.Exception) { path.Clear(); } } foreach (Cell c in path) pathTexture.SetPixel(c.x, c.y, pathColor); pathTexture.Apply(); pathPlane.GetComponent<Renderer>().material.SetTexture("_MainTex", pathTexture); lastPlayerPosition = playerPosition; }
private List<Address> checkColumn(ColumnPortion cp, Address origin, Map map, Queue<ColumnPortion> queue, int octant, int range) { List<Address> result = new List<Address>(); DirectionVector topVector = cp.TopVector; DirectionVector bottomVector = cp.BottomVector; int x = cp.X; int topY; if (cp.X == 0) topY = 0; else { int quotient = (2 * cp.X + 1) * cp.TopVector.Y / (2 * cp.TopVector.X); int remainder = (2 * cp.X + 1) * cp.TopVector.Y % (2 * cp.TopVector.X); topY = quotient; if (remainder > cp.TopVector.X) topY = quotient++; } int bottomY; if (cp.X == 0) bottomY = 0; else { int quotient = (2 * cp.X - 1) * cp.BottomVector.Y / (2 * cp.BottomVector.X); int remainder = (2 * cp.X - 1) * cp.BottomVector.Y % (2 * cp.BottomVector.X); bottomY = quotient; if (remainder >= cp.BottomVector.X) bottomY = quotient++; } bool? wasLastCellOpaque = null; for (int y = topY; y >= bottomY; --y) { bool inRadius = IsInRadius(x, y, range); if (inRadius) { Address temp = TranslateOctant(new Address(x, y), octant); // The current cell is in the field of view. result.Add(new Address(origin.x + temp.x, origin.y + temp.y)); } // A cell that was too far away to be seen is effectively // an opaque cell; nothing "above" it is going to be visible // in the next column, so we might as well treat it as // an opaque cell and not scan the cells that are also too // far away in the next column. bool currentIsOpaque = !inRadius || isOpaque(map, origin, x, y, octant); if (wasLastCellOpaque != null) { if (currentIsOpaque) { // We've found a boundary from transparent to opaque. Make a note // of it and revisit it later. if (!wasLastCellOpaque.Value) { // The new bottom vector touches the upper left corner of // opaque cell that is below the transparent cell. queue.Enqueue(new ColumnPortion( x + 1, new DirectionVector(x * 2 - 1, y * 2 + 1), topVector)); } } else if (wasLastCellOpaque.Value) { // We've found a boundary from opaque to transparent. Adjust the // top vector so that when we find the next boundary or do // the bottom cell, we have the right top vector. // // The new top vector touches the lower right corner of the // opaque cell that is above the transparent cell, which is // the upper right corner of the current transparent cell. topVector = new DirectionVector(x * 2 + 1, y * 2 + 1); } } wasLastCellOpaque = currentIsOpaque; } // Make a note of the lowest opaque-->transparent transition, if there is one. if (wasLastCellOpaque != null && !wasLastCellOpaque.Value) queue.Enqueue(new ColumnPortion(x + 1, bottomVector, topVector)); return result; }
private bool isOpaque(Map map, Address origin, int x, int y, int octant) { Address temp = TranslateOctant(new Address(x, y), octant); x = temp.x; y = temp.y; return opaqueCells.Contains(map.cellsOnMap[origin.x + x, origin.y + y].type); }
private List<Address> findVisibleCellsRayRound(Map map, Address position, int range) { List<Address> result = new List<Address>(); //bresenham circle: //set up the values needed for the algorithm int f = 1 - range; //used to track the progress of the drawn circle (since its semi-recursive) int ddFx = 1; //step x int ddFy = -2 * range; //step y int x = 0; int y = range; //this algorithm doesn't account for the farthest points, //so we have to set them manually result.AddRange(CastRay(position.x, position.y, position.x + range, position.y, map, opaqueCells)); result.AddRange(CastRay(position.x, position.y, position.x - range, position.y, map, opaqueCells)); result.AddRange(CastRay(position.x, position.y, position.x, position.y + range, map, opaqueCells)); result.AddRange(CastRay(position.x, position.y, position.x, position.y - range, map, opaqueCells)); while (x < y) { if (f >= 0) { y--; ddFy += 2; f += ddFy; } x++; ddFx += 2; f += ddFx; //build our current arc result.AddRange(CastRay(position.x, position.y, position.x + x, position.y + y, map, opaqueCells)); result.AddRange(CastRay(position.x, position.y, position.x - x, position.y + y, map, opaqueCells)); result.AddRange(CastRay(position.x, position.y, position.x + x, position.y - y, map, opaqueCells)); result.AddRange(CastRay(position.x, position.y, position.x - x, position.y - y, map, opaqueCells)); result.AddRange(CastRay(position.x, position.y, position.x + y, position.y + x, map, opaqueCells)); result.AddRange(CastRay(position.x, position.y, position.x - y, position.y + x, map, opaqueCells)); result.AddRange(CastRay(position.x, position.y, position.x + y, position.y - x, map, opaqueCells)); result.AddRange(CastRay(position.x, position.y, position.x - y, position.y - x, map, opaqueCells)); } return result; }
private List<Address> findVisibleCellsColumn(Map map, Address position, int range) { List<Address> visibleCells = new List<Address>(); for (int i = 0; i < 8; i++) { Queue<ColumnPortion> workQueue = new Queue<ColumnPortion>(); workQueue.Enqueue(new ColumnPortion(0, new DirectionVector(1, 0), new DirectionVector(1, 1))); while (workQueue.Count > 0) { ColumnPortion currentPortion = workQueue.Dequeue(); visibleCells.AddRange(checkColumn(currentPortion, position, map, workQueue, i, range)); } } return visibleCells; }
public int ReturnManhattanDist(Address start, Address target) { return Mathf.Abs(target.x - start.x) + Mathf.Abs(target.y - start.y); }
private List<Address> findVisibleCellsFlood(Map map, Address position, int range) { int arrSize = range * 2 + 1; int[,] arr = new int[arrSize, arrSize]; for (int i = 0; i < arrSize; i++) for (int j = 0; j < arrSize; j++) arr[i, j] = range + 1; //init array with high distance arr[range, range] = 0; // center cell is target Stack<Address> stack = new Stack<Address>(); stack.Push(new Address(range, range)); while (stack.Count != 0) { Address current = stack.Pop(); Address mapAddress = new Address(position.x + current.x - range, position.y + current.y - range); if (!map.Contains(mapAddress) || opaqueCells.Contains(map.cellsOnMap[mapAddress.x, mapAddress.y].type)) continue; int newDist = arr[current.x, current.y] + 1; if (current.x > 0 && newDist < arr[current.x - 1, current.y]) { stack.Push(new Address(current.x - 1, current.y)); arr[current.x - 1, current.y] = newDist; } if (current.x < arrSize - 1 && newDist < arr[current.x + 1, current.y]) { stack.Push(new Address(current.x + 1, current.y)); arr[current.x + 1, current.y] = newDist; } if (current.y > 0 && newDist < arr[current.x, current.y - 1]) { stack.Push(new Address(current.x, current.y - 1)); arr[current.x, current.y - 1] = newDist; } if (current.y < arrSize - 1 && newDist < arr[current.x, current.y + 1]) { stack.Push(new Address(current.x, current.y + 1)); arr[current.x, current.y + 1] = newDist; } } List<Address> result = new List<Address>(); for (int i = 0; i < arrSize; i++) { for (int j = 0; j < arrSize; j++) { Address mapAddress = new Address(position.x + i - range, position.y + j - range); if (map.Contains(mapAddress) && arr[i, j] <= range) result.Add(mapAddress); } } return result; }
public void UpdateFoW(Address playerPosition) { if (fogOfWarPlane == null) return; //GenerateFog(map); fadeExploredCells(); List<Address> visibleCells; switch (type) { case ShadowType.Rekursive: visibleCells = findVisibleCellsColumn(map, playerPosition, visibilityRange); break; case ShadowType.RaySquare: visibleCells = findVisibleCellsRaySquare(map, playerPosition, visibilityRange); break; case ShadowType.RayRound: visibleCells = findVisibleCellsRayRound(map, playerPosition, visibilityRange); break; case ShadowType.Flood: visibleCells = findVisibleCellsFlood(map, playerPosition, visibilityRange); break; case ShadowType.RekursiveFlood: visibleCells = findVisibleCellsColumn(map, playerPosition, visibilityRange); List<Address> a = findVisibleCellsFlood(map, playerPosition, 2 * visibilityRange); visibleCells.RemoveAll(add => a.Find(bdd => bdd.Equals(add)) == null); break; case ShadowType.RaySquareFlood: visibleCells = findVisibleCellsRaySquare(map, playerPosition, visibilityRange); List<Address> b = findVisibleCellsFlood(map, playerPosition, 2 * visibilityRange); visibleCells.RemoveAll(add => b.Find(bdd => bdd.Equals(add)) == null); break; case ShadowType.RayRoundFlood: visibleCells = findVisibleCellsRayRound(map, playerPosition, visibilityRange); List<Address> c = findVisibleCellsFlood(map, playerPosition, 2 * visibilityRange); visibleCells.RemoveAll(add => c.Find(bdd => bdd.Equals(add)) == null); break; default: visibleCells = new List<Address>(); break; } foreach (Address a in visibleCells) fogTexture.SetPixel(a.x, a.y, visible); fogTexture.Apply(); fogOfWarPlane.renderer.material.SetTexture("_MainTex", fogTexture); }
private void SetManhattenDist(Address start, Address target) { for (int x = 0; x < _map.size_X; ++x) { for (int y = 0; y < _map.size_Y; ++y) { Cell c = _world[x, y]; if (c.address.Equals(start)) continue; c.h = Mathf.Abs(target.x - x) + Mathf.Abs(target.y - y); c.g = int.MaxValue; c.f = 0; c.parent = null; } } }
public virtual void SetupPlayer(WorldMap newWorld, Address mapAdress, Address spawnPoint) { //Override this in respective movement scripts. }