/** Bounces off the sides of the scene */ class Bounce : Circular { override fun tick() { // We can find the "preferred" size of the game window from the GameInfo class // NOTE, if we allow the window to be resized, then this is NOT a good way to find the bounds of the game area! val gameInfo = Game.instance.resources.gameInfo // Find the axis aligned rectangle of the Actor's Appearance. // Note, if the Actor is rotated, then this is NOT a good way to test if the Actor has strayed outside of // game area. val rect = actor.appearance.worldRect() if ( (rect.left < 0 && velocity.x < 0) || (rect.right > gameInfo.width && velocity.x > 0) ) { //println( "Bounced X" ) velocity.x = - velocity.x } //println( "${rect.top} vs ${gameInfo.height} ... $velocity.y" ) if ( (rect.bottom < 0 && velocity.y < 0) || (rect.top > gameInfo.height && velocity.y > 0) ) { //println( "Bounced Y" ) velocity.y = - velocity.y } super.tick() } }