class Item : ActionRole { fun getNeighbourhood() = Play.instance.neighbourhood fun getBlock() = Play.instance.neighbourhood.blockAt( actor.x, actor.y ) override fun begin() { val block = getBlock() if (! block.occupants.isEmpty() ) { if ( this !is Leg ) { println( "Warning there is already an occupant here" ) actor.die() } } else { block.add(this) } super.begin() } override fun end() { getBlock().remove(this) } fun isSolid() = true fun isEmpty() = false fun occupant() : Occupant = null fun look( block : Block ) : Item { if ( block == null || block.occupants == null || block.occupants.isEmpty() ) { return NullItem.instance } else { return block.occupants.iterator().next() } } fun look( dx : int, dy : int ) = look(getBlock().neighbouringBlock(dx, dy)) fun lookNorth() = look( 0, 1 ) fun lookEast() = look( 1, 0 ) fun lookSouth() = look( 0, -1 ) fun lookWest() = look( -1, 0 ) fun look( direction : int ) = look( getDeltaX(direction), getDeltaY(direction) ) fun move( dx : int, dy : int ) { getBlock().remove(this) actor.x += getNeighbourhood().blockWidth * dx actor.y += getNeighbourhood().blockHeight * dy getBlock().add(this) } fun moveNorth() { move( 0, 1 ) } fun moveEast() { move( 1, 0 ) } fun moveSouth() { move( 0, -1 ) } fun moveWest() { move( -1, 0 ) } fun move( direction : int ) { move( getDeltaX(direction), getDeltaY(direction) ) } fun getDeltaX( direction : int ) : int { direction = direction % 4 if (direction < 0) direction += 4 return if ( direction == 0 ) { 1 } else if ( direction == 2 ) { -1 } else { 0 } } fun getDeltaY( direction : int ) : int { direction = direction % 4 if (direction < 0) direction += 4 return if ( direction == 1 ) { 1 } else if ( direction == 3 ) { -1 } else { 0 } } fun flash() : Action = ( Delay( 0.1 ) then EventAction( actor, "other" ) then Delay( 0.1 ) then EventAction( actor, "default" ) ).forever() fun kill() { actor.event( "die" ) actor.die() } }