ObjectData = 
{
	MotionModel = 
	{
		DefaultAnimation = [[normal]],
		Motions = 
		{
			motion9 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Eets\eets_happy_land.anim]],
						Name = [[eets_happy_land]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[rise]],
			},
			motion4 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Eets\eets_happy_fall.anim]],
						Name = [[eets_happy_fall]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[normal]],
			},
			motion6 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Eets\eets_happy_walk.anim]],
						Name = [[eets_happy_walk]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[walking]],
			},
			motion5 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Eets\eets_happy_fall.anim]],
						Name = [[eets_toolbar]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[toolbar]],
			},
			motion8 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Eets\eets_happy_squat.anim]],
						Name = [[eets_happy_squat]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[jump_begin]],
			},
			motion2 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Eets\eets_happy_eat.anim]],
						Name = [[eets_happy_eat]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[eat]],
			},
			motion1 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Eets\eets_happy_fall.anim]],
						Name = [[eets_happy_fall]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[falling]],
			},
			motion3 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Eets\eets_happy_jump.anim]],
						Name = [[eets_happy_jump]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[knock]],
			},
			motion7 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Eets\eets_happy_jump.anim]],
						Name = [[eets_happy_jump]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[jump_end]],
			},
		},
	},
	Extensions = 
	{
		PhysicsExtension = 
		{
			mass = 10.000000,
			static_friction = 0.700000,
			restitution = 0.600000,
			kinetic_friction = 0.200000,
		},
		LevelEditorExtension = 
		{
			main_character = true,
			locked = true,
			teleportable = true,
		},
		WalkingExtension = 
		{
			doesJump = true,
			isHit = true,
			jump_force_x = 3500.000000,
			jump_force_y = -8000.000000,
			walking_offset = 29.000000,
			collision_offet_y = 35.000000,
			speed = 2.000000,
			length = 35.000000,
			collision_offet_x = -20.000000,
		},
		EmotionExtension = 
		{
			emotion = [[happy]],
		},
		EdibleExtension = 
		{
			--eaten_by_whale = true,
			Eaters =
			{
				"whale",
			},
		},
		EaterExtension = 
		{
			excess_frames = 5.000000,
			frames_to_eat = 10.000000,
			radius = 37.500000,
			length_forward = 19.000000,
		},
		ThwackableExtension = 
		{
		},
		FlippableExtension = 
		{
		},
		PositionExtension = 
		{
		},
		LightingExtension = 
		{
		},
	},
}

-- Begin Scripting --

help_text_header = "$00615"
help_text = "$00616"

function OnInit()

	LIGHT_CHECK_FREQ = 24
	SCARED_VALUE = 200
	counter = 0
	
	sound_table = 
	{		
		eat =
		{
			{ "eets_happy_eat", 4, "Eets Happy Eating" }
		},
		walking =
		{
			{ "eets_happy_walk", 1, "Eets Happy Walking 1" },			
			{ "eets_happy_walk", 7, "Eets Happy Walking 1" }
		},
		jump_begin =
		{
			{ "eets_happy_squat", 2, "Eets Happy Walking 1" }
		},
		jump_end =
		{
			{ "eets_happy_jump", 2, "Eets Happy Jumping" },
			{ "eets_happy_jump", 1, "Eets Happy Walking 2" }
		},
		rise =
		{
			{ "eets_happy_land", 1, "Eets Happy Landing" }
		},
	}
	
	happy_fall_sound = nil
end

dust_effect = nil


function OnEndUpdate()
        --Print("Eets ONENDUPDATE" )
        if dust_effect ~= nil then
              dust_effect:Stop( true )
              dust_effect = nil
        end
end

function Update()

	Profile_Start("Eets_Happy")

	local motion_model = this:GetMotionModel()
	
	local frame_num = motion_model:GetCurrentMotion():GetCurrentAnim():GetCurrentFrameIndex()
	local motion_name = motion_model:GetCurrentMotionName()
	
	local emotion_ext = this:GetEmotionExtension()
	if not emotion_ext:RecentlyChanged() then
		
		local sound = DetermineSound( 	motion_model:GetCurrentMotionName(), 
			motion_model:GetCurrentMotion():GetCurrentAnimName(),
			motion_model:GetCurrentMotion():GetCurrentAnim():GetCurrentFrameIndex() )
		
		--make the sound
		if sound ~= nil then
			Sound_CreateSound( sound, false, 0, this:GetPosition() )
			World_Alert(this:GetPosition(), 300)
		end
		
	end
	
	local motion_model = this:GetMotionModel()
		
	if motion_name  == "walking" and (frame_num == 1 or frame_num == 7) then
		this:CreateEffect( "eets/happy_walk_dust" )
	end
	
	if motion_name == "jump_begin" and frame_num == 3 then
		this:CreateEffect("eets/happy_jump_dust")
	end
			
	if motion_name == "eat" and frame_num == 12 then
		this:CreateEffect( "eets/yum" )
	end

	--only run this every LIGHT_CHECK_FREQ frames
	counter = counter + 1
	
	if counter == LIGHT_CHECK_FREQ and this:IsVisible() then
		local light_ext = this:GetLightingExtension()
		if not light_ext:IsLit() then
			World_ChangeEmotion( 1, SCARED_VALUE )
		end
		
		counter = 0
	end
	
	Profile_Stop("Eets_Happy")
end

function DetermineSound( motion_name, anim_name, frame )

	local local_table = sound_table[ motion_name ]
	if local_table == nil then
		return nil
	end
	
	--search in subtree
	for i in local_table do
		if local_table[i][2] == frame and local_table[i][1] == anim_name then
			return local_table[i][3]
		end
	end

	--not found
	return nil
	
end

