./hud_healthbars.lua

  1. Hud_healthbars_status = {	 
  2. 	 
  3. 	--Hostiles Info 
  4. 	hostiles = { }, 
  5. 	 
  6. 	hostile_elements = { 
  7. 		grp_h = 0, fill_h = 0, border_h = 0, 
  8. 	}, 
  9. 	 
  10. 	--animations 
  11. 	anims = { }, 
  12. 	 
  13. 	anim_elements = { 
  14. 		healthbar_anim_h = 0,  
  15. 	}, 
  16. 	 
  17. 	handles = {}, 
  18. } 
  19.  
  20. USE_OBJECT_HANDLE_KEY = 0 
  21.  
  22. COLOR_RED				= 0 
  23. COLOR_BLUE				= 1 
  24. COLOR_NEUTRAL			= 2 
  25.  
  26. function hud_healthbars_init() 
  27. 	--mini healthbar 
  28. 	local h = -1 
  29. 	Hud_healthbars_status.hostile_elements.grp_h = vint_object_find("health_mini_grp") 
  30. 	vint_set_property(Hud_healthbars_status.hostile_elements.grp_h, "visible", false) 
  31. 	 
  32. 	h = Hud_healthbars_status.hostile_elements.grp_h 
  33. 	Hud_healthbars_status.hostile_elements.fill_h = vint_object_find("health_mini_fill", h) 
  34. 	Hud_healthbars_status.hostile_elements.border_h = vint_object_find("health_mini_border", h) 
  35. 	Hud_healthbars_status.hostile_elements.grit_h = vint_object_find("health_mini_grit", h) 
  36. 	Hud_healthbars_status.handles.mp_arrow_h = vint_object_find("mp_arrow", h) 
  37. 	Hud_healthbars_status.handles.mp_name_h = vint_object_find("mp_name", h) 
  38. 	Hud_healthbars_status.handles.overhead_cash_h = vint_object_find("overhead_cash", h) 
  39. 	Hud_healthbars_status.handles.race_pos_h = vint_object_find("race_pos", h) 
  40. 	 
  41. 	--large healthbar 
  42. 	Hud_healthbars_status.handles.health_large_grp = vint_object_find("health_large_grp") 
  43. 	vint_set_property(Hud_healthbars_status.handles.health_large_grp, "visible", false) 
  44. 	 
  45. 	h = Hud_healthbars_status.handles.health_large_grp 
  46. 	Hud_healthbars_status.handles.health_large_fill = vint_object_find("health_large_fill", h) 
  47. 	Hud_healthbars_status.handles.health_large_border = vint_object_find("health_large_border", h) 
  48. 	 
  49. 	--animation 
  50. 	Hud_healthbars_status.anim_elements.healthbar_anim_h = vint_object_find("health_mini_fade_out") 
  51. 	--large 
  52. 	Hud_healthbars_status.handles.health_large_fade_out = vint_object_find("health_large_fade_out") 
  53. 	--overhead_cash fade out 
  54. 	Hud_healthbars_status.handles.overhead_cash_fade_out = vint_object_find("overhead_cash_fade_out") 
  55. 	 
  56. 	--Pause animations 
  57. 	vint_set_property(Hud_healthbars_status.anim_elements.healthbar_anim_h, "is_paused", true) 
  58. 	--large 
  59. 	vint_set_property(Hud_healthbars_status.handles.health_large_fade_out, "is_paused", true) 
  60. 	--overhead_cash fade out 
  61. 	vint_set_property(Hud_healthbars_status.handles.overhead_cash_fade_out, "is_paused", true) 
  62. 		 
  63. 	--Health Bar Subscription 
  64. 	vint_datagroup_add_subscription("sr2_local_player_septic", "insert", "hud_healthbars_update") 
  65. 	vint_datagroup_add_subscription("sr2_local_player_septic", "update", "hud_healthbars_update") 
  66. 	vint_datagroup_add_subscription("sr2_local_player_septic", "remove", "hud_healthbars_update") 
  67. end 
  68.  
  69. function hud_healthbars_cleanup() 
  70. end 
  71.  
  72. function tint_element( handle, color ) 
  73. 	if color == COLOR_RED then 
  74. 		vint_set_property( handle, "tint", 0.71, 0, 0 ) 
  75. 	elseif color == COLOR_BLUE then 
  76. 		vint_set_property( handle, "tint", COLOR_SAINTS_PURPLE.R, COLOR_SAINTS_PURPLE.G, COLOR_SAINTS_PURPLE.B ) 
  77. 		--SR2 COOP Blue 
  78. 		--vint_set_property( handle,	"tint", 0.27, 0.51, 0.84 ) 
  79. 	else 
  80. 		vint_set_property( handle, "tint", 1, 1, 1 ) 
  81. 	end 
  82. end 
  83.  
  84. function tint_healthbar( hostile, color ) 
  85. 	tint_element( hostile.fill_h, color ) 
  86. 	tint_element( hostile.mp_name_h, color ) 
  87. 	tint_element( hostile.mp_arrow_h, color ) 
  88. 	tint_element( hostile.cash_h, color ) 
  89. 	tint_element( hostile.race_pos_h, color ) 
  90. end 
  91.  
  92. function hud_healthbars_update(di_h, event) 
  93.  
  94. 	local no_object_key, object_handle, screen_x, screen_y, z_depth, 
  95. 		distance, health_pct, name, overhead_cash, race_position, 
  96. 		is_visible, color, is_big, health_full_alpha, mp_respawn_invulnerable, 
  97. 		show_bar, show_name, show_arrow, show_cash, show_race_pos = vint_dataitem_get(di_h) 
  98. 	 
  99. 	local decoded_name = vint_debug_decode_wide_string( name )	 
  100. 	 
  101. 	local key = ( no_object_key == USE_OBJECT_HANDLE_KEY ) and object_handle or no_object_key 
  102. 	 
  103. 	if event == "update" then 
  104. 	 
  105. 		--check to see if there is a hostile indicator for the key 
  106. 		if Hud_healthbars_status.hostiles[key] == nil then 
  107. 			 
  108. 			--Doesn't currently exist so create the hostile indicator 
  109. 			local grp_h = vint_object_clone(Hud_healthbars_status.hostile_elements.grp_h) 
  110. 			local fill_h = vint_object_find("health_mini_fill", grp_h) 
  111. 			local border_h = vint_object_find("health_mini_border", grp_h) 
  112. 			local grit_h = vint_object_find("health_mini_grit", grp_h) 
  113. 			local mp_name_h = vint_object_find("mp_name", grp_h) 
  114. 			local mp_arrow_h = vint_object_find("mp_arrow", grp_h) 
  115. 			local overhead_cash_h = vint_object_find("overhead_cash", grp_h) 
  116. 			local race_pos_h = vint_object_find("race_pos", grp_h) 
  117. 			 
  118. 			--Create the animation clones			 
  119. 			local anim_clone_0 = vint_object_clone(Hud_healthbars_status.anim_elements.healthbar_anim_h ) 
  120. 			local anim_0_fade = vint_object_find("mini_alpha_twn", anim_clone_0) 
  121. 			vint_set_property(anim_clone_0, "is_paused", true) 
  122. 			vint_set_property(anim_0_fade, "target_handle",	grp_h) 
  123. 			 
  124. 			 
  125. 			local invulnerable_anim_0 = 0  
  126. 			if MP_enabled == true then 
  127. 				 
  128. 				--Create invulnerable animation clones				 
  129. 				invulnerable_anim_0 = vint_object_clone(vint_object_find("health_anim_1")) 
  130. 				 
  131. 				---   0 = 191; 112; 0  ->> 0 = 255; 255; 147 
  132. 				local h = vint_object_find("health_flash_0_tween", invulnerable_anim_0) 
  133. 				vint_set_property(h, "start_value", 0.749, 0.439, 0) 
  134. 				vint_set_property(h, "end_value", 1, 1, 0.576) 
  135. 				vint_set_property(h, "loop_mode", "bounce") 
  136. 				vint_set_property(h, "target_handle", fill_h) 
  137. 				 
  138. 				--Destroy the other tween :) 
  139. 				local h = vint_object_find("health_flash_1_tween", invulnerable_anim_0) 
  140. 				vint_object_destroy(h) 
  141. 				 
  142. 				if mp_respawn_invulnerable == false then 
  143. 					vint_set_property(invulnerable_anim_0, "is_paused", true) 
  144. 				else 
  145. 					lua_play_anim(invulnerable_anim_0, 0) 
  146. 				end 
  147. 			end 
  148. 			 
  149. 			 
  150. 			--Create the animation clones			 
  151. 			local cash_fade_clone = vint_object_clone(Hud_healthbars_status.handles.overhead_cash_fade_out) 
  152. 			local cash_fade_1 = vint_object_find("over_cash_fade_1", cash_fade_clone) 
  153. 			local cash_fade_2 = vint_object_find("over_cash_fade_2", cash_fade_clone) 
  154. 			vint_set_property(cash_fade_clone, "is_paused", true) 
  155. 			vint_set_property(cash_fade_1, "target_handle",	overhead_cash_h) 
  156. 			vint_set_property(cash_fade_2, "target_handle",	overhead_cash_h) 
  157.  
  158. 			-- Assign the handles (these values shouldn't change after creation) 
  159. 			Hud_healthbars_status.hostiles[key] = { 
  160. 				--dont delete items from this table................ 
  161. 				grp_h = grp_h, 
  162. 				fill_h = fill_h, 
  163. 				border_h = border_h, 
  164. 				grit_h = grit_h, 
  165. 				health_pct = health_pct , 
  166. 				anim_clone_0 = anim_clone_0, 
  167. 				anim_0_fade = anim_0_fade, 
  168. 				invulnerable_anim_0 = invulnerable_anim_0, 
  169. 				mp_respawn_invulnerable = mp_respawn_invulnerable, 
  170. 				cash_fade_clone = cash_fade_clone, 
  171. 				cash_fade_1 = cash_fade_1, 
  172. 				cash_fade_2 = cash_fade_2, 
  173. 				distance = distance, 
  174. 				is_visible = is_visible, 
  175. 				screen_x = screen_x, 
  176. 				screen_y = screen_y, 
  177. 				z_depth = z_depth, 
  178. 				mp_arrow_h = mp_arrow_h, 
  179. 				mp_name_h = mp_name_h,  
  180. 				overhead_cash_h = overhead_cash_h, 
  181. 				overhead_cash = overhead_cash, 
  182. 				race_pos_h = race_pos_h, 
  183. 				race_position = race_position, 
  184. 			} 
  185. 			 
  186. 			-- Convenience local var 
  187. 			local hostile = Hud_healthbars_status.hostiles[key] 
  188. 						 
  189. 			if is_big then 
  190. 				-- Different fill graphics 
  191. 				vint_set_property(hostile.fill_h, "image", "ui_hud_meter_fill") 
  192. 				vint_set_property(hostile.border_h, "image", "ui_hud_meter_border") 
  193. 				vint_set_property(hostile.grit_h, "image", "ui_hud_meter_grit")	 
  194. 			end 
  195. 			 
  196. --			debug_print( "vint_healthbars", "Creating healthbar for " .. decoded_name .. " key: " .. key .. "\n" ) 
  197. 		end 
  198. 		 
  199. 		-- Convenience local var 
  200. 		local hostile = Hud_healthbars_status.hostiles[key] 
  201. 		 
  202. 		-- This is all debug calculations, so comment this out if you don't need to see it 
  203. 		do 
  204. 			local current_visible = vint_get_property( hostile.grp_h, "visible" ) 
  205. 			local current_bar = vint_get_property( hostile.fill_h, "visible" ) 
  206. 			local current_name = vint_get_property( hostile.mp_name_h, "visible" ) 
  207. 			local current_arrow = vint_get_property( hostile.mp_arrow_h, "visible" ) 
  208. 			local current_cash = vint_get_property( hostile.overhead_cash_h, "visible" ) 
  209. 			local current_race_pos = vint_get_property( hostile.race_pos_h, "visible" ) 
  210. 			 
  211. 			if current_visible ~= is_visible or 
  212. 				current_bar ~= show_bar or 
  213. 				current_name ~= show_name or 
  214. 				current_arrow ~= show_arrow or 
  215. 				current_cash ~= show_cash or 
  216. 				current_race_pos ~= show_race_pos then 
  217. 				 
  218. 				--[[ 
  219. 				debug_print( "vint_healthbars", "Updating healthbar for " .. decoded_name .. 
  220. 					" key: " .. key .. 
  221. 					( is_visible and " visible " or "" ) .. 
  222. 					( show_bar and " bar " or "" ) .. 
  223. 					( show_name and " name " or "" ) .. 
  224. 					( show_arrow and " arrow " or "" ) .. 
  225. 					( show_cash and " cash " or "" ) .. 
  226. 					( show_race_pos and " race_pos " or "" ) .. 
  227. 					"\n" )	]] 
  228. 			end 
  229. 		end 
  230. 		 
  231. 		if is_big then 
  232. 			--big bar 
  233. 			local w, h = element_get_actual_size(Hud_healthbars_status.handles.health_large_fill) 
  234. 			vint_set_property(hostile.fill_h, "source_se", w * health_pct, h) 
  235. 			vint_set_property(hostile.border_h, "source_se", (w * health_pct) + 4, h + 4) 
  236. 		else 
  237. 			--regular bar 
  238. 			local w, h = element_get_actual_size(Hud_healthbars_status.hostile_elements.fill_h) 
  239. 			vint_set_property(hostile.fill_h, "source_se", w * health_pct, h) 
  240. 			vint_set_property(hostile.border_h, "source_se", (w * health_pct) + 4, h + 4) 
  241. 		end 
  242. 		 
  243. 		--debug_print("vint", "health_full_alpha " .. var_to_string(health_full_alpha) .. "\n") 
  244. 		 
  245. 		if health_pct ~= hostile.health_pct and health_pct == 0 then 
  246. 			if health_full_alpha == false then 
  247. 				lua_play_anim(hostile.anim_clone_0, 0) 
  248. 			end 
  249. 		end 
  250. 		 
  251. 		if health_full_alpha == true then 
  252. 			vint_set_property(hostile.grp_h, "alpha", 1) 
  253. 		end 
  254. 		 
  255. 		-- Change visibility for: whole group 
  256. 		vint_set_property(hostile.grp_h, "visible", is_visible) 
  257. 		 
  258. 		-- ...bar 
  259. 		vint_set_property(hostile.fill_h, "visible", show_bar) 
  260. 		vint_set_property(hostile.border_h, "visible", show_bar) 
  261. 		vint_set_property(hostile.grit_h, "visible", show_bar) 
  262. 		 
  263. 		-- ...name 
  264. 		vint_set_property(hostile.mp_name_h, "visible", show_name) 
  265. 		 
  266. 		-- ...arrow 
  267. 		vint_set_property(hostile.mp_arrow_h, "visible", show_arrow) 
  268. 		 
  269. 		-- ...cash 
  270. 		if overhead_cash ~= 0 then 
  271. 			vint_set_property(hostile.overhead_cash_h, "text_tag", "+$" .. format_cash(overhead_cash)) 
  272. 		end 
  273. 		 
  274. 		if overhead_cash == 0 and hostile.overhead_cash ~= 0  then 
  275. 			lua_play_anim(hostile.cash_fade_clone) 
  276. 		else 
  277. 			vint_set_property(hostile.overhead_cash_h, "visible", show_cash) 
  278. 			vint_set_property(hostile.overhead_cash_h, "alpha", 1) 
  279. 			vint_set_property(hostile.case_fade_clone, "is_paused", true) 
  280. 		end 
  281. 		 
  282. 		hostile.overhead_cash = overhead_cash 
  283. 		 
  284. 		-- ...race_pos 
  285. 		vint_set_property(hostile.race_pos_h, "visible", show_race_pos) 
  286. 		if show_race_pos then 
  287. 			vint_set_property(hostile.race_pos_h, "image", "ingame_race_position_" .. race_position)  
  288. 		end 
  289. 		 
  290. 		-- Set the name 
  291. 		vint_set_property(hostile.mp_name_h, "text_tag", name) 
  292. 		 
  293. 		-- Set the color 
  294. 		tint_healthbar( hostile, color ) 
  295. 		 
  296. 		--Screen Position		 
  297. 		local x = screen_x 
  298. 		local y = screen_y 
  299. 		vint_set_property(hostile.grp_h, "anchor", x, y)	 
  300. 		 
  301. 		--Scale 
  302. 		local maxclamp = 0.40 
  303. 		local minclamp = 0.1 
  304. 		 
  305. 		local maxscale = 1 
  306. 		local minscale = 0.5 
  307. 		 
  308. 		--Clamp the distances 
  309. 		if distance <= minclamp then 
  310. 			distance = minclamp  
  311. 		elseif distance >= maxclamp then 
  312. 			distance = maxclamp 
  313. 		end 
  314.  
  315. 		local newdist = (distance - minclamp) 
  316. 		local ratio = 1 - (newdist / (maxclamp - minclamp)) 
  317. 		local scale = (ratio * (maxscale-minscale)) + minscale 
  318. 		 
  319. 		vint_set_property(hostile.grp_h, "scale", scale, scale) 
  320. 		--vint_set_property(hostile.grp_h, "alpha", scale) 
  321. 			 
  322. 		--Z Depth 
  323. 		if z_depth ~= hostile.z_depth then 
  324. 			vint_set_property(hostile.grp_h,  "depth", z_depth) 
  325. 		end 
  326. 		 
  327. 		--Invulnerability change 
  328. 		if MP_enabled == true then 
  329. 			if hostile.mp_respawn_invulnerable ~= mp_respawn_invulnerable then 
  330. 				if mp_respawn_invulnerable == false then 
  331. 					vint_set_property(hostile.invulnerable_anim_0, "is_paused", true) 
  332. 				else 
  333. 					lua_play_anim(hostile.invulnerable_anim_0, 0) 
  334. 				end 
  335. 			end 
  336. 			hostile.mp_respawn_invulnerable = mp_respawn_invulnerable 
  337. 		end 
  338. 		 
  339. 		--dont ever delete this......... 
  340. 		hostile.health_pct = health_pct 
  341. 		 
  342. 		-- For debugging purposes we also store the decoded name 
  343. 		hostile.decoded_name = decoded_name 
  344. 	end 
  345.  
  346. 		 
  347. 	--If you got a remove event, remove the clones	 
  348. 	if event == "remove" and Hud_healthbars_status.hostiles[key] ~= nil then 
  349. --		debug_print("vint_healthbars", "Remove healthbar for " .. decoded_name .. " key: " .. key .. "\n") 
  350. 		local hostile = Hud_healthbars_status.hostiles[key] 
  351. 		vint_object_destroy(hostile.grp_h) 
  352. 		vint_object_destroy(hostile.anim_clone_0) 
  353. 		vint_object_destroy(hostile.cash_fade_clone) 
  354. 		vint_object_destroy(hostile.invulnerable_anim_0) 
  355. 		Hud_healthbars_status.hostiles[key] = nil 
  356. 	end 
  357. 		 
  358. end 
  359.  
  360. -- Prints out info for every entry in the current hostiles list 
  361. -- 
  362. function hud_healthbars_debug_print() 
  363. --	debug_print( "vint_healthbars", "Current healthbar entries:\n" ) 
  364. 	 
  365. --	for key, entry in pairs(Hud_healthbars_status.hostiles) do 
  366. --		debug_print( "vint_healthbars", "\tkey: " .. key .. " name: " .. entry.decoded_name .. "\n" ) 
  367. --	end 
  368. end 
  369.  
  370.  
  371.  
  372.  
  373.