-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenemy.gui_script
More file actions
41 lines (33 loc) · 1.47 KB
/
enemy.gui_script
File metadata and controls
41 lines (33 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
local global = require "main.global" -- Import the module
function init(self)
self.health_bar_node = gui.get_node("bar")
self.owner = nil
self.camera_position = vmath.vector3(0, 0, 0)
self.global_pos = vmath.vector3()
msg.post(".", "release_input_focus")
end
function on_message(self, message_id, message, sender)
if message_id == hash("update_health") then
self.global_pos = global.convert_click_pos(message.position, self.camera_position)
self.global_pos.y = self.global_pos.y + 15
local health_ratio = math.max(0, math.min(message.health / message.max_health, 1)) -- Ensure range [0,1]
local red = 1 - health_ratio -- More red when health is low
local green = health_ratio -- More green when health is high
local color = vmath.vector4(red, green, 0, 1) -- RGB with full opacity
gui.set_position(self.health_bar_node, self.global_pos)
local max_width = 100 -- Set max width of the bar
local new_width = max_width * health_ratio
local size = gui.get_size(self.health_bar_node)
size.x = new_width*2
gui.set_size(self.health_bar_node, size)
gui.set_color(self.health_bar_node, color)
elseif message_id == hash("offset") then
-- Update the camera position
self.camera_position = message.pos
if self.owner then
local owner_position = go.get_position(self.owner) -- Get owner's new position
self.global_pos = global.convert_click_pos(owner_position, self.camera_position)
--gui.set_position(self.health_node, self.global_pos)
end
end
end