// public func OccupyGridSquareUsingPixels(gridReferenceInPixels: SquareFillPoint, shapeInSquare: Shape!) public void OccupyGridSquareUsingPixels(SquareFillPoint gridReferenceInPixels, Shape shapeInSquare) { ChangeGridSquareOccupation( gridReferenceInPixels: gridReferenceInPixels, shapeInSquare: shapeInSquare, occupied: true); }
// private func IsSomethingInTheWay( // newSquareTopLeftCorner: SquareFillPoint, // newSquareGridOrigin: SquareFillPoint, // occupiedGridSquares: Grid) -> Bool private bool IsSomethingInTheWay( SquareFillPoint newSquareTopLeftCorner, SquareFillPoint newSquareGridOrigin, Grid occupiedGridSquares) { var somethingIsInTheWay = ThereAreShapesInTheWay; var newGridXCoords = GetNewGridCoordinates(newPixelValue: newSquareTopLeftCorner.X, newGridCoord: newSquareGridOrigin.X); var newGridYCoords = GetNewGridCoordinates(newPixelValue: newSquareTopLeftCorner.Y, newGridCoord: newSquareGridOrigin.Y); foreach (var xCoord in newGridXCoords) { foreach (var yCoord in newGridYCoords) { if (xCoord >= occupiedGridSquares.Width || yCoord >= occupiedGridSquares.Height || xCoord < 0 || yCoord < 0) { somethingIsInTheWay = true; } else { somethingIsInTheWay = somethingIsInTheWay || occupiedGridSquares.IsSquareOccupied(x: xCoord, y: yCoord); } } } return(somethingIsInTheWay); }
// public func VacateGridSquareUsingPixels(gridReferenceInPixels: SquareFillPoint) public void VacateGridSquareUsingPixels(SquareFillPoint gridReferenceInPixels) { ChangeGridSquareOccupation( gridReferenceInPixels: gridReferenceInPixels, shapeInSquare: null, occupied: false); }
// public func SnapToGrid(newTopLeftCorner: SquareFillPoint) public void SnapToGrid(SquareFillPoint newTopLeftCorner) { var snappedTopLeftCorner = SquareFillPoint( x: CalculateSnappedX(newTopLeftCornerX: newTopLeftCorner.X), y: CalculateSnappedY(newTopLeftCornerY: newTopLeftCorner.Y)); UpdateTopLeftCorner(newTopLeftCorner: snappedTopLeftCorner); }
// public func UpdateTopLeftCorner (newTopLeftCorner: SquareFillPoint) public void UpdateTopLeftCorner(SquareFillPoint newTopLeftCorner) { _topLeftCorner = newTopLeftCorner; foreach (var element in _squares) { element.MoveTopLeftCorner(newTopLeftCorner: newTopLeftCorner); } }
// public func MoveTopLeftCorner(newTopLeftCorner: SquareFillPoint) public void MoveTopLeftCorner(SquareFillPoint newTopLeftCorner) { _topLeftCorner = CalculatePotentialTopLeftCorner(parentTopLeftCorner: newTopLeftCorner); if (_sprite != null) { _sprite.MoveTopLeftCorner( newX: newTopLeftCorner.X + (_positionRelativeToParentCorner.X * ShapeConstants.SquareWidth), newY: newTopLeftCorner.Y + (_positionRelativeToParentCorner.Y * ShapeConstants.SquareWidth)); } }
// private func InitialiseTopLeftCorner(topLeftCorner: SquareFillPoint, topLeftCornerIsGridRef: Bool) private void InitialiseTopLeftCorner(SquareFillPoint topLeftCorner, bool topLeftCornerIsGridRef) { if (topLeftCornerIsGridRef) { _topLeftCorner = topLeftCorner.ConvertToPixels(); } else { _topLeftCorner = SquareFillPoint(x: topLeftCorner.X, y: topLeftCorner.Y); } }
// public func IsInShape(point: SquareFillPoint) -> Bool public bool IsInShape(SquareFillPoint point) { var isInShape = false; foreach (var element in _squares) { isInShape = isInShape || element.IsInSquare(point: point); } return(isInShape); }
// public func CheckWhetherMovementIsPossible( // occupiedGridSquares: Grid, // newTopLeftCorner: SquareFillPoint) -> MovementAnalyser public MovementAnalyser CheckWhetherMovementIsPossible( Grid occupiedGridSquares, SquareFillPoint newTopLeftCorner) { var movementAnalyser = new MovementAnalyser( squares: _squares, occupiedGridSquares: occupiedGridSquares, newTopLeftCorner: newTopLeftCorner); return(movementAnalyser); }
// private func ChangeGridSquareOccupation( // gridReferenceInPixels: SquareFillPoint, // shapeInSquare: Shape!, // occupied: Bool) private void ChangeGridSquareOccupation( SquareFillPoint gridReferenceInPixels, Shape shapeInSquare, bool occupied) { var occupiedXCoordinate = gridReferenceInPixels.X / ShapeConstants.SquareWidth; var occupiedYCoordinate = gridReferenceInPixels.Y / ShapeConstants.SquareWidth; OccupyGridSquare(x: occupiedXCoordinate, y: occupiedYCoordinate, occupied: occupied); PlaceShapeInSquare(x: occupiedXCoordinate, y: occupiedYCoordinate, shapeInSquare: shapeInSquare); }
// private func CheckWhetherMovementIsPossible( // square: Square, // occupiedGridSquares: Grid, // newParentTopLeftCorner: SquareFillPoint) private void CheckWhetherMovementIsPossible( Square square, Grid occupiedGridSquares, SquareFillPoint newParentTopLeftCorner) { var newSquareTopLeftCorner = square.CalculatePotentialTopLeftCorner(parentTopLeftCorner: newParentTopLeftCorner); var newSquareGridOrigin = CalculateGridOrigin(topLeftCorner: newSquareTopLeftCorner); if (ShapeHasCrossedAHorizontalGridBoundary || ShapeHasCrossedAVerticalGridBoundary) { ThereAreShapesInTheWay = IsSomethingInTheWay( newSquareTopLeftCorner: newSquareTopLeftCorner, newSquareGridOrigin: newSquareGridOrigin, occupiedGridSquares: occupiedGridSquares); } }
// private func CalculateGridOrigin(topLeftCorner: SquareFillPoint) -> SquareFillPoint private SquareFillPoint CalculateGridOrigin(SquareFillPoint topLeftCorner) { var gridOrigin = new SquareFillPoint( x: topLeftCorner.X / ShapeConstants.SquareWidth, y: topLeftCorner.Y / ShapeConstants.SquareWidth); if (topLeftCorner.X < 0) { gridOrigin.X = gridOrigin.X - 1; } if (topLeftCorner.Y < 0) { gridOrigin.Y = gridOrigin.Y - 1; } return(gridOrigin); }
// private func CheckWhetherBoundariesHaveBeenCrossed( // anySquare: Square, // newTopLeftCorner: SquareFillPoint) private void CheckWhetherBoundariesHaveBeenCrossed( Square anySquare, SquareFillPoint newTopLeftCorner) { var oldSquareGridOrigin = anySquare.GetGridOrigin(); var newSquareTopLeftCorner = anySquare.CalculatePotentialTopLeftCorner(parentTopLeftCorner: newTopLeftCorner); var newSquareGridOrigin = CalculateGridOrigin(topLeftCorner: newSquareTopLeftCorner); ShapeHasCrossedAHorizontalGridBoundary = HasBoundaryBeenCrossed( oldPixelValue: anySquare.TopLeftCornerX, newPixelValue: newSquareTopLeftCorner.X, oldGridRef: oldSquareGridOrigin.X, newGridRef: newSquareGridOrigin.X); ShapeHasCrossedAVerticalGridBoundary = HasBoundaryBeenCrossed( oldPixelValue: anySquare.TopLeftCornerY, newPixelValue: newSquareTopLeftCorner.Y, oldGridRef: oldSquareGridOrigin.Y, newGridRef: newSquareGridOrigin.Y); }
// public func SelectShape(selectedPoint: SquareFillPoint, selectedPointIsGridRef: Bool = false) -> Shape! public Shape SelectShape(SquareFillPoint selectedPoint, bool selectedPointIsGridRef = false) { var convertedSelectedPoint = selectedPoint; Shape selectedShape = null; if (selectedPointIsGridRef) { convertedSelectedPoint = selectedPoint.ConvertToPixels(); } foreach (var element in _shapes) { if (element.IsInShape(point: convertedSelectedPoint)) { selectedShape = element; } } return(selectedShape); }
// init( // squares: [Square], // occupiedGridSquares: Grid, // newTopLeftCorner: SquareFillPoint) public MovementAnalyser( List <Square> squares, Grid occupiedGridSquares, SquareFillPoint newTopLeftCorner) { // Even though these values are initialised in the methods below, // we have to set them here too otherwise Swift will complain. ShapeHasCrossedAHorizontalGridBoundary = false; ShapeHasCrossedAVerticalGridBoundary = false; ThereAreShapesInTheWay = false; CheckWhetherBoundariesHaveBeenCrossed(anySquare: squares[0], newTopLeftCorner: newTopLeftCorner); foreach (var element in squares) { CheckWhetherMovementIsPossible( square: element, occupiedGridSquares: occupiedGridSquares, newParentTopLeftCorner: newTopLeftCorner); } }
// init( // topLeftCorner: SquareFillPoint, // squareDefinitions: [Square], // topLeftCornerIsGridRef: Bool = true) public Shape( SquareFillPoint topLeftCorner, List <Square> squareDefinitions, bool topLeftCornerIsGridRef = true) { // Even though these values are initialised in the methods below, // we have to set them here too otherwise Swift will complain. _topLeftCorner = SquareFillPoint(x: 0, y: 0); _squares = squareDefinitions; _numSquaresLeftOfTopLeftCorner = 0; _numSquaresRightOfTopLeftCorner = 0; _numSquaresAboveTopLeftCorner = 0; _numSquaresBelowTopLeftCorner = 0; InitialiseTopLeftCorner( topLeftCorner: topLeftCorner, topLeftCornerIsGridRef: topLeftCornerIsGridRef); CalculateNumSquaresAroundTopLeftCorner(); UpdateTopLeftCorner(newTopLeftCorner: _topLeftCorner); }
// private func PointIsEqualOrToRightOfLeftEdge(point: SquareFillPoint) -> Bool private bool PointIsEqualOrToRightOfLeftEdge(SquareFillPoint point) { return(point.X >= _topLeftCorner.X); }
// private func PointIsToLeftOfRightEdge(point: SquareFillPoint) -> Bool private bool PointIsToLeftOfRightEdge(SquareFillPoint point) { return(point.X < RightEdge); }
// private func PointIsBetweenTopAndBottomEdges(point: SquareFillPoint) -> Bool private bool PointIsBetweenTopAndBottomEdges(SquareFillPoint point) { return(PointIsEqualOrBelowTopEdge(point: point) && PointIsAboveBottomEdge(point: point)); }
// private func PointIsEqualOrBelowTopEdge(point: SquareFillPoint) -> Bool private bool PointIsEqualOrBelowTopEdge(SquareFillPoint point) { return(point.Y >= _topLeftCorner.Y); }
// private func PointIsAboveBottomEdge(point: SquareFillPoint) -> Bool private bool PointIsAboveBottomEdge(SquareFillPoint point) { return(point.Y < BottomEdge); }
// init() public Square() { _topLeftCorner = SquareFillPoint(x: 0, y: 0); _positionRelativeToParentCorner = SquareFillPoint(x: 0, y: 0); _sprite = new NullSquareView(); }
// public func CalculateCursorPositionBasedOnTopLeftCorner(topLeftCornerRelativeToCursorPosition: SquareFillPoint) -> SquareFillPoint public SquareFillPoint CalculateCursorPositionBasedOnTopLeftCorner(SquareFillPoint topLeftCornerRelativeToCursorPosition) { return(SquareFillPoint( x: _topLeftCorner.X - topLeftCornerRelativeToCursorPosition.X, y: _topLeftCorner.Y - topLeftCornerRelativeToCursorPosition.Y)); }
// private func PointIsBetweenLeftAndRightEdges(point: SquareFillPoint) -> Bool private bool PointIsBetweenLeftAndRightEdges(SquareFillPoint point) { return(PointIsEqualOrToRightOfLeftEdge(point: point) && PointIsToLeftOfRightEdge(point: point)); }
// init (positionRelativeToParentCorner: SquareFillPoint, sprite: ISquareView) public Square(SquareFillPoint positionRelativeToParentCorner, ISquareView sprite) { _positionRelativeToParentCorner = positionRelativeToParentCorner; _sprite = sprite; _topLeftCorner = SquareFillPoint(x: 0, y: 0); }
// public func CalculatePotentialTopLeftCorner(parentTopLeftCorner: SquareFillPoint) -> SquareFillPoint public SquareFillPoint CalculatePotentialTopLeftCorner(SquareFillPoint parentTopLeftCorner) { return(SquareFillPoint( x: parentTopLeftCorner.X + (_positionRelativeToParentCorner.X * ShapeConstants.SquareWidth), y: parentTopLeftCorner.Y + (_positionRelativeToParentCorner.Y * ShapeConstants.SquareWidth))); }
// public func CalculateTopLeftCornerRelativeToCursorPosition(cursorPosition: SquareFillPoint) -> SquareFillPoint public SquareFillPoint CalculateTopLeftCornerRelativeToCursorPosition(SquareFillPoint cursorPosition) { return(SquareFillPoint( x: _topLeftCorner.X - cursorPosition.X, y: _topLeftCorner.Y - cursorPosition.Y)); }
// public func IsInSquare(point: SquareFillPoint) -> Bool public bool IsInSquare(SquareFillPoint point) { return(PointIsBetweenLeftAndRightEdges(point: point) && PointIsBetweenTopAndBottomEdges(point: point)); }