public void occupyPosition(Vector3 pos, TDGrid.CellState occupyState) { Vector3 res = from3dTo2d(pos); TDGrid.Cell cell = m_grid.getCell(res); m_grid.setCellState(cell, occupyState); }
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); } } }