powercontrol.pde 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. class PowerBar {
  2. int index;
  3. int xPos;
  4. int yPos;
  5. int height = 20;
  6. int width = 40;
  7. PowerBar(int index, int xPos, int yPos) {
  8. this.index = index;
  9. this.xPos = xPos;
  10. this.yPos = yPos;
  11. }
  12. void render(int powerLevel) {
  13. stroke(255,255,255,255);
  14. fill(0, 255, 0, 0);
  15. if(powerLevel > this.index) {
  16. fill(0, 255, 0, 255);
  17. }
  18. rect(this.xPos, this.yPos - (this.index * (this.height + 4)), this.width, this.height);
  19. }
  20. }
  21. class PowerButton {
  22. int xPos;
  23. int yPos;
  24. int width = 40;
  25. int height = 40;
  26. int buttonPressed = 0;
  27. boolean powerLevelReported = false;
  28. PowerButton(int xPos, int yPos) {
  29. this.xPos = xPos;
  30. this.yPos = yPos;
  31. }
  32. void listen() {
  33. if(mousePressed && this.buttonPressed == 0) {
  34. if(mouseX > this.xPos && mouseX < this.xPos + this.width) {
  35. if(mouseY > this.yPos && mouseY < this.yPos + this.height) {
  36. this.buttonPressed = mouseButton;
  37. }
  38. }
  39. }
  40. }
  41. void releaseListen() {
  42. this.buttonPressed = 0;
  43. this.powerLevelReported = false;
  44. }
  45. void render() {
  46. stroke(255,255,255,0);
  47. fill(255, 255, 255, 255);
  48. if(this.buttonPressed == LEFT || this.buttonPressed == RIGHT) {
  49. fill(96, 96, 255, 255);
  50. }
  51. ellipse(this.xPos + (this.width / 2), this.yPos + (this.height / 2), this.width, this.height);
  52. }
  53. int getPowerLevelChange() {
  54. if(!this.powerLevelReported) {
  55. if (this.buttonPressed == LEFT) {
  56. this.powerLevelReported = true;
  57. return 1;
  58. } else if (this.buttonPressed == RIGHT) {
  59. this.powerLevelReported = true;
  60. return -1;
  61. }
  62. }
  63. return 0;
  64. }
  65. }
  66. class PowerMeter {
  67. int xPos;
  68. int yPos;
  69. int powerBarHeight;
  70. PowerButton powerButton;
  71. PowerBar[] powerBars;
  72. int powerLevel = 0;
  73. PowerMeter(int xPos, int yPos, int meterSize) {
  74. this.powerBarHeight = meterSize;
  75. this.powerBars = new PowerBar[this.powerBarHeight];
  76. this.xPos = xPos;
  77. this.yPos = yPos;
  78. for(int i = 0; i < this.powerBarHeight; i++) {
  79. this.powerBars[i] = new PowerBar(i, this.xPos, this.yPos);
  80. }
  81. this.powerButton = new PowerButton(this.xPos, this.yPos + 22);
  82. }
  83. void render() {
  84. for(int i = 0; i < this.powerBars.length; i++) {
  85. this.powerBars[i].render(this.powerLevel);
  86. }
  87. this.powerButton.render();
  88. }
  89. void listen() {
  90. this.powerButton.listen();
  91. }
  92. void releaseListen() {
  93. this.powerButton.releaseListen();
  94. }
  95. int update(int maxPowerRemaining) {
  96. int difference = this.powerButton.getPowerLevelChange();
  97. if(difference != 0) {
  98. if(maxPowerRemaining - difference >= 0) {
  99. if(this.powerLevel + difference <= this.powerBarHeight && this.powerLevel + difference >= 0) {
  100. this.powerLevel += difference;
  101. maxPowerRemaining -= difference;
  102. }
  103. }
  104. }
  105. return maxPowerRemaining;
  106. }
  107. }
  108. class PowerReserve {
  109. int xPos;
  110. int yPos;
  111. int maxPowerReserve;
  112. int powerRemaining;
  113. PowerBar[] powerBars;
  114. PowerReserve(int xPos, int yPos, int powerReserve) {
  115. powerBars = new PowerBar[powerReserve];
  116. this.xPos = xPos;
  117. this.yPos = yPos;
  118. this.maxPowerReserve = powerReserve;
  119. this.powerRemaining = this.maxPowerReserve;
  120. for(int i = 0; i < this.maxPowerReserve; i++) {
  121. this.powerBars[i] = new PowerBar(i, this.xPos, this.yPos);
  122. }
  123. }
  124. void render() {
  125. for(int i = 0; i < this.powerBars.length; i++) {
  126. this.powerBars[i].render(this.powerRemaining);
  127. }
  128. }
  129. }
  130. class Star {
  131. int xPos;
  132. int yPos;
  133. int boundaryX;
  134. int boundaryY;
  135. int depth;
  136. int[] starColor = new int[3];
  137. Star(int boundaryX, int boundaryY, int maxDepth) {
  138. this.depth = (int) random(1, maxDepth);
  139. this.boundaryX = boundaryX;
  140. this.boundaryY = boundaryY;
  141. this.xPos = (int)random(0, boundaryX);
  142. this.yPos = (int)random(0, boundaryY);
  143. this.starColor[0] = (int)random(0, 255);
  144. this.starColor[1] = (int)random(0, 255);
  145. this.starColor[2] = (int)random(0, 255);
  146. }
  147. void render() {
  148. stroke(this.starColor[0], this.starColor[1], this.starColor[2]);
  149. point(this.xPos, this.yPos);
  150. }
  151. void update() {
  152. this.xPos += this.depth;
  153. this.yPos += this.depth;
  154. if(this.xPos > this.boundaryX) {
  155. this.xPos = 0;
  156. }
  157. if(this.yPos > this.boundaryY) {
  158. this.yPos = 0;
  159. }
  160. }
  161. }
  162. PowerMeter[] meters;
  163. PowerReserve reserve;
  164. Star[] stars = new Star[100];
  165. void setup() {
  166. background(0, 0, 0);
  167. size(400, 600);
  168. for(int i = 0; i < stars.length; i++) {
  169. stars[i] = new Star(400, 600, 3);
  170. }
  171. meters = new PowerMeter[(int)random(0, 4) + 3];
  172. for(int i = 0; i < meters.length; i++) {
  173. meters[i] = new PowerMeter((i * 50) + 70, 500, (int)random(1, 12));
  174. }
  175. reserve = new PowerReserve(20, 500, (int)random(4, 20));
  176. }
  177. void draw() {
  178. background(0, 0, 0);
  179. for(int i = 0; i < stars.length; i++) {
  180. stars[i].update();
  181. stars[i].render();
  182. }
  183. for(int i = 0; i < meters.length; i++) {
  184. meters[i].listen();
  185. meters[i].render();
  186. reserve.powerRemaining = meters[i].update(reserve.powerRemaining);
  187. }
  188. reserve.render();
  189. }
  190. void mouseReleased() {
  191. for(int i = 0; i < meters.length; i++) {
  192. meters[i].releaseListen();
  193. }
  194. }