From 4c610c8ca72f42444fb17d847e18ee02bcee9934 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Thu, 29 Dec 2022 20:14:44 +0100 Subject: [PATCH] continuous shooting and bullet deletion template --- .gitignore | 28 ---------------------------- src/Classes/Bullet.java | 6 ++++++ src/Classes/Gun.java | 10 ++++++++-- src/Main.java | 10 ++++------ 4 files changed, 18 insertions(+), 36 deletions(-) diff --git a/.gitignore b/.gitignore index 660bebc..8d8e07c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,30 +1,2 @@ -### IntelliJ IDEA ### out/ -!**/src/main/**/out/ -!**/src/test/**/out/ .idea/* - -### Eclipse ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache -bin/ -!**/src/main/**/bin/ -!**/src/test/**/bin/ - -### NetBeans ### -/nbproject/private/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ - -### VS Code ### -.vscode/ - -### Mac OS ### -.DS_Store \ No newline at end of file diff --git a/src/Classes/Bullet.java b/src/Classes/Bullet.java index 553ef72..8d8983c 100644 --- a/src/Classes/Bullet.java +++ b/src/Classes/Bullet.java @@ -3,6 +3,8 @@ package Classes; import processing.core.PApplet; import processing.core.PVector; +import java.util.ArrayList; + public class Bullet { private final PVector position, velocity, offset; private final float diameter, heading; @@ -17,6 +19,10 @@ public class Bullet { heading = direction; } + public void delete(ArrayList others) { + // If outside of screen, delete from given list + } + public void update() { position.add(velocity); } diff --git a/src/Classes/Gun.java b/src/Classes/Gun.java index ef0aa1e..7cd7c1d 100644 --- a/src/Classes/Gun.java +++ b/src/Classes/Gun.java @@ -21,7 +21,11 @@ public class Gun { public PVector position; public float rotation; - public Gun(PApplet sketch, String filePath, int gunWidth, int gunHeight, float gunX, float gunY, float laserOffset, float gunRotation, boolean mirrorGun, float bulletDiameter, float shootingSpeed) { + // Gun constructor + public Gun(PApplet sketch, String filePath, + int gunWidth, int gunHeight, float gunX, float gunY, + float laserOffset, float gunRotation, boolean mirrorGun, + float bulletDiameter, float shootingSpeed) { processing = sketch; bullets = new ArrayList<>(); diameter = bulletDiameter; @@ -38,8 +42,10 @@ public class Gun { // Update Gun and children public void update() { - for (Bullet b : bullets) + for (Bullet b : bullets) { b.update(); + b.delete(bullets); + } } public void show(float lineLength, int lineColor, int bulletColor) { diff --git a/src/Main.java b/src/Main.java index ac44808..9994a1d 100644 --- a/src/Main.java +++ b/src/Main.java @@ -38,7 +38,7 @@ public class Main extends PApplet { textSize(height/24f); testTarget = new Target(this, width - 200, height/2f, 100, 100); - testGun = new Rifle(this, 3, 150, height/2f); + testGun = new Pistol(this, 1, 150, height/2f); } @@ -51,6 +51,9 @@ public class Main extends PApplet { stroke(0); testTarget.show(); + if (mousePressed) + testGun.shoot(); + testGun.setHeading(new PVector(mouseX, mouseY)); testGun.show(MAX_LINE_LENGTH, color(255, 0, 0), color(0)); testGun.update(); @@ -58,9 +61,4 @@ public class Main extends PApplet { noStroke(); text("FPS: %d".formatted(round(frameRate)), width, 0); } - - @Override - public void mousePressed() { - testGun.shoot(); - } }