ObjectData = 
{
	MotionModel = 
	{
		DefaultAnimation = [[standing]],
		Motions = 
		{
			motion4 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Marshomech\marshomech_roar.anim]],
						Name = [[marshomech(alert)]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[alert]],
			},
			motion1 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Marshomech\marshomech_idle.anim]],
						Name = [[marshomech(standing)]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[falling]],
			},
			motion5 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Marshomech\marshomech_hit_into_screen.anim]],
						Name = [[marshomech(hitting_away_from_screen)]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[thwack]],
			},
			motion3 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Marshomech\marshomech_growl_walk.anim]],
						Name = [[marshomech(walking)]],
						MinFreq = 2.000000,
						MaxFreq = 4.000000,
					},
				},
				Name = [[walking]],
			},
			motion6 = 
			{
				Animations = 
				{
					anim4 = 
					{
						Path = [[DATA:Animations\Marshomech\marshomech_idle.anim]],
						Name = [[marshomech(standing)]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
					anim1 = 
					{
						Path = [[DATA:Animations\Marshomech\marshomech_suspicious_look_right.anim]],
						Name = [[marshomech(look_left)]],
						MinFreq = 4.000000,
						MaxFreq = 6.000000,
					},
					anim3 = 
					{
						Path = [[DATA:Animations\Marshomech\marshomech_scratch.anim]],
						Name = [[marshomech(scratching)]],
						MinFreq = 10.000000,
						MaxFreq = 15.000000,
					},
					anim2 = 
					{
						Path = [[DATA:Animations\Marshomech\marshomech_blink.anim]],
						Name = [[marshomech(blinking)]],
						MinFreq = 2.000000,
						MaxFreq = 2.000000,
					},
				},
				Name = [[standing]],
			},
			motion2 = 
			{
				Animations = 
				{
					anim1 = 
					{
						Path = [[DATA:Animations\Marshomech\marshomech(tool_bar).anim]],
						Name = [[marshomech(tool_bar)]],
						MinFreq = 1.000000,
						MaxFreq = 1.000000,
					},
				},
				Name = [[toolbar]],
			},
		},
	},
	Extensions = 
	{
		AlertableExtension = 
		{
		},
		PhysicsExtension = 
		{
			mass = 900.000000,
			static_friction = 1.700000,
			restitution = 0.800000,
			kinetic_friction = 1.200000,
		},
		LevelEditorExtension = 
		{
			flippable = true,
			teleportable = true,
		},
		WalkingExtension = 
		{
			walking_offset = 47.000000,
			autoWalks = false,
			collision_offet_y = 60.000000,
			speed = 4.300002,
			length = 50.000000,
			msgListener = true,
			collision_offet_x = -25.000000,
		},
		ThwackerExtension = 
		{
			length_forward = 60.000000,
			radius = 80.000000,
		},
		PositionExtension = 
		{
		},
	},
}

-- Begin Scripting --

help_text_header = "$00651"
help_text = "$00652"

function OnInit()

	MARSHOMECH_HASH = World_HashString("marshomech")
	MERCH_HASH = World_HashString("merch")

	sound_table = 
	{
		walking = 
		{
			{ "marshomech(walking)", 1, "Marshomech Walking 1" },
			{ "marshomech(walking)", 19, "Marshomech Walking 2" },		
		},
		thwack =
		{
			{ "marshomech(hitting_away_from_screen)", 1, "Marshomech Buildup" },
		},
		alert =		
		{
			{ "marshomech(alert)", 15, "Marshomech Roar" }
		}
	}

	local walking = this:GetWalkingExtension( )

	

	local walk_speeds = { 

			1.5, 2.2, 2.1, 2.1, 1.9, 1.9, -- 1 to 6

			2.7, 4.3, 7.1, 8.9, 9.0, 5.3, -0.0, -1.9, -1.9, 0.0, --7 to 16

			1.5, 2.2, 2.1, 2.1, 1.9, 1.9, -- 17 to 22

			2.7, 4.3, 7.1, 8.9, 9.0, 5.3, -0.0, -1.9, -1.9,  -- 23 to 31

			}

	for i in walk_speeds do

		walking:SetWalkSpeed( walk_speeds[ i ] )

	end



	local thwacker = this:GetThwackerExtension( )

	

	local thwack_speeds = { 

			0.0, 0.0, 0.0, -1.0, -2.0, -2.0, -2.0, -2.0, -- 1 to 8

			-2.0, -2.0, -2.0, -1.0, 0.0, 0.0, 0.0, 0.0, --9 to 16

			15.0, 25.0, 30.0, 15.1, 5.9, 0.0, 0.0, 0.0, -- 17 to 24

			0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,  -- 25 to 34
			
			0.0, 0.0, 0.0,											-- 35 to 37

			}

	for i in thwack_speeds do

		thwacker:SetThwackSpeed( thwack_speeds[ i ] )

	end

end

smoke_effect = nil

function Update()

	Profile_Start("marshomech")

	local objects_collided = {}
	for report in this:GetCollisionReports() do
		local object = World_GetObjectByID( report.id_2 )
		if object then
			table.insert( objects_collided, object )
		end
	end
	
	local motion_model = this:GetMotionModel()
	local frame_num = motion_model:GetCurrentMotion():GetCurrentAnim():GetCurrentFrameIndex()
	local current_state = this:GetWalkingExtension():GetState()
	
	if  current_state == WalkingExtension.WES_Thwacking then
		if not isColliding then 
			for i in objects_collided do
				local object = objects_collided[ i ]
				if object:GetBlueprintHash() == World_HashString("platform") or object:GetEmotionPlatformExtension() then
					isColliding = true
					this:SetFlipped( not this:GetFlipped() )
					break
				end
			end
		end
		
	else
		isColliding = false
	end
	
	if ( current_state == WalkingExtension.WES_Walking ) then
		
		World_Scare(this:GetPosition(), 200, 5)
		
		if frame_num == 5 or frame_num == 20 then
			this:CreateEffect( "marshomech/mech_dust" )
		end

	elseif ( current_state == WalkingExtension.WES_Alert ) then
		World_Scare(this:GetPosition(), 200, 40)
		
		if motion_model:GetCurrentMotion():GetCurrentAnim():GetCurrentFrameIndex() == 15 then
			World_CreateEffect( "marshomech/roar", this:GetPosition() + Vector2(60,-50) )
		end

	-- Gets marshomech to activate when something hits it.
	else
		for i in objects_collided do
			local object = objects_collided[ i ]
			
			local we = object:GetWalkingExtension()

			if (not we or we:GetState() ~= WalkingExtension.WES_Walking) and object:IsVisible()
				and not object:GetPositionExtension():IsBackground()
				and object:GetBlueprintHash() ~= MARSHOMECH_HASH
				and object:GetBlueprintHash() ~= MERCH_HASH then
				World_Alert(object:GetPosition(), 128)
			end
		end

	end

	if World_IsSimulating() then
	
		local sound = DetermineSound( 	motion_model:GetCurrentMotionName(), 
													motion_model:GetCurrentMotion():GetCurrentAnimName(),
													motion_model:GetCurrentMotion():GetCurrentAnim():GetCurrentFrameIndex() )

		if sound ~= nil then
			Sound_CreateSound( sound, false, 0, this:GetPosition() )
			if( sound == "marshomech_hit" ) then
				World_Scare(this:GetPosition(), 200, 100)
			end
		end
		
	end
	
	Profile_Stop("marshomech")
	
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

