what is so good about oop ?
Check this little nugget of code. I have a base controller object which must be extended. The keyboard implimentation will interpret keyboard input and dump universal (off,hit,tick,held,release) input states into the controller object. The program can then read these states from the controller object regardless of which input periphial it came from.
Further extensions of the control object could use joypad, mouse, network. It would just require making extended types for each of said input methods. Now my program can transparently refer to...
control.inputbuffer_up
... to find out the up state of a "controller".
Without OOP, a method like this would be doable but require a less intuative approach. The idea of using objects is just a different one to grasp, but in this example you can see that it gives you a "plug and play" ability which procedural languages can only fake.
'class Type Ccontrol Abstract Const inputbuffer_off = 0 Const inputbuffer_press = 1 Const inputbuffer_tick = 2 Const inputbuffer_release = 3 Const inputbuffer_held = 4 Field inputbuffer_up Field inputbuffer_down Field inputbuffer_left Field inputbuffer_right Field inputbuffer_button1 Field inputbuffer_button2 Field inputbuffer_button3 Field inputbuffer_button4 Field inputbuffer_button5 Field inputbuffer_button6 Field inputtick_up Field inputtick_down Field inputtick_left Field inputtick_right Field inputtick_button1 Field inputtick_button2 Field inputtick_button3 Field inputtick_button4 Field inputtick_button5 Field inputtick_button6 Method SetButtons(button1,button2,button3,button4,button5,button6) Abstract Method SetAxis(axisup,axisdown,axisleft,axisright) Abstract Method Update() Abstract End Type '-------------------------------------------------------- 'class Type Ckeyboard Extends Ccontrol 'keyboard input for a player Field key_up Field key_down Field key_left Field key_right Field key_button1 Field key_button2 Field key_button3 Field key_button4 Field key_button5 Field key_button6 Method SetButtons(button1,button2,button3,button4,button5,button6) 'configures keyboard buttons key_button1 = button1 key_button2 = button2 key_button3 = button3 key_button4 = button4 key_button5 = button5 key_button6 = button6 End Method Method SetAxis(axisup,axisdown,axisleft,axisright) 'configures keyboard axis key_up = axisup key_down = axisdown key_left = axisleft key_right = axisright End Method Method Update() 'gets input from input source and stores in control input buffers inputbuffer_up = NewInputBufferState(key_up,inputbuffer_up,inputtick_up) inputbuffer_down = NewInputBufferState(key_down,inputbuffer_down,inputtick_down) inputbuffer_left = NewInputBufferState(key_left,inputbuffer_left,inputtick_left) inputbuffer_right = NewInputBufferState(key_right,inputbuffer_right,inputtick_right) inputbuffer_button1 = NewInputBufferState(key_jump,inputbuffer_button1,inputtick_button1) inputbuffer_button2 = NewInputBufferState(key_kick,inputbuffer_button2,inputtick_button2) inputbuffer_button3 = NewInputBufferState(key_punch,inputbuffer_button3,inputtick_button3) inputbuffer_button4 = NewInputBufferState(key_action,inputbuffer_button4,inputtick_button4) inputbuffer_button5 = NewInputBufferState(key_punch,inputbuffer_button5,inputtick_button5) inputbuffer_button6 = NewInputBufferState(key_action,inputbuffer_button6,inputtick_button6) End Method Method NewInputBufferState(key,buffer,tick) 'reads curretn input buffer state and works out new state Select buffer Case Ccontrol.inputbuffer_off 'check for key hit If KeyHit(key) Return Ccontrol.inputbuffer_press Else Return Ccontrol.inputbuffer_off End If Case Ccontrol.inputbuffer_press 'check for key held If KeyDown(key) Return Ccontrol.inputbuffer_held Else Return Ccontrol.inputbuffer_release End If Case Ccontrol.inputbuffer_tick 'check for instant tick If KeyDown(key) If tick = 0 Return Ccontrol.inputbuffer_tick Else Return Ccontrol.inputbuffer_held End If Else Return Ccontrol.inputbuffer_release End If Case Ccontrol.inputbuffer_release 'reset key state Return Ccontrol.inputbuffer_off Default 'check for key held If KeyDown(key) If (buffer-Ccontrol.inputbuffer_held) >= tick Return Ccontrol.inputbuffer_held + ((buffer-Ccontrol.inputbuffer_held) - tick) Else Return buffer+MilliSecs() End If Else Return Ccontrol.inputbuffer_release End If End Select End Method End Type 'create class function Function CreateKeyboardControl:Ccontrol() Local control:Ccontrol = New Ckeyboard Return control End Function local control:Ccontrol = CreateKeyboardControl()
Check this little nugget of code. I have a base controller object which must be extended. The keyboard implimentation will interpret keyboard input and dump universal (off,hit,tick,held,release) input states into the controller object. The program can then read these states from the controller object regardless of which input periphial it came from.
Further extensions of the control object could use joypad, mouse, network. It would just require making extended types for each of said input methods. Now my program can transparently refer to...
control.inputbuffer_up
... to find out the up state of a "controller".
Without OOP, a method like this would be doable but require a less intuative approach. The idea of using objects is just a different one to grasp, but in this example you can see that it gives you a "plug and play" ability which procedural languages can only fake.