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

public class CarController : MonoBehaviour{

    public GameController gameController;
    public float invulnerableTime = 2f;
    Animator anim;

    // Start is called before the first frame update
    void Start(){
        anim = GetComponent<Animator>();
    }

    void OnTriggerEnter2D(Collider2D col){
        if(col.CompareTag("Player")){
            gameController.playered = true;
        }
    }
    void OnTriggerExit2D(Collider2D col){
        if (col.CompareTag("Player")){
            gameController.playered = false;
        }
    }
    bool damaged;
    void OnCollisionEnter2D(Collision2D col){
        if (col.gameObject.CompareTag("Enemy") && !damaged || col.gameObject.CompareTag("BigEnemy") && !damaged){
            if(gameController.level == 2){
                gameController.health.TakeDamage(3.5f);
            }else if(gameController.level == 3){
                gameController.health.TakeDamage(5);
            }else{
                gameController.health.TakeDamage(2);
            }
            AudioManager.instance.Play("Hit");
            anim.SetBool("Damaged", true);
            damaged = true;
            Invoke(nameof(Damaged), invulnerableTime);
        }
        if (col.gameObject.CompareTag("RocketEnemy") && !damaged){
            if (gameController.level == 2){
                gameController.health.TakeDamage(7f);
            }else if (gameController.level == 3){
                gameController.health.TakeDamage(8.5f);
            }else{
                gameController.health.TakeDamage(4);
            }
            AudioManager.instance.Play("Hit");
            anim.SetBool("Damaged", true);
            damaged = true;
            Invoke(nameof(Damaged), invulnerableTime);
        }
    }

    void Damaged(){
        damaged = false;
        anim.SetBool("Damaged", false);
    }

    void OnCollisionStay2D(Collision2D col){
        if (col.gameObject.CompareTag("CircleEnemy") && !damaged){
            if (col.gameObject.GetComponent<CircleEnemyController>().hp == 2){
                gameController.health.TakeDamage(0.025f);
            }else if (col.gameObject.GetComponent<CircleEnemyController>().hp == 1){
                gameController.health.TakeDamage(0.01f);
            }
        }
    }
}
