./hud_collection.lua

  1. local	HUD_COLLECTION_FLASHPOINT		= 0 
  2. local	HUD_COLLECTION_COLLECTIBLE_GENERIC      = 1 
  3. local	HUD_COLLECTION_COLLECTIBLE_PHOTO_OP	= 2 
  4. local	HUD_COLLECTION_HITMAN			= 3 
  5. local	HUD_COLLECTION_CHOP_SHOP		= 4 
  6. local	HUD_COLLECTION_STUNT_JUMP		= 5 
  7. local	HUD_COLLECTION_BARNSTORMING		= 6 
  8. local	HUD_COLLECTION_SURVIVAL			= 7 
  9. local	HUD_COLLECTION_SECRET			= 8 
  10. local	HUD_COLLECTION_STAG			= 9 
  11.  
  12. local	STATS_INVALID = -1 
  13.  
  14. local	DT_NONE				= -1 
  15. local	DT_CHOP_SHOP		= 2 
  16. local	DT_BARNSTORMING	= 4 
  17. local DT_EXPLORATION		= 5 
  18. local	DT_FLASHPOINT		= 6 
  19. local	DT_HITMAN			= 7 
  20. local	DT_SURVIVAL			= 12 
  21.  
  22. local Hud_collection_types = { 
  23. 	[HUD_COLLECTION_FLASHPOINT] =		{ 
  24. 		audio = "ui_hud_gang_flash_complete", 
  25. 		d_type = DT_FLASHPOINT, 
  26. 	}, 
  27. 	[HUD_COLLECTION_COLLECTIBLE_GENERIC] =	{ 
  28. 		audio = "ui_hud_generic_div_complete", 
  29. 		d_type = DT_NONE, 
  30. 	}, 
  31. 	[HUD_COLLECTION_COLLECTIBLE_PHOTO_OP] =	{ 
  32. 		audio = "ui_hud_photo_op_complete", 
  33. 		d_type = DT_NONE, 
  34. 	}, 
  35. 	[HUD_COLLECTION_HITMAN] =		{ 
  36. 		audio = "ui_hud_hitman_complete", 
  37. 		d_type = DT_HITMAN, 
  38. 	}, 
  39. 	[HUD_COLLECTION_CHOP_SHOP] =		{ 
  40. 		audio = "ui_hud_chop_shop_complete", 
  41. 		d_type = DT_CHOP_SHOP, 
  42. 	}, 
  43. 	[HUD_COLLECTION_STUNT_JUMP] =		{ 
  44. 		d_type = DT_NONE, 
  45. 	}, 
  46. 	[HUD_COLLECTION_BARNSTORMING] =		{ 
  47. 		d_type = DT_BARNSTORMING, 
  48. 	}, 
  49. 	[HUD_COLLECTION_SURVIVAL] =		{ 
  50. 		audio = "ui_hud_survival_complete", 
  51. 		d_type = DT_SURVIVAL, 
  52. 	}, 
  53. 	[HUD_COLLECTION_SECRET] =		{ 
  54. 		d_type = DT_EXPLORATION, 
  55. 	}, 
  56. 	[HUD_COLLECTION_STAG] =		{ 
  57. 		audio = "ui_hud_generic_div_complete", 
  58. 		d_type = DT_NONE, 
  59. 	} 
  60. } 
  61.  
  62. local Hud_collection_anims = {} 
  63. local Hud_collection_data = {} 
  64. local Hud_collection_initialized = false 
  65. local Hud_collection_is_active = false 
  66. local Current_collection_index = 0 
  67. local Num_collection_data = 0 
  68. local Queued_collection_data = { } 
  69.  
  70. local Hud_collection_control_meter 				= 0		--Control meter object... 
  71. local Hud_collection_respect_meter 				= 0		--Respect meter object... 
  72. local Hud_collection_cash_h 					 	= 0		--Cash Display... 
  73.  
  74. local HUD_COLLECTION_ICON_WIDTH 			= 64 
  75. local HUD_COLLECTION_CONTROL_WIDTH		= 84 
  76. local HUD_COLLECTION_RESPECT_WIDTH 		= 84 
  77. local HUD_COLLECTION_MAX_WIDTH 			= 818 
  78. local HUD_COLLECTION_HEADER_MAX_WIDTH 	= 495 
  79.  
  80. ------------------------------------------------------------------------------- 
  81. -- This is included with hud.vint_doc 
  82. ------------------------------------------------------------------------------- 
  83. function hud_collection_init() 
  84. 	vint_dataitem_add_subscription("collection_hud_element", "update", "hud_collection_update") 
  85. 	vint_dataitem_add_subscription("game_paused_item", "update", "hud_collection_game_is_paused")	--to check if game is paused... 
  86. 	 
  87. 	local h = vint_object_find("collection_grp") 
  88. 	vint_set_property(h, "visible", false) 
  89. 	 
  90. 	--Animations stored to globals 
  91. 	Hud_collection_anims.in_anim_h 			= vint_object_find("collection_in_anim") 
  92. 	Hud_collection_anims.district_anim_h		= vint_object_find("collection_district_anim") 
  93. 	Hud_collection_anims.respect_anim_h 		= vint_object_find("collection_respect_anim") 
  94. 	Hud_collection_anims.cash_anim_h 		= vint_object_find("collection_cash_anim") 
  95. 	Hud_collection_anims.complete_anim_h		= vint_object_find("collection_complete_anim") 
  96. 	 
  97. 	-- Setup callbacks to tweens.... 
  98. 	local control_twn_h = vint_object_find("collection_control_twn") 
  99. 	local respect_twn_h = vint_object_find("collection_respect_twn") 
  100. 	local cash_twn_h = vint_object_find("collection_cash_twn") 
  101. 	local collection_fade_out_twn_h = vint_object_find("collection_fade_out_twn") 
  102. 	 
  103. 	vint_set_property(control_twn_h, "end_event", "hud_collection_start_control") 
  104. 	vint_set_property(respect_twn_h, "start_event", "hud_collection_show_respect") 
  105. 	vint_set_property(respect_twn_h, "end_event", "hud_collection_start_respect") 
  106. 	vint_set_property(cash_twn_h, "start_event", "hud_collection_start_cash") 
  107. 	vint_set_property(collection_fade_out_twn_h, "start_event", "hud_collection_audio_out")		--audio play out... 
  108. 	vint_set_property(collection_fade_out_twn_h, "end_event", "hud_collection_finished") 
  109.  
  110. 	--Initialize control meter. 
  111. 	Hud_collection_control_meter = Vdo_city_control:new("collection_control_meter") 
  112. 	 
  113. 	--Initialize respect meter. 
  114. 	Hud_collection_respect_meter = Vdo_city_control:new("collection_respect_meter") 
  115. 	Hud_collection_respect_meter:change_to_respect() 
  116.  
  117. 	Hud_collection_cash_h = vint_object_find("collection_cash_txt") 
  118. 	 
  119. 	local collection_hud_bg_tile_h = vint_object_find("collection_hud_bg_tile") 
  120. 	local collection_hud_bg_tile_twn = vint_object_find("collection_hud_bg_tile_twn") 
  121. 	vint_set_property(collection_hud_bg_tile_twn, "duration", 30) 
  122. 	vint_set_property(collection_hud_bg_tile_twn, "loop_mode", "cycle") 
  123. end 
  124.  
  125. ------------------------------------------------------------- 
  126. -- Fills in the data object for populating the screen... 
  127. ------------------------------------------------------------- 
  128. --		VINT_PROP_TYPE_INT,           // Collection type 
  129. --		VINT_PROP_TYPE_UINT,          //    Title of the Collection message ("Gang Operation", "Hitman", "Chopshop")  
  130. --		VINT_PROP_TYPE_UINT,          //    Subtitle (Destroyed, Completed, Found) 
  131. --		VINT_PROP_TYPE_INT,           //    How many items the player has left in district or game 
  132. --		VINT_PROP_TYPE_INT,           // Total items in district or game 
  133. --		 
  134. --		// if the following are nil then we omit their display... 
  135. --		VINT_PROP_TYPE_FLOAT,         //    Cash Reward 
  136. --		VINT_PROP_TYPE_INT,           // Respect Reward  
  137. --		VINT_PROP_TYPE_FLOAT,  		 	//    District percentage before... 
  138. --		VINT_PROP_TYPE_FLOAT,   		// District percentage after... 
  139. --		VINT_PROP_TYPE_STRING,  		// DISTRICT name 
  140. --		VINT_PROP_TYPE_BOOL,				// Whether the rewards should be saved on mission failure/restart 
  141. --		VINT_PROP_TYPE_BOOL,				// whether to award cash / respect 
  142.  
  143. function hud_collection_update(di_h) 
  144. 	local collection_type, title, sub_title, items_left, items_total, cash_reward, respect_reward, district_pct_old, district_pct_new, district_name, save_in_mission, no_reward = vint_dataitem_get(di_h) 
  145. 	 
  146. 	if Hud_collection_initialized == false then 
  147. 		--This gets called on subscription but we have nothing to process at boot... so we exit. 
  148. 		Hud_collection_initialized = true 
  149. 		return 
  150. 	end 
  151.  
  152. 	--determine what kind of collection type we are and do some shit based on that... 
  153. 	local collection_type = collection_type 
  154.  
  155. 	if title == 0 then 
  156. 		--No data... Abort Collection screen. 
  157. 		return 
  158. 	end 
  159. 	local gained_pct = district_pct_new - district_pct_old	 
  160. 	 
  161. 	--Queue up any data into memory... 
  162. 	Queued_collection_data[Num_collection_data] = { 
  163. 		collection_type = collection_type, 
  164. 		title = title,  
  165. 		sub_title = sub_title,  
  166. 		items_left = items_left,  
  167. 		items_total = items_total,  
  168. 		cash_reward = cash_reward,  
  169. 		respect_reward = respect_reward,  
  170. 		district_pct_old = district_pct_old,  
  171. 		district_pct_new = district_pct_new,  
  172. 		district_name = district_name, 
  173. 		save_in_mission = save_in_mission, 
  174. 		no_reward = no_reward, 
  175. 	} 
  176.  
  177. 	Num_collection_data = Num_collection_data + 1 
  178. 	if Num_collection_data == 1 then 
  179. 		hud_collection_start() 
  180. 	end 
  181. end 
  182.  
  183.  
  184. function hud_collection_start() 
  185. 	Hud_collection_is_active = true 
  186. 	Hud_collection_data = Queued_collection_data[Current_collection_index] 
  187. 	Current_collection_index = Current_collection_index + 1 
  188. 	 
  189. 	--Determine what rewards to display... 
  190. 	Hud_collection_data.has_district 	= true 
  191. 	Hud_collection_data.has_respect	 	= true 
  192. 	Hud_collection_data.has_cash			= true 
  193. 	 
  194. 	if Hud_collection_data.district_pct_old == Hud_collection_data.district_pct_new then 
  195. 		--No district 
  196. 		Hud_collection_data.has_district = false 
  197. 	end 
  198. 	 
  199. 	if Hud_collection_data.respect_reward == 0 then 
  200. 		--No cash reward... 
  201. 		Hud_collection_data.has_respect = false 
  202. 	end 
  203. 	 
  204. 	if Hud_collection_data.cash_reward == 0 then 
  205. 		--No cash reward... 
  206. 		Hud_collection_data.shas_cash = false 
  207. 	end 
  208. 	 
  209. 	--All reward elements start out hidden... 
  210. 	Hud_collection_control_meter:set_alpha(0) 
  211. 	Hud_collection_respect_meter:set_alpha(0) 
  212. 	vint_set_property(Hud_collection_cash_h, "alpha", 0) 
  213. 	 
  214. 	 
  215. 	--Initialize Control meter... 
  216. 	Hud_collection_control_meter:update(Hud_collection_data.district_pct_old, false) 
  217. 	 
  218. 	--Do update of text items... 
  219. 	local header_txt_h = vint_object_find("collection_header_txt") 
  220. 	local insert_values = { [0] = Hud_collection_data.title, [1] = Hud_collection_data.items_left, [2] = Hud_collection_data.items_total} 
  221. 	 
  222. 	local text_string 
  223. 	if Hud_collection_data.collection_type == HUD_COLLECTION_STAG then 
  224. 		text_string = Hud_collection_data.title 
  225. 	else 
  226. 		text_string = vint_insert_values_in_string("{0} {1}/{2}", insert_values) 
  227. 	end 
  228. 	 
  229. 	vint_set_property(header_txt_h, "text_tag", text_string) 
  230. 	 
  231. 	local sub_txt_h = vint_object_find("collection_sub_txt") 
  232. 	vint_set_property(sub_txt_h, "text_tag", Hud_collection_data.sub_title) 
  233.  
  234. 	--Set cash total... 
  235. 	vint_set_property(Hud_collection_cash_h, "text_tag", "$" .. format_cash(Hud_collection_data.cash_reward)) 
  236. 	 
  237. 	--calculate widths of headers and cash 
  238. 	local header_width, header_height = element_get_actual_size(header_txt_h) 
  239. 	local header_sub_width, header_sub_height = element_get_actual_size(sub_txt_h) 
  240. 	 
  241. 	--Need to scale but by how much? 
  242. 	local new_header_width = max(header_width, header_sub_width) 
  243. 	local scale = 1.0 
  244. 	local max_width = HUD_COLLECTION_HEADER_MAX_WIDTH 
  245. 	 
  246. 	if header_width > header_sub_width then 
  247. 		if header_width > max_width then 
  248. 			scale = max_width / header_width 
  249. 			new_header_width = header_width * scale 
  250. 		end 
  251. 	else 
  252. 		if header_sub_width > max_width then 
  253. 			scale =  max_width / header_sub_width 
  254. 			new_header_width = header_sub_width * scale 
  255. 		end 
  256. 	end 
  257. 	 
  258. 	--Apply scale to whole text group... 
  259. 	local collection_title_grp_h = vint_object_find("collection_title_grp") 
  260. 	vint_set_property(collection_title_grp_h, "scale", scale, scale) 
  261. 	 
  262. 	local pos_y = 102							--Start position for reward elements... 
  263. 	local start_time = 1.5					--start offset for first reward... 
  264. 	local start_time_spacing = 1.2		--Time between rewards.. 
  265. 	local header_width_min = 0 			-- min size of element 		 
  266. 	local HUD_COLLECTION_TEXT_SPACING = 15 
  267. 	--positions 
  268. 	local title_x, title_y = vint_get_property(collection_title_grp_h, "anchor") 
  269. 	local x, y = Hud_collection_control_meter:get_anchor() 
  270. 	x = title_x + new_header_width + HUD_COLLECTION_TEXT_SPACING  
  271. 	 
  272. 	--Positions for elements... 
  273. 	local control_x 	= 0					 
  274. 	local respect_x 	= 0 
  275. 	local cash_x 		= 0 
  276. 	 
  277. 	--Start times for elements... 
  278. 	local control_start_time 	= 0 
  279. 	local respect_start_time 	= 0 
  280. 	local cash_start_time 		= 0  
  281. 	local complete_start_time 	= 0 
  282. 	 
  283. 	local HUD_COLLECTION_ITEM_SPACING = 10 
  284. 	 
  285. 	--Determine what we have and then calculate each item based on that... 
  286. 	if Hud_collection_data.has_district then 
  287. 		local half_width = (HUD_COLLECTION_CONTROL_WIDTH *.5) 
  288. 		control_x = x + half_width 
  289. 		x = x + HUD_COLLECTION_CONTROL_WIDTH + HUD_COLLECTION_ITEM_SPACING 
  290. 		 
  291. 		control_start_time = start_time 
  292. 		start_time =  start_time + start_time_spacing 
  293. 	end 
  294. 	 
  295. 	if Hud_collection_data.has_respect then 
  296. 		local half_width = (HUD_COLLECTION_RESPECT_WIDTH *.5) 
  297. 		respect_x = x + half_width 
  298. 		x = x + HUD_COLLECTION_RESPECT_WIDTH + HUD_COLLECTION_ITEM_SPACING 
  299. 	 
  300. 		respect_start_time = start_time 
  301. 		start_time =  start_time + start_time_spacing 
  302. 	end 
  303. 	 
  304. 	if Hud_collection_data.has_cash then 
  305. 		local cash_width, cash_height = element_get_actual_size(Hud_collection_cash_h) 
  306. 		local half_width = (cash_width *.5) 
  307. 		cash_x = x  + half_width 
  308. 		x = x + cash_width + HUD_COLLECTION_ITEM_SPACING		 
  309. 		 
  310. 		cash_start_time = start_time 
  311. 	end 
  312.  
  313.  
  314. 	local total_width = x							--total width of the element is x....	 
  315. 	complete_start_time = start_time + 3		--Delay close out animation by 3 seconds... 
  316.  
  317. 	--Set positions... 
  318. 	Hud_collection_control_meter:set_anchor(control_x, y) 
  319. 	Hud_collection_respect_meter:set_anchor(respect_x, y) 
  320. 	vint_set_property(Hud_collection_cash_h, "anchor", cash_x, y) 
  321. 	 
  322. 	local collection_elements_grp_h = vint_object_find("collection_elements_grp") 
  323. 	local collection_x, collection_y = vint_get_property(collection_elements_grp_h, "anchor") 
  324. 	collection_x = 640 - (total_width * 0.5) 
  325. 	vint_set_property(collection_elements_grp_h, "anchor", collection_x, collection_y) 
  326. 	 
  327. 	--Unhide 
  328. 	local h = vint_object_find("collection_grp") 
  329. 	vint_set_property(h, "visible", true) 
  330. 	 
  331. 	 
  332. 	--Playback animations at different times... 
  333. 	 
  334. 	-- Play back main animation.... 
  335. 	lua_play_anim(Hud_collection_anims.in_anim_h, 0) 
  336. 	 
  337. 	ui_audio_post_event("ui_generic_band_anim_in") 
  338. 			 
  339. 	--Play back animations... but only if they are needed... this is to prevent bad callbacks... 
  340. 	if Hud_collection_data.has_district then 
  341. 		lua_play_anim(Hud_collection_anims.district_anim_h, control_start_time) 
  342. 	end 
  343. 	if Hud_collection_data.has_respect then 
  344. 		lua_play_anim(Hud_collection_anims.respect_anim_h, respect_start_time) 
  345. 	end 
  346. 	if Hud_collection_data.has_cash then 
  347. 		lua_play_anim(Hud_collection_anims.cash_anim_h, cash_start_time) 
  348. 	end 
  349. 	 
  350. 	--This animation closes it out... 
  351. 	lua_play_anim(Hud_collection_anims.complete_anim_h, complete_start_time) 
  352. 		 
  353. 	local collection_hud_bg_tile_h = vint_object_find("collection_hud_bg_tile") 
  354. 	lua_play_anim(collection_hud_bg_tile_h) 
  355. 	 
  356. 	--Get audio string from collection type... 
  357. 	local audio = Hud_collection_types[Hud_collection_data.collection_type].audio 
  358. 	if audio ~= nil then 
  359. 		--Play event... 
  360. 		audio_object_post_event(audio, nil, nil, nil, false) 
  361. 	end 
  362. end 
  363.  
  364. function hud_collection_audio_out() 
  365. 	ui_audio_post_event("ui_generic_band_anim_out") 
  366. end 
  367.  
  368. function hud_collection_finished() 
  369.  
  370. 	--pause looping bg animation.. 
  371. 	local collection_hud_bg_tile_h = vint_object_find("collection_hud_bg_tile") 
  372. 	vint_set_property(collection_hud_bg_tile_h, "is_paused", true) 
  373. 	 
  374. 	--award the stuff for real... 
  375. 	if Hud_collection_data.has_respect and Hud_collection_data.no_reward ~= true then 
  376. 		game_award_respect(Hud_collection_data.respect_reward, STATS_INVALID, Hud_collection_types[Hud_collection_data.collection_type].d_type, Hud_collection_data.save_in_mission)		 
  377. 	end 
  378. 	if Hud_collection_data.has_cash and Hud_collection_data.no_reward ~= true then 
  379. 		game_award_cash(Hud_collection_data.cash_reward, Hud_collection_types[Hud_collection_data.collection_type].d_type, Hud_collection_data.save_in_mission)			 
  380. 	end 
  381.  
  382. 	if Current_collection_index == Num_collection_data then 
  383. 		Num_collection_data = 0 
  384. 		Current_collection_index = 0 
  385. 		Queued_collection_data = { } 
  386. 		Hud_collection_is_active = false 
  387. 		 
  388. 		--only save after last collection is done... 
  389. 		game_autosave() 
  390. 	else  
  391. 		hud_collection_start() 
  392. 	end 
  393. end 
  394.  
  395. ------------------------------------------------------------- 
  396. -- Starts control meter fill/update (done via callback) 
  397. ------------------------------------------------------------- 
  398. function hud_collection_start_control() 
  399. 	Hud_collection_control_meter:update(Hud_collection_data.district_pct_new, true) 
  400. end 
  401.  
  402. ------------------------------------------------------------- 
  403. -- Makes respect meter visible....(done via callback) 
  404. ------------------------------------------------------------- 
  405. function hud_collection_show_respect() 
  406. 	--Query respect meter for current values... 
  407. 	local respect_total, respect_pct, respect_level = Hud_respect:get_data() 
  408.  
  409. 	--Initialize meter... 
  410. 	Hud_collection_respect_meter:update_respect(Hud_collection_data.respect_reward, respect_pct, respect_level, false) 
  411. end 
  412.  
  413. ------------------------------------------------------------- 
  414. -- Starts respect meter fill/update (done via callback) 
  415. ------------------------------------------------------------- 
  416. function hud_collection_start_respect() 
  417.  
  418. 	-- Query for the new values... 
  419. 	local respect_total_new, respect_pct_new, respect_level_new  = Hud_respect:get_data() 
  420.  
  421. 	--Start animationg the meter... 
  422. 	Hud_collection_respect_meter:update_respect(Hud_collection_data.respect_reward, respect_pct_new, respect_level_new, true) 
  423. end 
  424.  
  425. ------------------------------------------------------------- 
  426. -- Starts cash count/update (done via callback) 
  427. ------------------------------------------------------------- 
  428. function hud_collection_start_cash() 
  429. 	hud_collection_animate_cash()		 
  430. end 
  431.  
  432. ----------------------------------------- 
  433. --Cash count... 
  434. ----------------------------------------- 
  435. function hud_collection_animate_cash() 
  436.    thread_new("hud_collection_anim_cash_thread") 
  437. end 
  438.  
  439. function hud_collection_anim_cash_thread() 
  440. 	--init stuff 
  441. 	local start_cash = 0 
  442. 	local cash_this_frame = -1 
  443. 	local is_complete = false 
  444.  
  445. 	--get the variables from the global 
  446. 	local cash = Hud_collection_data.cash_reward 
  447.  
  448. 	local amt_min = 100 
  449. 	local amt_max = 5000 
  450. 	 
  451. 	local time_min = 300 
  452. 	local time_max = 2999 
  453. 	local init_time = floor(vint_get_time_index() * 1000) 
  454. 	local cur_time = init_time 
  455. 	local time_to_count = floor(time_min + ((time_max - time_min) * (cash / amt_max))) 
  456. 	 
  457. 	if time_to_count > time_max then 
  458. 		time_to_count = time_max 
  459. 	end 
  460. 	 
  461. 	--init sound IDs 
  462. 	local activity_cash_count = 0 
  463. 	local activity_cash_hit = 0 
  464.  
  465. 	while is_complete == false do 
  466. 		cur_time = floor(vint_get_time_index() * 1000) - init_time 
  467. 		 
  468. 		--set my values 
  469. 		cash_this_frame = cash * (cur_time / time_to_count) 
  470. 		vint_set_property(Hud_collection_cash_h, "text_tag", "$" .. format_cash(cash_this_frame)) 
  471. 		 
  472. 		if cur_time >= time_to_count then 
  473. 			--game_audio_stop(activity_cash_count) 
  474. 			--activity_cash_hit = game_audio_play(Completion_audio.cash_hit) 
  475. 			vint_set_property(Hud_collection_cash_h, "text_tag", "$" .. format_cash(cash)) 
  476. 			is_complete = true 
  477. 		end 
  478. 		thread_yield() 
  479. 	end		 
  480. end 
  481.  
  482. ------------------------------------------------------------------------------- 
  483. -- Pausing support for hud collection... 
  484. -- 
  485. function hud_collection_game_is_paused(di_h) 
  486. 	local is_paused = vint_dataitem_get(di_h) 
  487. 	if Hud_collection_is_active then 
  488. 		if is_paused == true then 
  489. 			--Stop anims 
  490. 			vint_set_property(Hud_collection_anims.complete_anim_h, "is_paused", true) 
  491. 		else 
  492. 			--Play all anims 
  493. 			vint_set_property(Hud_collection_anims.complete_anim_h, "is_paused", false) 
  494. 		end 
  495. 	end 
  496. end