Search in sources :

Example 1 with Sprite

use of com.demod.fbsr.Sprite in project Factorio-FBSR by demodude4u.

the class UndergroundBeltRendering method createRenderers.

@Override
public void createRenderers(Consumer<Renderer> register, WorldMap map, DataTable dataTable, BlueprintEntity entity, EntityPrototype prototype) {
    boolean input = entity.json().getString("type").equals("input");
    Direction structDir = input ? entity.getDirection() : entity.getDirection().back();
    Sprite beltSprite = TransportBeltRendering.getBeltSprite(prototype, entity.getDirection(), BeltBend.NONE);
    Sprite sprite = RenderUtils.getSpriteFromAnimation(prototype.lua().get("structure").get(input ? "direction_in" : "direction_out").get("sheet"));
    sprite.source.x += sprite.source.width * (structDir.cardinal());
    register.accept(RenderUtils.spriteRenderer(Layer.ENTITY, beltSprite, entity, prototype));
    register.accept(RenderUtils.spriteRenderer(Layer.ENTITY2, sprite, entity, prototype));
}
Also used : Sprite(com.demod.fbsr.Sprite) Direction(com.demod.fbsr.Direction)

Example 2 with Sprite

use of com.demod.fbsr.Sprite in project Factorio-FBSR by demodude4u.

the class WallRendering method createRenderers.

@Override
public void createRenderers(Consumer<Renderer> register, WorldMap map, DataTable dataTable, BlueprintEntity entity, EntityPrototype prototype) {
    Point2D.Double pos = entity.getPosition();
    boolean northGate = map.isVerticalGate(Direction.NORTH.offset(pos));
    boolean eastGate = map.isHorizontalGate(Direction.EAST.offset(pos));
    boolean southGate = map.isVerticalGate(Direction.SOUTH.offset(pos));
    boolean westGate = map.isHorizontalGate(Direction.WEST.offset(pos));
    int adjCode = 0;
    adjCode |= ((map.isWall(Direction.NORTH.offset(pos)) || northGate ? 1 : 0) << 0);
    adjCode |= ((map.isWall(Direction.EAST.offset(pos)) || eastGate ? 1 : 0) << 1);
    adjCode |= ((map.isWall(Direction.SOUTH.offset(pos)) || southGate ? 1 : 0) << 2);
    adjCode |= ((map.isWall(Direction.WEST.offset(pos)) || westGate ? 1 : 0) << 3);
    String spriteName = wallSpriteNameMapping[adjCode];
    LuaValue spriteLua = prototype.lua().get("pictures").get(spriteName);
    List<LuaValue> layersChoices = new ArrayList<>();
    if (spriteLua.get("layers") != LuaValue.NIL) {
        layersChoices.add(spriteLua.get("layers"));
    } else {
        Utils.forEach(spriteLua, l -> layersChoices.add(l.get("layers")));
    }
    LuaValue layersLua = layersChoices.get(Math.abs((int) pos.x + (int) pos.y) % layersChoices.size());
    Sprite sprite = RenderUtils.getSpriteFromAnimation(layersLua.get(1));
    Sprite spriteShadow = RenderUtils.getSpriteFromAnimation(layersLua.get(2));
    register.accept(RenderUtils.spriteRenderer(spriteShadow, entity, prototype));
    register.accept(RenderUtils.spriteRenderer(sprite, entity, prototype));
    if (northGate || eastGate || southGate || westGate) {
        Sprite wallDiodeSprite = RenderUtils.getSpriteFromAnimation(prototype.lua().get("wall_diode_red"));
        register.accept(RenderUtils.spriteRenderer(wallDiodeSprite, entity, prototype));
    }
}
Also used : Sprite(com.demod.fbsr.Sprite) Point2D(java.awt.geom.Point2D) ArrayList(java.util.ArrayList) LuaValue(org.luaj.vm2.LuaValue)

Example 3 with Sprite

use of com.demod.fbsr.Sprite in project Factorio-FBSR by demodude4u.

the class InfinityContainerRendering method createRenderers.

@Override
public void createRenderers(Consumer<Renderer> register, WorldMap map, DataTable dataTable, BlueprintEntity entity, EntityPrototype prototype) {
    super.createRenderers(register, map, dataTable, entity, prototype);
    if (entity.json().has("infinity_settings") && entity.json().getJSONObject("infinity_settings").has("filters")) {
        List<String> items = new ArrayList<>();
        Utils.<JSONObject>forEach(entity.json().getJSONObject("infinity_settings").getJSONArray("filters"), j -> {
            if (j.getInt("count") > 0)
                items.add(j.getString("name"));
        });
        if (!items.isEmpty()) {
            String itemName = items.get(0);
            Optional<ItemPrototype> optItem = dataTable.getItem(itemName);
            if (optItem.isPresent()) {
                Sprite spriteIcon = new Sprite();
                spriteIcon.image = FactorioData.getIcon(optItem.get());
                spriteIcon.source = new Rectangle(0, 0, spriteIcon.image.getWidth(), spriteIcon.image.getHeight());
                spriteIcon.bounds = new Rectangle2D.Double(-0.3, -0.3, 0.6, 0.6);
                Renderer delegate = RenderUtils.spriteRenderer(spriteIcon, entity, prototype);
                register.accept(new Renderer(Layer.OVERLAY4, delegate.getBounds()) {

                    @Override
                    public void render(Graphics2D g) throws Exception {
                        g.setColor(new Color(0, 0, 0, 128));
                        g.fill(spriteIcon.bounds);
                        delegate.render(g);
                    }
                });
            }
        }
    }
}
Also used : Sprite(com.demod.fbsr.Sprite) Color(java.awt.Color) ArrayList(java.util.ArrayList) Rectangle(java.awt.Rectangle) Rectangle2D(java.awt.geom.Rectangle2D) Graphics2D(java.awt.Graphics2D) ItemPrototype(com.demod.factorio.prototype.ItemPrototype) JSONObject(org.json.JSONObject) Renderer(com.demod.fbsr.Renderer)

Example 4 with Sprite

use of com.demod.fbsr.Sprite in project Factorio-FBSR by demodude4u.

the class InserterRendering method createRenderers.

@Override
public void createRenderers(Consumer<Renderer> register, WorldMap map, DataTable dataTable, BlueprintEntity entity, EntityPrototype prototype) {
    Point2D.Double pos = entity.getPosition();
    Direction dir = entity.getDirection();
    Sprite sprite = RenderUtils.getSpriteFromAnimation(prototype.lua().get("platform_picture").get("sheet"));
    sprite.source.x += sprite.source.width * (dir.back().cardinal());
    Sprite spriteArmHand = RenderUtils.getSpriteFromAnimation(prototype.lua().get("hand_open_picture"));
    double armStretch = -prototype.lua().get("pickup_position").get(2).todouble();
    boolean modded = entity.json().has("pickup_position") || entity.json().has("drop_position");
    Point2D.Double pickupPos;
    Point2D.Double insertPos;
    Point2D.Double inPos;
    Point2D.Double outPos;
    if (entity.json().has("pickup_position")) {
        pickupPos = Utils.parsePoint2D(entity.json().getJSONObject("pickup_position"));
        inPos = new Point2D.Double(pos.x + pickupPos.x, pos.y + pickupPos.y);
    } else if (modded) {
        inPos = dir.offset(pos, armStretch);
        pickupPos = new Point2D.Double(inPos.x - pos.x, inPos.y - pos.y);
    } else {
        pickupPos = Utils.parsePoint2D(prototype.lua().get("pickup_position"));
        inPos = dir.offset(pos, armStretch);
    }
    if (entity.json().has("drop_position")) {
        insertPos = Utils.parsePoint2D(entity.json().getJSONObject("drop_position"));
        outPos = new Point2D.Double(pos.x + insertPos.x, pos.y + insertPos.y);
    } else if (modded) {
        outPos = dir.offset(pos, -armStretch);
        insertPos = new Point2D.Double(outPos.x - pos.x, outPos.y - pos.y);
    } else {
        insertPos = Utils.parsePoint2D(prototype.lua().get("insert_position"));
        outPos = dir.offset(pos, -armStretch);
    }
    register.accept(RenderUtils.spriteRenderer(sprite, entity, prototype));
    register.accept(new Renderer(Layer.ENTITY2, sprite.bounds) {

        @Override
        public void render(Graphics2D g) {
            AffineTransform pat = g.getTransform();
            Rectangle2D.Double bounds = spriteArmHand.bounds;
            Rectangle source = spriteArmHand.source;
            BufferedImage image = spriteArmHand.image;
            g.translate(pos.x, pos.y);
            g.rotate(dir.back().ordinal() * Math.PI / 4.0);
            g.translate(bounds.x, 0);
            g.scale(bounds.width, armStretch);
            g.drawImage(image, 0, 1, 1, 0, source.x, source.y, source.x + source.width, source.y + source.height, null);
            g.setTransform(pat);
        }
    });
    register.accept(new Renderer(Layer.OVERLAY3, inPos) {

        @Override
        public void render(Graphics2D g) {
            AffineTransform pat = g.getTransform();
            Sprite grabberMarker = getGrabMarker(dataTable);
            Rectangle2D.Double bounds = grabberMarker.bounds;
            Rectangle source = grabberMarker.source;
            BufferedImage image = grabberMarker.image;
            double pickupRotate = Math.atan2(pickupPos.y, pickupPos.x);
            if (modded) {
                g.setTransform(pat);
                g.setColor(RenderUtils.withAlpha(Color.yellow, 64));
                g.setStroke(new BasicStroke(0.1f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
                g.draw(new Line2D.Double(pos, inPos));
            }
            g.setTransform(pat);
            g.translate(inPos.x, inPos.y);
            // HACK magic numbers
            Point2D.Double magicImageShift = new Point2D.Double(bounds.x + 0.1, bounds.y + -0.05);
            g.translate(magicImageShift.x, magicImageShift.y);
            if (modded) {
                g.translate(-Math.cos(pickupRotate) * 0.2, -Math.sin(pickupRotate) * 0.2);
                g.rotate(pickupRotate + Math.PI / 2.0, -magicImageShift.x, -magicImageShift.y);
            } else {
                g.rotate(dir.back().ordinal() * Math.PI / 4.0, -magicImageShift.x, -magicImageShift.y);
            }
            // magic numbers from Factorio code
            g.scale(0.8, 0.8);
            g.drawImage(image, 0, 0, 1, 1, source.x, source.y, source.x + source.width, source.y + source.height, null);
            g.setTransform(pat);
        }
    });
    register.accept(new Renderer(Layer.OVERLAY3, outPos) {

        @Override
        public void render(Graphics2D g) {
            AffineTransform pat = g.getTransform();
            Sprite placeMarker = getPlaceMarker(dataTable);
            Rectangle2D.Double bounds = placeMarker.bounds;
            Rectangle source = placeMarker.source;
            BufferedImage image = placeMarker.image;
            double insertRotate = Math.atan2(insertPos.y, insertPos.x);
            if (modded) {
                g.setTransform(pat);
                g.setColor(RenderUtils.withAlpha(Color.yellow, 64));
                g.setStroke(new BasicStroke(0.1f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
                g.draw(new Line2D.Double(pos, outPos));
            }
            g.setTransform(pat);
            g.translate(outPos.x, outPos.y);
            // HACK magic numbers
            Point2D.Double magicImageShift = new Point2D.Double(bounds.x + 0.1, bounds.y + 0.35);
            g.translate(magicImageShift.x, magicImageShift.y);
            if (modded) {
                g.translate(Math.cos(insertRotate) * 0.2, Math.sin(insertRotate) * 0.2);
                g.rotate(insertRotate + Math.PI / 2.0, -magicImageShift.x, -magicImageShift.y);
            } else {
                g.rotate(dir.back().ordinal() * Math.PI / 4.0, -magicImageShift.x, -magicImageShift.y);
            }
            // magic numbers from Factorio code
            g.scale(0.8, 0.8);
            g.drawImage(image, 0, 0, 1, 1, source.x, source.y, source.x + source.width, source.y + source.height, null);
            g.setTransform(pat);
        }
    });
    if (entity.json().has("filters")) {
        List<String> items = new ArrayList<>();
        Utils.forEach(entity.json().getJSONArray("filters"), (JSONObject j) -> {
            items.add(j.getString("name"));
        });
        if (!items.isEmpty()) {
            String itemName = items.get(0);
            Sprite spriteIcon = new Sprite();
            Optional<ItemPrototype> optItem = dataTable.getItem(itemName);
            if (optItem.isPresent()) {
                spriteIcon.image = FactorioData.getIcon(optItem.get());
                spriteIcon.source = new Rectangle(0, 0, spriteIcon.image.getWidth(), spriteIcon.image.getHeight());
                spriteIcon.bounds = new Rectangle2D.Double(-0.3, -0.3, 0.6, 0.6);
                Renderer delegate = RenderUtils.spriteRenderer(spriteIcon, entity, prototype);
                register.accept(new Renderer(Layer.OVERLAY2, delegate.getBounds()) {

                    @Override
                    public void render(Graphics2D g) throws Exception {
                        g.setColor(new Color(0, 0, 0, 128));
                        g.fill(spriteIcon.bounds);
                        delegate.render(g);
                    }
                });
            }
        }
    }
}
Also used : BasicStroke(java.awt.BasicStroke) Sprite(com.demod.fbsr.Sprite) Color(java.awt.Color) Rectangle(java.awt.Rectangle) ArrayList(java.util.ArrayList) Rectangle2D(java.awt.geom.Rectangle2D) Direction(com.demod.fbsr.Direction) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) ItemPrototype(com.demod.factorio.prototype.ItemPrototype) JSONObject(org.json.JSONObject) Point2D(java.awt.geom.Point2D) Renderer(com.demod.fbsr.Renderer) AffineTransform(java.awt.geom.AffineTransform)

Example 5 with Sprite

use of com.demod.fbsr.Sprite in project Factorio-FBSR by demodude4u.

the class LinkedBeltRendering method createRenderers.

@Override
public void createRenderers(Consumer<Renderer> register, WorldMap map, DataTable dataTable, BlueprintEntity entity, EntityPrototype prototype) {
    boolean input = entity.json().getString("type").equals("input");
    Direction structDir = input ? entity.getDirection() : entity.getDirection().back();
    Sprite beltSprite = TransportBeltRendering.getBeltSprite(prototype, entity.getDirection(), BeltBend.NONE);
    Sprite sprite = RenderUtils.getSpriteFromAnimation(prototype.lua().get("structure").get(input ? "direction_in" : "direction_out").get("sheet"));
    sprite.source.x += sprite.source.width * (structDir.cardinal());
    register.accept(RenderUtils.spriteRenderer(Layer.ENTITY, beltSprite, entity, prototype));
    register.accept(RenderUtils.spriteRenderer(Layer.ENTITY2, sprite, entity, prototype));
}
Also used : Sprite(com.demod.fbsr.Sprite) Direction(com.demod.fbsr.Direction)

Aggregations

Sprite (com.demod.fbsr.Sprite)28 LuaValue (org.luaj.vm2.LuaValue)15 Color (java.awt.Color)8 ArrayList (java.util.ArrayList)8 Direction (com.demod.fbsr.Direction)6 Renderer (com.demod.fbsr.Renderer)6 Graphics2D (java.awt.Graphics2D)6 Rectangle (java.awt.Rectangle)6 Point2D (java.awt.geom.Point2D)6 Rectangle2D (java.awt.geom.Rectangle2D)6 ItemPrototype (com.demod.factorio.prototype.ItemPrototype)5 JSONObject (org.json.JSONObject)5 Layer (com.demod.fbsr.Renderer.Layer)2 AffineTransform (java.awt.geom.AffineTransform)2 Double (java.awt.geom.Point2D.Double)2 FactorioData (com.demod.factorio.FactorioData)1 RecipePrototype (com.demod.factorio.prototype.RecipePrototype)1 BeltBend (com.demod.fbsr.WorldMap.BeltBend)1 BasicStroke (java.awt.BasicStroke)1 BufferedImage (java.awt.image.BufferedImage)1