/src/hype/extended/behavior/SimpleProximity.as

https://github.com/GunioRobot/hype
ActionScript | 126 lines | 76 code | 18 blank | 32 comment | 2 complexity | 426c6df766e0106f73181c08d675ba20 MD5 | raw file
  1. package hype.extended.behavior {
  2. import hype.framework.behavior.AbstractBehavior;
  3. import hype.framework.behavior.IBehavior;
  4. import hype.framework.core.HypeMath;
  5. import flash.display.Sprite;
  6. /**
  7. * Make a property change in proportion to the distance from the mouse cursor
  8. */
  9. public class SimpleProximity extends AbstractBehavior implements IBehavior {
  10. protected var _prop:String;
  11. protected var _min:Number;
  12. protected var _max:Number;
  13. protected var _radius:Number;
  14. protected var _radiusSq:Number;
  15. protected var _spring:Number;
  16. protected var _ease:Number;
  17. protected var _speed:Number;
  18. protected var _range:Number;
  19. /**
  20. * Constructor
  21. *
  22. * @param target Target object
  23. * @param prop Target property
  24. * @param spring The springiness of the movement
  25. * @param ease The ease of the movement
  26. * @param min Base value of the property (when outside of the radius)
  27. * @param max Value when the mouse cursor is directly on top of the target
  28. * @param radius Radius of the circle in which this behavior will have an affect
  29. */
  30. public function SimpleProximity(target:Object, prop:String, spring:Number, ease:Number, min:Number, max:Number, radius:Number) {
  31. super(target);
  32. _prop = prop;
  33. _min = min;
  34. _max = max;
  35. _radius = radius;
  36. _radiusSq = radius * radius;
  37. _spring = spring;
  38. _ease = ease;
  39. _speed = 0;
  40. _range = (_max - _min);
  41. }
  42. /**
  43. * @protected
  44. */
  45. public function run(target:Object):void {
  46. var goal:Number;
  47. var value:Number = getProperty(_prop);
  48. var sprite:Sprite = target as Sprite;
  49. var dist:Number = Math.pow(sprite.y - sprite.parent.mouseY, 2) +
  50. Math.pow(sprite.x - sprite.parent.mouseX, 2);
  51. if (dist < _radiusSq) {
  52. goal = (1 - (dist / _radiusSq)) * _range + _min;
  53. } else {
  54. goal = _min;
  55. }
  56. _speed = (_speed * _spring) + (HypeMath.getDistance(_prop, value, goal) * _ease);
  57. setProperty(_prop, value + _speed);
  58. }
  59. /**
  60. * Springiness of the movement
  61. */
  62. public function get spring():Number {
  63. return _spring;
  64. }
  65. public function set spring(spring:Number):void {
  66. _spring = spring;
  67. }
  68. /**
  69. * Ease of the movement
  70. */
  71. public function get ease():Number {
  72. return _ease;
  73. }
  74. public function set ease(ease:Number):void {
  75. _ease = ease;
  76. }
  77. /**
  78. * Value when the mouse is directly on top of the target
  79. */
  80. public function get max():Number {
  81. return _max;
  82. }
  83. public function set max(value:Number):void {
  84. _max = value;
  85. _range = (_max - _min);
  86. }
  87. /**
  88. * Value when the mouse is fully outside of the radius
  89. */
  90. public function get min():Number {
  91. return _min;
  92. }
  93. public function set min(value:Number):void {
  94. _min = value;
  95. _range = (_max - _min);
  96. }
  97. /**
  98. * Radius of the interaction area
  99. */
  100. public function get radius():Number {
  101. return _radius;
  102. }
  103. public function set radius(radius:Number):void {
  104. _radius = radius;
  105. _radiusSq = radius * radius;
  106. }
  107. }
  108. }