sr2lua/hud_demo_derby.lua

  1. Hud_dd_status = {	 
  2. 	 
  3. 	--Player Info 
  4. 	player_grp = { }, 
  5. 	 
  6. 	player = { 
  7. 		health_pct = -1, 
  8. 	}, 
  9. 	health_bar = 	{ bg_h = 0, fill_0_h = 0,  fill_1_h = 0, fill_decay_h = 0,  
  10. 						fill_start_angle = PI + 0.03, fill_end_angle = PI2  - 0.03, angle_offset = 0.045,  
  11. 						anim = 0, anim_delay_start = .25, anim_delay = .25, anim_delay_decay = .9,  
  12. 						is_flashing = false, hits = 0, last_hit_time = 0,  
  13. 						 
  14. 	},		 
  15.  
  16. 		 
  17. 	--Coop Info 
  18. 	coop = {	health_pct = -1, grp_h = 0, fill_h = 0,  car_h = 0, 
  19. 	}, 
  20. 	 
  21. 	--Hostiles Info 
  22. 	hostiles = { }, 
  23. 	 
  24. 	hostile_elements = { 
  25. 		grp_h = 0, fill_h = 0, border_h = 0, 
  26. 	}, 
  27. 	 
  28. 	anims = { }, 
  29. 	 
  30. 	anim_elements = { 
  31. 		hostile_anim_h = 0,  
  32. 	}, 
  33. 	 
  34. 	old_time = 0, 
  35. 	 
  36. 	handles = {}, 
  37. 	 
  38. } 
  39.  
  40. Hud_dd_health_flash_status = { 
  41. 	is_flashing = false, 
  42. 	is_queued = false, 
  43. 	last_flash_time_index = 0 
  44. } 
  45.  
  46.  
  47. function hud_demo_derby_init() 
  48. 	peg_load("ui_demo_derby") 
  49. 	 
  50. 	--Player Health 
  51. 	--handle 
  52. 	local h = vint_object_find("health_grp") 
  53. 	local bar = Hud_dd_status.health_bar 
  54. 	bar.bg_h = vint_object_find("health_bar_bg", h) 
  55. 	bar.fill_0_h = vint_object_find("health_bar_fill_0", h) 
  56. 	bar.fill_1_h = vint_object_find("health_bar_fill_1", h) 
  57. 	bar.fill_decay_h = vint_object_find("health_bar_fill_decay", h) 
  58. 	 
  59. 	Hud_dd_status.handles.health_grp = vint_object_find("health_grp") 
  60. 	 
  61. 	--Reset angles on health meter 
  62. 	vint_set_property(bar.bg_h, 			"start_angle",		bar.fill_start_angle - bar.angle_offset) 
  63. 	vint_set_property(bar.bg_h, 			"end_angle", 		bar.fill_end_angle + bar.angle_offset) 
  64. 	vint_set_property(bar.fill_0_h,		"start_angle", 	bar.fill_start_angle) 
  65. 	vint_set_property(bar.fill_0_h, 		"end_angle", 		bar.fill_end_angle) 
  66. 	vint_set_property(bar.fill_1_h, 		"start_angle",		bar.fill_start_angle) 
  67. 	vint_set_property(bar.fill_1_h, 		"end_angle", 		bar.fill_end_angle) 
  68. 	vint_set_property(bar.fill_decay_h, "start_angle",		bar.fill_start_angle) 
  69. 	vint_set_property(bar.fill_decay_h, "end_angle", 		bar.fill_end_angle) 
  70. 	 
  71. 	--Coop Player 
  72. 	local h = vint_object_find("coop_grp") 
  73. 	Hud_dd_status.coop.grp_h = h 
  74. 	Hud_dd_status.coop.fill_h = vint_object_find("coop_health_fill", h) 
  75. 	Hud_dd_status.coop.car_h = vint_object_find("coop_car", h) 
  76. 			 
  77. 	--Hide Coop Indicator 
  78. 	vint_set_property(Hud_dd_status.coop.grp_h, "visible", false) 
  79.  
  80. 	--Hostiles 
  81. 	Hud_dd_status.hostile_elements.grp_h = vint_object_find("hostile_grp") 
  82. 	Hud_dd_status.hostile_elements.fill_h = vint_object_find("hostile_health_fill") 
  83. 	Hud_dd_status.hostile_elements.border_h = vint_object_find("hostile_border") 
  84. 	Hud_dd_status.anim_elements.hostile_anim_h = vint_object_find("hostile_health_anim_1") 
  85. 	 
  86. 	--Pause animations 
  87. 	vint_set_property(Hud_dd_status.anim_elements.hostile_anim_h, "is_paused", true) 
  88. 	vint_set_property(vint_object_find("health_anim_0"), "is_paused", true) 
  89. 	vint_set_property(vint_object_find("health_anim_1"), "is_paused", true) 
  90. 	 
  91. 	--Set callback for player health flashing animation 
  92. 	local flash_twn = vint_object_find("health_fill_1") 
  93. 	vint_set_property(flash_twn, "end_event", "hud_demo_derby_flash_end") 
  94. 	 
  95. 	 
  96. 	--Set callback for player health decay 
  97. 	local decay_twn = vint_object_find("health_fill_tween") 
  98. 	vint_set_property(decay_twn, "end_event", "hud_demo_derby_decay_end") 
  99. 	 
  100. 	--Demo Derby Subscription 
  101. 	vint_datagroup_add_subscription("sr2_local_player_demo_derby", "insert", "hud_dd_update") 
  102. 	vint_datagroup_add_subscription("sr2_local_player_demo_derby", "update", "hud_dd_update") 
  103. 	vint_datagroup_add_subscription("sr2_local_player_demo_derby", "remove", "hud_dd_update") 
  104. end 
  105.  
  106. function hud_demo_derby_cleanup() 
  107. 	peg_unload("ui_demo_derby") 
  108. end 
  109.  
  110.  
  111. function hud_dd_player_health_decrease_anim() 
  112. 	 
  113. 	local bar = Hud_dd_status.health_bar 
  114. 	 
  115. 	--Get the original angle before the hit 
  116. 	local angle_before = vint_get_property(bar.fill_decay_h, "end_angle") 
  117. 	local angle_end = vint_get_property(bar.fill_0_h, "end_angle") 
  118.  
  119. 	 
  120. 	---Set tween values of decay animation, using the before and after values 
  121. 	local fill_tween = vint_object_find("health_fill_tween") 
  122. 	vint_set_property(fill_tween, "start_value", angle_before) 
  123. 	vint_set_property(fill_tween, "end_value", angle_end) 
  124. 	 
  125. 	local bg_tween = vint_object_find("health_bg_tween") 
  126. 	vint_set_property(bg_tween, "start_value", angle_before + bar.angle_offset) 
  127. 	vint_set_property(bg_tween, "end_value", angle_end + bar.angle_offset) 
  128. 	 
  129. 	--play the animation, using the delay we setup 
  130. 	lua_play_anim(vint_object_find("health_anim_0"), .5) 
  131. 	 
  132. 	 
  133. end 
  134.  
  135.  
  136. function hud_dd_update(di_h, event) 
  137. --debug_print("vint", "health_pct " .. Hud_dd_status.player.health_pct .. "\n") 
  138. 	local key, index, screen_x, screen_y, z_depth, distance, health_pct, is_coop, is_player = vint_dataitem_get(di_h) 
  139. 	 
  140. 	if event == "update" then 
  141. 		--Global reference 
  142. 		local bar = Hud_dd_status.health_bar	 
  143. 		 
  144. 		 
  145. 		--PLAYER HEALTH 
  146. 		if is_player == true then  
  147. 		 
  148. 			--store the angles of the bars for the decay 
  149. 			local fill_end_angle = (bar.fill_end_angle - bar.fill_start_angle) * health_pct + bar.fill_start_angle  
  150. 			local bg_end_angle = fill_end_angle + bar.angle_offset 
  151. 			 
  152. 			--##### 
  153. 			--begin hit 
  154. 			--##### 
  155. 			if health_pct ~= Hud_dd_status.player.health_pct then 
  156. 				 
  157. 				--Set angles and fill of bar 
  158. 				vint_set_property(bar.fill_0_h, "end_angle", fill_end_angle) 
  159. 				vint_set_property(bar.fill_1_h, "end_angle", fill_end_angle) 
  160. 				 
  161. 				--Determine if health is greater or lesser than before 
  162. 				if health_pct < Hud_dd_status.player.health_pct then 
  163. 				 
  164. 				else 
  165. 				 
  166. 				end 
  167.  
  168. 				 
  169. 				--play the decay animation 
  170. 				if health_pct < Hud_dd_status.player.health_pct then 
  171. 					-- Flash health bar  
  172. 					if Hud_dd_health_flash_status.is_flashing == true then 
  173. 						--Compare current time index with previous 
  174. 						local current_time_index = vint_get_time_index()  
  175. 						local dif_time_index = current_time_index - Hud_dd_health_flash_status.last_flash_time_index  
  176. 						if dif_time_index > .20 then 
  177. 							Hud_dd_health_flash_status.is_queued = true 
  178. 						end 
  179. 					else 
  180. 						--Play flashing animation 
  181. 						hud_demo_derby_flash_play() 
  182. 						Hud_dd_health_flash_status.is_flashing = true 
  183. 					end 
  184. 					--play the decay anim and be sure to the Global health pct 
  185. 					Hud_dd_status.player.health_pct = health_pct 
  186. 					hud_dd_player_health_decrease_anim() 
  187. 				else  
  188. 					--raise the decay fill and bg 
  189. 					vint_set_property(bar.fill_decay_h, "end_angle", fill_end_angle) 
  190. 					vint_set_property(bar.bg_h, "end_angle", bg_end_angle) 
  191. 					--remove the queue if any 
  192. 					Hud_dd_health_flash_status.is_queued = false 
  193. 				end 
  194. 				 
  195. 				--Hide bg of meter if health is empty, and make the car icon transparent. 
  196. 				if health_pct == 0 then 
  197. 					--vint_set_property(bar.bg_h, "visible", false) 
  198. 					vint_set_property(vint_object_find("weapon_icon"), "alpha", 0.5) 
  199. 				else 
  200. 					vint_set_property(bar.bg_h, "visible", true) 
  201. 					vint_set_property(vint_object_find("weapon_icon"), "alpha", 1) 
  202. 				end 
  203. 				 
  204. 				--store the current  health pct so we can judge it later 
  205. 				Hud_dd_status.player.health_pct = health_pct 
  206. 				 
  207. 			end 
  208.  
  209. 		end 
  210. 		 
  211. 		 
  212. 		--COOP HEALTH 
  213. 		if is_coop == true then 
  214. 			 
  215. 			--Creating reference to the Global... 
  216. 			local coop_status = Hud_dd_status.coop 
  217. 			 
  218. 			--Make Coop Indicator visibile 
  219. 			vint_set_property(coop_status.grp_h, "visible", true) 
  220. 			 
  221. 			--Update Health 
  222. 			if health_pct ~= coop_status.health_pct then 
  223. 				local end_angle = 1.57 + (1.57 * health_pct) 
  224. 				vint_set_property(coop_status.fill_h, "end_angle", end_angle ) 
  225. 				Hud_dd_status.coop.health_pct = health_pct 
  226. 			end	 
  227. 			 
  228. 			if coop_status.health_pct == 0 then 
  229. 				vint_set_property(coop_status.grp_h, "alpha", 0.5) 
  230. 			else 
  231. 				vint_set_property(coop_status.grp_h, "alpha", 1) 
  232. 			end 
  233. 			 
  234. 			--TODO: Make Car dim when coop is dead 
  235.  
  236. 		end 
  237. 		 
  238. 		 
  239. 		--ENEMIES HEALTH 
  240. 		if is_player == false and is_coop == false then 
  241. 			 
  242. 			--check to see if there is a hostile indicator for the index 
  243. 			if Hud_dd_status.hostiles[index] == nil then 
  244. 				 
  245. 				 
  246. 				--Doesn't currently exist so create the hostile indicator 
  247. 				local grp_h = vint_object_clone(Hud_dd_status.hostile_elements.grp_h) 
  248. 				local health_fill_h = vint_object_find("hostile_health_fill", grp_h) 
  249. 				local border_h = vint_object_find("hostile_border", grp_h) 
  250. 				 
  251. 				--Create the animation clones 
  252. 				local anim_0 = vint_object_find("hostile_health_anim_1") 
  253. 				local anim_clone_0 = vint_object_clone(anim_0) 
  254. 				local anim_0_tint_tween = vint_object_find("hostile_health_tween_1", anim_clone_0) 
  255. 				vint_set_property(anim_0_tint_tween, "target_handle",	health_fill_h) 
  256. 				 
  257. 				Hud_dd_status.hostiles[index] = { 
  258. 					grp_h = grp_h, 
  259. 					health_fill_h = health_fill_h, 
  260. 					border_h = border_h, 
  261. 					anim_0 = anim_0, 
  262. 					anim_clone_0 = anim_clone_0, 
  263. 					anim_0_tint_tween = anim_0_tint_tween, 
  264. 					health_pct = health_pct , 
  265. 					screen_x = screen_x, 
  266. 					screen_y = screen_y, 
  267. 					z_depth = z_depth, 
  268. 					distance = distance, 
  269. 				}			 
  270.  
  271. 			end 
  272. 			 
  273. 			--Shorter name for ease of use 
  274. 			local hostile = Hud_dd_status.hostiles[index] 
  275. 			 
  276. 			 
  277. 			--Hide element if health is less than zero. 
  278. 			if hostile.health_pct == 0 then  
  279. 				vint_set_property(hostile.grp_h, "visible", false) 
  280. 			else 
  281. 				vint_set_property(hostile.grp_h, "visible", true) 
  282. 			end 
  283. 			 
  284. 				 
  285. 			--Health Update 
  286. 			if health_pct < hostile.health_pct then 
  287. 				lua_play_anim(hostile.anim_clone_0, 0); 
  288. 			end 
  289. 			 
  290. 			--Decrease health bar length 
  291. 			vint_set_property(hostile.health_fill_h, "source_se", 86 * health_pct, 10) 
  292. 			vint_set_property(hostile.border_h, "source_se", (86 * health_pct) + 4, 14) 
  293. 			 
  294. 			--Screen Position 
  295. 			local grp_width = 106			 
  296. 			local x = screen_x - (grp_width * 0.5) 
  297. 			local y = screen_y 
  298. 			vint_set_property(hostile.grp_h, "anchor", x, y)	 
  299.  
  300. 			 
  301. 			--Scale 
  302. 			local maxclamp = 0.40 
  303. 			local minclamp = 0.1 
  304. 			 
  305. 			local maxscale = 1 
  306. 			local minscale = 0.5 
  307. 			 
  308. 			--Clamp the distances 
  309. 			if distance <= minclamp then 
  310. 				distance = minclamp  
  311. 				elseif distance >= maxclamp then 
  312. 				distance = maxclamp 
  313. 			end 
  314.  
  315. 			local newdist = (distance - minclamp) 
  316. 			local ratio = 1 - (newdist / (maxclamp - minclamp)) 
  317. 			local scale = (ratio * (maxscale-minscale)) + minscale 
  318. 			 
  319. 			vint_set_property(hostile.grp_h, "scale", scale, scale) 
  320. 			vint_set_property(hostile.grp_h, "alpha", scale) 
  321. 				 
  322. 			--Z Depth 
  323. 			if z_depth ~= hostile.z_depth then 
  324. 				vint_set_property(hostile.grp_h,  "depth", z_depth) 
  325. 			end 
  326. 			 
  327. 			hostile.health_pct = health_pct 
  328. 			hostile.screen_x = screen_x 		 
  329. 			hostile.screen_y = screen_y 		 
  330. 			hostile.z_depth = z_depth 		 
  331. 			hostile.distance = distance 
  332.  
  333.  
  334. 		end 
  335. 		 
  336. 	end 
  337. 		 
  338. 	--If you got a remove event, remove the clones	 
  339. 	if event == "remove" then 
  340. 		debug_print("vint", "remove hostiles\n") 
  341. 		local hostile = Hud_dd_status.hostiles[index] 
  342. 		if hostile ~= nil then 
  343. 			vint_object_destroy(hostile.grp_h) 
  344. 			Hud_dd_status.hostiles[index] = nil 
  345. 		end 
  346. 	end 
  347. 		 
  348. end 
  349.  
  350. function hud_demo_derby_flash_play() 
  351. 	local health_flash_anim = vint_object_find("health_anim_1") 
  352. 	lua_play_anim(health_flash_anim, 0) 
  353. 	Hud_dd_health_flash_status.last_flash_time_index = vint_get_time_index() 
  354. end 
  355.  
  356.  
  357. function hud_demo_derby_flash_end(tween_h, event) 
  358. 	if Hud_dd_health_flash_status.is_queued == true then 
  359. 		--Queued Flashing so, play another flash 
  360. 		hud_demo_derby_flash_play() 
  361. 		Hud_dd_health_flash_status.is_queued = false 
  362. 	else 
  363. 		--No queue so we aren't going to flash anymore. 
  364. 		Hud_dd_health_flash_status.is_flashing = false 
  365. 	end 
  366. end 
  367.  
  368. function hud_demo_derby_decay_end(tween_h, event) 
  369. 	local bar = Hud_dd_status.health_bar 
  370. 	debug_print("vint", "decay end event") 
  371. 	if Hud_dd_status.player.health_pct == 0 then 
  372. 		vint_set_property(bar.bg_h, "visible", false) 
  373. 	end 
  374. end 
  375.  
  376.  
  377. function hud_demo_derby_coop_test() 
  378. 	local health_pct = 0 
  379. 	local coop_status = Hud_dd_status.coop 
  380. 	vint_set_property(coop_status.grp_h, "visible", true) 
  381. 	local end_angle = 1.57 + (1.57 * health_pct) 
  382. 	vint_set_property(coop_status.fill_h, "end_angle", end_angle ) 
  383.  
  384. 	if health_pct == 0 then 
  385. 		vint_set_property(coop_status.grp_h, "alpha", 0.5) 
  386. 	else 
  387. 		vint_set_property(coop_status.grp_h, "alpha", 1) 
  388. 	end 
  389.  
  390. 	Hud_dd_status.coop.health_pct = health_pct 
  391. end 
  392.  
  393.  
  394. function hud_demo_derby_health_hide() 
  395. 	vint_set_property(Hud_dd_status.handles.health_grp, "visible", false) 
  396. end 
  397.  
  398. function hud_demo_derby_health_show() 
  399. 	vint_set_property(Hud_dd_status.handles.health_grp, "visible", true) 
  400. end 
  401.  
  402.  
  403.  
  404.  
  405.  
  406.