using System;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

namespace UnityStandardAssets._2D
{
	[RequireComponent(typeof (PlatformerCharacter2D))]
	public class Platformer2DUserControlAudio : MonoBehaviour
	{
		private PlatformerCharacter2D m_Character;
		private bool m_Jump;
		private float threshold = 0.30f;
		private float thresholdJump = 0.650f;
		public GameObject audioInputObject;
		MicrophoneInput micIn;
		
		
		
		private void Awake()
		{
			m_Character = GetComponent<PlatformerCharacter2D>();
		}

		void Start(){

          
                
                micIn = (MicrophoneInput)audioInputObject.GetComponent("MicrophoneInput");

            

        }
		
		private void Update()
		{
			if (!m_Jump)
			{
				if (micIn.loudness >= thresholdJump) { 
                    // Read the jump input in Update so button presses aren't missed.
                    Debug.Log("Jump");
					m_Jump = true;
				}
			}
		}
		
		
		private void FixedUpdate()
		{
			float h = 0;
			bool crouch = Input.GetKey (KeyCode.LeftControl);
			// Überprüfe ob Eingabe Lautstärke den SChwellwert übersteigt
			if (micIn.loudness > threshold) {
				h = micIn.loudness;
         			}
			// Pass all parameters to the character control script.
			m_Character.Move(h, crouch, m_Jump);
			m_Jump = false;
		}
	}
}