sr2lua/building_purchase.lua

  1. Building_purchase_elements = { } 
  2. Building_purchase_anims 	= { } 
  3. Building_purchase_subs 		= { } 
  4. Building_purchasing_data 	= { } 
  5. Building_purchase_is_crib	= false 
  6. Menu_sound_confirm			= audio_get_audio_id("SYS_HUD_CONF_SERIOUS")	-- Confirmation/warning dialog opens 
  7. Building_purchase_sound 	= audio_get_audio_id("SYS_MENU_BUY_BUILDING") 
  8.  
  9. function building_purchase_init() 
  10. 	Building_purchase_is_crib = building_purchase_is_crib_being_purchased() 
  11. 	 
  12. 	--Find objects 
  13. 	Building_purchase_elements.ctrl_info_grp_h = vint_object_find("ctrl_info_grp") 
  14. 	Building_purchase_elements.store_subtitle_txt_h = vint_object_find("store_subtitle_txt") 
  15. 	Building_purchase_elements.store_title_txt_h = vint_object_find("store_title_txt") 
  16. 	Building_purchase_elements.description_grp_h = vint_object_find("description_grp") 
  17. 	Building_purchase_elements.description_txt_h = vint_object_find("description_txt") 
  18.  
  19. 	--Set Scaler for screen 
  20. 	local h = vint_object_find("main") 
  21. 	local scale_x, scale_y = vint_get_property(h, "scale")	 
  22. 	--Store and pause animations 
  23. 	Building_purchase_anims.main_anim_h = vint_object_find("main_anim") 
  24. 	 
  25. 	vint_set_property(Building_purchase_anims.main_anim_h, "is_paused", true) 
  26. 	 
  27. 	--Set alpha on all objects to 0 
  28. 	vint_set_property(Building_purchase_elements.ctrl_info_grp_h, "alpha", 0) 
  29. 	vint_set_property(Building_purchase_elements.store_subtitle_txt_h, "alpha", 0) 
  30. 	vint_set_property(Building_purchase_elements.store_title_txt_h, "alpha", 0) 
  31. 	vint_set_property(Building_purchase_elements.description_grp_h, "alpha", 0) 
  32. 	 
  33. 	Building_purchase_subs[0] = vint_subscribe_to_input_event(nil, "all_unassigned",	"bp_nothing") 
  34. 	 
  35. 	if Building_purchase_is_crib == true then 
  36. 		vint_dataresponder_request("crib_purchase_get_crib_info", "update_building_purchase_text", 0) 
  37. 	else 
  38. 		vint_dataresponder_request("shop_purchase_get_shop_info", "update_building_purchase_text", 0) 
  39. 	end 
  40. end 
  41.  
  42. function building_purchase_clean() 
  43. 	building_purchase_cleanup(0, DIALOG_ACTION_CLOSE) 
  44. end 
  45.  
  46. function building_purchase_cleanup(result, action) 
  47. 	if action ~= DIALOG_ACTION_CLOSE then 
  48. 		return 
  49. 	end 
  50. 	 
  51. 	for index, value in Building_purchase_subs do 
  52. 		vint_unsubscribe_to_input_event(value) 
  53. 	end 
  54. 	 
  55. 	vint_document_unload(vint_document_find("building_purchase"))	 
  56. end 
  57.  
  58. function update_building_purchase_text(building_name, description_txt, store_cost, player_cash, profit) 
  59. 	Building_purchasing_data.name = building_name 
  60. 	Building_purchasing_data.description_txt = description_txt 
  61. 	Building_purchasing_data.store_cost = store_cost 
  62. 	Building_purchasing_data.player_cash = player_cash 
  63. 	 
  64. 	if Building_purchase_is_crib == false then 
  65. 		Building_purchasing_data.profit = profit 
  66. 	end 
  67. 	 
  68. 	if player_cash < store_cost then 
  69. 		-- Show Dialog that says you can't afford it. 
  70. 		local header = "MENU_TITLE_NOTICE" 
  71. 		local body = "HUD_SHOP_INSUFFICIENT_FUNDS" 
  72.  
  73. 		local options = { [0] = "CONTROL_CONTINUE" } 
  74. 		dialog_box_open(header, body, options, "building_purchase_cleanup") 
  75. 	else 
  76. 		--	Ask player if they really want to purchase it. 
  77. 		local header = "MENU_TITLE_CONFIRM" 
  78. 		local body 
  79. 		local insert_values = { [0] = building_name, [1] = format_cash(store_cost) } 
  80. 		if Building_purchase_is_crib == true then 
  81. 			body = vint_insert_values_in_string("BUILDING_PURCHASE_CRIB", insert_values) 
  82. 		else 
  83. 			body = vint_insert_values_in_string("BUILDING_PURCHASE_BUILDING", insert_values) 
  84. 		end 
  85. 		dialog_box_confirmation(header, body, "building_purchased") 
  86. 	end 
  87.  
  88. 	 
  89. 	 
  90. end 
  91.  
  92. function building_purchased(result, action)	 
  93. 	if action ~= DIALOG_ACTION_CLOSE then 
  94. 		return 
  95. 	end	 
  96. 	 
  97. 	if result == 0 then 
  98. 		if Building_purchase_is_crib == true then 
  99. 			crib_purchase_purchase_crib() 
  100. 		else  
  101. 			shop_purchase_purchase_shop() 
  102. 		end 
  103. 		 
  104. 		audio_play(Building_purchase_sound) 
  105. 		 
  106. 		vint_set_property(Building_purchase_elements.store_title_txt_h, "text_tag_crc", Building_purchasing_data.name) 
  107. 		 
  108. 		--Play text purchased animation 
  109. 		lua_play_anim(Building_purchase_anims.main_anim_h, .5) 
  110. 				 
  111. 		--	Set the text properly 
  112. 		vint_set_property(Building_purchase_elements.description_txt_h, "text_tag_crc", Building_purchasing_data.description_txt) 
  113. 		if Building_purchase_is_crib == false then 
  114. 			vint_set_property(Building_purchase_elements.description_txt_h, "insert_values", Building_purchasing_data.profit) 
  115. 		end 
  116.  
  117. 		-- get the right button 
  118. 		local button_h = vint_object_find("ctrl_btn", Building_purchase_elements.ctrl_info_grp_h) 
  119. 		vint_set_property(button_h, "image", get_a_button()) 
  120. 		local button_highlight_h = vint_object_find("ctrl_btn_highlight", Building_purchase_elements.ctrl_info_grp_h) 
  121. 		vint_set_property(button_highlight_h, "image", get_a_button()) 
  122. 			 
  123. 		Building_purchase_subs[1] = vint_subscribe_to_input_event(nil, "select", "building_purchase_clean") 
  124.  
  125. 		--get coords and heights, then move the button tips 
  126. 		local width, height = element_get_actual_size(Building_purchase_elements.description_txt_h) 
  127. 		local x, y = vint_get_property(Building_purchase_elements.description_grp_h, "anchor") 
  128. 		local ctrl_x, ctrl_y = vint_get_property(Building_purchase_elements.ctrl_info_grp_h, "anchor") 
  129. 		local gap = 30 
  130. 	 
  131. 		vint_set_property(Building_purchase_elements.ctrl_info_grp_h, "anchor", ctrl_x, height + y + gap) 
  132. 	else 
  133. 		building_purchase_cleanup(0, DIALOG_ACTION_CLOSE) 
  134. 	end 
  135. end