sr2lua/hud_mayhem.lua

  1. Hud_mayhem_elements = { 
  2. 	in_game_grp_h = -1, 
  3. 	bonus_grp_h = -1, 
  4. 	bonus_item_grp_h = -1, 
  5. 	bonus_grp_start_x = -1, 
  6. 	bonus_grp_start_y = -1, 
  7. } 
  8.  
  9. Hud_mayhem_anims = { 
  10. 	rise_anim_h = -1, 
  11. 	bonus_item_grp_anim_h = -1 
  12. } 
  13.  
  14. Hud_mayhem_world_cash_status = { 
  15. 	depth = 0, 
  16. 	text_intensity = 0, 
  17. 	color_r = .89, 
  18. 	color_g = .749, 
  19. 	color_b = .05, 
  20. } 
  21. Hud_mayhem_world_cash = {} 
  22.  
  23. Hud_mayhem_bonus_mod_status = { 
  24. 	current_index = 0, 
  25. 	cleared_index = 0, 
  26. 	line_height = 25, 
  27. }  
  28.  
  29. Hud_mayhem_bonus_mods = {} 
  30.  
  31.  
  32. Hud_mayhem_world_cash_colors = { 
  33. 	["WHITE"] 		= { ["r"] = .898, ["g"] = .894, ["b"] = .874}, 	--White 
  34. 	["YELLOW"] 		= { ["r"] = .89, 	["g"] = .749, ["b"] = .05}, 	--Yellow 
  35. 	["ORANGE"]		= { ["r"] = .984, ["g"] = .509, ["b"] = .054}, 	--Orange 
  36. 	["RED"]			= { ["r"] = .780, ["g"] = 0, 	["b"] = .004}, 	--Red 
  37. 	["BLUE"]			= { ["r"] = .26,	["g"] = .501, 	["b"] = .835},  
  38. 	["MID_BLUE"]	= { ["r"] = .145,	["g"] = .419, 	["b"] = .811},  
  39. 	["DARK_BLUE"]	= { ["r"] = .027,	["g"] = .341, 	["b"] = .788}, 
  40. }   
  41.  
  42. function hud_mayhem_init() 
  43.  
  44. 	debug_print("vint", "hud_mayhem_init()\n") 
  45. 	 
  46. 	--Find and store elements 
  47. 	local h = vint_object_find("mayhem_grp") 
  48. 	 
  49. 	--In world cash 
  50. 	Hud_mayhem_elements.in_game_grp_h = vint_object_find("cash_grp") 
  51. 	Hud_mayhem_anims.rise_anim_h = vint_object_find("mayhem_rise_anim") 
  52. 	 
  53. 	--Bonus Items 
  54. 	Hud_mayhem_elements.bonus_grp_h = vint_object_find("bonus_grp", h) 
  55. 	Hud_mayhem_elements.bonus_item_grp_h = vint_object_find("bonus_item_grp", h) 
  56. 	Hud_mayhem_anims.bonus_item_grp_anim_h = vint_object_find("bonus_item_grp_anim") 
  57. 	 
  58. 	--Get initial y value for bonus group. 
  59. 	local x, y = vint_get_property(Hud_mayhem_elements.bonus_grp_h, "anchor") 
  60. 	Hud_mayhem_elements.bonus_grp_start_x = x 
  61. 	Hud_mayhem_elements.bonus_grp_start_y = y 
  62. 	 
  63. 	--Pause Animations 
  64. 	vint_set_property(Hud_mayhem_anims.rise_anim_h, "is_paused", true) 
  65. 	vint_set_property(Hud_mayhem_anims.bonus_item_grp_anim_h, "is_paused", true) 
  66. 	 
  67. 	--Subscribe to data items 
  68. 	vint_datagroup_add_subscription("mayhem_local_player_bonus_modifiers", "update", "hud_mayhem_bonus_mod_update") 
  69. 	vint_datagroup_add_subscription("mayhem_local_player_world_cash", "insert", "hud_mayhem_world_cash_update") 
  70. 	vint_datagroup_add_subscription("mayhem_local_player_world_cash", "update", "hud_mayhem_world_cash_update") 
  71. 	vint_datagroup_add_subscription("mayhem_local_player_world_cash", "remove", "hud_mayhem_world_cash_remove") 
  72. end 
  73.  
  74. --function hud_mayhem_bonus_mod_update(bonus_type, is_multiplier, bonus_text_crc, multiplier, di_h) 
  75.  
  76. function hud_mayhem_bonus_mod_update(di_h) 
  77. 	local bonus_type, is_multiplier, bonus_text_crc, multiplier = vint_dataitem_get(di_h) 
  78.  
  79. 	--[[ 
  80. 	VINT_PROP_TYPE_ENUM            - Bonus type (Vehicle == 0, Weapon == 1, Team == 2) 
  81. 	VINT_PROP_TYPE_BOOL              - True means this is a multiplier, false means this is just a straight cash bonus 
  82. 	VINT_PROP_TYPE_UINT               - The owned string hash for the localized name of what gave the bonus (car/weapon/team name) 
  83. 	VINT_PROP_TYPE_INT                  - Bonus multiplier or cash value depending on the second element 
  84. 	]] 
  85. 	 
  86. 	--Clone object 
  87. 	local grp_h = vint_object_clone(Hud_mayhem_elements.bonus_item_grp_h) 
  88. 	 
  89. 	--Set Initial object properties 
  90. 	vint_set_property(grp_h, "visible", true) 
  91. 	 
  92. 	--calculate its y value 
  93. 	local grp_y = Hud_mayhem_bonus_mod_status.current_index * Hud_mayhem_bonus_mod_status.line_height 
  94. 	vint_set_property(grp_h, "anchor", 0, grp_y) 
  95.  
  96. 	 
  97. 	local x, y = vint_get_property(Hud_mayhem_elements.bonus_grp_h, "anchor") 
  98.  
  99. 	--Clone Animation 
  100. 	local anim_h = vint_object_clone(Hud_mayhem_anims.bonus_item_grp_anim_h) 
  101. 	 
  102. 	--Retarget Animation 
  103. 	vint_set_property(anim_h, "target_handle", grp_h) 
  104. 	 
  105. 	--Set Callback 
  106. 	local callback_twn_h = vint_object_find("cash_txt_alpha_twn_2", anim_h) 
  107. 	vint_set_property(callback_twn_h , "end_event", "hud_mayhem_bonus_mod_destroy") 
  108. 	 
  109. 	--Get text objects 
  110. 	local bonus_cash_txt_h = vint_object_find("bonus_cash_txt", grp_h) 
  111. 	local description_txt_h = vint_object_find("description_txt", grp_h) 
  112. 	 
  113. 	--Set Bonus Text 
  114. 	vint_set_property(description_txt_h, "text_tag_crc", bonus_text_crc) 
  115. 	 
  116. 	if is_multiplier == false then 
  117. 		--Use Cash 
  118. 		local cash = format_cash(multiplier) 
  119. 		vint_set_property(bonus_cash_txt_h, "text_tag", "$" .. cash) 
  120. 	else  
  121. 		--Multiplier 
  122. 		vint_set_property(bonus_cash_txt_h, "text_tag", multiplier .. "x") 
  123. 	end 
  124. 	 
  125. 	--Play Anim 
  126. 	lua_play_anim(anim_h, 0) 
  127. 	 
  128. 	--Store Data 
  129. 	Hud_mayhem_bonus_mods[di_h] = { 
  130. 		idx = Hud_mayhem_bonus_mod_status.current_index, 
  131. 		grp_h = grp_h, 
  132. 		anim_h = anim_h, 
  133. 		callback_twn_h = callback_twn_h, 
  134. 		slide_twn_h = -1, 
  135. 	} 
  136. 	 
  137. 	--Increment index 
  138. 	Hud_mayhem_bonus_mod_status.current_index = Hud_mayhem_bonus_mod_status.current_index + 1 
  139. end 
  140.  
  141. function hud_mayhem_bonus_mod_destroy(twn_h, event) 
  142. 	for idx, val in Hud_mayhem_bonus_mods do 
  143. 		if val.callback_twn_h == twn_h then 
  144. 			--Destroy animation and text object 
  145. 			vint_object_destroy(val.grp_h) 
  146. 			vint_object_destroy(val.anim_h) 
  147. 			 
  148. 			--Increment Cleared Index 
  149. 			Hud_mayhem_bonus_mod_status.cleared_index = Hud_mayhem_bonus_mod_status.cleared_index + 1 
  150. 			 
  151. 			--Destroy stored value 
  152. 			Hud_mayhem_bonus_mods[idx] = nil 
  153. 		end 
  154. 	end 
  155. 	 
  156. 	--Slide the bonus mods up using a tween. 
  157. 	hud_mayhem_bonus_mod_slide() 
  158. end 
  159.  
  160. Hud_mayhem_bon_sl_twn_h = -1	--Reference to the bonus sliding twn 
  161.  
  162. function hud_mayhem_bonus_mod_slide() 
  163. 	 
  164. 	--Need to verify if tween already exists. Destroy it if so. 
  165. 	if Hud_mayhem_bon_sl_twn_h ~= -1 then 
  166. 		vint_object_destroy(Hud_mayhem_bon_sl_twn_h) 
  167. 		Hud_mayhem_bon_sl_twn_h = -1 
  168. 	end 
  169. 		 
  170. 	local bonus_index = Hud_mayhem_bonus_mod_status.cleared_index 
  171. 	 
  172. 	local start_x, start_y = vint_get_property(Hud_mayhem_elements.bonus_grp_h, "anchor") 
  173. 	local target_x = start_x 
  174. 	local target_y = Hud_mayhem_elements.bonus_grp_start_y -(bonus_index * Hud_mayhem_bonus_mod_status.line_height)  
  175. 	 
  176. 	local twn_h = vint_object_create("tween" .. bonus_index, "tween", vint_object_find("root_animation")) 
  177. 	vint_set_property(twn_h, "duration", .2) 
  178. 	vint_set_property(twn_h, "target_handle", Hud_mayhem_elements.bonus_grp_h) 
  179. 	vint_set_property(twn_h, "target_property", "anchor") 
  180. 	vint_set_property(twn_h, "start_value", start_x, start_y) 
  181. 	vint_set_property(twn_h, "end_value", target_x, target_y) 
  182. 	vint_set_property(twn_h, "start_time",	vint_get_time_index()) 
  183. 	vint_set_property(twn_h, "is_paused", false) 
  184. 	vint_set_property(twn_h, "end_event", "hud_mayhem_bonus_mod_slide_end") 
  185.  
  186. 	Hud_mayhem_bon_sl_twn_h = twn_h 
  187. end 
  188.  
  189. function hud_mayhem_bonus_mod_slide_end(twn_h, event) 
  190.  
  191. 	--Destroy Tween and its reference 
  192. 	vint_object_destroy(twn_h) 
  193. 	Hud_mayhem_bon_sl_twn_h = -1 
  194. 	 
  195. 	--Check to see if we can reset the bonus mod positions 
  196. 	local bonus_count = 0 
  197. 	for idx, val in Hud_mayhem_bonus_mods do 
  198. 		bonus_count = bonus_count + 1 
  199. 		break 
  200. 	end 
  201.  
  202. 	if bonus_count == 0 then 
  203. 		--Reset Indexes  
  204. 		Hud_mayhem_bonus_mod_status.current_index = 0 
  205. 		Hud_mayhem_bonus_mod_status.cleared_index = 0 
  206. 		 
  207. 		local x, y = vint_get_property(Hud_mayhem_elements.bonus_grp_h, "anchor") 
  208. 			 
  209. 		--reset bonus group position 
  210. 		vint_set_property(Hud_mayhem_elements.bonus_grp_h, "anchor", Hud_mayhem_elements.bonus_grp_start_x, Hud_mayhem_elements.bonus_grp_start_y) 
  211. 		 
  212. 		local x, y =  vint_get_property(Hud_mayhem_elements.bonus_grp_h, "anchor")	 
  213. 	end 
  214. end 
  215.  
  216. function hud_mayhem_world_cash_update(di_h) 
  217. 	--[[ 
  218. 		VINT_PROP_TYPE_FLOAT          - World position (x) 
  219. 		VINT_PROP_TYPE_FLOAT          - World position (y) 
  220. 		VINT_PROP_TYPE_FLOAT          - World position (z) 
  221. 		VINT_PROP_TYPE_VECTOR2F    	- Screen position 
  222. 		VINT_PROP_TYPE_INT            - Cash value 
  223. 		VINT_PROP_TYPE_FLOAT          - Text intensity 
  224. 		VINT_PROP_TYPE_INT        		- Multiplier value 
  225. 		VINT_PROP_TYPE_INT            - Type 0 = $$, 1 = Points, 2 = Seconds 
  226. 		 
  227. 	]] 
  228. 	local world_x, world_y, world_z, screen_pos_x, screen_pos_y, cash_value, text_intensity, multiplier, display_type = vint_dataitem_get(di_h) 
  229. 								 
  230. 	--debug_print("vint", "hud_mayhem_world_cash_update: di_h " .. di_h .. "\n") 
  231. 	--debug_print("vint", "hud_mayhem_world_cash_update: cash_value $" .. cash_value .. "\n") 
  232.  
  233. 	if display_type == nil then 
  234. 		display_type = 0 
  235. 	end 
  236.  
  237. 	--Don't do anything else if this is a dummy object. 
  238. 	if cash_value == 0 then 
  239. 		--reset text color 
  240. 		hud_mayhem_text_color_change(text_intensity) 
  241. 		return 
  242. 	end 
  243. 	if Hud_mayhem_world_cash[di_h] ~= nil then 
  244. 		--Element already exists... simply update its position and its all good in the hood baby. 
  245. 		vint_set_property(Hud_mayhem_world_cash[di_h].grp_h, "anchor", screen_pos_x, screen_pos_y) 
  246. 	else 
  247. 		--Reset text color 
  248. 		hud_mayhem_text_color_change(text_intensity) 
  249. 		 
  250. 		--Clone New item 
  251. 		local grp_h = vint_object_clone(Hud_mayhem_elements.in_game_grp_h) 
  252. 		local sub_grp_h = vint_object_find("cash_sub_grp", grp_h) 
  253. 		local cash_txt_h = vint_object_find("cash_txt", grp_h) 
  254. 		local multiplier_txt_h = vint_object_find("multiplier_txt", grp_h) 
  255. 		local anim_h = vint_object_clone(Hud_mayhem_anims.rise_anim_h) 
  256. 		 
  257. 		--Reposition, rotate and set depth 
  258. 		vint_set_property(grp_h, "anchor", screen_pos_x, screen_pos_y) 
  259. 		vint_set_property(grp_h, "rotation", rand_float(0.249, -0.249)) 
  260. 		vint_set_property(grp_h, "depth", Hud_mayhem_world_cash_status.depth) 
  261. 		 
  262. 		--Hide the group for the first frame since it takes a frame for tweens to play. 
  263. 		vint_set_property(sub_grp_h, "alpha", 0) 
  264. 		 
  265. 		--Retarget Tweens 
  266. 		vint_set_property(anim_h, "target_handle", grp_h) 
  267. 		 
  268. 		--Randomize Tween Direction 
  269. 		local twn_h = vint_object_find("txt_anchor_twn_1", anim_h) 
  270. 		vint_set_property(twn_h, "end_value", rand_int(-20, 20), rand_int(-0,-30)) 
  271. 		 
  272. 		--Callback tween to kill objects and stuff 
  273. 		local callback_twn_h = vint_object_find("txt_anchor_twn_1", anim_h) 
  274. 		vint_set_property(callback_twn_h, "end_event", "hud_mayhem_cash_destroy") 
  275. 						 
  276. 		--Tint Cash Text 
  277. 		vint_set_property(cash_txt_h, "tint", Hud_mayhem_world_cash_status.color_r, Hud_mayhem_world_cash_status.color_g, Hud_mayhem_world_cash_status.color_b) 
  278. 		 
  279. 		-- +{0}sec   HUD_AMT_SECS 
  280. 		-- +{0}points   HUD_AMT_POINTS 
  281. 		 
  282. 		--HUD_AMT_POINTS 
  283. 		local insertion_text = { [0] = cash_value } 
  284. 		local amt = "" 
  285. 		 
  286. 		if display_type == 0 then 
  287. 			--Cash 
  288. 			amt =  "$" .. cash_value 
  289. 		elseif display_type == 1 then 
  290. 			--Points 
  291. 			amt = vint_insert_values_in_string("HUD_AMT_POINTS", insertion_text) 
  292. 		elseif display_type == 2 then 
  293. 			--Seconds 
  294. 			amt = vint_insert_values_in_string("HUD_AMT_SECS", insertion_text) 
  295. 		elseif display_type == 3 then 
  296. 			--seconds + green 
  297. 			amt = vint_insert_values_in_string("HUD_AMT_SECS", insertion_text) 
  298. 			--Force green text 
  299. 			vint_set_property(cash_txt_h, "tint", 0, 1, 0.25) 
  300. 		end 
  301. 		 
  302. 		vint_set_property(cash_txt_h, "text_tag", amt)	 
  303. 		 
  304.  
  305. 		--Format Text with or without multiplier 
  306. 		if multiplier ~= 0 then 
  307. 			--Show Multiplier and Align text 
  308. 			 
  309. 			--Set MultiplierText Value 
  310. 			vint_set_property(multiplier_txt_h, "text_tag", "X" .. multiplier) 
  311. 			 
  312. 			--Alignment 
  313. 			local spacing = 5 
  314. 			local c_w, c_h = vint_get_property(cash_txt_h, "screen_size") 
  315. 			local c_x, c_y = vint_get_property(cash_txt_h, "anchor") 
  316. 			local m_w, m_h = vint_get_property(multiplier_txt_h, "screen_size") 
  317. 			local m_x, m_y = vint_get_property(multiplier_txt_h, "anchor") 
  318. 			local half_w = (c_w + m_w + spacing) / 2 
  319. 			local c_x = 0 - half_w  
  320. 			local m_x = c_x + c_w + spacing 
  321. 			 
  322. 			--Set Properties 
  323. 			vint_set_property(cash_txt_h, "anchor", c_x, c_y) 
  324. 			vint_set_property(multiplier_txt_h, "anchor", m_x, m_y) 
  325. 		else 
  326. 			--Hide multiplier and center text 
  327. 			 
  328. 			vint_set_property(multiplier_txt_h, "visible", false) 
  329. 			 
  330. 			--Alignment 
  331. 			local c_w, c_h = vint_get_property(cash_txt_h, "screen_size") 
  332. 			local c_x, c_y = vint_get_property(cash_txt_h, "anchor") 
  333. 			local c_x = 0 - (c_w / 2) 
  334. 			 
  335. 			--Set Properties 
  336. 			vint_set_property(cash_txt_h, "anchor", c_x, c_y) 
  337. 		end 
  338. 		 
  339. 		--play tween in animation 
  340. 		lua_play_anim(anim_h, 0) 
  341. 		 
  342. 		--Decrement depth 
  343. 		Hud_mayhem_world_cash_status.depth = Hud_mayhem_world_cash_status.depth - 1 
  344. 		 
  345. 		--Store values and handles to process later 
  346. 		Hud_mayhem_world_cash[di_h] = { 
  347. 			di_h = di_h, 
  348. 			grp_h = grp_h, 
  349. 			sub_grp_h = sub_grp_h, 
  350. 			anim_h = anim_h, 
  351. 			twn_h = callback_twn_h 
  352. 		} 
  353. 		 
  354. 	end 
  355. end 
  356.  
  357. function hud_mayhem_text_color_change(text_intensity) 
  358. 	--Combo Color update 
  359. 	--Prepare color morphing based on intensity 
  360. 	local color1, color2, morph_value 
  361. 	if text_intensity < 0.5 then 
  362. 		if MP_enabled == true then 
  363. 			color1 = Hud_mayhem_world_cash_colors["BLUE"] 
  364. 			color2 = Hud_mayhem_world_cash_colors["MID_BLUE"] 
  365. 		else 
  366. 			color1 = Hud_mayhem_world_cash_colors["YELLOW"] 
  367. 			color2 = Hud_mayhem_world_cash_colors["ORANGE"] 
  368. 		end 
  369. 		morph_value = text_intensity / 0.5 
  370. 	else 
  371. 		if text_intensity > 1 then  
  372. 			text_intensity = 1 
  373. 		end 
  374. 		 
  375. 		if MP_enabled == true then 
  376. 			color1 = Hud_mayhem_world_cash_colors["MID_BLUE"] 
  377. 			color2 = Hud_mayhem_world_cash_colors["DARK_BLUE"] 
  378. 		else 
  379. 			color1 = Hud_mayhem_world_cash_colors["ORANGE"] 
  380. 			color2 = Hud_mayhem_world_cash_colors["RED"] 
  381. 		end 
  382. 		morph_value = (text_intensity - 0.5) / 0.5 
  383. 	end 
  384. 	 
  385. 	Hud_mayhem_world_cash_status.color_r = color1.r - ((color1.r - color2.r) * morph_value) 
  386. 	Hud_mayhem_world_cash_status.color_g = color1.g - ((color1.g - color2.g) * morph_value) 
  387. 	Hud_mayhem_world_cash_status.color_b = color1.b - ((color1.b - color2.b) * morph_value) 
  388. 	Hud_mayhem_world_cash_status.text_intensity = text_intensity 
  389. end 
  390.  
  391. function hud_mayhem_world_cash_remove(di_h) 
  392. 	--Destroy objects, animation and values 
  393. 	if Hud_mayhem_world_cash[di_h] ~= nil then 
  394. 		--Destroy animation 
  395. 		vint_object_destroy(Hud_mayhem_world_cash[di_h].anim_h) 
  396. 		--Destroy group object 
  397. 		vint_object_destroy(Hud_mayhem_world_cash[di_h].grp_h) 
  398. 		--Destroy stored values 
  399. 		Hud_mayhem_world_cash[di_h] = nil	 
  400. 	end 
  401. end 
  402.  
  403. --Destroys world cash text 
  404. function hud_mayhem_cash_destroy(twn_h, event) 
  405. 	for idx, val in Hud_mayhem_world_cash do 
  406. 		if val.twn_h == twn_h then 
  407. 		 
  408. 			--Destroy animation, text object and Data item 
  409. 			vint_object_destroy(val.anim_h) 
  410. 			vint_object_destroy(val.grp_h) 
  411. 			if val.di_h ~= -1 then 
  412. 				vint_datagroup_remove_item("mayhem_local_player_world_cash", val.di_h) 
  413. 			end 
  414. 			 
  415. 			--Destroy stored values 
  416. 			Hud_mayhem_world_cash[idx] = nil	 
  417. 		end 
  418. 	end 
  419. 	 
  420. 	--reset values if this is the last cash item 
  421. 	local cash_item_count = 0 
  422. 	local is_last_item = true 
  423. 	for idx, val in Hud_mayhem_world_cash do 
  424. 		cash_item_count = cash_item_count + 1 
  425. 		is_last_item = false 
  426. 		break 
  427. 	end 
  428. end 
  429.  
  430.  
  431. function hud_mayhem_bonus_test() 
  432. 	local bonus_type = 0 
  433. 	local is_multiplier = false 
  434. 	local bonus_text_crc = "Vehicle Bonus" 
  435. 	local multiplier = rand_int(2000, 3000) 
  436. 	local di_h = rand_int(0, 10000) 
  437. 	hud_mayhem_bonus_mod_update(bonus_type, is_multiplier, bonus_text_crc, multiplier, di_h) 
  438. end 
  439.