@Override public void logic() { for (Shape shape : layer.children) { Sprite sprite = shape.toSprite(); sprite.moveForward(); sprite.bounceInside(rectangle); } }
public void toSprites(Sprite sprite, Layer layer, Shape pivotShape, Relativity relativity) { LinkedList newSprites = new LinkedList(); if (pivotShape == null) pivotShape = sprite; for (Sprite templateSprite : sprites) { Sprite newSprite = new Sprite(); templateSprite.copySpriteTo(newSprite); setShape(sprite, templateSprite, newSprite); newSprites.addLast(newSprite); } layer.insert(newSprites, pivotShape, relativity); }
@Override public Sprite getTileSprite(Sprite sprite, double dX, double dY, double xScale, double yScale) { serviceRectangle.setCoords(sprite.getX() * xScale + dX, sprite.getY() * yScale + dY); serviceRectangle.setSize(sprite.getWidth() * xScale, sprite.getHeight() * yScale); serviceRectangle.shapeType = sprite.shapeType; return serviceRectangle; }
@Override public void render() { cursor.setAsViewport(); layer.draw(); rectangle.drawContour(2d); resetViewport(); cursor.drawContour(2d); printText("SetAsViewport, ResetViewport example", Align.TO_CENTER, Align.TO_BOTTOM); }
@Override public void init() { cursor = new Sprite(0, 0, 8, 6); for (int n = 1; n < spritesQuantity; n++) { Sprite sprite = new Sprite( ShapeType.oval, Service.random(-13d, 13d), Service.random(-8d, 8d), 0d, 0d, Service.random(Math.PI * 2d), Service.random(3d, 7d)); sprite.setDiameter(Service.random(0.5d, 1.5d)); sprite.visualizer.setRandomColor(); layer.addLast(sprite); } }
@Override public boolean check(Sprite oval, Sprite rectangle) { oval = oval.toCircle(rectangle, serviceOval1); if ((rectangle.getX() - rectangle.getWidth() * 0.5 <= oval.getX() && oval.getX() <= rectangle.getX() + rectangle.getWidth() * 0.5) || (rectangle.getY() - rectangle.getHeight() * 0.5 <= oval.getY() && oval.getY() <= rectangle.getY() + rectangle.getHeight() * 0.5)) { if (2.0 * Math.abs(oval.getX() - rectangle.getX()) < oval.getWidth() + rectangle.getWidth() - inaccuracy && 2.0 * Math.abs(oval.getY() - rectangle.getY()) < oval.getWidth() + rectangle.getHeight() - inaccuracy) return true; } else { double dX = Math.abs(rectangle.getX() - oval.getX()) - 0.5 * rectangle.getWidth(); double dY = Math.abs(rectangle.getY() - oval.getY()) - 0.5 * rectangle.getHeight(); double radius = 0.5 * oval.getWidth() - inaccuracy; if (dX * dX + dY * dY < radius * radius) return true; } return false; }
public static SpriteTemplate fromSprites( LinkedList<Sprite> sprites, Layer layer, Shape pivotShape, Relativity relativity) { SpriteTemplate template = null; double leftX = 0d, topY = 0d, rightX = 0d, bottomY = 0d; Sprite newSprite = new Sprite(); for (Sprite sprite : sprites) { if (sprite.shapeType.getNum() == ShapeType.spriteTemplate.getNum()) continue; if (template != null) { leftX = Math.min(leftX, sprite.leftX()); topY = Math.min(topY, sprite.topY()); rightX = Math.max(rightX, sprite.rightX()); bottomY = Math.max(bottomY, sprite.bottomY()); } else { template = new SpriteTemplate(); leftX = sprite.leftX(); topY = sprite.topY(); rightX = sprite.rightX(); bottomY = sprite.bottomY(); if (pivotShape == null) pivotShape = sprite; } } if (template != null) { newSprite.setCoords(0.5d * (leftX + rightX), 0.5d * (topY + bottomY)); newSprite.setHeight(bottomY - topY); newSprite.shapeType = template; if (layer != null) layer.insert(newSprite, pivotShape, relativity); for (Sprite sprite : sprites) { if (sprite.shapeType.getNum() == ShapeType.spriteTemplate.getNum()) continue; layer.remove(sprite); sprite.setCoords( (newSprite.getX() - sprite.getX()) / newSprite.getWidth(), (newSprite.getY() - sprite.getY()) / newSprite.getHeight()); sprite.setSize( sprite.getWidth() / newSprite.getWidth(), sprite.getHeight() / newSprite.getHeight()); template.sprites.addLast(sprite); } } return template; }
public void setShape(Sprite sprite, Sprite templateSprite, Sprite spriteShape) { double angle = sprite.displayingAngle; if (templateSprite.visualizer.rotating) angle += sprite.angle; if (angle == 0) { spriteShape.setCoords( templateSprite.getX() * sprite.getWidth() + sprite.getX(), templateSprite.getY() * sprite.getHeight() + sprite.getY()); } else { double relativeX = templateSprite.getX() * sprite.getWidth(); double relativeY = templateSprite.getY() * sprite.getHeight(); spriteShape.setCoords( relativeX * Math.cos(angle) - relativeY * Math.sin(angle) + sprite.getX(), relativeX * Math.sin(angle) + relativeY * Math.cos(angle) + sprite.getY()); } spriteShape.setSize( sprite.getWidth() * templateSprite.getWidth(), sprite.getHeight() * templateSprite.getHeight()); spriteShape.angle = sprite.angle + templateSprite.angle; spriteShape.displayingAngle = sprite.displayingAngle + templateSprite.displayingAngle; }