On Grid we can now write methods that check for permission to push in all 4 directions.
canPushCellNorthFromLocation: aPoint
| direction |
direction := GridDirection directionFor: #north.
^self canPushCell: direction fromLocation: aPoint.
canPushCellEastFromLocation: aPoint
| direction |
direction := GridDirection directionFor: #east.
^self canPushCell: direction fromLocation: aPoint.
canPushCellSouthFromLocation: aPoint
| direction |
direction := GridDirection directionFor: #south.
^self canPushCell: direction fromLocation: aPoint.
canPushCellWestFromLocation: aPoint
| direction |
direction := GridDirection directionFor: #west.
^self canPushCell: direction fromLocation: aPoint.
With that model code ready we switch gears and work on the graphics side. We can map those same 4 direction push permission requests right back to our click region push hierarchy. Here are 4 new class methods. One for each push click region.
New class method on CellClickRegionPushNorth.
canPushCell: aCell withinGrid: aGrid
^aGrid canPushCellNorthFromLocation: aCell gridLocation
New class method on CellClickRegionPushEast.
canPushCell: aCell withinGrid: aGrid
^aGrid canPushCellEastFromLocation: aCell gridLocation
New class method on CellClickRegionPushSouth.
canPushCell: aCell withinGrid: aGrid
^aGrid canPushCellSouthFromLocation: aCell gridLocation
And the new class method on CellClickRegionPushWest.
canPushCell: aCell withinGrid: aGrid
^aGrid canPushCellWestFromLocation: aCell gridLocation
We're using polymorphism here and letting the click region classes work out the direction. Now we write a new class method on CellClickRegionInside to send this method to the region.
canActOnCellAtPoint: aPoint cell: aCell withinGrid: aGrid
| pushRegion |
pushRegion := self pushRegionForPoint: aPoint.
^pushRegion canPushCell: aCell withinGrid: aGrid
The CellClickRegionOutside class handles requests for rotation. This part is easy. It's always the same answer. Here's the class method as implemented on CellClickRegionOutside.
canActOnCellAtPoint: aPoint cell: aCell withinGrid: aGrid
^true