﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Pig : MonoBehaviour
{

    public float speed = 4;
    private Rigidbody rb;

    public float thrust = 100;
    public float maxJumpTime = 0.1f;
    public bool jumping;
    private float jumpTimer;
    private Vector3 startPos;

    void Start()
    {

        rb = GetComponent<Rigidbody>();
        startPos = transform.position;
    }

    void Update()
    {
        Camera.main.transform.position =
        new Vector3(Camera.main.transform.position.x, transform.position.y, Camera.main.transform.position.z);
        if (transform.position.y <= startPos.y - 15)
        {
            string thisScene = SceneManager.GetActiveScene().name;
            print(thisScene);
            SceneManager.LoadScene(thisScene);
        }
    }

    void FixedUpdate()
    {


        if (Input.GetKey(KeyCode.UpArrow) && jumpTimer <= maxJumpTime)
        {
            jumping = true;
            rb.AddForce(Vector2.up * thrust);
        }

        if (jumping)
        {
            jumpTimer += Time.deltaTime;
        }

        if (rb.velocity.y < -0.1f || jumpTimer >= maxJumpTime)
        {


            Physics.gravity = new Vector3(0, -25f, 0);
        }

        if (jumpTimer > 2f)
        {
            ResetJump();
        }


    }

    void OnCollisionEnter(Collision col)
    {
        if (col.collider.gameObject.tag == "Floor")
        {
            print("Floor");
            ResetJump();
        }

    }

    void ResetJump()
    {
        Physics.gravity = new Vector3(0, -9.8f, 0);
        jumping = false;
        jumpTimer = 0;
    }



}
