/** The DollsHouse is "free-play". Drag Dolls and other Draggable objects around just for fun. Unlike the Play scenes, nothing breaks when knocked over. You can scale Dolls and other Draggable items while dragging them. Rewards are Dolls and other Draggables. Completing a Play scene may give 0 or more Rewards. Newly gained awards are highlighted with yellow Stars, but this is handled in DollsHouseProducer. Click a Postcard to begin one of the Play scenes. */ class DollsHouse : DollsHouseDirector { var dragPosition = Vector2() var mouseJoint: TickleMouseJoint = null var hand: GraphicalMouse = null var elastic: Elastic = null var gravityUp: Input var gravityDown: Input var grow: Input var shrink: Input var fat: Input var thin: Input override fun begin() { super.begin() val resources = Game.instance.resources gravityUp = findInput("gravityUp") gravityDown = findInput("gravityDown") grow = findInput("grow") shrink = findInput("shrink") fat = findInput("fat") thin = findInput("thin") // All DollsHouse scenes must have one and only one GraphicalMouse object hand = glassView.stage.findRoleByClass(GraphicalMouse) // All DollsHouse scenes must have one and only one Elastic object. elastic = mainView.stage.findRoleByClass( Elastic ) // Ensure that the dolls' limbs overlap in a sensible fashion by giving each Doll a unique zOrder // even if there are multiple dolls with the same costume. // Each Doll Costume has a zOrder set in the Editor, but that value is ignore in the DollsHouse. var dollZOrder = 50 for( doll in mainView.stage.findRolesByClass( Doll ) ) { doll.actor.zOrder = dollZOrder++ } if (Options.instance.cheat == false) { // Only keep the objects that have been "earned". // Some objects will only appear if the player has successfully completed a scene. // If the scene hasn't been completed, then kill the actor before the scene starts. for( reward in mainView.stage.findRolesByClass( Reward ) ) { if (reward.rewardForScene() != "") { if (!DollsHouseProducer.scenePreferences(reward.rewardForScene()).getBoolean("completed", false)) { reward.actor.die() } } } for ( postcard in mainView.stage.findRolesByClass( Postcard ) ) { if (postcard.requires != "") { if (!DollsHouseProducer.scenePreferences(postcard.requires).getBoolean("completed", false)) { postcard.actor.die() } } } } } override fun onKey(event: KeyEvent) { super.onKey(event) if (gravityUp.matches(event) ) { changeGravity( 50 ) } if (gravityDown.matches(event) ) { changeGravity( -50 ) } if (grow.matches(event) ) { grow( 1.2 ) } if (shrink.matches(event) ) { grow( 1/1.2 ) } if ( fat.matches( event) ) { grow( Vector2(1.2, 1) ) } if ( thin.matches( event) ) { grow( Vector2( 1/1.2, 1) ) } } fun grow( by : float ) { grow( Vector2( by, by ) ) } fun grow( by : Vector2 ) { if ( elastic.dragging == null) return if ( elastic.dragging is Scalable ) { elastic.dragging.scale( by ) } else { elastic.dragging.actor.scale *= by } // The mass will have changed by the sqaure of the scaling factor, so do the same to the force. elastic.mouseJoint.maxForce *= by.x * by.y } fun changeGravity( by : float ) { val world = findStage("main").world world.gravity = ImmutableVector2( 0, world.gravity.y + by ) } override fun onMouseButton(event: MouseEvent) { mainView.worldToViewport( event.windowPosition, event.viewportPosition ) mainView.viewportToWorld( event.viewportPosition , event.worldPosition ) if (event.state == ButtonState.PRESSED) { hand.actor.event("down") // Change pose to the "Pinch" if (event.button == 0) { if ( pickUpObject(event) ) { return } } } else if (event.state == ButtonState.RELEASED) { hand.actor.event("default") // Change back to the non "Pinch" pose. if ( elastic.dragging != null ) { dropObject() event.release() return } } super.onMouseButton(event) } fun pickUpObject(event: MouseEvent) : boolean { mainView.windowToWorld( event.windowPosition, event.worldPosition ) val role = mainView.findRoleAt( Draggable, event.worldPosition) if (role != null) { event.capture() // Have the doll say its name! if (role is DollPart) { (role as DollPart).doll.actor.event( "name" ) } elastic.join( role, hand, event.worldPosition ) return true } return false } fun dropObject() { elastic.unjoin() hand.actor.event("default") } }