Skip to content

Instantly share code, notes, and snippets.

@nrubin29
Last active June 24, 2016 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nrubin29/8173b0658ea9a9242436a02994346d34 to your computer and use it in GitHub Desktop.
Save nrubin29/8173b0658ea9a9242436a02994346d34 to your computer and use it in GitHub Desktop.
ProjectileLib
import java.awt.Rectangle;
class Entity {
private int x, y, w, h, xSpeed, ySpeed;
public Entity(int x, int y, int w, int h, int xSpeed, int ySpeed) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getWidth() {
return w;
}
public int getHeight() {
return h;
}
protected void switchX() {
this.xSpeed *= -1;
}
protected void switchY() {
this.ySpeed *= -1;
}
public void update() {
this.x += xSpeed;
this.y += ySpeed;
}
private Rectangle getRectangle() {
return new Rectangle(x, y, w, h);
}
public boolean intersects(Entity other) {
return getRectangle().intersects(other.getRectangle());
}
}
public class Projectile extends Entity {
public Projectile(int x, int y, int xSpeed, int ySpeed) {
super(x, y, 5, 10, xSpeed, ySpeed);
}
}
public class Target extends Entity {
private boolean bounceTop, bounceBottom, bounceLeft, bounceRight;
private PImage image;
public Target(int x, int y, int w, int h, int xSpeed, int ySpeed, boolean bounceTop, boolean bounceBottom, boolean bounceLeft, boolean bounceRight, PImage image) {
super(x, y, w, h, xSpeed, ySpeed);
this.bounceTop = bounceTop;
this.bounceBottom = bounceBottom;
this.bounceLeft = bounceLeft;
this.bounceRight = bounceRight;
this.image = image;
}
public PImage getImage() {
return image;
}
@Override
public void update() {
super.update();
if (getX() < 0 && bounceLeft) {
switchX();
}
if (getX() + getWidth() > width && bounceRight) {
switchX();
}
if (getY() < 0 && bounceTop) {
switchY();
}
if (getY() + getHeight() > height && bounceBottom) {
switchY();
}
}
}
private ArrayList<Projectile> projectiles = new ArrayList<Projectile>();
private ArrayList<Target> targets = new ArrayList<Target>();
public int numProjectiles, numTargets;
void fireProjectile(int x, int y, int xSpeed, int ySpeed) {
projectiles.add(new Projectile(x, y, xSpeed, ySpeed));
numProjectiles = projectiles.size();
}
void addTarget(int x, int y, int w, int h) {
addTarget(x, y, w, h, 0, 0);
}
void addTarget(int x, int y, int w, int h, int xSpeed, int ySpeed) {
addTarget(x, y, w, h, xSpeed, ySpeed, true, true, true, true);
}
void addTarget(int x, int y, int w, int h, int xSpeed, int ySpeed, boolean bounceTop, boolean bounceBottom, boolean bounceLeft, boolean bounceRight) {
addTarget(x, y, w, h, xSpeed, ySpeed, bounceTop, bounceBottom, bounceLeft, bounceRight, null);
}
void addTarget(int x, int y, int w, int h, int xSpeed, int ySpeed, boolean bounceTop, boolean bounceBottom, boolean bounceLeft, boolean bounceRight, PImage image) {
targets.add(new Target(x, y, w, h, xSpeed, ySpeed, bounceTop, bounceBottom, bounceLeft, bounceRight, image));
numTargets = targets.size();
}
void update() {
for (int i = projectiles.size() - 1; i >= 0; i--) {
Projectile p = projectiles.get(i);
rect(p.getX(), p.getY(), 5, 10);
if (p.getX() > width || p.getY() > height || p.getX() + 5 < 0 || p.getY() + 10 < 0) {
projectiles.remove(i);
numProjectiles = projectiles.size();
}
else {
boolean remove = false;
for (int j = targets.size() - 1; j >= 0; j--) {
Target t = targets.get(j);
if (p.intersects(t)) {
targetHit();
targets.remove(j);
numTargets = targets.size();
remove = true;
break;
}
}
if (remove) {
projectiles.remove(i);
numProjectiles = projectiles.size();
}
else {
p.update();
}
}
}
for (int i = targets.size() - 1; i >= 0; i--) {
Target t = targets.get(i);
if (t.getImage() == null) {
rect(t.getX(), t.getY(), t.getWidth(), t.getHeight());
}
else {
image(t.getImage(), t.getX(), t.getY());
}
if (t.getX() > width || t.getY() > height || t.getX() + t.getWidth() < 0 || t.getY() + t.getHeight() < 0) {
targetMissed();
targets.remove(i);
numTargets = targets.size();
}
else {
t.update();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment