Skip to content

Commit

Permalink
redid loop priority
Browse files Browse the repository at this point in the history
  • Loading branch information
Pegacraft committed Feb 24, 2021
1 parent 50714b4 commit 1b9595c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 37 deletions.
5 changes: 5 additions & 0 deletions src/main/java/engine/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public abstract class Entity {
private boolean isAnchored = false;
private int offsetX = 0, offsetY = 0;
public List<Entity> objectList = new ArrayList<>();
/**
* if the entity is enabled
* if this variable is false, it won't be ticked nor rendered
*/
public boolean enabled = true;

/**
* This Method will be called once on creation of the object
Expand Down
19 changes: 15 additions & 4 deletions src/main/java/engine/listeners/Mouse.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.util.function.Consumer;
import java.util.function.Predicate;

import static engine.rendering.Graphics.getCamPos;


/**
* This handles all mouse presses.
Expand Down Expand Up @@ -94,8 +96,8 @@ public void mouseLoop(Display display) {
- display.getCanvas().getLocationOnScreen().getX());
y = (int) Math.round(MouseInfo.getPointerInfo().getLocation().getY()
- display.getCanvas().getLocationOnScreen().getY());
x = (int) scaledX(x) - Graphics.getCamPos().x;
y = (int) scaledY(y) - Graphics.getCamPos().y;
x = (int) scaledX(x) - getCamPos().x;
y = (int) scaledY(y) - getCamPos().y;
} catch (IllegalComponentStateException ignore) {
}

Expand Down Expand Up @@ -154,14 +156,23 @@ public boolean isHeld(int mouseButton) {
}

/**
* Use this method to get the mouse position relative to the window.
* Use this method to get the mouse position relative to the world.
*
* @return Returns a dimension with the mouse position.
* @return Returns a Point with the mouse position.
*/
public Point getMousePos() {
return new Point(x, y);
}

/**
* Use this method to get the mouse position relative to the window.
*
* @return Returns a Point with the mouse position.
*/
public Point getMousePosToCam() {
return new Point(x + getCamPos().x, y + getCamPos().y);
}

private double scaledX(int x) {
return (x / ((double) (display.getWidth()) / Graphics.stdWidth));
}
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/engine/loops/Loop.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import engine.Scene;
import engine.rendering.Graphics;

import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.List;

/**
* This is a engine internal function. It shall not be used.
Expand Down Expand Up @@ -47,12 +49,15 @@ private void logicLoop() {
if (s != null) {
try {
s.logicLoop();
s.getObjectList().forEach(Entity::logicLoop);
s.getObjectList().forEach(entity -> {
entity.getObjectList().forEach(Entity::logicLoop);
});
List<Entity> queue = new ArrayList<>(s.getObjectList());
while (!queue.isEmpty()) {
Entity ent = queue.remove(0);
if (ent.enabled) {
ent.logicLoop();
queue.addAll(ent.getObjectList());
}
}
} catch (ConcurrentModificationException ignore) {
System.out.println(ignore);
}
}
e.mouseListener.mouseLoop(e);
Expand Down
70 changes: 42 additions & 28 deletions src/main/java/engine/rendering/Graphics.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

import java.awt.*;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.List;

/**
* This is the <code>Graphics</code> class. It is used for everything that has to do with graphics.
Expand All @@ -24,6 +26,8 @@ public class Graphics {
public static double stdHeight = 720d;
private static int dHeight = (int) stdHeight;
private static int dWidth = (int) stdHeight;
private static double scaleWidth = 1;
private static double scaleHeight = 1;


private Graphics() {
Expand All @@ -49,42 +53,25 @@ public static void graphicsLoop() {
g.setColor(e.getCanvas().getBackground());
g.fillRect(0, 0, e.getWidth(), e.getHeight());
g.scale(e.getWidth() / stdWidth, e.getHeight() / stdHeight);

scaleWidth = e.getWidth() / stdWidth;
scaleHeight = e.getHeight() / stdHeight;
Scene s = Game.getScene(e.getAttachedScene());
if (s != null)
for (Entity entity : s.getObjectList()) {
for (Entity entity1 : entity.getObjectList()) {
entity1.anchorToCam();
if (s != null) {
List<Entity> queue = new ArrayList<>(s.getObjectList());
while (!queue.isEmpty()) {
Entity ent = queue.remove(0);
if (ent.enabled) {
ent.anchorToCam();
queue.addAll(ent.getObjectList());
}
entity.anchorToCam();
}
}

Graphics.g.translate(xOffset, yOffset);
if (s != null) {
try {
s.renderLoop();
for (Entity entity : s.getObjectList()) {
if (entity.rotatePos == null) {
g.rotate(entity.rotation, entity.x, entity.y);
entity.renderLoop();
g.rotate(-entity.rotation, entity.x, entity.y);
} else {
g.rotate(entity.rotation, entity.rotatePos.x, entity.rotatePos.y);
entity.renderLoop();
g.rotate(-entity.rotation, entity.rotatePos.x, entity.rotatePos.y);
}
for (Entity entity1 : entity.getObjectList()) {
if (entity1.rotatePos == null) {
g.rotate(entity1.rotation, entity1.x, entity1.y);
entity1.renderLoop();
g.rotate(-entity1.rotation, entity1.x, entity1.y);
} else {
g.rotate(entity1.rotation, entity1.rotatePos.x, entity1.rotatePos.y);
entity1.renderLoop();
g.rotate(-entity1.rotation, entity1.rotatePos.x, entity1.rotatePos.y);
}
}
}
renderChildren(s.getObjectList());
} catch (ConcurrentModificationException ignore) {
}
}
Expand All @@ -94,6 +81,25 @@ public static void graphicsLoop() {
} catch (IllegalStateException ignore) {
}
});


}

private static void renderChildren(List<Entity> entities) {
for (Entity entity : entities) {
if (entity.enabled) {
if (entity.rotatePos == null) {
g.rotate(entity.rotation, entity.x, entity.y);
entity.renderLoop();
g.rotate(-entity.rotation, entity.x, entity.y);
} else {
g.rotate(entity.rotation, entity.rotatePos.x, entity.rotatePos.y);
entity.renderLoop();
g.rotate(-entity.rotation, entity.rotatePos.x, entity.rotatePos.y);
}
renderChildren(entity.getObjectList());
}
}
}

/**
Expand Down Expand Up @@ -121,4 +127,12 @@ public static Point screenCenter() {
public static Point getCamPos() {
return new Point(xOffset, yOffset);
}

public static double getScaleHeight() {
return scaleHeight;
}

public static double getScaleWidth() {
return scaleWidth;
}
}

0 comments on commit 1b9595c

Please sign in to comment.