-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperson.gui_script
More file actions
141 lines (124 loc) · 5.71 KB
/
person.gui_script
File metadata and controls
141 lines (124 loc) · 5.71 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
local global = require "main.global" -- Import the module
function init(self)
self.health_bar_node = gui.get_node("bar")
self.inventory_bar_node = gui.get_node("inventory")
self.damage_node = gui.get_node("damage")
self.harvest_node = gui.get_node("harvest")
self.build_node = gui.get_node("build")
self.accuracy_node = gui.get_node("accuracy")
gui.set_visible(self.damage_node, true)
self.owner = nil
self.camera_position = vmath.vector3(0, 0, 0)
self.global_pos = vmath.vector3()
self.line = gui.get_node("line")
gui.set_size(self.line, vmath.vector3(2, 2, 0))
gui.set_enabled(self.line, false)
self.start_pos = vmath.vector3()
self.end_pos = vmath.vector3()
msg.post(".", "release_input_focus")
end
local function allfour(self, one, two, three, four)
gui.set_visible(self.damage_node, one)
gui.set_visible(self.harvest_node, two)
gui.set_visible(self.build_node, three)
gui.set_visible(self.accuracy_node, four)
end
local function show_numbers(self, state, numbers)
local dmg_pos = vmath.vector3(self.global_pos.x - 15, self.global_pos.y + 8, 0.1)
local hrv_pos = vmath.vector3(self.global_pos.x, self.global_pos.y + 8, 0.1)
local bld_pos = vmath.vector3(self.global_pos.x + 15, self.global_pos.y + 8, 0.1)
local acc_pos = vmath.vector3(self.global_pos.x - 15, self.global_pos.y + 24, 0.1)
local tm_pos = vmath.vector3(self.global_pos.x, self.global_pos.y + 24, 0.1)
gui.set_position(self.damage_node, dmg_pos)
gui.set_position(self.harvest_node, hrv_pos)
gui.set_position(self.build_node, bld_pos)
gui.set_position(self.accuracy_node, acc_pos)
gui.set_text(self.damage_node, numbers[1])
gui.set_text(self.harvest_node, numbers[2])
gui.set_text(self.build_node, numbers[3])
gui.set_text(self.accuracy_node, numbers[4])
allfour(self, false, false, false, false)
if state == "Selected" then
allfour(self, true, true, true, true)
elseif state == "Training" then
allfour(self, true, false, false, true)
gui.set_position(self.damage_node, hrv_pos)
gui.set_position(self.accuracy_node, tm_pos)
elseif state == "Harvesting" then
allfour(self, false, false, false, false)
elseif state == "Building" then
allfour(self, false, false, true, false)
gui.set_position(self.build_node, hrv_pos)
end
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 / 100, 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)
--show_numbers(self, message.state, message.numbers)
elseif message_id == hash("update_inventory") then
self.global_pos = global.convert_click_pos(message.position, self.camera_position)
self.global_pos.y = self.global_pos.y + 20
local health_ratio = math.max(0, math.min(message.space / 50, 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.inventory_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.inventory_bar_node)
size.x = new_width*2
gui.set_size(self.inventory_bar_node, size)
gui.set_color(self.inventory_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
elseif message_id == hash("line") then
gui.set_enabled(self.line, true)
local colour = vmath.vector4(1, 1, 1, 0.2)
if message.todo == "Fighting" or message.todo == "Training" then
colour = vmath.vector4(1, 0, 0 ,0.2)
elseif message.todo == "Harvesting" then
colour = vmath.vector4(0, 1, 0, 0.2)
elseif message.todo == "Building" then
colour = vmath.vector4(0, 0, 1, 0.2)
end
self.start_pos = global.convert_click_pos(message.start_pos, self.camera_position)
self.end_pos = global.convert_click_pos(message.end_pos, self.camera_position)
--print(start_pos, end_pos)
local node = self.line
-- Calculate direction and distance from start to end position
local dx = self.end_pos.x - self.start_pos.x
local dy = self.end_pos.y - self.start_pos.y
local distance = math.sqrt(dx * dx + dy * dy)
local angle = math.atan2(dy, dx)
-- Update the line's size based on the distance
local line_size = vmath.vector3(distance, 2, 0) -- Line width based on distance
gui.set_size(node, line_size)
--print(gui.get_enabled(node))
-- Set the position of the line to the midpoint between the start and end positions
local line_position = vmath.vector3(self.start_pos.x + dx / 2, self.start_pos.y + dy / 2, 0)
gui.set_position(node, line_position)
gui.set_color(node, colour)
-- Rotate the line to point in the direction of movement
gui.set_rotation(node, vmath.quat_rotation_z(angle))
elseif message_id == hash("done") then
gui.set_enabled(self.line, false)
end
end