public void occupyPosition(Vector3 pos, TDGrid.CellState occupyState) { Vector3 res = from3dTo2d(pos); TDGrid.Cell cell = m_grid.getCell(res); m_grid.setCellState(cell, occupyState); }
public TDGrid.CellState positionState(Vector3 pos) { Vector3 res = from3dTo2d(pos); TDGrid.Cell cell = m_grid.getCell(res); return(m_grid.cellState(cell)); }
public Vector3 truncate3d(Vector3 pos) { Vector3 res = from3dTo2d(pos); TDGrid.Cell cell = m_grid.getCell(res); res = m_grid.getCenter(cell); res = from2dTo3d(res); return(res); }
void Awake() { m_strategy = new TDTowerStrategy(); m_configuration = new TDConfiguration(); m_configuration.readFromResource(); GameObject terrain = getTerrain(); Bounds terrainBounds = terrain.renderer.bounds; Vector3 lowPnt = from3dTo2d(terrainBounds.min); Vector3 highPnt = from3dTo2d(terrainBounds.max); m_grid = new TDGrid(); m_grid.initialize(m_configuration.gridNbCellsX, m_configuration.gridNbCellsY, lowPnt.x, lowPnt.y, highPnt.x - lowPnt.x, highPnt.y - lowPnt.y); // Take into account obstacles GameObject [] aObstacles = getAllObstacles(); foreach (GameObject obj in aObstacles) { TDGrid.CellState cellState = TDGrid.CellState.eBusy; switch (obj.tag) { case "Player": cellState = TDGrid.CellState.ePlayer; break; case "EnemyRespawn": cellState = TDGrid.CellState.eEnemyRespawn; break; } Bounds b = obj.renderer.bounds; occupyRegion(b.min, b.max, cellState); } // put the dynamic obstacles uint treesBuilt = 0; while (treesBuilt < m_configuration.nbTrees) { uint i = (uint)((Random.value) * (float)(m_grid.nbCellsX)); uint j = (uint)((Random.value) * (float)(m_grid.nbCellsY)); TDGrid.Cell cell = new TDGrid.Cell(); cell.m_i = i; cell.m_j = j; if (m_grid.cellState(cell) != TDGrid.CellState.eFree) { continue; } Vector3 pos = m_grid.getCenter(cell); pos = from2dTo3d(pos); occupyPosition(pos, TDGrid.CellState.eBusy); addTree(pos); treesBuilt++; } }
public void occupyRegion(Vector3 minPos, Vector3 maxPos, TDGrid.CellState occupyState) { minPos = from3dTo2d(minPos); TDGrid.Cell minCell = m_grid.getCell(minPos); maxPos = from3dTo2d(maxPos); TDGrid.Cell maxCell = m_grid.getCell(maxPos); TDGrid.Cell cell = new TDGrid.Cell(); for (uint i = minCell.m_i; i <= maxCell.m_i; i++) { for (uint j = minCell.m_j; j <= maxCell.m_j; j++) { cell.m_i = i; cell.m_j = j; m_grid.setCellState(cell, occupyState); } } }
public bool canTowerBeBuiltAt(Vector3 pos) { GameObject player = getPlayer(); GameObject [] aRespawnPoints = getAllEnemyRespawnPoints(); TDGrid.Cell startCell = m_grid.getCell(from3dTo2d(player.transform.position)); TDGrid.Cell checkCell = m_grid.getCell(from3dTo2d(pos)); if (m_grid.cellState(checkCell) != TDGrid.CellState.eFree) { return(false); } bool canBuild = true; try { m_grid.setCellState(checkCell, TDGrid.CellState.eBusy); foreach (GameObject respawnPoint in aRespawnPoints) { TDGrid.Cell endCell = m_grid.getCell(from3dTo2d(respawnPoint.transform.position)); TDGrid.Cell[] path; bool pathExists = m_grid.buildPath(startCell, endCell, out path); if (!pathExists) { canBuild = false; break; } } } catch (UnityException) { } finally { m_grid.setCellState(checkCell, TDGrid.CellState.eFree); } return(canBuild); }
bool buildPath(GameObject target) { m_currentCellIndex = -1; setTarget(null); TDWorld world = TDWorld.getWorld(); TDGrid grid = world.m_grid; TDGrid.Cell startCell = grid.getCell(world.from3dTo2d(gameObject.transform.position)); TDGrid.Cell endCell = grid.getCell(world.from3dTo2d(target.transform.position)); bool pathExists = false; if (canFly()) { pathExists = grid.buildAirPath(startCell, endCell, out m_path); } else { pathExists = grid.buildPath(startCell, endCell, out m_path); } m_currentCellIndex = 0; setTarget(target); return(pathExists); }
void Awake() { m_strategy = new TDTowerStrategy(); m_configuration = new TDConfiguration(); m_configuration.readFromResource(); GameObject terrain = getTerrain(); Bounds terrainBounds = terrain.renderer.bounds; Vector3 lowPnt = from3dTo2d(terrainBounds.min); Vector3 highPnt = from3dTo2d(terrainBounds.max); m_grid = new TDGrid(); m_grid.initialize(m_configuration.gridNbCellsX, m_configuration.gridNbCellsY, lowPnt.x, lowPnt.y, highPnt.x - lowPnt.x, highPnt.y - lowPnt.y); // Take into account obstacles GameObject [] aObstacles = getAllObstacles(); foreach (GameObject obj in aObstacles) { TDGrid.CellState cellState = TDGrid.CellState.eBusy; switch (obj.tag) { case "Player": cellState = TDGrid.CellState.ePlayer; break; case "EnemyRespawn": cellState = TDGrid.CellState.eEnemyRespawn; break; } Bounds b = obj.renderer.bounds; occupyRegion(b.min, b.max, cellState); } // put the dynamic obstacles uint treesBuilt = 0; while (treesBuilt < m_configuration.nbTrees) { uint i = (uint)((Random.value)*(float)(m_grid.nbCellsX)); uint j = (uint)((Random.value)*(float)(m_grid.nbCellsY)); TDGrid.Cell cell = new TDGrid.Cell(); cell.m_i = i; cell.m_j = j; if (m_grid.cellState(cell) != TDGrid.CellState.eFree) continue; Vector3 pos = m_grid.getCenter(cell); pos = from2dTo3d(pos); occupyPosition(pos, TDGrid.CellState.eBusy); addTree(pos); treesBuilt++; } }
public void occupyRegion(Vector3 minPos, Vector3 maxPos, TDGrid.CellState occupyState) { minPos = from3dTo2d(minPos); TDGrid.Cell minCell = m_grid.getCell(minPos); maxPos = from3dTo2d(maxPos); TDGrid.Cell maxCell = m_grid.getCell(maxPos); TDGrid.Cell cell = new TDGrid.Cell(); for (uint i=minCell.m_i; i<=maxCell.m_i; i++) for (uint j=minCell.m_j; j<=maxCell.m_j; j++) { cell.m_i = i; cell.m_j = j; m_grid.setCellState(cell, occupyState); } }