Guest User

Untitled

a guest
Dec 12th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. int maxSize = 50;//number of stars
  2.  
  3. int mult = 1000;
  4. float size = 0.5; //starSize
  5. PVector[] pos1 = new PVector [maxSize]; //star positions
  6. PVector midPoint; //For PeasyCam
  7.  
  8. //Tracers
  9. float startSize = 30; // this is the tracers starting size
  10. PVector[] posh = new PVector[maxSize];
  11. PVector[] velh = new PVector[maxSize];
  12. PVector[] acch = new PVector[maxSize];
  13. float [] sizes = new float[maxSize];
  14. float shrinkscale = 0.99;
  15. float velscale = 2;
  16.  
  17. //Ship Control
  18. PVector pos; //Position of ship
  19. float dir = 0; // Angle of ship
  20. float speed = 0; // Speed of ship
  21. boolean leftBool, rightBool, thrustBool;
  22. PShape rocket;
  23.  
  24. PVector vel;
  25. PVector acc;
  26. PVector sDir;
  27. float speedLimit = 10;
  28.  
  29. //Bullets
  30. ArrayList <PVector> velB = new ArrayList <PVector>();
  31. ArrayList <PVector> posB = new ArrayList <PVector>();
  32.  
  33. float bulletSpeed = 5;
  34. float bulletSize = 5;
  35.  
  36. void setup1() {
  37.  
  38. pos = new PVector(600, 600, 0);
  39. vel = new PVector();
  40. acc = new PVector();
  41. sDir = new PVector(0, 1, 0);
  42.  
  43. init1();
  44. // below is for star background
  45.  
  46. for (int i = 1; i < maxSize; i++) {
  47. pos1[i] = (PVector.random3D());
  48. pos1[i].mult(mult);
  49. // pos1[i].add(midPoint);
  50. }
  51.  
  52. cursor(CROSS);
  53. }
  54. void init1() {
  55. //initialize arrays
  56. for (int i = 0; i < maxSize; i++) {
  57. posh[i] = new PVector(-1000, -1000);
  58. velh[i] = PVector.random2D();
  59. velh[i].mult(velscale);
  60. acch[i] = new PVector(0, 0);
  61.  
  62. sizes[i] = startSize;
  63. }
  64. }
  65.  
  66. void offEdge() {
  67. if (pos.x > width) {
  68. pos.x = 0;
  69. } else if (pos.x < 0) {
  70. pos.x = width;
  71. } else if (pos.y > height) {
  72. pos.y = 0;
  73. } else if (pos.y < 0) {
  74. pos.y = height;
  75. }
  76. }
  77.  
  78. void mousePressed() {
  79. }
  80.  
  81. boolean detect(PVector pos1, PVector pos2, float rad) {
  82. boolean isFar = PVector.dist(pos1, pos2) > rad;
  83. return isFar;
  84. }
  85.  
  86. void bulletControl() {
  87. for (int i = 0; i < posB.size(); i++) {
  88. PVector p = posB.get(i);
  89. PVector v = velB.get(i);
  90. p.add(v);
  91. noStroke();
  92. pushMatrix();
  93. fill(255, 0, 0);
  94. translate(p.x, p.y, p.z);
  95. sphere(bulletSize);
  96. popMatrix();
  97. //detects if the bullet is outside the screen
  98. if (detect(p, new PVector(width/2, height/2), width)) {
  99. posB.remove(p);
  100. velB.remove(v);
  101. }
  102. }
  103. }
  104.  
  105. void keyPressed() {
  106. if (key == ' ') {
  107. //position where the bullet starts
  108. posB.add(new PVector(pos.x, pos.y, pos.z));
  109. //speed and direction of bullet
  110. velB.add(new PVector(sDir.x, sDir.y, sDir.z).mult(bulletSpeed));
  111.  
  112. //Spread is kinda broken
  113. //PVector tempV = new PVector(sDir.x, sDir.y, sDir.z).mult(bulletSpeed);
  114. //tempV.rotate(random(-PI/20.,PI/20.));
  115. //velB.add(tempV);
  116. }
  117.  
  118. if (keyCode == LEFT || key == 'a') {
  119. leftBool = true;
  120. }
  121. if (keyCode == RIGHT || key == 'd') {
  122. rightBool = true;
  123. }
  124. if (keyCode == UP || key == 'w') {
  125. thrustBool = true;
  126. }
  127. }
  128.  
  129. void keyReleased() { //Better control for ship (multiple keys at once without confusing keys for others)
  130. if (keyCode == LEFT || key == 'a') {
  131. leftBool = false;
  132. }
  133. if (keyCode == RIGHT || key == 'd') {
  134. rightBool = false;
  135. }
  136. if (keyCode == UP || key == 'w') {
  137. thrustBool = false;
  138. }
  139. }
  140.  
  141.  
  142. void starBackground() {
  143. for (int i=1; i < maxSize; i++) { //Background stars
  144. pushMatrix();
  145. fill(255. -(float)i/maxSize*255, 255, 255);
  146. translate(pos1[i].x, pos1[i].y, pos1[i].z);
  147. sphere(random(size, size*2));
  148. popMatrix();
  149. }
  150. }
  151.  
  152. void tracers() {
  153. for (int i = maxSize -1; i > 0; i--) { //Move each tracer back in array
  154.  
  155. posh[i] = posh[i-1];
  156.  
  157. sizes[i] = sizes[i-1]*shrinkscale; //Shrink tracer as moving through array
  158.  
  159. velh[i] = velh[i-1];
  160. }
  161.  
  162. if (thrustBool) {
  163. posh[0] = new PVector(pos.x, pos.y);
  164.  
  165. velh[0] = PVector.random2D();
  166.  
  167. velh[0].mult(velscale);
  168.  
  169. sizes[0] = startSize;
  170. } else { // If not pressing thrust, Then spawn Tracers out of screen
  171.  
  172. posh[0] = new PVector(-1000, -1000);
  173. }
  174.  
  175. for (int i = 0; i < maxSize; i++) {
  176. posh[i].add(velh[i]);
  177. fill(255, 100 - (float)i/maxSize*254., 0, 255. - (float)i/15*254.);
  178. //fill(50 + (float)i/maxSize*205, 128+127*sin((float)frameCount* PI/60), 127. - cos((float)frameCount*PI/30), 255. - (float)i/maxSize*254.);
  179. pushMatrix();
  180. translate(cos(dir)*speed, sin(dir)*speed);
  181. translate(posh[i].x, posh[i].y);
  182. scale(0.35);
  183.  
  184. sphere(15);
  185.  
  186. popMatrix();
  187. }
  188. }
  189.  
  190.  
  191. void shipDriving() {
  192.  
  193. vel.add(acc);
  194. pos.add(vel);
  195. acc.set(0, 0, 0);
  196.  
  197. fill(255);
  198. pushMatrix();
  199. translate(pos.x, pos.y);
  200. rotate(sDir.heading());
  201. stroke(1);
  202. box(30, 10, 10);
  203. popMatrix();
  204.  
  205. if (leftBool) { //add to rotation
  206. sDir.rotate(-0.05);
  207. //dir -= .05;
  208. }
  209. if (rightBool) {
  210. sDir.rotate(0.05);
  211. // dir += .05;
  212. }
  213.  
  214. if (thrustBool) { //Increase Speed
  215. //speed += .1;
  216. acc = sDir.copy().mult(0.05);
  217. } else { //Decrease Speed
  218. vel.mult(0.98);
  219. //speed *= 0.99;
  220. }
  221. vel.limit(speedLimit);
  222. // speed = constrain(speed, 0, 4); //speedlimit
  223. offEdge();
  224. }
  225.  
  226.  
  227. void draw1() {
  228. noStroke();
  229. tracers();
  230. shipDriving();
  231. bulletControl();
  232. //starBackground();
  233. }
Add Comment
Please, Sign In to add comment