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