From 5ad3bbe3427ff6dc8b4fc276eed336676aa74c9d Mon Sep 17 00:00:00 2001 From: octarine-noise Date: Tue, 26 Aug 2014 21:22:33 +0200 Subject: [PATCH] add multiple leaf particle types --- .../betterfoliage/client/TextureMatcher.java | 67 +++++++++++++++++++ .../render/impl/EntityFXFallingLeaves.java | 4 +- .../client/resource/LeafParticleTextures.java | 52 ++++++++++---- 3 files changed, 109 insertions(+), 14 deletions(-) create mode 100644 src/main/java/mods/betterfoliage/client/TextureMatcher.java diff --git a/src/main/java/mods/betterfoliage/client/TextureMatcher.java b/src/main/java/mods/betterfoliage/client/TextureMatcher.java new file mode 100644 index 0000000..5460d72 --- /dev/null +++ b/src/main/java/mods/betterfoliage/client/TextureMatcher.java @@ -0,0 +1,67 @@ +package mods.betterfoliage.client; + +import java.util.List; +import java.util.Map; + +import mods.betterfoliage.common.util.ResourceUtils; +import net.minecraft.client.renderer.texture.TextureAtlasSprite; +import net.minecraft.util.IIcon; +import net.minecraft.util.ResourceLocation; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; + +public class TextureMatcher { + + public static class TextureMapping { + public String matchDomain; + public String matchName; + public String textureType; + + public TextureMapping(String matchDomain, String matchName, String textureType) { + this.matchDomain = matchDomain; + this.matchName = matchName; + this.textureType = textureType; + } + + public boolean matches(TextureAtlasSprite icon) { + ResourceLocation iconLocation = new ResourceLocation(icon.getIconName()); + if (matchDomain != null && !matchDomain.equals(iconLocation.getResourceDomain())) return false; + return iconLocation.getResourcePath().contains(matchName); + } + } + + public List mappings = Lists.newLinkedList(); + + public Map types = Maps.newHashMap(); + + public String put(TextureAtlasSprite icon) { + if (types.keySet().contains(icon)) return types.get(icon); + for (TextureMapping mapping : mappings) if (mapping.matches(icon)) { + types.put(icon, mapping.textureType); + return mapping.textureType; + } + return null; + } + + public String get(IIcon icon) { + return types.get(icon); + } + + public void loadMappings(ResourceLocation resource) { + mappings = Lists.newLinkedList(); + + for (String line : ResourceUtils.getLines(resource)) { + String[] lineSplit = line.split("="); + if (lineSplit.length != 2) continue; + + String[] match = lineSplit[0].split(":"); + if (match.length == 2) { + mappings.add(new TextureMapping(match[0], match[1], lineSplit[1])); + } else if (match.length == 1) { + mappings.add(new TextureMapping(null, match[0], lineSplit[1])); + } + } + } + +} diff --git a/src/main/java/mods/betterfoliage/client/render/impl/EntityFXFallingLeaves.java b/src/main/java/mods/betterfoliage/client/render/impl/EntityFXFallingLeaves.java index 5b378ba..fd9846b 100644 --- a/src/main/java/mods/betterfoliage/client/render/impl/EntityFXFallingLeaves.java +++ b/src/main/java/mods/betterfoliage/client/render/impl/EntityFXFallingLeaves.java @@ -40,12 +40,12 @@ public class EntityFXFallingLeaves extends EntityFX { isMirrored = (rand.nextInt() & 1) == 1; motionY = -Config.leafFXSpeed; particleRotation = rand.nextInt(64); - particleScale = (float) Config.leafFXSize; - particleIcon = BetterFoliageClient.leafParticles.icons.get(rand.nextInt(1024)); + Block block = world.getBlock(x, y, z); IIcon blockIcon = block.getIcon(world, x, y, z, ForgeDirection.DOWN.ordinal()); + particleIcon = BetterFoliageClient.leafParticles.getIconSet(blockIcon).get(rand.nextInt(1024)); calculateParticleColor(BetterFoliageClient.leafParticles.getColor(blockIcon), block.colorMultiplier(world, x, y, z)); } diff --git a/src/main/java/mods/betterfoliage/client/resource/LeafParticleTextures.java b/src/main/java/mods/betterfoliage/client/resource/LeafParticleTextures.java index 49a9e49..3ce4960 100644 --- a/src/main/java/mods/betterfoliage/client/resource/LeafParticleTextures.java +++ b/src/main/java/mods/betterfoliage/client/resource/LeafParticleTextures.java @@ -7,6 +7,7 @@ import java.util.Map; import javax.imageio.ImageIO; +import mods.betterfoliage.client.TextureMatcher; import mods.betterfoliage.client.render.IconSet; import mods.betterfoliage.client.resource.LeafTextureEnumerator.LeafTextureFoundEvent; import net.minecraft.client.Minecraft; @@ -21,17 +22,20 @@ import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; -/** Holds the texture for the falling leaf particles, and stores average texture color values for leaf textures +/** Holds the textures for the falling leaf particles, and stores average texture color values for leaf textures * @author octarine-noise */ @SideOnly(Side.CLIENT) public class LeafParticleTextures { - /** Icons for leaf particles */ - public IconSet icons = new IconSet("betterfoliage", "falling_leaf_default_%d"); - + /** Icons for leaf particles */ + public Map iconSets = Maps.newHashMap(); + + /** Leaf type mappings */ + public TextureMatcher leafTypes = new TextureMatcher(); + /** Map of average color values */ - public Map colors = Maps.newHashMap(); + public Map iconColors = Maps.newHashMap(); /** Default color value */ public int defaultColor = 0x208040; @@ -40,15 +44,32 @@ public class LeafParticleTextures { this.defaultColor = defaultColor; } + public IconSet getIconSet(IIcon icon) { + String leafType = leafTypes.get(icon); + if (leafType == null) leafType = "default"; + IconSet result = iconSets.get(leafType); + return result.hasIcons() ? result : iconSets.get("default"); + } + public int getColor(IIcon icon) { - Integer result = colors.get(icon); + Integer result = iconColors.get(icon); return result == null ? defaultColor : result; } - /** Calculate average color value (in HSB color space) for a texture and store it in the map. - * @param icon texture - */ protected void addAtlasTexture(TextureAtlasSprite icon) { + Integer textureColor = calculateTextureColor(icon); + if (textureColor != null) iconColors.put(icon, textureColor); + + String leafType = leafTypes.put(icon); + if (leafType != null && !iconSets.keySet().contains(leafType)) { + iconSets.put(leafType, new IconSet("betterfoliage", String.format("falling_leaf_%s_%%d", leafType))); + } + } + + /** Calculate average color value (in HSB color space) for a texture. + * @param icon texture + */ + protected Integer calculateTextureColor(TextureAtlasSprite icon) { ResourceLocation locationNoDirs = new ResourceLocation(icon.getIconName()); ResourceLocation locationWithDirs = new ResourceLocation(locationNoDirs.getResourceDomain(), String.format("textures/blocks/%s.png", locationNoDirs.getResourcePath())); try { @@ -74,20 +95,27 @@ public class LeafParticleTextures { // average hue as usual for circular values - transform average unit vector back to polar angle float avgHue = (float) (Math.atan2(sumHueY, sumHueX) / (2.0 * Math.PI) + 0.5); - colors.put(icon, Color.HSBtoRGB(avgHue, sumSaturation / numOpaque, sumBrightness / numOpaque)); + return Color.HSBtoRGB(avgHue, sumSaturation / numOpaque, sumBrightness / numOpaque); } catch (IOException e) { + return null; } } @SubscribeEvent public void handleTextureReload(TextureStitchEvent.Pre event) { if (event.map.getTextureType() != 0) return; - colors.clear(); - icons.registerIcons(event.map); + iconSets.clear(); + iconColors.clear(); + + leafTypes.loadMappings(new ResourceLocation("betterfoliage", "leafTextureMappings.cfg")); + IconSet defaultIcons = new IconSet("betterfoliage", "falling_leaf_default_%d"); + iconSets.put("default", defaultIcons); + defaultIcons.registerIcons(event.map); } @SubscribeEvent public void handleRegisterTexture(LeafTextureFoundEvent event) { addAtlasTexture(event.icon); } + }