Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ArrayList <PVector> bullets;//bullet positions
- ArrayList <PVector> bVels; // the bullet velocities
- PVector pos; //the player position
- float launchSpeed = 30; //the launch speed of the bullets
- void setup(){
- size (400,400);
- init();
- }
- void init (){
- bullets = new ArrayList<PVector>();//makes the bullets list
- bVels = new ArrayList<PVector>();//makes the velocities list
- pos = new PVector(50, 300);
- }
- void drawTurret(PVector location, PVector direction){
- fill(255,0,0);
- //prepare to do the transformations to draw the
- //turret at the appropriate place and angle
- pushMatrix();
- translate(location.x, location.y);//moves the origin to this location, too bad translate does not just take a PVector as a parameter
- rotate(direction.heading()); //rotates the coordinate system
- rect(0, 0, 100, 20);//notice I am drawing the rectangle at the origin
- popMatrix();//remove all the transformations that were done since pushMatrix();
- }
- void draw(){
- background(200);
- //make a new PVector to store teh mouse position
- PVector mouse = new PVector(mouseX, mouseY);
- PVector dir = PVector.sub(mouse, pos);
- drawTurret(pos, dir);
- if (mousePressed){
- //scale the speed
- dir.normalize();//makes the length 1
- dir.mult(launchSpeed);//makes it have the launch speed length
- bullets.add(new PVector(pos.x, pos.y));
- //need a temp vectro to do some stuff on
- bVels.add(dir);
- }
- //draw the bullets
- for (int i = 0; i < bullets.size(); i++){
- //make a temp reference to the bullet in the list we are manipulating
- PVector bullet = bullets.get(i);
- PVector bVelocity = bVels.get(i);
- bVelocity.y += 1.2;//1.2 is my gravity
- bullet.add(bVelocity);//instead of using bVelocity, you could use bVel.get(i)
- if (bullet.y > height){
- bullets.remove(bullet);
- bVels.remove(bVelocity);
- }
- fill(0,0,200);
- //
- ellipse(bullet.x, bullet.y, 20, 20);
- }
- }
Add Comment
Please, Sign In to add comment