Search in sources :

Example 1 with RecipePrototype

use of com.demod.factorio.prototype.RecipePrototype in project Factorio-FBSR by demodude4u.

the class AssemblingMachineRendering method populateLogistics.

@Override
public void populateLogistics(WorldMap map, DataTable dataTable, BlueprintEntity entity, EntityPrototype prototype) {
    String recipeName = entity.json().optString("recipe", null);
    if (recipeName != null) {
        Optional<RecipePrototype> optRecipe = dataTable.getRecipe(recipeName);
        if (optRecipe.isPresent()) {
            RecipePrototype protoRecipe = optRecipe.get();
            setLogisticMachine(map, dataTable, entity, prototype, protoRecipe);
        }
    }
}
Also used : RecipePrototype(com.demod.factorio.prototype.RecipePrototype)

Example 2 with RecipePrototype

use of com.demod.factorio.prototype.RecipePrototype in project Factorio-FBSR by demodude4u.

the class AssemblingMachineRendering method createRenderers.

@Override
public void createRenderers(Consumer<Renderer> register, WorldMap map, DataTable dataTable, BlueprintEntity entity, EntityPrototype prototype) {
    LuaValue animationLua = prototype.lua().get("idle_animation");
    if (animationLua.isnil()) {
        animationLua = prototype.lua().get("animation");
    }
    List<Sprite> sprites = RenderUtils.getSpritesFromAnimation(animationLua, entity.getDirection());
    register.accept(RenderUtils.spriteRenderer(sprites, entity, prototype));
    Sprite spriteIcon = new Sprite();
    String recipe = entity.json().optString("recipe", null);
    if (recipe != null) {
        Optional<RecipePrototype> optRecipe = dataTable.getRecipe(recipe);
        if (optRecipe.isPresent()) {
            RecipePrototype protoRecipe = optRecipe.get();
            if (!protoRecipe.lua().get("icon").isnil() || !protoRecipe.lua().get("icons").isnil()) {
                spriteIcon.image = FactorioData.getIcon(protoRecipe);
            } else {
                String name;
                if (protoRecipe.lua().get("results") != LuaValue.NIL) {
                    name = protoRecipe.lua().get("results").get(1).get("name").toString();
                } else {
                    name = protoRecipe.lua().get("result").toString();
                }
                Optional<? extends DataPrototype> protoProduct = dataTable.getItem(name);
                if (!protoProduct.isPresent()) {
                    protoProduct = dataTable.getFluid(name);
                }
                spriteIcon.image = protoProduct.map(FactorioData::getIcon).orElse(RenderUtils.EMPTY_IMAGE);
            }
            spriteIcon.source = new Rectangle(0, 0, spriteIcon.image.getWidth(), spriteIcon.image.getHeight());
            spriteIcon.bounds = new Rectangle2D.Double(-0.7, -1.0, 1.4, 1.4);
            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, 180));
                    g.fill(spriteIcon.bounds);
                    delegate.render(g);
                }
            });
        }
    }
}
Also used : Sprite(com.demod.fbsr.Sprite) Color(java.awt.Color) Rectangle(java.awt.Rectangle) Rectangle2D(java.awt.geom.Rectangle2D) FactorioData(com.demod.factorio.FactorioData) RecipePrototype(com.demod.factorio.prototype.RecipePrototype) Graphics2D(java.awt.Graphics2D) Renderer(com.demod.fbsr.Renderer) LuaValue(org.luaj.vm2.LuaValue)

Example 3 with RecipePrototype

use of com.demod.factorio.prototype.RecipePrototype in project Factorio-FBSR by demodude4u.

the class AssemblingMachineRendering method populateWorldMap.

@Override
public void populateWorldMap(WorldMap map, DataTable dataTable, BlueprintEntity entity, EntityPrototype prototype) {
    String recipeName = entity.json().optString("recipe", null);
    boolean hasFluid = false;
    if (recipeName != null) {
        Optional<RecipePrototype> optRecipe = dataTable.getRecipe(recipeName);
        if (optRecipe.isPresent()) {
            RecipePrototype protoRecipe = optRecipe.get();
            List<LuaValue> items = new ArrayList<>();
            Utils.forEach(protoRecipe.lua().get("ingredients"), (Consumer<LuaValue>) items::add);
            LuaValue resultsLua = protoRecipe.lua().get("results");
            if (resultsLua != LuaValue.NIL) {
                items.add(resultsLua);
            }
            hasFluid = items.stream().anyMatch(lua -> {
                LuaValue typeLua = lua.get("type");
                return typeLua != LuaValue.NIL && typeLua.toString().equals("fluid");
            });
        }
    }
    LuaValue fluidBoxesLua = prototype.lua().get("fluid_boxes");
    boolean offWhenNoFluidRecipe = fluidBoxesLua.isnil() ? true : fluidBoxesLua.get("off_when_no_fluid_recipe").optboolean(false);
    if (!fluidBoxesLua.isnil() && (!offWhenNoFluidRecipe || hasFluid)) {
        Utils.forEach(fluidBoxesLua, fluidBoxLua -> {
            if (!fluidBoxLua.istable()) {
                return;
            }
            Utils.forEach(fluidBoxLua.get("pipe_connections"), pipeConnectionLua -> {
                Point2D.Double offset = Utils.parsePoint2D(pipeConnectionLua.get("position"));
                if (Math.abs(offset.y) > Math.abs(offset.x)) {
                    offset.y += -Math.signum(offset.y);
                } else {
                    offset.x += -Math.signum(offset.x);
                }
                Point2D.Double pos = entity.getDirection().left().offset(entity.getDirection().back().offset(entity.getPosition(), offset.y), offset.x);
                Direction direction = offset.y > 0 ? entity.getDirection().back() : entity.getDirection();
                map.setPipe(pos, direction);
            });
        });
    }
}
Also used : Color(java.awt.Color) Rectangle(java.awt.Rectangle) Point2D(java.awt.geom.Point2D) DataPrototype(com.demod.factorio.prototype.DataPrototype) Rectangle2D(java.awt.geom.Rectangle2D) RecipePrototype(com.demod.factorio.prototype.RecipePrototype) BlueprintEntity(com.demod.fbsr.BlueprintEntity) DataTable(com.demod.factorio.DataTable) ArrayList(java.util.ArrayList) RenderUtils(com.demod.fbsr.RenderUtils) Consumer(java.util.function.Consumer) Renderer(com.demod.fbsr.Renderer) Sprite(com.demod.fbsr.Sprite) WorldMap(com.demod.fbsr.WorldMap) EntityRendererFactory(com.demod.fbsr.EntityRendererFactory) List(java.util.List) Utils(com.demod.factorio.Utils) FactorioData(com.demod.factorio.FactorioData) Direction(com.demod.fbsr.Direction) Graphics2D(java.awt.Graphics2D) LuaValue(org.luaj.vm2.LuaValue) Optional(java.util.Optional) EntityPrototype(com.demod.factorio.prototype.EntityPrototype) Layer(com.demod.fbsr.Renderer.Layer) Point2D(java.awt.geom.Point2D) ArrayList(java.util.ArrayList) LuaValue(org.luaj.vm2.LuaValue) Direction(com.demod.fbsr.Direction) RecipePrototype(com.demod.factorio.prototype.RecipePrototype)

Aggregations

RecipePrototype (com.demod.factorio.prototype.RecipePrototype)3 FactorioData (com.demod.factorio.FactorioData)2 Renderer (com.demod.fbsr.Renderer)2 Sprite (com.demod.fbsr.Sprite)2 Color (java.awt.Color)2 Graphics2D (java.awt.Graphics2D)2 Rectangle (java.awt.Rectangle)2 Rectangle2D (java.awt.geom.Rectangle2D)2 LuaValue (org.luaj.vm2.LuaValue)2 DataTable (com.demod.factorio.DataTable)1 Utils (com.demod.factorio.Utils)1 DataPrototype (com.demod.factorio.prototype.DataPrototype)1 EntityPrototype (com.demod.factorio.prototype.EntityPrototype)1 BlueprintEntity (com.demod.fbsr.BlueprintEntity)1 Direction (com.demod.fbsr.Direction)1 EntityRendererFactory (com.demod.fbsr.EntityRendererFactory)1 RenderUtils (com.demod.fbsr.RenderUtils)1 Layer (com.demod.fbsr.Renderer.Layer)1 WorldMap (com.demod.fbsr.WorldMap)1 Point2D (java.awt.geom.Point2D)1