Awesome so far. I'm glad you are understanding my explanations.
Part ThreeOkay, so now we have the if statement all set up, now we just have to write up the code that will correctly draw the images. We are going to do this using two images, the full heart and the empty heart. So first thing we do is store the full heart image in a variable. I used bitmap previously, so I will use it again. Now comes the confusing part.
What do I need to do so that I get the correct proportions for the hearts? Well, if I have 678 hp, I’ll have 6 full hearts and one partial heart. But how much of the heart should there be? Well, there should be 78% of a heart. This is because at 700hp there will be 7 full hearts. Since we are using 100 hp per heart it is very easy to see that 678 lies 78% of the way between 600 and 700 and therefore that is how much of the heart should be full.
So how can we extract that value 78? Well, 100 – (700 – 678) = 78 right? Seems easy, but hp changes. Your actor could have 1325 hp. Then the equation becomes 100 – (1400 – 1325) = 25. So we need to create some code that will round up the current hp to the next hundred. How do we do this? Well, we have actually seen this before haven’t we?
hp = ($game_party.actors[0].hp+99)/100*100
Here I took the actors current hp and rounded it up to the next hundred. However this is slightly different than before. This time I added *100 to the end. This is because if we go back to the example of having 678 hp + 99 = 777 / 100 = 7 right? Well we wanted to round up to the next
hundred so 7*100 = 700. Simple isn’t it?
So now what we have is the actor’s current hp, 678 for example, and a way of calculating the next hundred and we stored that in the variable ‘hp’. Now that equation we looked at earlier can be coded and will work for and actor hp value.
This is the equation we want to use: 100 – (next hundred – actor’s hp)
For simplicity sake I used a new variable for the next line, although you can easily use hp. Let’s just make a new variable called ‘amount’. This variable will hold the value of the equation. So let’s set that up now.
amount = 100 - (hp - $game_party.actors[0].hp)
This should seem straight forward. So what we have right now is the percentage of the image that needs to be displayed stored in amount. But this is just a number, 78% is the integer 78. So we have to do some calculations to modify this number and scale it down so that it is proportional to the size of the image. So how do we do that? Well, from math you most likely know that if you want to find how much 78% is of 90 you put 78% into decimal form, 0.78 and multiply by 90. 0.78 * 90 = 70.2. The same logic applies here. Except there is a slight problem! If we divide 78 by 100 and store it as an integer, the value will become 0. So a very easy way around this would be to multiply by 90 first and then divide by 100. 78 * 90 = 7020 / 100 = 70.2. We arrive at the same answer (obviously).
So let’s put this into terms of the image. We want the value stored in ‘amount’, for example 78, to be multiplied by the images height then divide by 100. So the first thing that may come to mind is the following code:
amount = amount * bitmap.height / 100
There are many ways of writing this same code. If you choose to use *= instead of = you would have to split this code into 2 lines.
amount *= bitmap.height
amount /= 100
This does the same thing as the previous line. Now what do we have? Well using the previous example, 78 * image height(24) = 1872 / 100 = 18. Therefore, 18 / 24 = 78 / 100. So we only want to draw the bottom 18 pixels of the full heart. Now we saw when fooling around with the src_rect that we can clip off parts of an image. Well that is where this all comes into play. This next line of code you will understand better later, but this is needed. Without this next line, the heart will empty from the bottom up(along with a couple other changes)!
amount = 24 – amount
Now let’s define the src_rect.
src_rect = Rect.new(0, amount, bitmap.width, bitmap.height- amount)
This may look confusing, so I’ll try to explain. I’ll start with the height. As you can see, I am taking the total height of the bitmap(24) and subtracting the value of amount (24 – 18 = 6). 24 – 6 = 18. It seems like we go around in a big circle doesn’t it? It was 18, 24 – 18 = 6, so then it was 6, and now 24 – 6 = 18 it’s back to 18. Why? Well as you can see the y value is set to amount, in this case 6. This is so that the image keeps it’s correct alignment. This is also why the previous piece of code was needed.
Now we have to draw the image.
self.contents.blt(x, y + amount, bitmap, src_rect)
The y value is y + amount because as we saw earlier, when we change the y value of the src_rect the entire image shifts. So we have to counter balance that shift so the image is displayed in the correct location.
And similarly, we have to set the empty hear to the bitmap variable then set up its bitmap:
src_rect = Rect.new(0, 0, bitmap.width, amount)
Almost there! Now in the original code we had this code:
src_rect = Rect.new(0, 0, bitmap.width, bitmap.height)
self.contents.blt(x, y, bitmap, src_rect)
Well, we have to move the src_rect now. This can be deleted and put at the bottom of the first if and the else. This is because in the else if we set up the src_rect at the end, and if we didn’t delete the src_rect setup right before we drew the image then the src_rect will be incorrect. I hope you understand that very long sentence :|. This is it, I gave you all the harder bits of code, now you have to figure out how to use them and where they go! Then do the same for the stars! Good luck, and I will answer further questions you may have.
There is also some little changes I have made that you may need to change later, but for now let’s just try to get the draining part working!
*I have commented the new edits with #-------=[new edit]
as you can see I have both sp and hp ready for their algorithms... ~.^
I should have warned you...I learn just as fast as I can forget X-P ~g
You are one step ahead of me with doing the stars one
. And lol about the forgetting thing.