예제 #1
0
 // public func OccupyGridSquareUsingPixels(gridReferenceInPixels: SquareFillPoint, shapeInSquare: Shape!)
 public void OccupyGridSquareUsingPixels(SquareFillPoint gridReferenceInPixels, Shape shapeInSquare)
 {
     ChangeGridSquareOccupation(
         gridReferenceInPixels: gridReferenceInPixels,
         shapeInSquare: shapeInSquare,
         occupied: true);
 }
예제 #2
0
        // 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);
        }
예제 #3
0
 // public func VacateGridSquareUsingPixels(gridReferenceInPixels: SquareFillPoint)
 public void VacateGridSquareUsingPixels(SquareFillPoint gridReferenceInPixels)
 {
     ChangeGridSquareOccupation(
         gridReferenceInPixels: gridReferenceInPixels,
         shapeInSquare: null,
         occupied: false);
 }
예제 #4
0
        // 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);
        }
예제 #5
0
 // public func UpdateTopLeftCorner (newTopLeftCorner: SquareFillPoint)
 public void UpdateTopLeftCorner(SquareFillPoint newTopLeftCorner)
 {
     _topLeftCorner = newTopLeftCorner;
     foreach (var element in _squares)
     {
         element.MoveTopLeftCorner(newTopLeftCorner: newTopLeftCorner);
     }
 }
예제 #6
0
 // 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));
     }
 }
예제 #7
0
 // 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);
     }
 }
예제 #8
0
        // 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);
        }
예제 #9
0
        // 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);
        }
예제 #10
0
        // 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);
        }
예제 #11
0
        // 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);
            }
        }
예제 #12
0
        // 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);
        }
예제 #13
0
        // 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);
        }
예제 #14
0
        // 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);
        }
예제 #15
0
        // 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);
            }
        }
예제 #16
0
        // 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);
        }
예제 #17
0
 // private func PointIsEqualOrToRightOfLeftEdge(point: SquareFillPoint) -> Bool
 private bool PointIsEqualOrToRightOfLeftEdge(SquareFillPoint point)
 {
     return(point.X >= _topLeftCorner.X);
 }
예제 #18
0
 // private func PointIsToLeftOfRightEdge(point: SquareFillPoint) -> Bool
 private bool PointIsToLeftOfRightEdge(SquareFillPoint point)
 {
     return(point.X < RightEdge);
 }
예제 #19
0
 // private func PointIsBetweenTopAndBottomEdges(point: SquareFillPoint) -> Bool
 private bool PointIsBetweenTopAndBottomEdges(SquareFillPoint point)
 {
     return(PointIsEqualOrBelowTopEdge(point: point) &&
            PointIsAboveBottomEdge(point: point));
 }
예제 #20
0
 // private func PointIsEqualOrBelowTopEdge(point: SquareFillPoint) -> Bool
 private bool PointIsEqualOrBelowTopEdge(SquareFillPoint point)
 {
     return(point.Y >= _topLeftCorner.Y);
 }
예제 #21
0
 // private func PointIsAboveBottomEdge(point: SquareFillPoint) -> Bool
 private bool PointIsAboveBottomEdge(SquareFillPoint point)
 {
     return(point.Y < BottomEdge);
 }
예제 #22
0
 // init()
 public Square()
 {
     _topLeftCorner = SquareFillPoint(x: 0, y: 0);
     _positionRelativeToParentCorner = SquareFillPoint(x: 0, y: 0);
     _sprite = new NullSquareView();
 }
예제 #23
0
 // public func CalculateCursorPositionBasedOnTopLeftCorner(topLeftCornerRelativeToCursorPosition: SquareFillPoint) -> SquareFillPoint
 public SquareFillPoint CalculateCursorPositionBasedOnTopLeftCorner(SquareFillPoint topLeftCornerRelativeToCursorPosition)
 {
     return(SquareFillPoint(
                x: _topLeftCorner.X - topLeftCornerRelativeToCursorPosition.X,
                y: _topLeftCorner.Y - topLeftCornerRelativeToCursorPosition.Y));
 }
예제 #24
0
 // private func PointIsBetweenLeftAndRightEdges(point: SquareFillPoint) -> Bool
 private bool PointIsBetweenLeftAndRightEdges(SquareFillPoint point)
 {
     return(PointIsEqualOrToRightOfLeftEdge(point: point) &&
            PointIsToLeftOfRightEdge(point: point));
 }
예제 #25
0
 // init (positionRelativeToParentCorner: SquareFillPoint, sprite: ISquareView)
 public Square(SquareFillPoint positionRelativeToParentCorner, ISquareView sprite)
 {
     _positionRelativeToParentCorner = positionRelativeToParentCorner;
     _sprite        = sprite;
     _topLeftCorner = SquareFillPoint(x: 0, y: 0);
 }
예제 #26
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)));
 }
예제 #27
0
 // public func CalculateTopLeftCornerRelativeToCursorPosition(cursorPosition: SquareFillPoint) -> SquareFillPoint
 public SquareFillPoint CalculateTopLeftCornerRelativeToCursorPosition(SquareFillPoint cursorPosition)
 {
     return(SquareFillPoint(
                x: _topLeftCorner.X - cursorPosition.X,
                y: _topLeftCorner.Y - cursorPosition.Y));
 }
예제 #28
0
 // public func IsInSquare(point: SquareFillPoint) -> Bool
 public bool IsInSquare(SquareFillPoint point)
 {
     return(PointIsBetweenLeftAndRightEdges(point: point) &&
            PointIsBetweenTopAndBottomEdges(point: point));
 }