Jump to content

[1.15.2] Using doubles for decrementing a HUD bar


ultra_reemun

Recommended Posts

Hi there,

 

I am currently trying to make a cooldown bar decrement gradually over a set time, however am unable to do so as that would require me to decrement each tick by a double, which ingameGUI.blit does not support.

Is there anyway around this? My problem is this:

 

182 needs to gradually go down to 0 over X amount of ticks, by even decrements. However, each decrement must be an int.

 

Is there anyway to allow this to happen? Currently X = 80, which means if it decrements evenly each tick it must be a float, 2,275, which blit does not support.

I have tried Math.round() and Math.ceil(), however that messes up the timing of my cooldown

 

Here's my code:

@SubscribeEvent
	public static void playerTick(TickEvent.PlayerTickEvent event) {
		if (event.phase == TickEvent.Phase.END) {
			Minecraft mc = Minecraft.getInstance();
			PlayerEntity player = event.player;
			if (player == mc.player) {
				if (player.world.isRemote) {
					cooldown++;
					
                  //DodgeGui.dlen is the length of the dodge bar
					// Handle GUI. every tick, it needs to go down by cooldownLength/182
					if (DodgeGui.dlen > 0) {
				        DodgeGui.dlen = (int) (DodgeGui.dlen-Math.ceil(182/80));
				        System.out.println(80/182);
				        }
					
					if (cooldown == cooldownLength) {
						Minecraft.getInstance().player.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1f, 1);
						DodgeGui.dlen = 0; //Ensure it's hidden
					}
				}
			}
		}
	}
}

 

Any help would be greatly appreciated!

Edited by ultra_reemun
Fixed code
Link to comment
Share on other sites

Okay. You should first implement the cooldown timer without thinking about the HUD representation. If it needs to cool down for X ticks you should count down from X to 0 (or up from 0 to X, doesn't really matter). Then when drawing the HUD you represent that as a ratio (e.g. if 50 ticks have passed and you want to count down for 500 ticks that's 50/500 == 0.1 == 10%). You then multiply your total bar size (in the HUD) by that ratio, e.g. if your bar should be 180 wide you multiply 180 * 0.1 in this case => your bar is then 18 pixels in size at this point. If you want a smooth animation here you should also incorporate the partial ticks value. In your case using it is very simple, simply add (or subtract, if you are animating the bar from full to 0) it to your counter value before multiplying by the ratio (since you are changing your actual value by just 1 every tick you can use this simple formula, otherwise it would have to be actualValue = lastTickValue + (currentValue - lastTickValue) * partialTicks).

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.