turret renderer change

This commit is contained in:
nora 2021-01-01 18:09:26 +01:00
parent fef9302793
commit 014b87e4b8
2 changed files with 47 additions and 42 deletions

View file

@ -57,6 +57,7 @@ public class RenderEngine extends JPanel {
/** /**
* The paintComponent method is called from Swing. * The paintComponent method is called from Swing.
*
* @param g * @param g
*/ */
@Override @Override
@ -67,23 +68,26 @@ public class RenderEngine extends JPanel {
/** /**
* Add a {@code Renderer} to the scene. Gets added to the buffer * Add a {@code Renderer} to the scene. Gets added to the buffer
*
* @param d The {@code Renderer} * @param d The {@code Renderer}
*/ */
public void addRenderer(Renderer d){ public void addRenderer(Renderer d) {
layerManager.addToRendererBuffer(d); layerManager.addToRendererBuffer(d);
} }
/** /**
* Remove a {@code Renderer} from the engine because it won't be needed anymore. * Remove a {@code Renderer} from the engine because it won't be needed anymore.
* <p>If the object should only temporarily be invisible, change its {@code isVisible} field</p> * <p>If the object should only temporarily be invisible, change its {@code isVisible} field</p>
*
* @param d * @param d
*/ */
public void removeRenderer(Renderer d){ public void removeRenderer(Renderer d) {
layerManager.removeRenderer(d); layerManager.removeRenderer(d);
} }
/** /**
* Get the true width of the current screen * Get the true width of the current screen
*
* @return The width in Java2D coordinates * @return The width in Java2D coordinates
*/ */
public int getW() { public int getW() {
@ -92,6 +96,7 @@ public class RenderEngine extends JPanel {
/** /**
* Get the true height of the current screen * Get the true height of the current screen
*
* @return The height in Java2D coordinates * @return The height in Java2D coordinates
*/ */
public int getH() { public int getH() {
@ -113,26 +118,25 @@ public class RenderEngine extends JPanel {
this.currentCamera = currentCamera; this.currentCamera = currentCamera;
} }
public Graphics2D getG2d(){ public Graphics2D getG2d() {
return g2d; return g2d;
} }
//-------------------------------------- //--------------------------------------
public Vector2D shiftPoint(Vector2D point){ public Vector2D shiftPoint(Vector2D point) {
Vector2D newPosShifted = Vector2D.subtract(point, currentCamera.getPosition()); Vector2D newPosShifted = Vector2D.subtract(point, currentCamera.getPosition());
return Vector2D.divide(newPosShifted, currentCamera.getSize()); return Vector2D.divide(newPosShifted, currentCamera.getSize());
} }
public Vector2D scaleSize(Vector2D size){ public Vector2D scaleSize(Vector2D size) {
return Vector2D.divide(size, currentCamera.getSize()); return Vector2D.divide(size, currentCamera.getSize());
} }
//Drawing methods--------------------------------------------------------------------------------------------------------------- //Drawing methods---------------------------------------------------------------------------------------------------------------
public void fillRect(Vector2D position, Vector2D size, Color color, double rotation){ public void fillRect(Vector2D position, Vector2D size, Color color, double rotation) {
drawSetup(position, size, color, rotation); drawSetup(position, size, color, rotation);
g2d.fillRect( g2d.fillRect(
(int) sdc.pos.x, (int) sdc.pos.y, (int) sdc.pos.x, (int) sdc.pos.y,
@ -140,7 +144,7 @@ public class RenderEngine extends JPanel {
drawTearDown(); drawTearDown();
} }
public void fillRoundRect(Vector2D position, Vector2D size, Vector2D arcFactors, Color color, double rotation){ public void fillRoundRect(Vector2D position, Vector2D size, Vector2D arcFactors, Color color, double rotation) {
drawSetup(position, size, color, rotation); drawSetup(position, size, color, rotation);
g2d.fillRoundRect( g2d.fillRoundRect(
(int) sdc.pos.x, (int) sdc.pos.y, (int) sdc.pos.x, (int) sdc.pos.y,
@ -149,7 +153,7 @@ public class RenderEngine extends JPanel {
drawTearDown(); drawTearDown();
} }
public void fillOval(Vector2D position, Vector2D size, Color color, double rotation){ public void fillOval(Vector2D position, Vector2D size, Color color, double rotation) {
drawSetup(position, size, color, rotation); drawSetup(position, size, color, rotation);
g2d.fillOval( g2d.fillOval(
(int) sdc.pos.x, (int) sdc.pos.y, (int) sdc.pos.x, (int) sdc.pos.y,
@ -166,35 +170,48 @@ public class RenderEngine extends JPanel {
drawTearDown(); drawTearDown();
} }
public void drawLine(Vector2D position1, Vector2D position2, Vector2D size, Color color, int rotation){ public void drawLine(Vector2D position1, Vector2D position2, Vector2D size, Color color, double rotation) {
drawSetup(position1, size, color, rotation); drawLine(position1, position2, size, color, rotation, null);
}
public void drawLine(Vector2D position1, Vector2D position2, Vector2D size, Color color, double rotation, Vector2D rotationCenter) {
drawSetup(position1, size, color, rotation, rotationCenter);
g2d.setStroke(new BasicStroke((int) sdc.size.x, BasicStroke.CAP_BUTT, g2d.setStroke(new BasicStroke((int) sdc.size.x, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL)); BasicStroke.JOIN_BEVEL));
Vector2D absPos2 = Coordinates.getWorldCoordinates(shiftPoint(position2)); Vector2D absPos2 = Coordinates.getWorldCoordinates(shiftPoint(position2));
g2d.drawLine((int)sdc.pos.x, (int)sdc.pos.y, (int)absPos2.x, (int)absPos2.y); g2d.drawLine((int) sdc.pos.x, (int) sdc.pos.y, (int) absPos2.x, (int) absPos2.y);
g2d.setStroke(new BasicStroke());
drawTearDown(); drawTearDown();
} }
/** /**
* Called before every J2D draw method * Called before every J2D draw method
*/ */
private void drawSetup(Vector2D position, Vector2D size, Color color, double rotation, Object ... args){ private void drawSetup(Vector2D position, Vector2D size, Color color, double rotation, Object... args) {
drawSetup(position, size, color, rotation, null, args);
}
private void drawSetup(Vector2D position, Vector2D size, Color color, double rotation, Vector2D rotationCenter, Object... args) {
Vector2D abs = Coordinates.getWorldCoordinates(shiftPoint(position)); Vector2D abs = Coordinates.getWorldCoordinates(shiftPoint(position));
Vector2D sizeAbs = Coordinates.getWorldCoordinates(scaleSize(size)); Vector2D sizeAbs = Coordinates.getWorldCoordinates(scaleSize(size));
Vector2D centerAbs = new Vector2D((abs.x + sizeAbs.x / 2), (abs.y + sizeAbs.y / 2)); Vector2D centerAbs = new Vector2D((abs.x + sizeAbs.x / 2), (abs.y + sizeAbs.y / 2));
Vector2D rotCentAbs = null;
if(rotationCenter != null){
rotCentAbs = Coordinates.getWorldCoordinates(shiftPoint(rotationCenter));
}
sdc = new ShapeDrawContainer(abs, sizeAbs, centerAbs, color, rotation, args); sdc = new ShapeDrawContainer(abs, sizeAbs, centerAbs, color, rotation, rotCentAbs, args);
g2d.setPaint(sdc.color); g2d.setPaint(sdc.color);
g2d.rotate(sdc.rotation, sdc.center.x, sdc.center.y); g2d.rotate(sdc.rotation, sdc.rotationCenter.x, sdc.rotationCenter.y);
} }
/** /**
* Called after every J2D draw method * Called after every J2D draw method
*/ */
private void drawTearDown(){ private void drawTearDown() {
g2d.rotate(-sdc.rotation, sdc.center.x, sdc.center.y); g2d.rotate(-sdc.rotation, sdc.rotationCenter.x, sdc.rotationCenter.y);
sdc = null; //to avoid any drawing errors, might be changed at some point sdc = null; //to avoid any drawing errors, might be changed at some point
} }
@ -208,14 +225,16 @@ public class RenderEngine extends JPanel {
public Vector2D center; public Vector2D center;
public Color color; public Color color;
public double rotation; public double rotation;
public Vector2D rotationCenter;
public Object[] args; public Object[] args;
public ShapeDrawContainer(Vector2D pos, Vector2D size, Vector2D center, Color color, double rotation, Object ... args) { public ShapeDrawContainer(Vector2D pos, Vector2D size, Vector2D center, Color color, double rotation, Vector2D rotationCenter, Object... args) {
this.pos = pos; this.pos = pos;
this.size = size; this.size = size;
this.center = center; this.center = center;
this.color = color; this.color = color;
this.rotation = rotation; this.rotation = rotation;
this.rotationCenter = (rotationCenter == null) ? center : rotationCenter;
this.args = args; this.args = args;
} }
} }

View file

@ -34,39 +34,25 @@ public class Turret extends GameObject {
setRenderer(new CustomRenderer(mainColor, this) { setRenderer(new CustomRenderer(mainColor, this) {
@Override @Override
public void draw() { public void draw() {
Graphics2D g2d = re.getG2d();
g2d.setPaint(mainColor);
Vector2D abs = getWorldCoordsFromLocal(position);
int sizeAbs = (int) Coordinates.getWorldCoordinates(object.getSize()).x;
int xCenterAbs = (int) (abs.x + sizeAbs / 2);
int yCenterAbs = (int) (abs.y + sizeAbs / 2);
g2d.rotate(rotation, xCenterAbs, yCenterAbs); Vector2D mapPos = object.getMapPosition();
g2d.fillOval((int) abs.x, (int) abs.y, sizeAbs, sizeAbs); re.fillOval(mapPos, object.getSize(), mainColor, rotation);
Vector2D barrelSize = new Vector2D(object.getSize().x / barrelAmount / BARREL_THICKNESS, 0);
double barrelSpacing = size / (barrelAmount + 1);
double yCenter = mapPos.y + size / 2;
double xCenter = mapPos.x + size / 2;
//BARRELS--------------------------------------- //BARRELS---------------------------------------
g2d.setStroke(new BasicStroke((int) Coordinates.getWorldCoordinates(new Vector2D(object.getSize().x / barrelAmount / BARREL_THICKNESS, 0)).x, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL));
g2d.setPaint(Color.BLACK);
int barrelSpacing = sizeAbs / (barrelAmount + 1);
for (int i = 0; i < barrelAmount; i++) { for (int i = 0; i < barrelAmount; i++) {
int barrelX = (int) (abs.x + (i + 1) * barrelSpacing); double barrelX = mapPos.x + (i + 1) * barrelSpacing;
int frontPosY = (int) (abs.y - sizeAbs / 2); double frontPosY = mapPos.y - size / 2;
g2d.drawLine(barrelX, yCenterAbs, barrelX, frontPosY);
if (lastShot + SHOT_EFFECT_TIME > System.currentTimeMillis()) { re.drawLine(new Vector2D(barrelX, yCenter), new Vector2D(barrelX, frontPosY), barrelSize, Color.BLACK, rotation, new Vector2D(xCenter, yCenter));
g2d.setPaint(Color.YELLOW);
g2d.fillOval(barrelX - 5, frontPosY - 5, 10, 10);
g2d.setPaint(Color.BLACK);
}
} }
g2d.rotate(-rotation, xCenterAbs, yCenterAbs);
g2d.setStroke(new BasicStroke());
} }
@Override @Override