// Fired by AlienShip class AlienBullet : AbstractRole, Alien { // The speed of the bullet in pixels per tick. // Note, the direction is governed by Actor.direction. @Attribute var speed = 3.0 // An Action, which animates the bullet (it has two different poses) // tick() by call animation.act() for it to take effect. var animation : Action override fun begin() { animation = ( Delay( 0.1 ) then EventAction( actor, "other" ) then Delay( 0.1 ) then EventAction( actor, "default" ) ).forever() animation.begin() } override fun tick() { // Alternate between the two Poses animation.act() actor.moveForwards( speed ) // Get the collision algorithm, shared throughout the game. val overlapping = DrunkInvaders.instance.overlapping // Look for everything that this bullet can hit, and // check if we are overlapping them. for( other in actor.stage.findRolesByClass( Human ) ) { if (overlapping.overlapping( actor, other.actor ) ) { // Have we hit a shielded ship? if ( other is Ship && (other as Ship).shielded ) { // Change roles to Dying rather than just actor.die() // for a nice effect actor.role = Dying() actor.event( "hitShielded" ) // Play a sound??? } else { // Note, we let the "other" make a sound effect. other.hit() actor.die() // Die instantly, without the nice effect. } // If we are overlapping two things simultaneously, only hit the first one we find. break } } // Have we left the screen? if ( actor.x < 0 || actor.y < 0 || actor.x > 640 || actor.y > 480 ) { actor.die() } } // Called when we've been hit by a (human) Bullet override fun hit() { actor.event("die") actor.die() } }