Last active
June 24, 2016 17:16
-
-
Save nrubin29/cb64f2a86387e988951730d3308ae00e to your computer and use it in GitHub Desktop.
ObjectLib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Rectangle; | |
class Entity { | |
private int x, y, w, h; | |
public Entity(int x, int y, int w, int h) { | |
this.x = x; | |
this.y = y; | |
this.w = w; | |
this.h = h; | |
} | |
public int getX() { | |
return x; | |
} | |
public int getY() { | |
return y; | |
} | |
public int getWidth() { | |
return w; | |
} | |
public int getHeight() { | |
return h; | |
} | |
private Rectangle getRectangle() { | |
return new Rectangle(x, y, w, h); | |
} | |
public boolean intersects(Rectangle other) { | |
return getRectangle().intersects(other); | |
} | |
} | |
private ArrayList<Entity> objects = new ArrayList<Entity>(); | |
private int ol_w, ol_h; | |
private PImage ol_image; | |
void config(int w, int h, PImage image) { | |
ol_w = w; | |
ol_h = h; | |
ol_image = image; | |
} | |
void addObject(int w, int h) { | |
int x, y; | |
do { | |
x = (int) random(width); | |
y = (int) random(height); | |
} while (red(ol_image.get(x, y)) == 255 || green(ol_image.get(x, y)) == 255 || blue(ol_image.get(x, y)) == 255); | |
objects.add(new Entity(x, y, w, h)); | |
} | |
int update(int p1X, int p1Y, int p2X, int p2Y) { | |
for (int i = objects.size() - 1; i >= 0; i--) { | |
Entity o = objects.get(i); | |
if (o.intersects(new Rectangle(p1X - ol_w / 2, p1Y - ol_h / 2, ol_w, ol_h))) { | |
objects.remove(i); | |
return 1; | |
} | |
else if (o.intersects(new Rectangle(p2X - ol_w / 2, p2Y - ol_h / 2, ol_w, ol_h))) { | |
objects.remove(i); | |
return 2; | |
} | |
else { | |
rect(o.getX(), o.getY(), o.getWidth(), o.getHeight()); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment