public void LoadOnlyCostField() { if (File.Exists(GetLevelPath())) { byte[] levelBytes = File.ReadAllBytes(GetLevelPath()); if (levelBytes != null) { if (pathfinder.worldData != null) { pathfinder.worldData.costFields = WorldData.Load(levelBytes).costFields; } } } else { Debug.Log("Load new level"); } }
public void LoadLevel() { if (File.Exists(GetLevelPath())) { byte[] levelBytes = File.ReadAllBytes(GetLevelPath()); if (levelBytes != null) { if (pathfinder.worldData != null) { pathfinder.worldData = WorldData.Load(levelBytes); } } } else { // Debug.Log("Load new level"); } }
public void SetValuesFlatWorld(int left, int right, int top, int bot, WorldData worldData) { _worldData = worldData; LeftOffset = left; TopOffset = top; Origin = _worldData.TileManager.GetWorldPosition(LeftOffset, TopOffset); GridWidth = 1 + right - LeftOffset; GridLength = 1 + bot - TopOffset; SearchField = new bool[GridWidth][]; TileGrid = new Tile[GridWidth][]; for (int x = 0; x < GridWidth; x++) { TileGrid[x] = new Tile[GridLength]; SearchField[x] = new bool[GridLength]; } }
public void FlatAreaCopyTiles(int left, int right, int top, int bot, List <IntVector2> tileList, Tile[][] grid, int yLayer, WorldData worldData) { FlatArea = true; SetValues(left, right, top, bot, yLayer, worldData); _worldData.MultiLevelSectorManager.SetupSectorsWorldArea(this); bool firstTile = true; foreach (IntVector2 vec2 in tileList) { int x = vec2.X - LeftOffset; int y = vec2.Y - TopOffset; TileGrid[x][y] = grid[vec2.X][vec2.Y]; TileGrid[x][y].GridPos = new IntVector2(x, y); TileGrid[x][y].IntegrationValue = TileManager.TileResetIntegrationValue; TileGrid[x][y].WorldAreaIndex = Index; int sectorX = Mathf.FloorToInt(x / (float)LevelDimensions[0][0]); // sectorWidth int sectorY = Mathf.FloorToInt(y / (float)LevelDimensions[0][1]); // sectorHeight if (_worldData.Pathfinder.maxLevelAmount == 0) { TileGrid[x][y].SectorIndex = 0; } else { TileGrid[x][y].SectorIndex = (sectorY * LevelDimensions[0][2]) + sectorX; } MultiLevelSector sector = SectorGrid[0][TileGrid[x][y].SectorIndex]; int deltaX = x - sector.Left; int deltaY = y - sector.Top; TileGrid[x][y].IndexWithinSector = (deltaY * sector.TilesInWidth) + deltaX; if (firstTile) { Origin.y = TileGrid[x][y].YWorldPos; firstTile = false; } } }
public void SetValuesFlatWorld(int _left, int right, int _top, int bot, WorldData _worldData) { worldData = _worldData; leftOffset = _left; topOffset = _top; origin = worldData.tileManager.GetWorldPosition(leftOffset, topOffset); gridWidth = 1 + right - leftOffset; gridLength = 1 + bot - topOffset; searchField = new bool[gridWidth][]; tileGrid = new Tile[gridWidth][]; for (int x = 0; x < gridWidth; x++) { tileGrid[x] = new Tile[gridLength]; searchField[x] = new bool[gridLength]; } }
private void SetValues(IntVector2 offSet, int width, int length, int yLayer, WorldData worldData) { IntVectorComparer comparer = new IntVectorComparer(); WorldAreaTileConnections = new Dictionary <IntVector2, List <IntVector3> >(comparer); _worldData = worldData; LeftOffset = offSet.X; TopOffset = offSet.Y; Origin = _worldData.TileManager.GetWorldPosition(LeftOffset, TopOffset); Origin.y = _worldData.Pathfinder.worldStart.y + yLayer * _worldData.Pathfinder.characterHeight + _worldData.Pathfinder.characterHeight * 0.5f; GridWidth = width; GridLength = length; SearchField = new bool[GridWidth][]; TileGrid = new Tile[GridWidth][]; for (int x = 0; x < GridWidth; x++) { TileGrid[x] = new Tile[GridLength]; SearchField[x] = new bool[GridLength]; } }
public void FillFlowField(List <Tile> tiles, WorldData worldData) { WorldArea area = null; if (tiles.Count > 0) { area = worldData.worldAreas[tiles[0].worldAreaIndex]; } IntVector2 key = new IntVector2(); //Vector2 vec = new Vector2(); Tile lowestCostTile; foreach (Tile tile in tiles) { if (key.x != tile.worldAreaIndex) { area = worldData.worldAreas[tile.worldAreaIndex]; } key.x = tile.worldAreaIndex; key.y = tile.sectorIndex; lowestCostTile = worldData.tileManager.GetLowestIntergrationCostTile(tile, area); if (lowestCostTile != tile) { field[key][tile.indexWithinSector] = worldData.flowFieldManager.GetDirBetweenVectors(tile.gridPos, lowestCostTile.gridPos); //vec.x = lowestCostTile.gridPos.x - tile.gridPos.x; //vec.y = tile.gridPos.y - lowestCostTile.gridPos.y; //field[key][tile.indexWithinSector] = vec.normalized; //new Vector2(lowestCostTile.gridPos.x - tile.gridPos.x, tile.gridPos.y - lowestCostTile.gridPos.y).normalized; } } }
private void SetValues(int _left, int right, int _top, int bot, int Ylayer, WorldData _worldData) { IntVectorComparer comparer = new IntVectorComparer(); worldAreaTileConnections = new Dictionary <IntVector2, List <IntVector3> >(comparer); worldData = _worldData; leftOffset = _left; topOffset = _top; origin = worldData.tileManager.GetWorldPosition(leftOffset, topOffset); origin.y = worldData.pathfinder.worldStart.y + Ylayer * worldData.pathfinder.characterHeight + worldData.pathfinder.characterHeight * 0.5f; gridWidth = 1 + right - leftOffset; gridLength = 1 + bot - topOffset; searchField = new bool[gridWidth][]; tileGrid = new Tile[gridWidth][]; for (int x = 0; x < gridWidth; x++) { tileGrid[x] = new Tile[gridLength]; searchField[x] = new bool[gridLength]; } }
public void FlatAreaCopyTiles(IntVector2 offset, Tile[][] tileGrid, int yLayer, WorldData worldData) { FlatArea = true; SetValues(offset, tileGrid.Length, tileGrid[0].Length, yLayer, worldData); _worldData.MultiLevelSectorManager.SetupSectorsWorldArea(this); bool firstTile = true; for (int x = 0; x < tileGrid.Length; x++) { for (int y = 0; y < tileGrid[0].Length; y++) { if (tileGrid[x][y] == null) { continue; } TileGrid[x][y] = tileGrid[x][y]; // manually TileGrid[x][y].YWorldPos = worldData.Pathfinder.worldStart.y; // 0f; // forced y = 0. 2D worldAreas _worldData.LayerWorldAreaIndexes[yLayer][x + LeftOffset][y + TopOffset] = Index; // set world Index TileGrid[x][y].GridPos = new IntVector2(x, y); TileGrid[x][y].IntegrationValue = TileManager.TileResetIntegrationValue; TileGrid[x][y].WorldAreaIndex = Index; int sectorX = Mathf.FloorToInt(x / (float)LevelDimensions[0][0]); // sectorWidth int sectorY = Mathf.FloorToInt(y / (float)LevelDimensions[0][1]); // sectorHeight if (_worldData.Pathfinder.maxLevelAmount == 0) { TileGrid[x][y].SectorIndex = 0; } else { TileGrid[x][y].SectorIndex = (sectorY * LevelDimensions[0][2]) + sectorX; } MultiLevelSector sector = SectorGrid[0][TileGrid[x][y].SectorIndex]; int deltaX = x - sector.Left; int deltaY = y - sector.Top; TileGrid[x][y].IndexWithinSector = (deltaY * sector.TilesInWidth) + deltaX; if (firstTile) { Origin.y = TileGrid[x][y].YWorldPos; firstTile = false; } } } }
public void SlopedAreaCopyTiles(float angle, int left, int right, int top, int bot, List <List <IntVector2> > tileList, List <TemporaryWorldArea> tempWorldAreas, int yLayer, WorldData worldData) { Angle = angle; SetValues(left, right, top, bot, yLayer, worldData); _worldData.MultiLevelSectorManager.SetupSectorsWorldArea(this); Tile highestYTile = null; Tile lowestYTile = null; int i = 0; foreach (List <IntVector2> vec2List in tileList) { Tile[][] grid = tempWorldAreas[yLayer + i].TileGrid; foreach (IntVector2 vec2 in vec2List) { int x = vec2.X - LeftOffset; int y = vec2.Y - TopOffset; TileGrid[x][y] = grid[vec2.X][vec2.Y]; TileGrid[x][y].GridPos = new IntVector2(x, y); TileGrid[x][y].IntegrationValue = TileManager.TileResetIntegrationValue; TileGrid[x][y].WorldAreaIndex = Index; int sectorX = Mathf.FloorToInt(x / (float)LevelDimensions[0][0]); // sectorWidth int sectorY = Mathf.FloorToInt(y / (float)LevelDimensions[0][1]); // sectorHeight if (_worldData.Pathfinder.maxLevelAmount == 0) { TileGrid[x][y].SectorIndex = 0; } else { TileGrid[x][y].SectorIndex = (sectorY * LevelDimensions[0][2]) + sectorX; // *sectorGridWidth } MultiLevelSector sector = SectorGrid[0][TileGrid[x][y].SectorIndex]; int deltaX = x - sector.Left; int deltaY = y - sector.Top; TileGrid[x][y].IndexWithinSector = (deltaY * sector.TilesInWidth) + deltaX; if (lowestYTile == null || TileGrid[vec2.X - LeftOffset][vec2.Y - TopOffset].YWorldPos < lowestYTile.YWorldPos) { lowestYTile = TileGrid[vec2.X - LeftOffset][vec2.Y - TopOffset]; } if (highestYTile == null || TileGrid[vec2.X - LeftOffset][vec2.Y - TopOffset].YWorldPos > highestYTile.YWorldPos) { highestYTile = TileGrid[vec2.X - LeftOffset][vec2.Y - TopOffset]; } } i++; } if (highestYTile != null) { Origin.y = highestYTile.YWorldPos - ((highestYTile.YWorldPos - lowestYTile.YWorldPos) * 0.5f); // X or Z direction slope Vector3 rayStartingPoint = new Vector3(0, lowestYTile.YWorldPos + _worldData.Pathfinder.characterHeight * 0.4f, 0); float tileOffset = (_worldData.Pathfinder.tileSize * 0.5f); rayStartingPoint.x = _worldData.Pathfinder.worldStart.x + ((lowestYTile.GridPos.X + LeftOffset) * _worldData.Pathfinder.tileSize) + tileOffset; rayStartingPoint.z = _worldData.Pathfinder.worldStart.z - ((lowestYTile.GridPos.Y + TopOffset) * _worldData.Pathfinder.tileSize) - tileOffset; if (Physics.Raycast(rayStartingPoint, Vector3.down, out var hit, _worldData.Pathfinder.characterHeight * 0.6f, 1 << _worldData.Pathfinder.groundLayer)) { if (Mathf.Abs(hit.normal.x) > Mathf.Abs(hit.normal.z)) { AngleDirectionX = true; if (hit.normal.x > 0) { AnglePositive = -1; } else { AnglePositive = 1; } } else { AngleDirectionX = false; if (hit.normal.z > 0) { AnglePositive = 1; } else { AnglePositive = -1; } } } float length; //highestYTile.yWorldPos - lowestYTile.yWorldPos if (AngleDirectionX) { length = (GridWidth - 1) * _worldData.Pathfinder.tileSize; } else { length = (GridLength - 1) * _worldData.Pathfinder.tileSize; } if (length != 0) { AngledAreaResolutionDifference = Vector3.Distance(_worldData.TileManager.GetTileWorldPosition(lowestYTile, this), _worldData.TileManager.GetTileWorldPosition(highestYTile, this)) / length; //Debug.Log("angledAreaResolutionDifference " + angledAreaResolutionDifference); } } }
public void SlopedAreaCopyTiles(float _angle, int _left, int right, int _top, int bot, List <List <IntVector2> > tileList, List <TemporaryWorldArea> tempWorldAreas, int Ylayer, WorldData _worldData) { angle = _angle; SetValues(_left, right, _top, bot, Ylayer, _worldData); worldData.multiLevelSectorManager.SetupSectorsWorldArea(this); Tile highestYTile = null; Tile lowestYTile = null; int i = 0; foreach (List <IntVector2> vec2List in tileList) { Tile[][] grid = tempWorldAreas[Ylayer + i].tileGrid; foreach (IntVector2 vec2 in vec2List) { int x = vec2.x - leftOffset; int y = vec2.y - topOffset; tileGrid[x][y] = grid[vec2.x][vec2.y]; tileGrid[x][y].gridPos = new IntVector2(x, y); tileGrid[x][y].integrationValue = worldData.tileManager.tileResetIntegrationValue; tileGrid[x][y].worldAreaIndex = index; int SectorX = Mathf.FloorToInt(x / (float)levelDimensions[0][0]); // /sectorwidth int SectorY = Mathf.FloorToInt(y / (float)levelDimensions[0][1]); // /sectorheight if (worldData.pathfinder.maxLevelAmount == 0) { tileGrid[x][y].sectorIndex = 0; } else { tileGrid[x][y].sectorIndex = (SectorY * levelDimensions[0][2]) + SectorX;// *sectorgridWidth } MultiLevelSector sector = sectorGrid[0][tileGrid[x][y].sectorIndex]; int deltaX = x - sector.left; int deltaY = y - sector.top; tileGrid[x][y].indexWithinSector = (deltaY * sector.tilesInWidth) + deltaX; if (lowestYTile == null || tileGrid[vec2.x - leftOffset][vec2.y - topOffset].yWorldPos < lowestYTile.yWorldPos) { lowestYTile = tileGrid[vec2.x - leftOffset][vec2.y - topOffset]; } if (highestYTile == null || tileGrid[vec2.x - leftOffset][vec2.y - topOffset].yWorldPos > highestYTile.yWorldPos) { highestYTile = tileGrid[vec2.x - leftOffset][vec2.y - topOffset]; } } i++; } origin.y = highestYTile.yWorldPos - ((highestYTile.yWorldPos - lowestYTile.yWorldPos) * 0.5f); // X or Z direction slope Vector3 rayStartingPoint = new Vector3(0, lowestYTile.yWorldPos + worldData.pathfinder.characterHeight * 0.4f, 0); float tileOffset = (worldData.pathfinder.tileSize * 0.5f); rayStartingPoint.x = worldData.pathfinder.worldStart.x + ((lowestYTile.gridPos.x + leftOffset) * worldData.pathfinder.tileSize) + tileOffset; rayStartingPoint.z = worldData.pathfinder.worldStart.z - ((lowestYTile.gridPos.y + topOffset) * worldData.pathfinder.tileSize) - tileOffset; RaycastHit hit; if (Physics.Raycast(rayStartingPoint, Vector3.down, out hit, worldData.pathfinder.characterHeight * 0.6f, 1 << worldData.pathfinder.groundLayer)) { if (Mathf.Abs(hit.normal.x) > Mathf.Abs(hit.normal.z)) { angleDirectionX = true; if (hit.normal.x > 0) { anglePositive = -1; } else { anglePositive = 1; } } else { angleDirectionX = false; if (hit.normal.z > 0) { anglePositive = 1; } else { anglePositive = -1; } } } float length = 0; //highestYTile.yWorldPos - lowestYTile.yWorldPos if (angleDirectionX) { length = (gridWidth - 1) * worldData.pathfinder.tileSize; } else { length = (gridLength - 1) * worldData.pathfinder.tileSize; } if (length != 0) { angledAreaResolutionDiffrence = Vector3.Distance(worldData.tileManager.GetTileWorldPosition(lowestYTile, this), worldData.tileManager.GetTileWorldPosition(highestYTile, this)) / length; //Debug.Log("angledAreaResolutionDiffrence " + angledAreaResolutionDiffrence); } }
public void FlatAreaCopyTiles(int _left, int right, int _top, int bot, List <IntVector2> tileList, Tile[][] grid, int Ylayer, WorldData _worldData) { flatArea = true; SetValues(_left, right, _top, bot, Ylayer, _worldData); worldData.multiLevelSectorManager.SetupSectorsWorldArea(this); bool firstTile = true; foreach (IntVector2 vec2 in tileList) { int x = vec2.x - leftOffset; int y = vec2.y - topOffset; tileGrid[x][y] = grid[vec2.x][vec2.y]; tileGrid[x][y].gridPos = new IntVector2(x, y); tileGrid[x][y].integrationValue = worldData.tileManager.tileResetIntegrationValue; tileGrid[x][y].worldAreaIndex = index; int SectorX = Mathf.FloorToInt(x / (float)levelDimensions[0][0]); // /sectorwidth int SectorY = Mathf.FloorToInt(y / (float)levelDimensions[0][1]); // /sectorheight if (worldData.pathfinder.maxLevelAmount == 0) { tileGrid[x][y].sectorIndex = 0; } else { tileGrid[x][y].sectorIndex = (SectorY * levelDimensions[0][2]) + SectorX; } MultiLevelSector sector = sectorGrid[0][tileGrid[x][y].sectorIndex]; int deltaX = x - sector.left; int deltaY = y - sector.top; tileGrid[x][y].indexWithinSector = (deltaY * sector.tilesInWidth) + deltaX; if (firstTile) { origin.y = tileGrid[x][y].yWorldPos; firstTile = false; } } }
public void FlatAreaCopyTiles(IntVector2 offset, Tile[][] _tileGrid, int yLayer, WorldData _worldData) { flatArea = true; SetValues(offset, _tileGrid.Length, _tileGrid[0].Length, yLayer, _worldData); worldData.multiLevelSectorManager.SetupSectorsWorldArea(this); bool firstTile = true; for (int x = 0; x < _tileGrid.Length; x++) { for (int y = 0; y < _tileGrid[0].Length; y++) { tileGrid[x][y] = _tileGrid[x][y]; // manually tileGrid[x][y].yWorldPos = _worldData.pathfinder.worldStart.y; // 0f; // forced y = 0. 2D worldAreas worldData.layerdWorldAreaIndexes[yLayer][x + leftOffset][y + topOffset] = index; // set world Index tileGrid[x][y].gridPos = new IntVector2(x, y); tileGrid[x][y].integrationValue = worldData.tileManager.tileResetIntegrationValue; tileGrid[x][y].worldAreaIndex = index; int SectorX = Mathf.FloorToInt(x / (float)levelDimensions[0][0]); // /sectorwidth int SectorY = Mathf.FloorToInt(y / (float)levelDimensions[0][1]); // /sectorheight if (worldData.pathfinder.maxLevelAmount == 0) { tileGrid[x][y].sectorIndex = 0; } else { tileGrid[x][y].sectorIndex = (SectorY * levelDimensions[0][2]) + SectorX; } MultiLevelSector sector = sectorGrid[0][tileGrid[x][y].sectorIndex]; int deltaX = x - sector.left; int deltaY = y - sector.top; tileGrid[x][y].indexWithinSector = (deltaY * sector.tilesInWidth) + deltaX; if (firstTile) { origin.y = tileGrid[x][y].yWorldPos; firstTile = false; } } } }