private static List<RenderEntity> createRenderEntityList( List<IDrawable> drawList, Camera camera) { List<RenderEntity> entitiesList = new ArrayList<>(); for (IDrawable obj : drawList) { if (obj instanceof ILargeDrawable) { Collection<Vector3> drawPositionList = ((ILargeDrawable) obj).getDrawPositionList(); for (Vector3 position : drawPositionList) { entitiesList.add(new RenderEntity(position, obj, camera)); } } else { entitiesList.add(new RenderEntity(obj.getDrawPosition(), obj, camera)); } } return entitiesList; }
/** * Draw a collection of {@link IDrawable}, ordered by internal rules. * * @param objectCollection collection of {@link IDrawable} objects to be drawn. * @param g a {@link Graphics2D} object. * @param camera a {@link Camera} for drawing {@link IDrawable} onto the {@link Graphics2D}. */ public static void draw(Collection<IDrawable> objectCollection, Graphics2D g, Camera camera) { List<IDrawable> drawList = createListWithUniqueItem(objectCollection); List<RenderEntity> renderEntities = createRenderEntityList(drawList, camera); Collections.sort(renderEntities); // final Vector2 depthChecker = new Vector2(0, 1).rotate(camera.getRotationAngle()).multiply(1, // 0.5f); /* * drawList.sort(new Comparator<IDrawable>() { * * @Override public int compare(IDrawable o1, IDrawable o2) { // if (o1.getDrawZ() != o2.getDrawZ()) { // return * (o1.getDrawZ() < o2.getDrawZ()) ? -1 : 1; // } else { * * // float depthA = depthChecker.getX() * o1.getDrawX() + depthChecker.getY() * o1.getDrawY() + // * o1.getDrawZ(); float depthB = depthChecker.getX() * o2.getDrawX() + depthChecker.getY() * // o2.getDrawY() + * o2.getDrawZ(); * * float depthA = camera.getYPosition(-o1.getDrawX(), -o1.getDrawY(), o1.getDrawZ()); float depthB = * camera.getYPosition(-o2.getDrawX(), -o2.getDrawY(), o2.getDrawZ()); * * return (int) Math.signum(depthB - depthA); // } } }); */ Rectangle clipper = g.getClipBounds(); clipper = new Rectangle( (int) clipper.getX() - CLIPPER_PADDING, (int) clipper.getY() - CLIPPER_PADDING, (int) clipper.getWidth() + CLIPPER_PADDING * 2, (int) clipper.getHeight() + CLIPPER_PADDING * 2); for (RenderEntity renderEntity : renderEntities) { IDrawable drawableObj = renderEntity.getDrawableObject(); if (!isOutsideClippingArea(clipper, renderEntity.getDrawPosition())) { if (drawableObj instanceof ILargeDrawable) { ((ILargeDrawable) drawableObj).draw(g, camera, renderEntity.getPosition()); } else { drawableObj.draw(g, camera); } } } }