Forum Replies Created
-
AuthorPosts
-
Hi,
thanks for the hint nerobot. I’m just doing the “double work” out of habit, so should I ever change the way I disable controllers I only need to change it in one place, i.e. in Method Disable. I think here the performance impact should be negligible.
And cocon, I just came up with this manager class to keep it as flexible as possible. In the setup I basically just create controls with a condition and a callback. I could add this stuff into the render loop but I prefer to have it packaged in this class not to “pollute” the render loop too much, and also to easily disable/enable controls later. That’s where the Lamda functions are really useful.
So in the main program this is how I’m using this class:
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657#Import "utils/control_manager"Class MyApp Extends WindowField ControlManager:ControlManagerConst DegreesPerKeyDown := 1Const RadiansPerKeyDown := DegreesPerKeyDown * Pi / 180' ... more code hereMethod AddControls()ControlManager = New ControlManager()If Accel.Supported' Accelerometer controlsLocal unconditional := Lambda:Bool()Return TrueEndLocal rotatePlayerByAccelX := Lambda()Player.Rotation = -Accel.YEndControlManager.Add( "accelerometer", unconditional, rotatePlayerByAccelX )Else' Keyboard controlsLocal leftArrowDown := Lambda:Bool()Return Keyboard.KeyDown( Key.Left )EndLocal rightArrowDown := Lambda:Bool()Return Keyboard.KeyDown( Key.Right )EndLocal playerRotateLeft := Lambda()Player.Rotation -= RadiansPerKeyDownEndLocal playerRotateRight := Lambda()Player.Rotation += RadiansPerKeyDownEndControlManager.Add( "leftArrowDown", leftArrowDown, playerRotateLeft )ControlManager.Add( "rightArrowDown", rightArrowDown, playerRotateRight )EndEndMethod OnRender:Void( canvas:Canvas ) OverrideControlManager.OnUpdate()Player.OnUpdate()' ... more code hereEndEndI could later then just do something like
Monkey1ControlManager.Disable( "accelerometer" )<div id=”crayon-59d4ac7531a47621632511-36″ class=”crayon-line”></div>
Thanks Mark. That’s good advice.
In this case I actually changed it to a key map so that I have easy access to individual collection items via its key.
Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152Class ControlField Trigger:Bool()Field Callback:Void()Field Enabled:Bool = TrueEndClass ControlManagerField Controls := New Map<String,Control>Method Add ( key:String, trigger:Bool(), callback:Void() )Local control := New Control()control.Trigger = triggercontrol.Callback = callbackControls.Add( key, control )EndMethod Remove ( key:String )Controls.Remove( key )EndMethod Disable ( key:String )Controls.Get( key ).Enabled = FalseEndMethod Enable ( key:String )Controls.Get( key ).Enabled = TrueEndMethod DisableAll ()For Local key := Eachin Controls.KeysDisable( key )NextEndMethod EnableAll ()For Local key := Eachin Controls.KeysEnable( key )NextEndMethod OnUpdate ()For Local control := Eachin Controls.ValuesIf control.Enabled And control.Trigger()control.Callback()EndNextEndEndAlso, very nice that we have Lamda functions now. It’s really fun to use Monkey 2, now that I got more used to the syntax and strict types again.
Oh! Thanks Jesse. I could’ve sworn that I tried this, but apparently not. And yes, that works well, even with length 0.
Ah, great! Thanks for the info. I’ll try that.
-
AuthorPosts