PlaneControl.monkey2 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. Namespace mojo3d
  2. #Import "Useful"
  3. Using useful..
  4. Class PlaneControl Extends Behaviour
  5. Field speed:= 1.0
  6. Field turnRate := 0.5
  7. Field ascentionRate := 0.1
  8. Field lag := 100.0
  9. Field maxPitch := 30.0
  10. Field maxRoll := 60.0
  11. Field maxYaw := 15.0
  12. Field plane:Entity 'The plane model to which we'll apply roll
  13. Field target:Entity 'The camera target, positioned ahead of the plane
  14. Field camera:Entity
  15. Private
  16. Field _azimuth:Float
  17. Field _azimuthGoal:Float
  18. Field _pitch:Float
  19. Field _yaw:Float
  20. Field _roll:Float
  21. Field _throttle:Float = 1.0
  22. Field _zSpeed:Float
  23. Field _ySpeed:Float
  24. Field _ySpeedGoal:Float
  25. Public
  26. Method New( entity:Entity )
  27. Super.New( entity )
  28. End
  29. Method OnUpdate( elapsed:Float ) Override
  30. Local delta := elapsed * 60.0
  31. Local entity:=Entity
  32. If Keyboard.KeyDown( Key.Left )
  33. _azimuthGoal += turnRate
  34. _yaw = maxYaw
  35. _roll = -maxRoll
  36. Else If Keyboard.KeyDown( Key.Right )
  37. _azimuthGoal -= turnRate
  38. _yaw = -maxYaw
  39. _roll = maxRoll
  40. Else
  41. _yaw = 0
  42. _roll = 0
  43. Endif
  44. If Keyboard.KeyDown( Key.Up )
  45. _pitch = -maxPitch
  46. _ySpeedGoal = -ascentionRate * 2.0
  47. Else If Keyboard.KeyDown( Key.Down )
  48. _pitch = maxPitch
  49. _ySpeedGoal = ascentionRate
  50. Else
  51. _ySpeedGoal = 0
  52. _pitch = 0
  53. Endif
  54. _zSpeed = -speed * _throttle * delta
  55. _ySpeed = Smooth( _ySpeed, _ySpeedGoal, lag, delta )
  56. entity.Move( 0, _ySpeed, _zSpeed )
  57. _azimuth = Smooth( _azimuth, _azimuthGoal, lag, delta )
  58. entity.Ry = _azimuth
  59. plane.LocalRx = Smooth( plane.Rx, _pitch, lag, delta )
  60. plane.LocalRy = Smooth( plane.LocalRy, _yaw, lag, delta )
  61. plane.LocalRz = Smooth( plane.Rz, _roll, lag, delta )
  62. camera.LocalRz = plane.LocalRz/2.0
  63. target.LocalX = Smooth( target.LocalX, _yaw/4.0, lag, delta )
  64. End
  65. End