void OnDrawGizmos() { if (mCells == null) { return; } Gizmos.color = Color.green; for (int c = 0; c < mNumberOfColumns; c++) { for (int r = 0; r < mNumberOfRows; r++) { StageCell cell = mCells[c, r]; if (cell.North != null) { Gizmos.DrawLine(cell.Position, cell.North.Position); } if (cell.South != null) { Gizmos.DrawLine(cell.Position, cell.South.Position); } if (cell.West != null) { Gizmos.DrawLine(cell.Position, cell.West.Position); } if (cell.East != null) { Gizmos.DrawLine(cell.Position, cell.East.Position); } } } }
void Awake() { int bottomLeftCell_x = Mathf.FloorToInt(BottomLeftCellPosition.x); int bottomLeftCell_z = Mathf.FloorToInt(BottomLeftCellPosition.z); int topRightCell_x = Mathf.FloorToInt(TopRightCellPosition.x); int topRightCell_z = Mathf.FloorToInt(TopRightCellPosition.z); mNumberOfColumns = ((topRightCell_x - bottomLeftCell_x) / CellSize) + 1; mNumberOfRows = ((topRightCell_z - bottomLeftCell_z) / CellSize) + 1; // Create all cells mCells = new StageCell[mNumberOfColumns, mNumberOfRows]; for (int c = 0; c < mNumberOfColumns; c++) { for (int r = 0; r < mNumberOfRows; r++) { StageCell cell = new StageCell(); cell.Position = new Vector3(bottomLeftCell_x + (c * CellSize), 0, bottomLeftCell_z + (r * CellSize)); cell.North = null; cell.South = null; cell.West = null; cell.East = null; mCells[c, r] = cell; } } UpdateCellNeightbours(); }
void UpdateCellNeightbours() { for (int c = 0; c < mNumberOfColumns; c++) { for (int r = 0; r < mNumberOfRows; r++) { StageCell cell = mCells[c, r]; // link north cell if (r + 1 < mNumberOfRows) { StageCell northCell = mCells[c, r + 1]; if (CheckCellConnection(cell, northCell)) { cell.North = northCell; } } // link south cell if (r - 1 >= 0) { StageCell southCell = mCells[c, r - 1]; if (CheckCellConnection(cell, southCell)) { cell.South = southCell; } } // link east cell if (c + 1 < mNumberOfRows) { StageCell eastCell = mCells[c + 1, r]; if (CheckCellConnection(cell, eastCell)) { cell.East = eastCell; } } // link west cell if (c - 1 >= 0) { StageCell westCell = mCells[c - 1, r]; if (CheckCellConnection(cell, westCell)) { cell.West = westCell; } } } } }
void OnDrawGizmos() { if (mPath == null) { return; } Gizmos.color = Color.blue; for (int i = 0; i < mPath.Count - 1; i++) { StageCell cell01 = mPath[i]; StageCell cell02 = mPath[i + 1]; Gizmos.DrawLine(cell01.Position, cell02.Position); } }
bool CheckCellConnection(StageCell fromCell, StageCell toCell) { Vector3 origin = fromCell.Position; Vector3 destination = toCell.Position; Vector3 diff = destination - origin; Vector3 direction = diff.normalized; float distance = diff.magnitude; if (Physics.Raycast(origin, direction, distance, 1 << PacmanConstants.LAYER_WALL)) { return(false); } else { return(true); } }
void UpdatePathToGetAway() { Vector3 ghostPos = transform.position; StageCell ghostCell = Game.Instance.CurrentStage.GetStageCellAtPosition(ghostPos); int numberOfColumns = Game.Instance.CurrentStage.GetNumberOfColumns(); int numberOfRows = Game.Instance.CurrentStage.GetNumberOfRows(); int col = Random.Range(0, numberOfColumns - 1); int row = Random.Range(0, numberOfRows - 1); StageCell targetCell = Game.Instance.CurrentStage.GetStageCellAt(col, row); List <StageCell> newPath = Game.Instance.CurrentStage.FindShortestPath(ghostCell, targetCell); mPath.Clear(); mPathIndex = 0; for (int i = 0; i < newPath.Count; i++) { mPath.Add(newPath[i]); } }
void UpdatePathToPacman() { if (Game.Instance.Pacman == null) { return; } // Find cell where pacman is in Vector3 pacmanPos = Game.Instance.Pacman.transform.position; Vector3 ghostPos = transform.position; StageCell pacmanCell = Game.Instance.CurrentStage.GetStageCellAtPosition(pacmanPos); StageCell ghostCell = Game.Instance.CurrentStage.GetStageCellAtPosition(ghostPos); List <StageCell> newPath = Game.Instance.CurrentStage.FindShortestPath(ghostCell, pacmanCell); mPath.Clear(); mPathIndex = 0; for (int i = 0; i < newPath.Count; i++) { mPath.Add(newPath[i]); } }
public List<StageCell> FindShortestPath(StageCell fromCell, StageCell toCell) { // Reset all cell's traversal point for (int c=0; c<mNumberOfColumns; c++) { for (int r=0; r<mNumberOfRows; r++) { mCells[c, r].TraversalPoint = int.MaxValue; } } Stack<StageCell> cellStack = new Stack<StageCell>(); fromCell.TraversalPoint = 0; cellStack.Push(fromCell); // 1. Fill Traversal point until toCell found! while (cellStack.Count > 0) { StageCell cell = cellStack.Pop(); int v = cell.TraversalPoint + 1; if (cell.North != null) { if (cell.North.TraversalPoint > v) { cell.North.TraversalPoint = v; if (cell.North != toCell) { cellStack.Push(cell.North); } } } if (cell.East != null) { if (cell.East.TraversalPoint > v) { cell.East.TraversalPoint = v; if (cell.East != toCell) { cellStack.Push(cell.East); } } } if (cell.South != null) { if (cell.South.TraversalPoint > v) { cell.South.TraversalPoint = v; if (cell.South != toCell) { cellStack.Push(cell.South); } } } if (cell.West != null) { if (cell.West.TraversalPoint > v) { cell.West.TraversalPoint = v; if (cell.West != toCell) { cellStack.Push(cell.West); } } } } // 2. We won't use cell stack anymore cellStack.Clear(); cellStack = null; // 3. Traverse back List<StageCell> path = new List<StageCell>(); StageCell traverseBackCell = toCell; path.Add(traverseBackCell); while (traverseBackCell != fromCell) { StageCell[] neightbourCells = new StageCell[] { traverseBackCell.North, traverseBackCell.East, traverseBackCell.South, traverseBackCell.West }; StageCell minTraversePointCell = null; int minTraversePoint = int.MaxValue; for (int i=0; i<neightbourCells.Length; i++) { if (neightbourCells[i] == null) { continue; } if (neightbourCells[i].TraversalPoint < minTraversePoint) { minTraversePoint = neightbourCells[i].TraversalPoint; minTraversePointCell = neightbourCells[i]; } } traverseBackCell = minTraversePointCell; path.Add(traverseBackCell); } path.Reverse(); return path; }
bool CheckCellConnection(StageCell fromCell, StageCell toCell) { Vector3 origin = fromCell.Position; Vector3 destination = toCell.Position; Vector3 diff = destination - origin; Vector3 direction = diff.normalized; float distance = diff.magnitude; if (Physics.Raycast(origin, direction, distance, 1 << PacmanConstants.LAYER_WALL)) { return false; } else { return true; } }
void Awake() { int bottomLeftCell_x = Mathf.FloorToInt(BottomLeftCellPosition.x); int bottomLeftCell_z = Mathf.FloorToInt(BottomLeftCellPosition.z); int topRightCell_x = Mathf.FloorToInt(TopRightCellPosition.x); int topRightCell_z = Mathf.FloorToInt(TopRightCellPosition.z); mNumberOfColumns = ((topRightCell_x - bottomLeftCell_x) / CellSize) + 1; mNumberOfRows = ((topRightCell_z - bottomLeftCell_z) / CellSize) + 1; // Create all cells mCells = new StageCell[mNumberOfColumns, mNumberOfRows]; for (int c=0; c<mNumberOfColumns; c++) { for (int r=0;r<mNumberOfRows; r++) { StageCell cell = new StageCell(); cell.Position = new Vector3(bottomLeftCell_x + (c * CellSize), 0, bottomLeftCell_z + (r * CellSize)); cell.North = null; cell.South = null; cell.West = null; cell.East = null; mCells[c, r] = cell; } } UpdateCellNeightbours(); }
void Update() { if (GhostMode == GhostModeEnum.Chase) { if (mTimer > 0) { mTimer -= Time.deltaTime; transform.position += mMoveDirection * MoveSpeed * CurrentStage.CellSize * Time.deltaTime; } else { transform.position = mNextPosition; UpdatePathToPacman(); if (mPathIndex < mPath.Count - 1) { mPathIndex += 1; StageCell nextCell = mPath[mPathIndex]; mNextPosition = nextCell.Position; Vector3 diff = mNextPosition - transform.position; mMoveDirection = diff.normalized; mTimer = CurrentStage.CellSize / (MoveSpeed * CurrentStage.CellSize); } } } else if (GhostMode == GhostModeEnum.Frightened) { mFrightenedTimer += Time.deltaTime; if (mFrightenedTimer >= FrightenedTime) { ChangeGhostMode(GhostModeEnum.Chase); return; } if (mTimer > 0) { mTimer -= Time.deltaTime; transform.position += mMoveDirection * MoveSpeed * CurrentStage.CellSize * Time.deltaTime; } else { transform.position = mNextPosition; if (mPathIndex < mPath.Count - 1) { mPathIndex += 1; StageCell nextCell = mPath[mPathIndex]; mNextPosition = nextCell.Position; Vector3 diff = mNextPosition - transform.position; mMoveDirection = diff.normalized; mTimer = CurrentStage.CellSize / (MoveSpeed * CurrentStage.CellSize); } else { UpdatePathToGetAway(); } } } else if (GhostMode == GhostModeEnum.Scatter) { // Assignment } }
public List <StageCell> FindShortestPath(StageCell fromCell, StageCell toCell) { // Reset all cell's traversal point for (int c = 0; c < mNumberOfColumns; c++) { for (int r = 0; r < mNumberOfRows; r++) { mCells[c, r].TraversalPoint = int.MaxValue; } } Stack <StageCell> cellStack = new Stack <StageCell>(); fromCell.TraversalPoint = 0; cellStack.Push(fromCell); // 1. Fill Traversal point until toCell found! while (cellStack.Count > 0) { StageCell cell = cellStack.Pop(); int v = cell.TraversalPoint + 1; if (cell.North != null) { if (cell.North.TraversalPoint > v) { cell.North.TraversalPoint = v; if (cell.North != toCell) { cellStack.Push(cell.North); } } } if (cell.East != null) { if (cell.East.TraversalPoint > v) { cell.East.TraversalPoint = v; if (cell.East != toCell) { cellStack.Push(cell.East); } } } if (cell.South != null) { if (cell.South.TraversalPoint > v) { cell.South.TraversalPoint = v; if (cell.South != toCell) { cellStack.Push(cell.South); } } } if (cell.West != null) { if (cell.West.TraversalPoint > v) { cell.West.TraversalPoint = v; if (cell.West != toCell) { cellStack.Push(cell.West); } } } } // 2. We won't use cell stack anymore cellStack.Clear(); cellStack = null; // 3. Traverse back List <StageCell> path = new List <StageCell>(); StageCell traverseBackCell = toCell; path.Add(traverseBackCell); while (traverseBackCell != fromCell) { StageCell[] neightbourCells = new StageCell[] { traverseBackCell.North, traverseBackCell.East, traverseBackCell.South, traverseBackCell.West }; StageCell minTraversePointCell = null; int minTraversePoint = int.MaxValue; for (int i = 0; i < neightbourCells.Length; i++) { if (neightbourCells[i] == null) { continue; } if (neightbourCells[i].TraversalPoint < minTraversePoint) { minTraversePoint = neightbourCells[i].TraversalPoint; minTraversePointCell = neightbourCells[i]; } } traverseBackCell = minTraversePointCell; path.Add(traverseBackCell); } path.Reverse(); return(path); }
void Awake() { //Get XZ of BTlC int bottomLeftCell_x = Mathf.FloorToInt(BottomLeftCellPosition.x); int bottomLeftCell_z = Mathf.FloorToInt(BottomLeftCellPosition.z); //Get XZ of TRC int topRightCell_x = Mathf.FloorToInt(TopRightCellPosition.x); int topRightCell_z = Mathf.FloorToInt(TopRightCellPosition.z); //Get NUM of C mNumberOfColumns = ((topRightCell_x - bottomLeftCell_x) / CellSize) + 1; //Get NUM of R mNumberOfRows = ((topRightCell_z - bottomLeftCell_z) / CellSize) + 1; Debug.Log (mNumberOfColumns); Debug.Log (mNumberOfRows); // Create all cells mCells = new StageCell[mNumberOfColumns, mNumberOfRows]; for (int c=0; c<mNumberOfColumns; c++) { for (int r=0;r<mNumberOfRows; r++) { StageCell cell = new StageCell(); cell.Position = new Vector3(bottomLeftCell_x + (c * CellSize), 0, bottomLeftCell_z + (r * CellSize)); cell.North = null; cell.South = null; cell.West = null; cell.East = null; cell.C=c; cell.R=r; mCells[c, r] = cell; } } // Link cells for (int c=0; c<mNumberOfColumns; c++) { for (int r=0; r<mNumberOfRows; r++) { StageCell cell = mCells[c, r]; // link north cell if (r + 1 < mNumberOfRows) { StageCell northCell = mCells[c, r + 1]; bool canConnect = CheckCellConnection(cell, northCell); if (canConnect) { cell.North = northCell; } } // link south cell if (r - 1 >= 0) { StageCell southCell = mCells[c, r - 1]; bool canConnect = CheckCellConnection(cell, southCell); if (canConnect) { cell.South = southCell; } } // link west cell if (c - 1 >= 0) { StageCell westCell = mCells[c - 1, r]; bool canConnect = CheckCellConnection(cell, westCell); if (canConnect) { cell.West = westCell; } } // link east cell if (c + 1 < mNumberOfColumns) { StageCell eastCell = mCells[c + 1, r]; bool canConnect = CheckCellConnection(cell, eastCell); if (canConnect) { cell.East = eastCell; } } } } }