Skip to content

Instantly share code, notes, and snippets.

@dlwh
Created September 30, 2011 03:33
Show Gist options
  • Save dlwh/1252573 to your computer and use it in GitHub Desktop.
Save dlwh/1252573 to your computer and use it in GitHub Desktop.
trait Action {
// ...
def instantiate(inventory: ResourceMap[Resource], units: Iterable[Unit]):ActionMachine
}
class BuildAction(tpe: UnitType) extends AwesomeAction {
def instantiate(inventory: ResourceMap[Resource], units: Iterable[Unit]):ActionMachine = new ActionMachine {
val builder = units.head
// register failure handler
val failHandle = on(builder.death) { event => fail()}
var position = findBuildSpot(...)
self.loop(frame) until (builder.closeTo(position) | failure) { frame =>
if(!builder.canBuildHere(position,tpe)) {
position = findBuildSpot(...)
}
}
// suspend until building started, return new building
builder.startBuilding(position,tpe) match {
Some(building) =>
None => fail()
}
// register completion and failure handlers
on(newBuilding.complete) { event => succeed() }
on(newBuilding.death) { event => fail() }
}
val failure = new EventSource[Fail[MoveAction]]
val success = new EventSource[Success[MoveAction]]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment