Skip to content

Instantly share code, notes, and snippets.

@johansjoberg
Created January 6, 2012 23:52
Show Gist options
  • Save johansjoberg/1573064 to your computer and use it in GitHub Desktop.
Save johansjoberg/1573064 to your computer and use it in GitHub Desktop.
option refactoring
def click(x: Int, y: Int) {
val mx = screen.calculateTileIndex(screen.sx+x)
val my = screen.calculateTileIndex(screen.sy+y)
val tile = map.tiles(mx,my)
performPossibleUnitAction(tile.unit, mx, my)
}
private def performPossibleUnitAction(tileUnit:Option[Unit], mx: Int, my: Int) {
(tileUnit, selectedUnit) match {
case (None, Some(unit)) => {
unit.moveTo(mx, my)
}
case (Some(newSelectedUnit), Some(oldSelectedUnit)) => {
newSelectedUnit.select()
oldSelectedUnit.unselect()
}
case (Some(unit), None) => {
selectedUnit = Option(unit)
unit.select()
}
case (None, None) => //do nothing
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment