using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : PhysicsObject
{

    public float maxSpeed = 7;
    public float jumpTakeOffSpeed = 7;
    private SpriteRenderer spriteRenderer;
	private Rigidbody2D myRigidBody;
	private Vector2 startPost;
	public GameObject player;
    private void Awake()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
    }
	 
	private void Start(){
		startPost = transform.position;
		myRigidBody = GetComponent<Rigidbody2D> ();	
	}

    protected override void ComputeVelocity()
    {
        Vector2 move = Vector2.zero;

        move.x = Input.GetAxis("Horizontal");

        if (Input.GetButtonDown("Jump") && grounded)
        {
            velocity.y = jumpTakeOffSpeed;
        }
        else if (Input.GetButtonUp("Jump"))
        {
            if (velocity.y > 0)
            {
                velocity.y = velocity.y * 0.5f;
            }
        }

        bool flipSprite = (spriteRenderer.flipX ? (move.x > 0.01f) : (move.x < 0.01f));

        if (flipSprite)
        {
            spriteRenderer.flipX = !spriteRenderer.flipX;
        }

        targetVelocity = move * maxSpeed;
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
		if (collision.gameObject.CompareTag ("Finish")) {
			SceneLoader.LoadLevelScene ();
		} else if (collision.gameObject.CompareTag ("Water")) {
			myRigidBody.velocity = Vector2.zero;
			transform.position = startPost;
		}
    }
		
}

