:NAME game_hud_achievements

:ACHIEVEMENT string			name

:GLOBAL achievement[]		pending_achievements
:GLOBAL float32				current_achievement_notification_time

k_notification_delay= 3


function global_initialize(g)
	-- anything to do here?
	g.current_achievement_notification_time= -k_notification_delay
end

function start()
	sleep()
end

function notify_achievement(a)
	local w= world_get()
	local plate= w.get_plate("achievement")
	if (plate) then
		local ws= world_state_get()
		ws.place_plate(plate)
		local plate_state= plate.plate_state_get()
		if (plate_state) then
			local object= plate_state.object_get("achievement_name")
			local text= object.text_get()
			if (text) then
				text.text_set(a.name)
			end
		end
	end
end

function update()

	local t= world_state_get().master_time.time
	if ((t - global.current_achievement_notification_time) >= k_notification_delay) then
		
		-- I'm allowed to do something else
		if (global.pending_achievements.count > 0) then
			-- notify another one
			local iterator= global.pending_achievements.iterator_get()
			if (iterator) then
				local a= iterator.next()
				if (a) then
					notify_achievement(a)
					global.current_achievement_notification_time= t
					
					-- now remove me
					global.pending_achievements.erase(a)
				end
			end
		end
		
		if (global.pending_achievements.count == 0) then
			-- notify another one
			sleep()
		end
	end
	
end

function achievement_granted(achievement)

	wake()
	
	local a= global.pending_achievements.create()
	if a then
		a.name= achievement.name
	end
end

function player_rewarded(reward)
	local game_hud_script= world_state_get().global_script("game_hud")
	if (game_hud_script) then
		game_hud_script.reward= reward
	end
end


system_register(start, update, nil, nil)
event_register(achievement_granted, e_value("e_script_event", "_script_event_achievement"))
event_register(player_rewarded, e_value("e_script_event", "_script_event_campaign_reward"))
