Guest User

Untitled

a guest
Dec 9th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. package scripts;
  2.  
  3. import com.stencyl.graphics.G;
  4.  
  5. import com.stencyl.behavior.Script;
  6. import com.stencyl.behavior.Script.*;
  7. import com.stencyl.behavior.ActorScript;
  8. import com.stencyl.behavior.SceneScript;
  9. import com.stencyl.behavior.TimedTask;
  10.  
  11. import com.stencyl.models.Actor;
  12. import com.stencyl.models.GameModel;
  13. import com.stencyl.models.actor.Animation;
  14. import com.stencyl.models.actor.ActorType;
  15. import com.stencyl.models.actor.Collision;
  16. import com.stencyl.models.actor.Group;
  17. import com.stencyl.models.Scene;
  18. import com.stencyl.models.Sound;
  19. import com.stencyl.models.Region;
  20. import com.stencyl.models.Font;
  21. import com.stencyl.models.Joystick;
  22.  
  23. import com.stencyl.Engine;
  24. import com.stencyl.Input;
  25. import com.stencyl.utils.Utils;
  26.  
  27. import nme.ui.Mouse;
  28. import nme.display.Graphics;
  29.  
  30. import motion.Actuate;
  31. import motion.easing.Back;
  32. import motion.easing.Cubic;
  33. import motion.easing.Elastic;
  34. import motion.easing.Expo;
  35. import motion.easing.Linear;
  36. import motion.easing.Quad;
  37. import motion.easing.Quart;
  38. import motion.easing.Quint;
  39. import motion.easing.Sine;
  40.  
  41. /*
  42. How to use:
  43. var hp : ResourceBar = new ResourceBar(theActor, maximumValue, scalesLeft : Bool, simple : Bool);
  44. Ex: if scalesLeft is true, then the bar will move to the left, and keep its position on the right
  45. Ex: if simple is true, then the bar will not animate and it will instantly change
  46. reset(?newMaxHealth)
  47. destruct() // Prevents memory leaks if you constantly create new ones (I hope)
  48.  
  49.  
  50. */
  51.  
  52. class ResourceBar
  53. {
  54. var actor : Actor;
  55. var currentPercent : Float = 100;
  56. public var currentValue : Float = 0;
  57. var initialActorWidth : Float = 100;
  58. var maxValue : Float = 0;
  59. private var destinationValue : Float = 0;
  60. private var onePercentOfMax : Float = 0;
  61. private var isIncreasing = false;
  62. private var isDecreasing = false;
  63. private var initialX : Float = 0;
  64. private var initialY : Float = 0;
  65. public var isScalingLeft = false;
  66. public var isSimple = false;
  67. private var isDestructing = false;
  68.  
  69. private var shakeDeltaX : Float = 0;
  70. private var shakeDeltaY : Float = 0;
  71.  
  72.  
  73. public function destruct(){
  74. isDestructing = true;
  75. }
  76.  
  77. public function new(a : Actor, max : Float, ?scalesLeft : Bool, ?simple : Bool){
  78. actor = a;
  79. maxValue = max;
  80. currentValue = max;
  81. destinationValue = max;
  82. onePercentOfMax = max / 100;
  83. initialX = a.getX();
  84. initialY = a.getY();
  85. initialActorWidth = a.getWidth();
  86. if(scalesLeft != null){
  87. isScalingLeft = scalesLeft;
  88. }
  89. if(simple != null){
  90. isSimple = simple;
  91. }
  92. runPeriodically(20, function(timeTask:TimedTask):Void{
  93. if(isDestructing){
  94. return;}
  95. updateHealthBar();
  96. }, null);
  97. }
  98.  
  99. public function add(value : Float){
  100. if(isSimple){
  101. scaleTo(currentValue + value);
  102. } else {
  103. if(value < 0){
  104. isDecreasing = true;
  105. isIncreasing = false;
  106. } else {
  107. isIncreasing = true;
  108. isDecreasing = false;
  109. }
  110. destinationValue = destinationValue + value;
  111. }
  112. }
  113.  
  114. public function set(value : Float){
  115. if(value > destinationValue){
  116. isIncreasing = true;
  117. isDecreasing = false;
  118. } else {
  119. isIncreasing = false;
  120. isDecreasing = true;
  121. }
  122. destinationValue = value;
  123. }
  124.  
  125. public function reset(?m : Float){
  126. if(m != null){
  127. maxValue = m;
  128. }
  129. actor.growTo(1, 1, 0, Linear.easeNone);
  130. actor.setX(initialX);
  131. isIncreasing = false;
  132. isDecreasing = false;
  133. currentPercent = 100;
  134. currentValue = maxValue;
  135. destinationValue = maxValue;
  136. onePercentOfMax = maxValue / 100;
  137. }
  138.  
  139. public function shake(?amount : Float){
  140. // TO DO
  141. }
  142.  
  143. private function scaleTo(value : Float){
  144. var currentWidth = currentPercent * initialActorWidth / 100;
  145. //var currentDeltaWidth = initialActorWidth - currentWidth;
  146. var newPercent = value * 100 / maxValue;
  147. var newWidth = newPercent * initialActorWidth / 100;
  148. var newDeltaWidth = currentWidth - newWidth;
  149. actor.growTo(newPercent/100, 1, 0, Linear.easeNone);
  150. if(isScalingLeft){
  151. actor.setX(actor.getX() + newDeltaWidth/2 + shakeDeltaX);
  152. } else {
  153. actor.setX(actor.getX() - newDeltaWidth/2 + shakeDeltaX);
  154. }
  155. currentPercent = newPercent;
  156. currentValue = value;
  157. }
  158.  
  159. private function updateHealthBar(){
  160. if(currentValue > destinationValue + onePercentOfMax){
  161. scaleTo(currentValue);
  162. currentValue -= onePercentOfMax;
  163. } else if(currentValue < destinationValue - onePercentOfMax){
  164. scaleTo(currentValue);
  165. currentValue += onePercentOfMax;
  166. }
  167. return;
  168. // Redundant code below, might need another time if the above doesn't behave properly
  169. /*
  170. if(isDecreasing){
  171. if(currentValue > destinationValue){
  172. scaleTo(currentValue);
  173. currentValue -= onePercentOfMax;
  174. if(currentValue < destinationValue){
  175. currentValue = destinationValue;
  176. }
  177. }
  178. } else if(isIncreasing){
  179. if(currentValue < destinationValue){
  180. scaleTo(currentValue);
  181. currentValue += onePercentOfMax;
  182. if(currentValue > destinationValue){
  183. currentValue = destinationValue;
  184. }
  185. }
  186. } */
  187.  
  188.  
  189. }
  190.  
  191.  
  192. }
Add Comment
Please, Sign In to add comment