:NAME enemy_ai

:GLOBAL c_node*	head_node
:GLOBAL c_node*	neck_node
:GLOBAL c_node*	torso_node


function attach(object)
	local definition= object.definition
	local model= definition.model
	
	if (model) then
		local skeleton= model.skeleton
		if (skeleton) then
			global.head_node= skeleton.node_get_by_name("head")
			global.neck_node= skeleton.node_get_by_name("neck")
			global.torso_node= skeleton.node_get_by_name("torso")
		end
	end
end

function update(object)

	--[[
	local aim= object.aim_get()
	if (aim) then
		object.rotation_set(aim.angle)
	end
	--]]
	
	local laf= object.laf_get()
	if (laf and global.head_node and global.torso_node) then
	
		local head_state= object.node_state_from_node(global.head_node)
		local torso_state= object.node_state_from_node(global.torso_node)

		--root_state.rotation_set(laf.face_state.angle)
		--print("face: "..laf.face_state.angle)
		local angle_accum= laf.face_state.angle
		
		local torso_angle= laf.aim_state.angle - angle_accum
		--print("torso: "..torso_angle)
		angle_accum = angle_accum + torso_angle
		torso_state.rotation_set(torso_angle)
		
		local head_angle= laf.look_state.angle - angle_accum
		--print("head: "..head_angle)
		head_state.rotation_set(head_angle)
	
		object.mark_all_nodes_dirty()
		object.rotation_set(laf.face_state.angle)
	end
end

component_register(attach, update, nil, nil)
