Skip to content

Commit d7767e5

Browse files
Site changes [skip-ci]
1 parent f9c93a9 commit d7767e5

File tree

13 files changed

+333
-238
lines changed

13 files changed

+333
-238
lines changed

_data/examplesindex.json

Lines changed: 243 additions & 237 deletions
Large diffs are not rendered by default.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
-- < 1 >
2+
local min_size = 48
3+
local max_size = 235 - min_size
4+
5+
-- < 2 >
6+
local function set_healthbar(healthbar_node_name, health_percentage)
7+
local healthbar_node = gui.get_node(healthbar_node_name) -- < 3 >
8+
local healthbar_size = gui.get_size(healthbar_node) -- < 4 >
9+
healthbar_size.x = health_percentage * max_size + min_size -- < 5 >
10+
gui.set_size(healthbar_node, healthbar_size) -- < 6 >
11+
end
12+
13+
function init(self)
14+
-- < 7 >
15+
set_healthbar("left_health", 1.0)
16+
set_healthbar("right_health", 1.0)
17+
set_healthbar("center_health", 1.0)
18+
end
19+
20+
function on_message(self, message_id, message, sender)
21+
-- < 8 >
22+
if message_id == hash("update_health") then
23+
set_healthbar(message.health_name, message.health_percentage)
24+
end
25+
end
26+
27+
--[[
28+
1. Define minimum and maximum size of GUI healthbar (only width is changed).
29+
2. Define a local helper function to update healthbar.
30+
3. Get node of given name passed as "healthbar_node_name" and store it in local variable "healthbar_node".
31+
4. Get size of this node and store it in local variable "healthbar_size".
32+
5. Change size along X axis (width) of the node to given "health_percentage" scaled times "max_size" and added to "min_size", so that it can be no smaller than it.
33+
6. Set the newly updated size of the node.
34+
7. In init function, for each of three defined nodes set initial health_percentage to 1.0 (100%).
35+
8. In on_message function, if the GUI component receives message "update_health" call helper function to update given health bar.
36+
]]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function init(self)
2+
-- < 1 >
3+
self.player_one_health = 1.0
4+
self.player_two_health = 1.0
5+
self.game_boss_health = 1.0
6+
7+
-- < 2 >
8+
timer.delay(1, true, function()
9+
-- < 3 >
10+
self.player_one_health = math.max(self.player_one_health - 0.1, 0)
11+
self.player_two_health = math.max(self.player_two_health - 0.1, 0)
12+
self.game_boss_health = math.max(self.game_boss_health - 0.1, 0)
13+
-- < 4 >
14+
msg.post("hud", "update_health", { health_name = "left_health", health_percentage = self.player_one_health })
15+
msg.post("hud", "update_health", { health_name = "right_health", health_percentage = self.player_two_health })
16+
msg.post("hud", "update_health", { health_name = "center_health", health_percentage = self.game_boss_health })
17+
end)
18+
end
19+
20+
--[[
21+
1. Set initial health percentage (1.0 = 100%, 0.0 = 0%).
22+
2. Start a timer that will call every 1 second (first argument) repeateadly (second argument being true) a callback function (3rd argument)
23+
3. Reduce each health percentage by 0.1 (10%), but no less than 0 (using math.max to select `0`, if `self.player_one_health - 0.1` is less than `0`).
24+
4. Send messages to hud (gui component) to "updated_health" with health name and percentage to be set in GUI script.
25+
]]

examples/archive/archive_files.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"content":[{"name":"game.projectc","size":3945,"pieces":[{"name":"game0.projectc","offset":0}]},{"name":"game.arci","size":47808,"pieces":[{"name":"game0.arci","offset":0}]},{"name":"game.arcd","size":4143914,"pieces":[{"name":"game0.arcd","offset":0},{"name":"game1.arcd","offset":2097152}]},{"name":"game.dmanifest","size":52152,"pieces":[{"name":"game0.dmanifest","offset":0}]},{"name":"game.public.der","size":162,"pieces":[{"name":"game0.public.der","offset":0}]}],"total_size":4247981}
1+
{"content":[{"name":"game.projectc","size":3945,"pieces":[{"name":"game0.projectc","offset":0}]},{"name":"game.arci","size":48368,"pieces":[{"name":"game0.arci","offset":0}]},{"name":"game.arcd","size":4149590,"pieces":[{"name":"game0.arcd","offset":0},{"name":"game1.arcd","offset":2097152}]},{"name":"game.dmanifest","size":52751,"pieces":[{"name":"game0.dmanifest","offset":0}]},{"name":"game.public.der","size":162,"pieces":[{"name":"game0.public.der","offset":0}]}],"total_size":4254816}

examples/archive/game0.arcd

0 Bytes
Binary file not shown.

examples/archive/game0.arci

560 Bytes
Binary file not shown.

examples/archive/game0.dmanifest

599 Bytes
Binary file not shown.

examples/archive/game0.public.der

0 Bytes
Binary file not shown.

examples/archive/game1.arcd

5.54 KB
Binary file not shown.

examples/gui/healthbar/healthbar.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
layout: example
3+
permalink: examples/gui/healthbar/
4+
collection: gui/healthbar
5+
title: Health Bar
6+
brief: This example demonstrates how to add different health bars.
7+
scripts: healthbar.script, healthbar.gui_script
8+
---
9+
10+
Overview : Example shows 3 pairs of nodes each forming a "health bar" with different pivots.
11+
12+
Create a pair of Box nodes, so that child node is smaller than the parent:
13+
![healthbar](healthbar.png)
14+
![healthbar_inner](healthbar_inner.png)
15+
16+
Example contains 3 such pairs - each with different `Pivot` and `X Anchor` settings for inner health bars:
17+
18+
- `West` + `Left`
19+
- `East` + `Right`
20+
- `Center` + `None`
21+
22+
Health is indicated as the size on X Axis of the inner node, so define what can be maximum and minimum width here.
23+
24+
Create a collection with such GUI component and add it and your game object with script to collection:
25+
26+
![healthbar_collection](healthbar_collection.png)
27+
28+
Example shows communication between `controller#main` script component (`healthbar.script`) and `hud#main` gui component with gui_script (`healthbar.gui_script`).

examples/gui/healthbar/healthbar.png

109 KB
Loading
9.63 KB
Loading
109 KB
Loading

0 commit comments

Comments
 (0)