// Bees are like the "spirits" in Repton. They follow the edges of the walls. // A CostumeAttribute determines if they hug the walls clockwise or anti-clockwise // (unlike Repton, where they all went the same way). // // They have an initial direction defined as an Attribute. // // They can travel through empty, squashable and Soil, leaving them original // alternateOccupants untouched. // // They kill Players // // If they travel to a Flower, it turns into a Fruit (which is a Valuable). // // Bees can either be added directly into a scene, or be created from a Hive. // Those from a hive will return the hive after they have pollinated the flower. // When all bees return, the hive becomes collectable (Honey, yummy!) // // Homeless bees die on pollinating a flower. // class Bee : Movable { // Detrmines which direction around the loop. // When true, always tries to turn left. // i.e. if previous movement was NORTH, then try WEST next. // If previous movement was SOUTH, then try EAST next. @CostumeAttribute var leftFirst = true @Attribute var direction = EAST // When true, the bee dies when it pollinates a flower. // When false, it returns to a hive after it has pollinated. var homeless = true // Only used by bees from a hive. When true, the bee will return into the hive. var pollinated = false // An attempt to prevent bees becoming detatched from the edge they were following // because of another bee getting in their way. This is espcially needed when there // are dead ends, or bees travelling in different directions. var stuckCounter = 0 override fun isDeadly() = true override fun myTurn() { for ( i in 0 until 4 ) { val newDir = ( direction + ( if (leftFirst) 5 - i else 3 + i ) ) % 4 val next = look( newDir ) if ( next.isPlayer() || next.isEmpty() || next.squashable( newDir ) || next is Soil ) { direction = newDir makeMove( newDir ) return } if ( next is Bee ) { if ( ( next as Bee).stuckCounter < 5 ) { stuckCounter ++ if ( stuckCounter > 10 ) { stuckCounter = 0 } else { return } } } // Is it an unpollinated flower? if (pollinated == false && next is Flower && (next as Flower).state == 0) { pollinated = true // We can now return to the hive (if we aren't homeless) makeMove( newDir ) if ( homeless ) { speed = 2 and( ScaleTo( actor, 0.5, 0.2 ) ) } return } // Returned to the hive? if ( pollinated && (next is Hive) && (next as Hive).open()) { makeMove( newDir ) and( ScaleTo( actor, 0.5, 0.2 ) ) return } } } fun makeMove( direction : int ) { stuckCounter = 0 actor.event( "move${Item.directionAbbreviation(direction)}" ) move( direction ) } // Grow back to normal size, and normal speed. override fun moved( movement : Movement ) { super.moved( movement ) if ( speed != 8 ) { if ( homeless ) { actor.die() } } } override fun invaded( movement : Movement ) : boolean { if ( movement.item.isPlayer() ) { movement.item.kill() } return true } }