private void MoveToDirection() { ControlAnimation(); int prevX = _x; int prevY = _y; _moveTimer++; if (_moveTimer != _moveSpeed) { return; } _moveTimer = 0; SelectDirection(); switch (_direction) { case Direction.UP: _x--; break; case Direction.DOWN: _x++; break; case Direction.LEFT: _y--; break; case Direction.RIGHT: _y++; break; default: break; } if (_x < 0) { _x = Maze.maze.GetLength(0) - 1; } else if (_x >= Maze.maze.GetLength(0)) { _x = 0; } else if (_y < 0) { _y = Maze.maze.GetLength(1) - 1; } else if (_y >= Maze.maze.GetLength(1)) { _y = 0; } if (!Maze.IsPath(_x, _y)) { _x = prevX; _y = prevY; TurnDirection(); if (state == GhostState.Scatter) { ChangeTarget(); } } if ((_pack._x - _x) * (_pack._x - _x) + (_pack._y - _y) * (_pack._y - _y) < 8) { MeetPacman(); } }
private void MoveToDirection() { Direction prevDirection = _direction; ControllAnimation(); int prevX = _x; int prevY = _y; _moveTimer++; if (_moveTimer != _moveSpeed) { return; } _moveTimer = 0; Point p = DirectionControl.DirectionToXY(nextDirection); if (nextDirection != Direction.NO_DIRECTION && Maze.IsPath(_x + p.x, _y + p.y)) { _direction = nextDirection; nextDirection = Direction.NO_DIRECTION; } if (_x == 47 && _y == 57) { if (_direction == Direction.DOWN) { _direction = prevDirection; } } switch (_direction) { case Direction.UP: _x--; break; case Direction.DOWN: _x++; break; case Direction.LEFT: _y--; break; case Direction.RIGHT: _y++; break; default: break; } if (_x < 0) { _x = Maze.maze.GetLength(0) - 1; } else if (_x >= Maze.maze.GetLength(0)) { _x = 0; } else if (_y < 0) { _y = Maze.maze.GetLength(1) - 1; } else if (_y >= Maze.maze.GetLength(1)) { _y = 0; } if (!Maze.IsPath(_x, _y)) { _x = prevX; _y = prevY; } if (Maze.maze[_x, _y] == 1) { Maze.maze[_x, _y] = 0; Program.game.Score += 100; chompPlayer.Play(); for (int i = 0; i < Maze.pacs.Count; i++) { if (Maze.pacs[i].x == _x && Maze.pacs[i].y == _y) { Maze.pacs.RemoveAt(i); } } if (Maze.pacs.Count == 0 && Maze.pellets.Count == 0) { Program.game.isVictory = true; Program.game.GameOver(); } } else if (Maze.maze[_x, _y] == 2) { Maze.maze[_x, _y] = 0; Program.game.Score += 200; Program.game.TriggerFrightened(); pelletPlayer.Play(); for (int i = 0; i < Maze.pellets.Count; i++) { if (Maze.pellets[i].x == _x && Maze.pellets[i].y == _y) { Maze.pellets.RemoveAt(i); } } if (Maze.pacs.Count == 0 && Maze.pellets.Count == 0) { Program.game.isVictory = true; Program.game.GameOver(); } } }
private void SelectDirection() { Direction prevDirection = _direction; _stateTimer++; if (_stateFrequency == _stateTimer) { _stateTimer = 0; ChangeState(); } //if (state == GhostState.Scatter || state == GhostState.Chase || state == GhostState.Eaten || state == GhostState.Frightened) //{ int minDist = int.MaxValue; if (Maze.IsPath(_x, _y - 1) && prevDirection != Direction.RIGHT && minDist > target.GetTileDist(_x, _y - 1)) { minDist = target.GetTileDist(_x, _y - 1); _direction = Direction.LEFT; } if (Maze.IsPath(_x, _y + 1) && prevDirection != Direction.LEFT && minDist > target.GetTileDist(_x, _y + 1)) { minDist = target.GetTileDist(_x, _y + 1); _direction = Direction.RIGHT; } if (Maze.IsPath(_x - 1, _y) && prevDirection != Direction.DOWN && minDist > target.GetTileDist(_x - 1, _y)) { minDist = target.GetTileDist(_x - 1, _y); _direction = Direction.UP; } if (Maze.IsPath(_x + 1, _y) && prevDirection != Direction.UP && minDist > target.GetTileDist(_x + 1, _y)) { minDist = target.GetTileDist(_x + 1, _y); _direction = Direction.DOWN; } if (_x == 47 && _y == 57 && state != GhostState.Eaten) { if (_direction == Direction.DOWN) { _direction = prevDirection; } } else if (_x == 92 && _y == 52 && state != GhostState.Eaten) { if (_direction == Direction.UP) { _direction = prevDirection; } } else if (_x == 92 && _y == 62 && state != GhostState.Eaten) { if (_direction == Direction.UP) { _direction = prevDirection; } } //} /* * else if (state == GhostState.Frightened) * { * int rng = rand.Next(1, 5); * Point p = DirectionControl.DirectionToXY((Direction) rng); * while(rng == (int) DirectionControl.GetOpposite(_direction) || !Maze.IsPath(_x + p.x, _y + p.y)) * { * rng = rand.Next(1, 5); * p = DirectionControl.DirectionToXY((Direction)rng); * } * _direction = (Direction)rng; * }*/ }