-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from MihailRis/health
Add health system
- Loading branch information
Showing
7 changed files
with
133 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
local survival_hud = require "survival_hud" | ||
local gamemodes = require "gamemodes" | ||
|
||
function survival_hud.set_health(health) | ||
for i=1,10 do | ||
local img = "gui/health_point" | ||
if i * 2 - 1 == health then | ||
img = "gui/health_point_half" | ||
elseif i * 2 > health then | ||
img = "gui/health_point_off" | ||
end | ||
document["hp_"..tostring(i - 1)].src = img | ||
end | ||
end | ||
|
||
function on_open() | ||
local health = gamemodes.get_player_health(hud.get_player()) | ||
survival_hud.set_health(health.get_health()) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
local survival_hud = {} | ||
return survival_hud |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,76 @@ | ||
local health = ARGS.health or ARGS.max_health or SAVED_DATA.health | ||
local max_health = ARGS.max_health or SAVED_DATA.max_health | ||
local gamemodes = require "gamemodes" | ||
local base_util = require "base:util" | ||
|
||
local health = SAVED_DATA.health or ARGS.health or ARGS.max_health or 20 | ||
local max_health = SAVED_DATA.max_health or ARGS.max_health or 20 | ||
|
||
health = math.ceil(health) | ||
|
||
local fall_timer = 0.0 | ||
|
||
local immortal | ||
local spawnpoint | ||
local invid | ||
|
||
function set_player(pid) | ||
local gamemode = gamemodes.get(pid).current | ||
immortal = gamemode ~= "survival" | ||
spawnpoint = {player.get_spawnpoint(pid)} | ||
invid = player.get_inventory(pid) | ||
end | ||
|
||
function get_health() | ||
return health | ||
return health or 15 | ||
end | ||
|
||
function set_health(value) | ||
health = math.min(math.max(0, value), max_health) | ||
events.emit("base_survival:health.set", entity, health) | ||
end | ||
|
||
function die() | ||
set_health(max_health) | ||
if invid then | ||
local pos = entity.transform:get_pos() | ||
local size = inventory.size(invid) | ||
for i=0,size-1 do | ||
local itemid, count = inventory.get(invid, i) | ||
if itemid ~= 0 then | ||
base_util.drop(pos, itemid, count).rigidbody:set_vel(vec3.spherical_rand(5.0)) | ||
inventory.set(invid, i, 0) | ||
end | ||
end | ||
end | ||
if spawnpoint then | ||
entity.transform:set_pos(spawnpoint) | ||
end | ||
events.emit("base_survival:death", entity) | ||
end | ||
|
||
function damage(points) | ||
if immortal then | ||
return | ||
end | ||
set_health(health - points) | ||
if health == 0 then | ||
die() | ||
end | ||
end | ||
|
||
function on_save() | ||
SAVED_DATA.health = health | ||
SAVED_DATA.max_health = max_health | ||
end | ||
|
||
function on_grounded(force) | ||
local dmg = 5 * fall_timer * fall_timer - 3 | ||
damage(math.max(0, math.floor(dmg))) | ||
end | ||
|
||
function on_update(tps) | ||
fall_timer = fall_timer + 1.0 / tps | ||
end | ||
|
||
function on_fall() | ||
fall_timer = 0.0 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters