./building_purchase.lua

  1.  
  2.  
  3. local Building_purchase_subs 		= { } 
  4. local Building_purchasing_data 	= { } 
  5. local Building_purchase_is_crib	= false 
  6. local Menu_sound_confirm			= game_audio_get_audio_id("SYS_HUD_CONF_SERIOUS")	-- Confirmation/warning dialog opens 
  7. local Building_purchase_sound 	= game_audio_get_audio_id("SYS_MENU_BUY_BUILDING") 
  8.  
  9. local Mouse_input_tracker 
  10. local Bg_mask 
  11.  
  12. local Input_tracker = {} 
  13.  
  14. local City_control_meter = -1 
  15.  
  16. function building_purchase_init() 
  17.  
  18. 	-- TODO: RIP THIS OUT OF CODE?... or figure out if we need it?? (JMH 6/24/2010) 
  19. 	Building_purchase_is_crib = building_purchase_is_crib_being_purchased() 
  20. 	 
  21. 	--Find and hide objects until we need them... 
  22. 	vint_set_property(vint_object_find("ctrl_info_grp"), "visible", false) 
  23. 	vint_set_property(vint_object_find("guns"), "visible", false) 
  24. 	vint_set_property(vint_object_find("main"), "visible", false) 
  25. 	 
  26. 	Input_tracker = Vdo_input_tracker:new() 
  27. 	Input_tracker:add_input("pause", "building_purchase_pause", 50) 
  28. 	 
  29. 	bg_saints_show(false) 
  30.  
  31. 	--Initialize VDO for district control. 
  32. 	City_control_meter = Vdo_city_control:new("control_meter") 
  33. 	 
  34. 	Building_purchase_subs[0] = vint_subscribe_to_input_event("all_unassigned",	"bp_nothing") 
  35. 	 
  36. 	if Building_purchase_is_crib == true then 
  37. 		vint_dataresponder_request("crib_purchase_get_crib_info", "update_building_purchase_text", 0) 
  38. 	else 
  39. 		vint_dataresponder_request("shop_purchase_get_shop_info", "update_building_purchase_text", 0) 
  40. 	end 
  41. 	 
  42. 	--Set bg saints screen type... 
  43. 	bg_saints_set_type(BG_TYPE_COMPLETION) 
  44. 	 
  45. 	if game_get_platform() == "PC" then 
  46. 		Mouse_input_tracker = Vdo_input_tracker:new() 
  47. 		local doc_handle = vint_document_find("building_purchase") 
  48. 		Bg_mask = Vdo_base_object:new("bg_clickable", 0, doc_handle) 
  49. 		Mouse_input_tracker:add_mouse_input("mouse_move", "building_purchase_mouse_move", 0, Bg_mask.handle) 
  50. 		Mouse_input_tracker:add_mouse_input("mouse_click", "building_purchase_mouse_click", 0, Bg_mask.handle) 
  51. 		Mouse_input_tracker:subscribe(false) 
  52. 	end 
  53. end 
  54.  
  55. function building_purchase_pause(event) 
  56. 	dialog_open_pause_display() 
  57. end 
  58.  
  59. function building_purchase_clean() 
  60. 	game_UI_audio_play("UI_Main_Menu_Select") 
  61. 	 
  62. 	--Autosave after building purchase... 
  63. 	game_autosave() 
  64. 	 
  65. 	pop_screen() 
  66. end 
  67.  
  68. function building_not_enough_money() 
  69. 	pop_screen() 
  70. end 
  71.  
  72. function building_purchase_cleanup() 
  73. 	for index, value in pairs(Building_purchase_subs) do 
  74. 		vint_unsubscribe_to_input_event(value) 
  75. 	end 
  76. 	 
  77. -- DAD - 8/12/11 - Clean up is called from vint_doc destroy... we don't want to pop extra screens. 
  78. --	pop_screen() 
  79. end 
  80.  
  81. -- Percentages should be sent in as numbers between 0 and 100, cause we'll floor them an print as text 
  82. function update_building_purchase_text(building_name, description_txt, store_cost, player_cash, profit, control_pct, control_pct_new, control_pct_increase, district, discount, is_store) 
  83. 	Building_purchasing_data.name = building_name 
  84. 	Building_purchasing_data.description_txt = description_txt 
  85. 	Building_purchasing_data.store_cost = store_cost 
  86. 	Building_purchasing_data.player_cash = player_cash 
  87. 	Building_purchasing_data.control_pct = control_pct 
  88. 	Building_purchasing_data.control_pct_new = control_pct_new 
  89. 	Building_purchasing_data.control_pct_increase = control_pct_increase 
  90. 	Building_purchasing_data.control_district = district 
  91. 	Building_purchasing_data.discount = discount 
  92. 	Building_purchasing_data.is_store = is_store 
  93. 	 
  94. 	if Building_purchase_is_crib == false then 
  95. 		Building_purchasing_data.profit = profit 
  96. 	end 
  97. 	 
  98. 	--Set control Percentage to start with...  
  99. 	if Building_purchasing_data.control_pct == nil then 
  100. 		Building_purchasing_data.control_pct = 0 
  101. 		debug_print("vint", "warning: no control percentage assigned to this property/building\n") 
  102. 	end 
  103. 	 
  104. 	if Building_purchasing_data.control_pct_new == nil then 
  105. 		Building_purchasing_data.control_pct_new = 0 
  106. 		debug_print("vint", "warning: no control percentage assigned to this property/building\n") 
  107. 	end 
  108. 	 
  109. 	if Building_purchasing_data.profit == nil then 
  110. 		Building_purchasing_data.profit = 10 
  111. 		debug_print("vint", "warning: no profit assigned to this property/building\n") 
  112. 	end 
  113. 	 
  114. 	if Building_purchasing_data.control_district == nil then 
  115. 		Building_purchasing_data.control_district = "EMPTY DISTRICT NAME" 
  116. 		debug_print("vint", "warning: no district name assigned to this property/building\n") 
  117. 	end 
  118. 	 
  119. 	if Building_purchasing_data.discount == nil then 
  120. 		Building_purchasing_data.discount = 10 
  121. 		debug_print("vint", "warning: no profit assigned to this property/building\n") 
  122. 	end		 
  123. 	 
  124. 	if player_cash < store_cost then 
  125. 		-- Show Dialog that says you can't afford it. 
  126. 		local header = "MENU_TITLE_NOTICE" 
  127. 		local body = "HUD_SHOP_INSUFFICIENT_FUNDS" 
  128.  
  129. 		dialog_box_message(header, body, nil, nil, "building_not_enough_money") 
  130. 	else 
  131. 		--	Ask player if they really want to purchase it. 
  132. 		local header = "MENU_TITLE_CONFIRM" 
  133. 		local body 
  134. 		local insert_values = { [0] = building_name, [1] = format_cash(store_cost) } 
  135. 		if Building_purchase_is_crib == true then 
  136. 			body = vint_insert_values_in_string("BUILDING_PURCHASE_CRIB", insert_values) 
  137. 		else 
  138. 			body = vint_insert_values_in_string("BUILDING_PURCHASE_BUILDING", insert_values) 
  139. 		end 
  140. 		dialog_box_confirmation(header, body, "building_purchased") 
  141. 	end 
  142. end 
  143.  
  144. function building_purchased(result, action)	 
  145. 	if result == 0 then 
  146. 	 
  147. 		--Set Text Items and Prepare animations... Starts from top of the screen to bottom... GO!!! 
  148. 	 
  149. 		--Store Title 
  150. 		local store_title_txt_h = vint_object_find("store_title_txt") 
  151. 		vint_set_property(store_title_txt_h, "text_tag_crc", Building_purchasing_data.name) 
  152. 				 
  153. 		--Resize long strings to fit 
  154. 		vint_set_property(store_title_txt_h, "scale", 1, 1) 
  155. 		local max_width = 442 
  156. 		local text_width, text_height = element_get_actual_size(store_title_txt_h) 
  157. 		 
  158. 		if text_width >= max_width then 
  159. 			local text_scale = max_width/text_width 
  160. 			vint_set_property(store_title_txt_h, "scale", text_scale, 1) 
  161. 		end 
  162.  
  163. 		--Set control Percentage to start with...  
  164. 		City_control_meter:update(0.01 * Building_purchasing_data.control_pct, false) 
  165. 		 
  166. 		--Set callback to play control up during the animation... 
  167. 		local control_meter_twn_end_h = vint_object_find("control_meter_end_twn") 
  168. 		vint_set_property(control_meter_twn_end_h, "end_event", "building_purchase_control_anim") 
  169. 		 
  170. 		--Set text using values/tag cool guy stuff... 
  171. 		local values, tag 
  172. 		 
  173. 		--Control Increase... 
  174. 		local control_increase_txt_h = vint_object_find("control_increase_txt") 
  175. 		local new_pct = Building_purchasing_data.control_pct_increase 
  176. 		values = { [0] = floor(new_pct + 0.5)} 
  177. 		tag = vint_insert_values_in_string("BUILDING_PURCHASE_CONTROL_INCREASE", values)			--LOCALIZE EXAMPLE: "10% CONTROL INCREASE" 
  178. 		vint_set_property(control_increase_txt_h, "text_tag", tag) 
  179. 		 
  180. 		--Hoods taken over... 
  181. 		local control_increase_gang_txt_h = vint_object_find("control_increase_gang_txt") 
  182. 		--values = { [0] = Building_purchasing_data.control_district} 
  183. 		--tag = vint_insert_values_in_string("{0} DISTRICT", values)			--LOCALIZE EXAMPLE: "DECKERS DISTRICT" 
  184. 		vint_set_property(control_increase_gang_txt_h, "text_tag", Building_purchasing_data.control_district ) 
  185. 		 
  186. 		--Cash Earned /Day 
  187. 		local cash_txt_h = vint_object_find("cash_day_text") 
  188. 		if Building_purchase_is_crib then 
  189. 			vint_set_property(cash_txt_h, "visible", false) 
  190. 		else 
  191. 			values = { [0] = format_cash(Building_purchasing_data.profit)} 
  192. 			tag = vint_insert_values_in_string("BUILDING_PURCHASE_CASH_PER_HOUR", values)					--LOCALIZE EXAMPLE: "$3,000 PER HOUR" 
  193. 			vint_set_property(cash_txt_h, "text_tag", tag) 
  194. 		end 
  195. 		 
  196. 		--Discount Earned /Day 
  197. 		local discount_text_h = vint_object_find("discount_text") 
  198. 		if Building_purchase_is_crib or Building_purchasing_data.is_store ~= true then 
  199. 			vint_set_property(discount_text_h, "visible", false) 
  200. 		else 
  201. 			values = { [0] = floor(Building_purchasing_data.discount + 0.5)} 
  202. 			tag = vint_insert_values_in_string("BUILDING_PURCHASE_DISCOUNT", values)			--LOCALIZE EXAMPLE: "10% DISCOUNT" 
  203. 			vint_set_property(discount_text_h, "text_tag", tag) 
  204. 		end 
  205. 		 
  206. 		--Set button on the interface 
  207. 		local button = Vdo_hint_button:new("ctrl_btn") 
  208. 		button:set_button(CTRL_MENU_BUTTON_A)	 
  209. 		 
  210. 		local button_highlight_h = vint_object_find("ctrl_btn_highlight") 
  211. 		vint_set_property(button_highlight_h, "image", CTRL_MENU_BUTTON_A) 
  212. 		if game_is_active_input_gamepad() == false then 
  213. 			vint_set_property(button.handle, "visible", false) 
  214. 			vint_set_property(button_highlight_h, "visible", false) 
  215. 		end 
  216.  
  217. 		--Show Base Items... 
  218. 		vint_set_property(vint_object_find("ctrl_info_grp"), "visible", true) 
  219. 		vint_set_property(vint_object_find("guns"), "visible", true) 
  220. 		vint_set_property(vint_object_find("main"), "visible", true) 
  221. 		 
  222. 		--Play all animations in... 
  223. 		local twn_h = vint_object_find("purchase_anchor_twn") 
  224. 		vint_set_property(twn_h, "start_event", "building_purchase_play_audio") 
  225. 		 
  226. 		bg_saints_animate() 
  227. 		lua_play_anim(vint_object_find("text_anim")) 
  228. 		 
  229. 		--vint_set_property(vint_object_find("left_shadow"),"visible",true) 
  230. 		--vint_set_property(vint_object_find("right_shadow"),"visible",true) 
  231. 		 
  232. 		bg_saints_show(true) 
  233. 		 
  234. 		--TODO: maybe a programmer should look at this, and figure out why we need two? (JMH 6/24/2010) 
  235. 		if Building_purchase_is_crib == true then 
  236. 			crib_purchase_purchase_crib() 
  237. 		else  
  238. 			shop_purchase_purchase_shop() 
  239. 		end 
  240. 		 
  241. 		Building_purchase_subs[1] = vint_subscribe_to_input_event("select", "building_purchase_clean")	 
  242. 		if Mouse_input_tracker ~= nil then 
  243. 			Mouse_input_tracker:subscribe(true)		 
  244. 		end 
  245. 	else 
  246. 		pop_screen() 
  247. 	end 
  248. end 
  249.  
  250. function building_purchase_play_audio() 
  251. 	game_UI_audio_play("UI_Buy_Building_Screen") 
  252. end 
  253.  
  254. --Animates in the city control... this is for a  callback from a tween 
  255. function building_purchase_control_anim(tween_h, event) 
  256. 	City_control_meter:update(0.01 * Building_purchasing_data.control_pct_new, true) 
  257. end 
  258.  
  259. function building_purchase_mouse_click(event, target_handle) 
  260. 	building_purchase_clean() 
  261. end 
  262.  
  263. function building_purchase_mouse_move(event, target_handle) 
  264. end 
  265.  
  266.