Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public abstract class Component {
- private Game game;
- private Entity parent;
- protected String tag;
- public Component(Game game, Entity parent) {
- this.game =game;
- this.parent = parent;
- }
- public abstract void tick();
- public abstract void render(Screen screen);
- public Game getGame() {
- return game;
- }
- public void setGame(Game game) {
- this.game = game;
- }
- public Entity getParent() {
- return parent;
- }
- public void setParent(Entity parent) {
- this.parent = parent;
- }
- public String getTag() {
- return tag;
- }
- public void setTag(String tag) {
- this.tag = tag;
- }
- }
- //
- public class AABBComponent extends Component {
- private int centerX, centerY;
- private int halfWidth, halfHeight;
- public AABBComponent(Game game, Entity parent) {
- super(game, parent);
- this.tag = "aabb";
- }
- @Override
- public void tick() {
- double time = 1.0/ 60.0;
- @SuppressWarnings("unused")
- float dt = (float) time;
- centerX = (int) (getParent().getPosX() + (getParent().getWidth() / 2));
- centerY = (int) (getParent().getPosY() + (getParent().getHeight() / 2) + getParent().getPaddingTop() / 2);
- halfWidth = (getParent().getWidth()/2) - getParent().getPadding();
- halfHeight = (getParent().getHeight()/2) - (getParent().getPaddingTop() / 2);
- Physics.addAABBComponent(this);
- }
- @Override
- public void render(Screen screen) {
- }
- public int getCenterX() {
- return centerX;
- }
- public void setCenterX(int centerX) {
- this.centerX = centerX;
- }
- public int getCenterY() {
- return centerY;
- }
- public void setCenterY(int centerY) {
- this.centerY = centerY;
- }
- public int getHalfWidth() {
- return halfWidth;
- }
- public void setHalfWidth(int halfWidth) {
- this.halfWidth = halfWidth;
- }
- public int getHalfHeight() {
- return halfHeight;
- }
- public void setHalfHeight(int halfHeight) {
- this.halfHeight = halfHeight;
- }
- }
- //
- public class Physics {
- private static List<AABBComponent> aabbList = new ArrayList<AABBComponent>();
- public static void addAABBComponent(AABBComponent aabb) {
- aabbList.add(aabb);
- }
- public static void tick() {
- for (int i = 0; i < aabbList.size(); i++) {
- for (int j = i + 1; j < aabbList.size(); j++) {
- AABBComponent c0 = aabbList.get(i);
- AABBComponent c1 = aabbList.get(j);
- if(Math.abs(c0.getCenterX() - c1.getCenterX()) < c0.getHalfWidth() + c1.getHalfWidth()) {
- if(Math.abs(c0.getCenterY() - c1.getCenterY()) < c0.getHalfHeight() + c1.getHalfHeight()) {
- c0.getParent().collision(c1.getParent());
- c1.getParent().collision(c0.getParent());
- }
- }
- }
- }
- aabbList.clear();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement