// Update is called once per frame void Update() { if (TDWorld.getWorld().m_configuration.drawGrid == 0) { return; } TDGrid grid = TDWorld.getWorld().m_grid; if (grid == null) { return; } if (m_aLines == null) { m_aLines = new GameObject[grid.nbCellsX + grid.nbCellsY + 2]; GameObject terrainGridLinePrefab = (GameObject)Resources.Load("TerrainGridLinePrefab"); for (int i = 0; i < m_aLines.Length; i++) { GameObject obj = (GameObject)Instantiate(terrainGridLinePrefab, new Vector3(), new Quaternion()); m_aLines[i] = obj; obj.AddComponent <LineRenderer>(); } } int lrCount = 0; float startX = grid.m_startX; float startY = grid.m_startY; float stepX = grid.m_gridX; float stepY = grid.m_gridY; for (uint i = 0; i <= grid.nbCellsX; i++) { GameObject line = m_aLines[lrCount]; lrCount++; LineRenderer lineRenderer = line.GetComponent <LineRenderer>(); lineRenderer.SetWidth(1, 1); lineRenderer.SetColors(Color.blue, Color.blue); lineRenderer.SetVertexCount(2); Vector3 startLine = new Vector3(startX + ((float)i) * stepX, startY, 0); Vector3 endLine = new Vector3(startX + ((float)i) * stepX, startY + grid.width, 0); lineRenderer.SetPosition(0, TDWorld.getWorld().from2dTo3d(startLine)); lineRenderer.SetPosition(1, TDWorld.getWorld().from2dTo3d(endLine)); } for (uint i = 0; i <= grid.nbCellsY; i++) { GameObject line = m_aLines[lrCount]; lrCount++; LineRenderer lineRenderer = line.GetComponent <LineRenderer>(); lineRenderer.SetWidth(1, 1); lineRenderer.SetColors(Color.blue, Color.blue); lineRenderer.SetVertexCount(2); Vector3 startLine = new Vector3(startX, startY + ((float)i) * stepY, 0); Vector3 endLine = new Vector3(startX + grid.length, startY + ((float)i) * stepY, 0); lineRenderer.SetPosition(0, TDWorld.getWorld().from2dTo3d(startLine)); lineRenderer.SetPosition(1, TDWorld.getWorld().from2dTo3d(endLine)); } }
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++; } }
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); } }
public void occupyPosition(Vector3 pos, TDGrid.CellState occupyState) { Vector3 res = from3dTo2d(pos); TDGrid.Cell cell = m_grid.getCell(res); m_grid.setCellState(cell, occupyState); }