diff --git a/src/main/java/com/minelittlepony/unicopia/USounds.java b/src/main/java/com/minelittlepony/unicopia/USounds.java new file mode 100644 index 00000000..4d86db3d --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/USounds.java @@ -0,0 +1,15 @@ +package com.minelittlepony.unicopia; + +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.SoundEvent; +import net.minecraftforge.registries.IForgeRegistry; + +public class USounds { + + public static final SoundEvent WING_FLAP = new SoundEvent(new ResourceLocation(Unicopia.MODID, "wing_flap")) + .setRegistryName(Unicopia.MODID, "wing_flap"); + + static void init(IForgeRegistry registry) { + registry.registerAll(WING_FLAP); + } +} diff --git a/src/main/java/com/minelittlepony/unicopia/Unicopia.java b/src/main/java/com/minelittlepony/unicopia/Unicopia.java index d5399520..ae69f42a 100644 --- a/src/main/java/com/minelittlepony/unicopia/Unicopia.java +++ b/src/main/java/com/minelittlepony/unicopia/Unicopia.java @@ -7,8 +7,11 @@ import net.minecraft.item.EnumAction; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.IRecipe; +import net.minecraft.util.SoundEvent; import net.minecraftforge.client.event.ColorHandlerEvent; import net.minecraftforge.client.event.FOVUpdateEvent; +import net.minecraftforge.client.event.RenderGameOverlayEvent; +import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType; import net.minecraftforge.event.RegistryEvent; import net.minecraftforge.event.entity.item.ItemTossEvent; import net.minecraftforge.event.entity.player.PlayerDropsEvent; @@ -36,10 +39,12 @@ import com.minelittlepony.unicopia.client.particle.EntityMagicFX; import com.minelittlepony.unicopia.client.particle.EntityRaindropFX; import com.minelittlepony.unicopia.client.particle.Particles; import com.minelittlepony.unicopia.command.Commands; +import com.minelittlepony.unicopia.hud.UHud; import com.minelittlepony.unicopia.input.Keyboard; import com.minelittlepony.unicopia.network.MsgPlayerAbility; import com.minelittlepony.unicopia.network.MsgPlayerCapabilities; import com.minelittlepony.unicopia.network.MsgRequestCapabilities; +import com.minelittlepony.unicopia.player.IPlayer; import com.minelittlepony.unicopia.player.PlayerSpeciesList; import com.minelittlepony.unicopia.power.PowersRegistry; import com.minelittlepony.pony.data.IPony; @@ -86,9 +91,7 @@ public class Unicopia { @EventHandler public void init(FMLInitializationEvent event) { - channel = JumpingCastle.subscribeTo(MODID, () -> { - channel.send(new MsgRequestCapabilities(Minecraft.getMinecraft().player, clientPlayerRace), Target.SERVER); - }) + channel = JumpingCastle.subscribeTo(MODID, () -> {}) .listenFor(MsgRequestCapabilities.class) .listenFor(MsgPlayerCapabilities.class) .listenFor(MsgPlayerAbility.class); @@ -119,10 +122,15 @@ public class Unicopia { } @SubscribeEvent - public static void registerRecipesStatic(RegistryEvent.Register event) { + public static void registerSounds(RegistryEvent.Register event) { UItems.registerRecipes(event.getRegistry()); } + @SubscribeEvent + public static void registerRecipesStatic(RegistryEvent.Register event) { + USounds.init(event.getRegistry()); + } + @SubscribeEvent public static void registerEntitiesStatic(RegistryEvent.Register event) { UEntities.init(event.getRegistry()); @@ -133,7 +141,7 @@ public class Unicopia { public static void onGameTick(TickEvent.ClientTickEvent event) { Race newRace = getclientPlayerRace(); - if (newRace != clientPlayerRace) { + if (newRace != clientPlayerRace && Minecraft.getMinecraft().player != null) { clientPlayerRace = newRace; channel.send(new MsgRequestCapabilities(Minecraft.getMinecraft().player, clientPlayerRace), Target.SERVER); @@ -200,6 +208,23 @@ public class Unicopia { Commands.init(event); } + @SideOnly(Side.CLIENT) + @SubscribeEvent + public static void onRenderHud(RenderGameOverlayEvent.Post event) { + if (event.getType() != ElementType.ALL) { + return; + } + + if (UClient.isClientSide()) { + Minecraft mc = Minecraft.getMinecraft(); + if (mc.player != null && mc.world != null) { + IPlayer player = PlayerSpeciesList.instance().getPlayer(mc.player); + + UHud.instance.renderHud(player, event.getResolution()); + } + } + } + @SubscribeEvent public static void onPlayerRightClick(PlayerInteractEvent.RightClickItem event) { // Why won't you run!? diff --git a/src/main/java/com/minelittlepony/unicopia/hud/FlightExperienceBar.java b/src/main/java/com/minelittlepony/unicopia/hud/FlightExperienceBar.java new file mode 100644 index 00000000..a309109b --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/hud/FlightExperienceBar.java @@ -0,0 +1,62 @@ +package com.minelittlepony.unicopia.hud; + +import com.minelittlepony.unicopia.player.IPlayer; + +import net.minecraft.client.gui.Gui; +import net.minecraft.util.ResourceLocation; + +class FlightExperienceBar implements IHudElement { + + static final ResourceLocation TEXTURE = new ResourceLocation("textures/gui/bars.png"); + + @Override + public boolean shouldRender(IPlayer player) { + return player.getPlayerSpecies().canFly() + && player.getGravity().isFlying(); + } + + @Override + public void renderHud(UHud context) { + float xp = context.player.getGravity().getFlightExperience(); + float length = context.player.getGravity().getFlightDuration(); + + context.mc.getTextureManager().bindTexture(TEXTURE); + int x = (context.width - 184) / 2; + int y = context.height - 29; + + int xpFill = (int)Math.floor(xp * 184); + int xpBuff = (int)Math.floor((184 - xpFill) * length); + + Gui.drawModalRectWithCustomSizedTexture(x, y, 0, 0, 256, 5, 256, 256); + Gui.drawModalRectWithCustomSizedTexture(x, y, 0, 5, xpFill, 5, 256, 256); + + + Gui.drawModalRectWithCustomSizedTexture(x + xpFill, y, xpFill, 10, xpBuff, 5, 256, 256); + + // context.fonts.drawStringWithShadow("Flight experience: " + context.player.getFlightExperience(), 0, 0, 0xFFFFFF); + } + + +} + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/com/minelittlepony/unicopia/hud/IHudElement.java b/src/main/java/com/minelittlepony/unicopia/hud/IHudElement.java new file mode 100644 index 00000000..eb91feda --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/hud/IHudElement.java @@ -0,0 +1,9 @@ +package com.minelittlepony.unicopia.hud; + +import com.minelittlepony.unicopia.player.IPlayer; + +public interface IHudElement { + void renderHud(UHud context); + + boolean shouldRender(IPlayer player); +} diff --git a/src/main/java/com/minelittlepony/unicopia/hud/UHud.java b/src/main/java/com/minelittlepony/unicopia/hud/UHud.java new file mode 100644 index 00000000..384bf07a --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/hud/UHud.java @@ -0,0 +1,63 @@ +package com.minelittlepony.unicopia.hud; + +import java.util.ArrayList; +import java.util.List; + +import org.lwjgl.opengl.GL11; + +import com.minelittlepony.unicopia.player.IPlayer; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.client.gui.Gui; +import net.minecraft.client.gui.ScaledResolution; +import net.minecraft.client.renderer.GlStateManager; + +public class UHud extends Gui { + + public static final UHud instance = new UHud(); + + private List elements = new ArrayList<>(); + + Minecraft mc = Minecraft.getMinecraft(); + + FontRenderer fonts = mc.fontRenderer; + + IPlayer player; + + int width; + + int height; + + private UHud() { + elements.add(new FlightExperienceBar()); + } + + public void renderHud(IPlayer player, ScaledResolution resolution) { + this.width = resolution.getScaledWidth(); + this.height = resolution.getScaledHeight(); + this.player = player; + + elements.forEach(this::renderElement); + } + + private void renderElement(IHudElement element) { + if (!element.shouldRender(player)) { + return; + } + + GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS); + + GlStateManager.pushMatrix(); + GlStateManager.disableLighting(); + GlStateManager.color(1, 1, 1, 1); + + element.renderHud(this); + + GlStateManager.popMatrix(); + + GL11.glPopAttrib(); + + } + +} diff --git a/src/main/java/com/minelittlepony/unicopia/player/IGravity.java b/src/main/java/com/minelittlepony/unicopia/player/IGravity.java new file mode 100644 index 00000000..c8cb6ebd --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/player/IGravity.java @@ -0,0 +1,9 @@ +package com.minelittlepony.unicopia.player; + +public interface IGravity { + boolean isFlying(); + + float getFlightExperience(); + + float getFlightDuration(); +} diff --git a/src/main/java/com/minelittlepony/unicopia/player/IPlayer.java b/src/main/java/com/minelittlepony/unicopia/player/IPlayer.java index 77420446..ee3f0c6c 100644 --- a/src/main/java/com/minelittlepony/unicopia/player/IPlayer.java +++ b/src/main/java/com/minelittlepony/unicopia/player/IPlayer.java @@ -15,6 +15,8 @@ public interface IPlayer extends ICaster, IRaceContainer { return powers; } + @Override + public IGravity getGravity() { + return gravity; + } + @Override public boolean isClientPlayer() { return UClient.isClientSide() && diff --git a/src/main/java/com/minelittlepony/unicopia/player/PlayerGravityDelegate.java b/src/main/java/com/minelittlepony/unicopia/player/PlayerGravityDelegate.java index 21eeff72..e69aa40a 100644 --- a/src/main/java/com/minelittlepony/unicopia/player/PlayerGravityDelegate.java +++ b/src/main/java/com/minelittlepony/unicopia/player/PlayerGravityDelegate.java @@ -1,6 +1,7 @@ package com.minelittlepony.unicopia.player; import com.minelittlepony.unicopia.InbtSerialisable; +import com.minelittlepony.unicopia.USounds; import net.minecraft.block.Block; import net.minecraft.block.SoundType; @@ -16,14 +17,14 @@ import net.minecraft.util.SoundEvent; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.MathHelper; -class PlayerGravityDelegate implements IUpdatable, InbtSerialisable { +class PlayerGravityDelegate implements IUpdatable, IGravity, InbtSerialisable { private final IPlayer player; private static final float MAXIMUM_FLIGHT_EXPERIENCE = 500; - private int ticksInAir = 0; - private float flightExperience = 0; + public int ticksInAir = 0; + public float flightExperience = 0; public boolean isFlying = false; @@ -53,8 +54,8 @@ class PlayerGravityDelegate implements IUpdatable, InbtSerialisabl entity.addExhaustion(exhaustion * (1 - flightExperience)); - if (ticksInAir > 2000) { - ticksInAir = 1; + if (ticksInAir >= MAXIMUM_FLIGHT_EXPERIENCE) { + ticksInAir = 0; addFlightExperience(entity); entity.playSound(SoundEvents.ENTITY_GUARDIAN_FLOP, 1, 1); } @@ -64,8 +65,17 @@ class PlayerGravityDelegate implements IUpdatable, InbtSerialisabl entity.motionX += - forward * MathHelper.sin(entity.rotationYaw * 0.017453292F); entity.motionY -= 0.05F - ((entity.motionX * entity.motionX) + (entity.motionZ + entity.motionZ)) / 100; entity.motionZ += forward * MathHelper.cos(entity.rotationYaw * 0.017453292F); + + if (ticksInAir > 0 && ticksInAir % 12 == 0) { + entity.playSound(USounds.WING_FLAP, 0.1F, 1); + } } else { + if (ticksInAir != 0) { + entity.playSound(USounds.WING_FLAP, 0.04F, 1); + } + ticksInAir = 0; + flightExperience *= 0.9991342; } } } @@ -144,4 +154,19 @@ class PlayerGravityDelegate implements IUpdatable, InbtSerialisabl flightExperience = compound.getFloat("flightExperience"); isFlying = compound.getBoolean("isFlying"); } + + @Override + public boolean isFlying() { + return isFlying; + } + + @Override + public float getFlightExperience() { + return flightExperience / MAXIMUM_FLIGHT_EXPERIENCE; + } + + @Override + public float getFlightDuration() { + return ticksInAir / MAXIMUM_FLIGHT_EXPERIENCE; + } } diff --git a/src/main/resources/assets/unicopia/sounds.json b/src/main/resources/assets/unicopia/sounds.json new file mode 100644 index 00000000..74e0fc99 --- /dev/null +++ b/src/main/resources/assets/unicopia/sounds.json @@ -0,0 +1,12 @@ +{ + "wing_flap": { + "category": "player", + "subtitle": "unicopia.subtitle.flap_wings", + "sounds": [ + "unicopia:wing/wing0", + "unicopia:wing/wing1", + "unicopia:wing/wing2", + "unicopia:wing/wing3" + ] + } +} \ No newline at end of file diff --git a/src/main/resources/assets/unicopia/sounds/wing/dash1.ogg b/src/main/resources/assets/unicopia/sounds/wing/dash1.ogg new file mode 100644 index 00000000..c9a764dd Binary files /dev/null and b/src/main/resources/assets/unicopia/sounds/wing/dash1.ogg differ diff --git a/src/main/resources/assets/unicopia/sounds/wing/land1.ogg b/src/main/resources/assets/unicopia/sounds/wing/land1.ogg new file mode 100644 index 00000000..75ce0a19 Binary files /dev/null and b/src/main/resources/assets/unicopia/sounds/wing/land1.ogg differ diff --git a/src/main/resources/assets/unicopia/sounds/wing/wing0.ogg b/src/main/resources/assets/unicopia/sounds/wing/wing0.ogg new file mode 100644 index 00000000..0258b040 Binary files /dev/null and b/src/main/resources/assets/unicopia/sounds/wing/wing0.ogg differ diff --git a/src/main/resources/assets/unicopia/sounds/wing/wing1.ogg b/src/main/resources/assets/unicopia/sounds/wing/wing1.ogg new file mode 100644 index 00000000..2ddbe684 Binary files /dev/null and b/src/main/resources/assets/unicopia/sounds/wing/wing1.ogg differ diff --git a/src/main/resources/assets/unicopia/sounds/wing/wing2.ogg b/src/main/resources/assets/unicopia/sounds/wing/wing2.ogg new file mode 100644 index 00000000..284482aa Binary files /dev/null and b/src/main/resources/assets/unicopia/sounds/wing/wing2.ogg differ diff --git a/src/main/resources/assets/unicopia/sounds/wing/wing3.ogg b/src/main/resources/assets/unicopia/sounds/wing/wing3.ogg new file mode 100644 index 00000000..d4f09a30 Binary files /dev/null and b/src/main/resources/assets/unicopia/sounds/wing/wing3.ogg differ