// Pac-Man’s current position and orientation, and selecting the location four tiles straight // ahead of him. Works when PacMan is facing left, down, or right, but when facing upwards, // it's also four tiles to the left ValueTask <CellIndex> getChaseTargetCell() { var pacDir = _pacman.Direction; CellIndex pacTile = _pacman.Tile.Index; CellIndex offset = Maze.ConstrainCell( pacTile + (DirectionToIndexLookup.IndexVectorFor(pacDir) * Vector2s.Four).ToCellIndex()); // for the bug in the original pacman if (pacDir == GameComponents.Directions.Up) { offset = (offset.ToVector2() + new Vector2(-4, 0)).ToCellIndex(); } var newTarget = Maze.ConstrainCell(pacTile + offset); return(new ValueTask <CellIndex>(newTarget)); }
// To locate Inky’s target, we first start by selecting the position two tiles in front of Pac-Man // in his current direction of travel. // From there, imagine drawing a vector from Blinky’s position to this tile, and then doubling // the length of the vector. The tile that this new, extended vector ends on will be Inky’s actual target async ValueTask <CellIndex> getChaseTargetCell() { // ReSharper disable once HeapView.BoxingAllocation var blinky = await _mediator.Send(_getBlinkyRequest); var blinkyCell = blinky.Tile.Index; var pacDir = _pacman.Direction; CellIndex pacCellPos = _pacman.Tile.Index; var twoCellsInFront = Maze.ConstrainCell( pacCellPos + (DirectionToIndexLookup.IndexVectorFor(pacDir) * Vector2s.Two).ToCellIndex()); CellIndex diff = twoCellsInFront - blinkyCell; var diff2 = (diff.ToVector2() * Vector2s.Two).ToCellIndex(); var newTarget = Maze.ConstrainCell(blinkyCell + diff2); return(newTarget); }
public override ValueTask <MovementResult> Update(CanvasTimingInformation context) { if (!_readyToExit && _door.CanGhostLeave(Ghost)) { _readyToExit = true; return(new ValueTask <MovementResult>(MovementResult.NotFinished)); } if (_finished) { Ghost.Direction = new DirectionInfo(Directions.Left, Directions.Left); Ghost.SetMovementMode(GhostMovementMode.Undecided); return(new ValueTask <MovementResult>(MovementResult.Finished)); } Vector2 diff = _cellToMoveTo - _cellToMoveFrom; if (diff != Vector2.Zero) { diff = diff.Normalize(); Ghost.Position = Ghost.Position + diff / Vector2s.Two; var dir = DirectionToIndexLookup.GetDirectionFromVector(diff); Ghost.Direction = new DirectionInfo(dir, dir); } if (Ghost.Position.Floor() == _cellToMoveTo.Floor()) { Ghost.Position = _cellToMoveTo; whenAtTargetCell(); } return(new ValueTask <MovementResult>(MovementResult.NotFinished)); }