class DrunkInvaders : AbstractProducer { static var instance : DrunkInvaders // Reset when returning to the menu var lives = 4 // Reset when returning to the menu var score = 0 // The overlapping strategy used within this game. var overlapping : Overlapping init { instance = this } override fun begin() { loadWindowSize() // Note, if we have bigger objects later, then we'll need to increase the size. overlapping = PixelOverlapping( 200 /*size*/ , 128 /*threshold*/ ) } // Stores a boolean indicating that the scene has been completed, and is therefore unlocked // i.e. a player can start the game at that scene. // The data is stored in the "Registry", so it available the next time the game is started. fun unlockScene( sceneName : String ) { Game.instance.preferences.node( "scenes/$sceneName" ).putBoolean( "unlocked", true ) } // Reads the data from the registry, which may have been written by unlockScene (above). fun isSceneUnlocked( sceneName : String) = Game.instance.preferences.node( "scenes/$sceneName" ).getBoolean( "unlocked", false ) // The window can be resized, and the views are resized according to the details in the "Layouts" section. override fun layout() { super.layout() arrangeViews() } override fun onResize( event : ResizeEvent ) { super.onResize( event ) saveWindowSize() } // Scene Transitions... val sceneTransition = SceneTransition() override fun sceneEnding() { sceneTransition.prepareTransition() } override fun sceneLoaded() { val actor = sceneTransition.actor if (actor != null) { sceneTransition.beginTransition( randomTransition( actor ) ) } } fun randomTransition(actor : Actor) : Action { val seconds = 0.5 val i = Rand.randomInt( 3 ) return if ( i == 0 ) { MoveXBy( actor.position, seconds, actor.appearance.width() ) } else if ( i == 1 ) { MoveYBy( actor.position, seconds, actor.appearance.height() ) } else { Fade( actor, seconds, 0 ) } } // Make a "shake" effect, by moving a screenshot rapidly left and right fun shake(scale : float) { sceneTransition.apply { prepareTransition() actor.color.alpha = 0.5 val seconds = 0.03 val distance = 20 * scale beginTransition( ( MoveXBy( actor.position, seconds, distance ) then MoveXBy( actor.position, seconds*2, -2*distance ) then MoveXBy( actor.position, seconds, distance ) ).repeat(5 * scale) ) } } }