Guest User

Untitled

a guest
Jan 22nd, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. ArrayList <PVector> bullets;//bullet positions
  2. ArrayList <PVector> bVels; // the bullet velocities
  3. PVector pos; //the player position
  4.  
  5. float launchSpeed = 30; //the launch speed of the bullets
  6.  
  7. void setup(){
  8. size (400,400);
  9. init();
  10. }
  11.  
  12.  
  13. void init (){
  14. bullets = new ArrayList<PVector>();//makes the bullets list
  15. bVels = new ArrayList<PVector>();//makes the velocities list
  16.  
  17. pos = new PVector(50, 300);
  18. }
  19.  
  20. void drawTurret(PVector location, PVector direction){
  21. fill(255,0,0);
  22. //prepare to do the transformations to draw the
  23. //turret at the appropriate place and angle
  24. pushMatrix();
  25. translate(location.x, location.y);//moves the origin to this location, too bad translate does not just take a PVector as a parameter
  26. rotate(direction.heading()); //rotates the coordinate system
  27. rect(0, 0, 100, 20);//notice I am drawing the rectangle at the origin
  28. popMatrix();//remove all the transformations that were done since pushMatrix();
  29. }
  30.  
  31.  
  32.  
  33.  
  34. void draw(){
  35. background(200);
  36. //make a new PVector to store teh mouse position
  37. PVector mouse = new PVector(mouseX, mouseY);
  38. PVector dir = PVector.sub(mouse, pos);
  39. drawTurret(pos, dir);
  40. if (mousePressed){
  41.  
  42. //scale the speed
  43. dir.normalize();//makes the length 1
  44. dir.mult(launchSpeed);//makes it have the launch speed length
  45. bullets.add(new PVector(pos.x, pos.y));
  46. //need a temp vectro to do some stuff on
  47. bVels.add(dir);
  48. }
  49.  
  50. //draw the bullets
  51. for (int i = 0; i < bullets.size(); i++){
  52. //make a temp reference to the bullet in the list we are manipulating
  53. PVector bullet = bullets.get(i);
  54. PVector bVelocity = bVels.get(i);
  55.  
  56. bVelocity.y += 1.2;//1.2 is my gravity
  57. bullet.add(bVelocity);//instead of using bVelocity, you could use bVel.get(i)
  58.  
  59. if (bullet.y > height){
  60. bullets.remove(bullet);
  61. bVels.remove(bVelocity);
  62. }
  63.  
  64. fill(0,0,200);
  65. //
  66. ellipse(bullet.x, bullet.y, 20, 20);
  67.  
  68. }
  69.  
  70. }
Add Comment
Please, Sign In to add comment