Basic Heart System with Lua

Description: Libraries made for LunaLUA.
Moderator: Moderators

hacheipe399
Topic author, Honourable citizen
Honourable citizen
hacheipe399
Topic author, Honourable citizen
Honourable citizen
Reputation: 10
Posts: 38
Joined: 24 Feb 2014

Post #1by hacheipe399 » 28 Sep 2015, 3:57

Hello everybody! I did a way to have more hearts for the player. When the player receives a damage, LunaLua takes a heart from virtual hearts of the code. This is the same system which is used in the original LunaDLL. The code is very simple. It consists in 4 easy conditionals and 2 variables:

First step: setting up 2 variables "vhearts" (virtual hearts of the code) and "heartLimit" (max. number of hearts for your player). Create them and assign them the value that you want.
Second step: powerup system. It isn't perfect, but we'll keep the number of true hearts in 2 always, so the third heart will add 1 to the virtual hearts. After that, we need to keep the true hearts in 2 again:

Code: Select all

--Detect extra hearts. It'll add an infinite number of hearts.
      if ((player:mem(0x16, FIELD_WORD)) > 2) then
         vhearts = vhearts + 1;
         player:mem(0x16, FIELD_WORD, 2);
      end


Third step: the limit. That conditional will add 1 virtual heart everytime you acquire a powerup. That's why we created a second variable. Using the next conditional, the player won't get more hearts if the virtual hearts reached the limit:

Code: Select all

--You've defined a number of hearts for some reason, no?
      if (vhearts > heartLimit) then
         vhearts = heartLimit;
      end


Fourth step: substract hearts. When the player hurts, the number of hearts will decrease logically. Do you remember that the true hearts are always fixed in 2? Well, when the player receives a damage there is 1 true heart substracted. We can compare that single heart with the amount of virtual hearts here:

Code: Select all

--If damaged, take extra hearts from virtual hearts
      if ((player:mem(0x16, FIELD_WORD)) == 1) and (vhearts > 1) then
         vhearts = vhearts - 1;
      end


Fifth step: keep the player alive. If we run the code as is now, the player will start the game with 1 heart only and if you have various hearts, you'll switch to small at the first hit and then you'll die. So we'll keep the player in big state when the character has more than 2 hearts:

Code: Select all

--This keeps the player alive
      if (player.powerup == PLAYER_SMALL) and (vhearts > 2) then
         player.powerup = PLAYER_BIG;
      end


With this your heart system will work fine, but doesn't work for Toad and Peach. Also I made a small API to use this system and display it with a single line of code within your onLoad function:

Code: Select all

--How to use: _heartAPI.heartSystem(BASE TEXT, NUMBER OF HEARTS, X COORDINATES OF THE TEXT, Y COORD. OF THE TEXT, DISPLAY OR NOT).
--Example:
_heartAPI.heartSystem("TANKS:", 14, 194, 56, true);

With the second argument, we set the number of virtual hearts and the limit, obviously.

There is a video which shows how the system works:
phpBB [youtube]

Original link: https://www.youtube.com/watch?v=ZhdOaDn97_0



That's all! I hope this will be useful for you :)
Attachments
heartsystem.zip
File with the API. You can create a lunadll.lua or lunaworld.lua and add a single line to use it!
(1.42 KiB) Downloaded 188 times

Sambo M
Count
Count
Avatar
Sambo M
Count
Count
Age: 20
Reputation: 13
Posts: 264
Joined: 27 Jun 2014

Post #2by Sambo » 2 Oct 2015, 2:24

That's pretty cool. The heart counter you have doesn't really fit with the HUD though.

This would be really cool for a Zelda-type game. You could even make a heart system that works exactly like in Zelda with this.
Image
Current Project:
Image

hacheipe399
Topic author, Honourable citizen
Honourable citizen
hacheipe399
Topic author, Honourable citizen
Honourable citizen
Reputation: 10
Posts: 38
Joined: 24 Feb 2014

Post #3by hacheipe399 » 2 Oct 2015, 5:53

Thank you! You can choose if display the hearts on screen or not for your own HUD elements! The zelda gameplay is a good idea!

lighthouse64 M
Duke
Duke
Avatar
lighthouse64 M
Duke
Duke
Age: 15
Reputation: 13
Posts: 132
Joined: 15 Feb 2015
Location: IDK!

Post #4by lighthouse64 » 2 Oct 2015, 12:58

This kind of thing seems like a good idea for boss fights as well.
Player of hedgewars!!! :)
And hi... it's me from smbx
Currently bug testing for Mac :apple: And Windows :windows:

P3rf3ctXZer0
Good citizen
Good citizen
Avatar
P3rf3ctXZer0
Good citizen
Good citizen
Reputation: 2
Posts: 31
Joined: 1 Jul 2015

Post #5by P3rf3ctXZer0 » 19 Dec 2015, 3:19

Does this work with Mario Etc? Also could you rename hearts to something Like "HP" or "Energy"? I am not a skilled coder but I really like modding.

hacheipe399
Topic author, Honourable citizen
Honourable citizen
hacheipe399
Topic author, Honourable citizen
Honourable citizen
Reputation: 10
Posts: 38
Joined: 24 Feb 2014

Post #6by hacheipe399 » 19 Dec 2015, 20:26

P3rf3ctXZer0 wrote:Does this work with Mario Etc? Also could you rename hearts to something Like "HP" or "Energy"? I am not a skilled coder but I really like modding.
No, it works only for Link due to the powerup system. And yes, with the function heartSystem() you can name it as you want (in the example I named it "Tanks")


Return to “LunaLUA APIs”

Who is online (over the past 5 minutes)

Users browsing this forum: 1 guest