public bool CanMoveInDirection(GhostDirection direction) { bool result = true; GhostDirection currentDirection = direction; for (int r = 0; r < GameEngine.backLayer.Rows; r++) { for (int c = 0; c < GameEngine.backLayer.Columns; c++) { if (GameEngine.backLayer.TileObject[c, r] == null) { continue; } if (GameEngine.backLayer.TileObject[c, r].IsWall == true) { Sprite wall = GameEngine.backLayer.TileObject[c, r].Sprite; switch (currentDirection) { case GhostDirection.Up: result = !wall.IsCollidingWithRectangleBased(DetectU); break; case GhostDirection.Down: result = !wall.IsCollidingWithRectangleBased(DetectD); break; case GhostDirection.Left: result = !wall.IsCollidingWithRectangleBased(DetectL); break; case GhostDirection.Right: result = !wall.IsCollidingWithRectangleBased(DetectR); break; } if (result == false) { break; } } //if } //for if (result == false) { break; } }//for return(result); }
void ChooseTheFirtDirection() { int rand = Random.Range(0, 2); if (rand == 0) { ghostDirection = GhostDirection.Left; } if (rand == 1) { ghostDirection = GhostDirection.Right; } }
public bool Move() { bool result = false; //if can move in next direction if (CanMoveInDirection(ghostNextDirection) == true) { ghostDirection = ghostNextDirection; //set current direction } //can move in direction if (CanMoveInDirection(ghostDirection) == false) { return(false); } int x = base.Location.X; int y = base.Location.Y; switch (ghostDirection) { case GhostDirection.Up: y -= ghostSpeed; if (isChasing == true) { base.CurrentFrame = FrameU; } else { base.CurrentFrame = FrameScared; } break; case GhostDirection.Down: y += ghostSpeed; if (isChasing == true) { base.CurrentFrame = FrameD; } else { base.CurrentFrame = FrameScared; } break; case GhostDirection.Left: x -= ghostSpeed; if (isChasing == true) { base.CurrentFrame = FrameL; } else { base.CurrentFrame = FrameScared; } break; case GhostDirection.Right: x += ghostSpeed; if (isChasing == true) { base.CurrentFrame = FrameR; } else { base.CurrentFrame = FrameScared; } break; case GhostDirection.None: break; } base.Location = new Point(x, y); SetProximityDetectionRectangles(); //set detection rectangles location return(result); }
private Vector3 ChooseDirection() { Vector3 Direction = new Vector3(); RaycastHit2D hitup; RaycastHit2D hitleft; RaycastHit2D hitdown; RaycastHit2D hitright; switch (ghostDirection) { case GhostDirection.Up: hitup = Physics2D.Raycast(transform.position, Vector2.up, 0.5f, Wallsmask); if (hitup.collider == null) { ghostDirection = GhostDirection.Up; return(new Vector3(0.0f, 1.0f)); } break; case GhostDirection.Right: hitright = Physics2D.Raycast(transform.position, Vector2.right, 0.5f, Wallsmask); if (hitright.collider == null) { ghostDirection = GhostDirection.Right; return(new Vector3(1.0f, 0.0f)); } break; case GhostDirection.Down: hitdown = Physics2D.Raycast(transform.position, Vector2.down, 0.5f, Wallsmask); if (hitdown.collider == null) { ghostDirection = GhostDirection.Down; return(new Vector3(0.0f, -1.0f)); } break; case GhostDirection.Left: hitleft = Physics2D.Raycast(transform.position, Vector2.left, 0.5f, Wallsmask); if (hitleft.collider == null) { ghostDirection = GhostDirection.Left; return(new Vector3(-1.0f, 0.0f)); } break; } AvailableDirections.TrimExcess(); AvailableDirections.Clear(); hitup = Physics2D.Raycast(transform.position, Vector2.up, 1.0f, Wallsmask); if (hitup.collider == null) { AvailableDirections.Add("Up"); } hitleft = Physics2D.Raycast(transform.position, Vector2.left, 1.0f, Wallsmask); if (hitleft.collider == null) { AvailableDirections.Add("Left"); } hitdown = Physics2D.Raycast(transform.position, Vector2.down, 1.0f, Wallsmask); if (hitdown.collider == null) { AvailableDirections.Add("Down"); } hitright = Physics2D.Raycast(transform.position, Vector2.right, 1.0f, Wallsmask); if (hitright.collider == null) { AvailableDirections.Add("Right"); } int SelectedDirection = Random.Range(0, AvailableDirections.Count); if (AvailableDirections[SelectedDirection] == "Up") { ghostDirection = GhostDirection.Up; Direction = new Vector3(0.0f, 1.0f); } if (AvailableDirections[SelectedDirection] == "Left") { ghostDirection = GhostDirection.Left; Direction = new Vector3(-1.0f, 0.0f); } if (AvailableDirections[SelectedDirection] == "Down") { ghostDirection = GhostDirection.Down; Direction = new Vector3(0.0f, -1.0f); } if (AvailableDirections[SelectedDirection] == "Right") { ghostDirection = GhostDirection.Right; Direction = new Vector3(1.0f, 0.0f); } return(Direction); }