From c59e19c7291f273867d59e93e59bdf798542164a Mon Sep 17 00:00:00 2001 From: CrazyPants Date: Tue, 8 Jul 2014 13:30:59 +0800 Subject: [PATCH] Started work on sound detector upgrade. --- .../java/crazypants/enderio/ClientProxy.java | 12 ++ .../enderio/item/darksteel/SoundDetector.java | 107 ++++++++++++++++++ .../enderio/item/darksteel/SoundEntity.java | 48 ++++++++ .../enderio/item/darksteel/SoundRenderer.java | 64 +++++++++++ 4 files changed, 231 insertions(+) create mode 100644 src/main/java/crazypants/enderio/item/darksteel/SoundDetector.java create mode 100644 src/main/java/crazypants/enderio/item/darksteel/SoundEntity.java create mode 100644 src/main/java/crazypants/enderio/item/darksteel/SoundRenderer.java diff --git a/src/main/java/crazypants/enderio/ClientProxy.java b/src/main/java/crazypants/enderio/ClientProxy.java index 13cafac1..6cf1f889 100644 --- a/src/main/java/crazypants/enderio/ClientProxy.java +++ b/src/main/java/crazypants/enderio/ClientProxy.java @@ -14,6 +14,7 @@ import cpw.mods.fml.client.FMLClientHandler; import cpw.mods.fml.client.registry.ClientRegistry; import cpw.mods.fml.client.registry.RenderingRegistry; import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.common.registry.EntityRegistry; import crazypants.enderio.conduit.BlockConduitBundle; import crazypants.enderio.conduit.IConduit; import crazypants.enderio.conduit.TileConduitBundle; @@ -42,8 +43,12 @@ import crazypants.enderio.enderface.TileEnderIO; import crazypants.enderio.gui.TooltipAddera; import crazypants.enderio.item.YetaWrenchOverlayRenderer; import crazypants.enderio.item.YetaWrenchTickHandler; +import crazypants.enderio.item.darksteel.DarkSteelController; import crazypants.enderio.item.darksteel.KeyTracker; import crazypants.enderio.item.darksteel.PoweredItemRenderer; +import crazypants.enderio.item.darksteel.SoundEntity; +import crazypants.enderio.item.darksteel.SoundRenderer; +import crazypants.enderio.item.darksteel.SoundDetector; import crazypants.enderio.machine.AbstractMachineBlock; import crazypants.enderio.machine.AbstractMachineRenderer; import crazypants.enderio.machine.farm.BlockFarmStation; @@ -269,7 +274,14 @@ public class ClientProxy extends CommonProxy { MinecraftForgeClient.registerItemRenderer(EnderIO.itemDarkSteelAxe, dsr); //Ensure it is loaded and registered KeyTracker.instance.isGlideActive(); + + + EntityRegistry.registerGlobalEntityID(SoundEntity.class, "soundEntity", EntityRegistry.findGlobalUniqueEntityId()); + RenderingRegistry.registerEntityRenderingHandler(SoundEntity.class, new SoundRenderer()); + MinecraftForge.EVENT_BUS.register(SoundDetector.instance); + FMLCommonHandler.instance().bus().register(SoundDetector.instance); + } @Override diff --git a/src/main/java/crazypants/enderio/item/darksteel/SoundDetector.java b/src/main/java/crazypants/enderio/item/darksteel/SoundDetector.java new file mode 100644 index 00000000..8e935030 --- /dev/null +++ b/src/main/java/crazypants/enderio/item/darksteel/SoundDetector.java @@ -0,0 +1,107 @@ +package crazypants.enderio.item.darksteel; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.ListIterator; +import java.util.Map; + +import org.lwjgl.opengl.GL11; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.entity.EntityClientPlayerMP; +import net.minecraft.client.gui.ScaledResolution; +import net.minecraft.client.renderer.EntityRenderer; +import net.minecraft.client.renderer.Tessellator; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.util.AxisAlignedBB; +import net.minecraft.util.IIcon; +import net.minecraftforge.client.event.sound.PlaySoundEvent17; +import net.minecraftforge.client.event.sound.PlaySoundSourceEvent; +import net.minecraftforge.client.event.sound.SoundEvent; +import net.minecraftforge.client.event.sound.SoundEvent.SoundSourceEvent; +import net.minecraftforge.event.entity.PlaySoundAtEntityEvent; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.common.gameevent.TickEvent; +import cpw.mods.fml.common.gameevent.TickEvent.Phase; +import cpw.mods.fml.common.gameevent.TickEvent.RenderTickEvent; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import crazypants.render.BoundingBox; +import crazypants.render.CubeRenderer; +import crazypants.render.IconUtil; +import crazypants.render.RenderUtil; +import crazypants.util.BlockCoord; +import crazypants.util.Util; +import crazypants.vecmath.Camera; +import crazypants.vecmath.Matrix4d; +import crazypants.vecmath.Vector3d; +import crazypants.vecmath.ViewFrustum; + +public class SoundDetector { + + public static SoundDetector instance = new SoundDetector(); + + private List soundQueue = new ArrayList(); + + private List sounds = new LinkedList(); + + private Minecraft mc = Minecraft.getMinecraft(); + + boolean enabled = false; + + @SubscribeEvent + public void onSound(PlaySoundAtEntityEvent evt) { + if(enabled && evt.entity != null && evt.entity != Minecraft.getMinecraft().thePlayer) { + synchronized (soundQueue) { + soundQueue.add(new SoundSource(evt.entity)); + } + } + } + + @SideOnly(Side.CLIENT) + @SubscribeEvent + public void onClientTick(TickEvent.ClientTickEvent event) { + + if(!enabled) { + return; + } + + synchronized (soundQueue) { + sounds.addAll(soundQueue); + soundQueue.clear(); + } + + if(mc.thePlayer == null || mc.thePlayer.worldObj == null) { + return; + } + + for(SoundSource ss : sounds) { + mc.thePlayer.worldObj.spawnEntityInWorld(new SoundEntity(mc.thePlayer.worldObj, ss.pos)); + } + sounds.clear(); + + } + + private static class SoundSource { + + int expiresTicks; + Vector3d pos; + + public SoundSource(Entity ent) { + AxisAlignedBB bb = ent.boundingBox; + if(bb != null) { + pos = new Vector3d(bb.minX + (bb.maxX - bb.minX)/2, bb.minY + (bb.maxY - bb.minY)/2,bb.minZ + (bb.maxZ - bb.minZ)/2); + } else { + pos = new Vector3d(ent.posX, ent.posY,ent.posZ); + } + expiresTicks = 40; + } + + } + + +} diff --git a/src/main/java/crazypants/enderio/item/darksteel/SoundEntity.java b/src/main/java/crazypants/enderio/item/darksteel/SoundEntity.java new file mode 100644 index 00000000..2436fb0f --- /dev/null +++ b/src/main/java/crazypants/enderio/item/darksteel/SoundEntity.java @@ -0,0 +1,48 @@ +package crazypants.enderio.item.darksteel; + +import crazypants.vecmath.Vector3d; +import net.minecraft.entity.Entity; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.AxisAlignedBB; +import net.minecraft.world.World; + +public class SoundEntity extends Entity { + + int lifeSpan = 20; + + public SoundEntity(World world, Vector3d pos) { + super(world); + setPosition(pos.x, pos.y, pos.z); + } + + @Override + protected void entityInit() { + } + + protected boolean canTriggerWalking() { + return false; + } + + @Override + public AxisAlignedBB getBoundingBox() { + return AxisAlignedBB.getBoundingBox(posX - 0.5, posY - 0.5, posZ - 0.5, posX + 0.5, posY + 0.5, posZ + 0.5); + } + + @Override + public void onUpdate() { + super.onUpdate(); + lifeSpan--; + if(lifeSpan == 0) { + setDead(); + } + } + + @Override + protected void readEntityFromNBT(NBTTagCompound p_70037_1_) { + } + + @Override + protected void writeEntityToNBT(NBTTagCompound p_70014_1_) { + } + +} diff --git a/src/main/java/crazypants/enderio/item/darksteel/SoundRenderer.java b/src/main/java/crazypants/enderio/item/darksteel/SoundRenderer.java new file mode 100644 index 00000000..5cdd1956 --- /dev/null +++ b/src/main/java/crazypants/enderio/item/darksteel/SoundRenderer.java @@ -0,0 +1,64 @@ +package crazypants.enderio.item.darksteel; + +import org.lwjgl.opengl.GL11; + +import crazypants.render.BoundingBox; +import crazypants.render.CubeRenderer; +import crazypants.render.IconUtil; +import crazypants.render.RenderUtil; +import crazypants.vecmath.Matrix4d; +import crazypants.vecmath.Vector3d; +import net.minecraft.client.Minecraft; +import net.minecraft.client.entity.EntityClientPlayerMP; +import net.minecraft.client.renderer.Tessellator; +import net.minecraft.client.renderer.entity.Render; +import net.minecraft.client.renderer.entity.RenderEntity; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.util.IIcon; +import net.minecraft.util.ResourceLocation; + +public class SoundRenderer extends RenderEntity { + + public void doRender(Entity entity, double x, double y, double z, float p_76986_8_, float p_76986_9_) { + + GL11.glPushAttrib(GL11.GL_ENABLE_BIT); + GL11.glDisable(GL11.GL_DEPTH_TEST); + GL11.glEnable(GL11.GL_BLEND); + GL11.glColor4f(1, 1, 1, 0.5f); + + float scale = ((SoundEntity) entity).lifeSpan / 20f; + + EntityClientPlayerMP player = Minecraft.getMinecraft().thePlayer; + GL11.glPushMatrix(); + GL11.glTranslatef((float) x, (float) y, (float) z); + GL11.glRotatef(-player.rotationYaw, 0.0F, 1.0F, 0.0F); + GL11.glRotatef(player.rotationPitch, 1.0F, 0.0F, 0.0F); + + GL11.glPushMatrix(); + GL11.glRotatef(-90, 1, 0, 0); + + RenderUtil.bindBlockTexture(); + IIcon tex = IconUtil.whiteTexture; + double size = 0.25; + + Tessellator tessellator = Tessellator.instance; + tessellator.startDrawingQuads(); + tessellator.addVertexWithUV(-size, 0, size, tex.getMinU(), tex.getMinV()); + tessellator.addVertexWithUV(-size, 0, -size, tex.getMinU(), tex.getMaxV()); + tessellator.addVertexWithUV(size, 0, -size, tex.getMaxU(), tex.getMaxV()); + tessellator.addVertexWithUV(size, 0, size, tex.getMaxU(), tex.getMinV()); + + tessellator.addVertexWithUV(-size, 0, size, tex.getMinU(), tex.getMinV()); + tessellator.addVertexWithUV(size, 0, size, tex.getMaxU(), tex.getMinV()); + tessellator.addVertexWithUV(size, 0, -size, tex.getMaxU(), tex.getMaxV()); + tessellator.addVertexWithUV(-size, 0, -size, tex.getMinU(), tex.getMaxV()); + tessellator.draw(); + + GL11.glPopMatrix(); + + GL11.glPopAttrib(); + GL11.glPopMatrix(); + } + +}