:NAME vitality

:VAR boolean		dead
:VAR boolean		invincible
:VAR float32		damage


-- ------------------- initialization ------------------- 


function var_initialize(v)
	v.dead= false
	v.damage= 0
end


-- ------------------- component ------------------- 


function update(object)

	if (not var.dead) then
		local primary_player= world_state_get().primary_player_get()
		if (primary_player) then
			local player_vitality= primary_player._c.vitality
			if (player_vitality) then
				if (player_vitality.dead) then
					var.dead= true
				elseif (var.damage > 0) then
					if (not var.invincible) then
						-- transfer damage to the primary player
						player_vitality.scripted_damage = player_vitality.scripted_damage + var.damage
					end
					var.damage= 0
				end
			end
		end
	end
end


component_register(nil, update, nil, nil)
