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

public class Damager : MonoBehaviour
{
    public int player;
    ChessScript cs;
    float lastgiven;
    public float damagemultiplier;
    private void Awake()
    {
        lastgiven = -10000;
        player = transform.parent.GetComponent<Piece>().player;
        cs = GameObject.Find("Camera").GetComponent<ChessScript>();
    }
    private void OnTriggerEnter(Collider collider)
    {
        if (Time.time - lastgiven < 2) return;
        //print("fuh");
        if (transform.parent.GetComponent<Piece>().state != "jumping") return;
        if (collider.transform.GetComponent<Piece>() == null) return;
        int otherplayer = collider.transform.GetComponent<Piece>().player;
        if (otherplayer != player) // && (GetComponent<Rigidbody>().velocity.magnitude < collision.rigidbody.velocity.magnitude))
        {
            //print("I got ger");
            Vector3 diff = (collider.transform.position - transform.position).normalized;
            diff = new Vector3(diff.x, 0, diff.z);
            
            collider.GetComponent<Rigidbody>().velocity = Vector3.zero;
            collider.transform.position = collider.transform.position + Vector3.up * 0.2f;
            
            collider.GetComponent<Rigidbody>().AddForce(diff * 400 *damagemultiplier + Vector3.up * 300*damagemultiplier); //AddExplosionForce(1000, transform.position, 50);
            
            transform.parent.GetComponent<Rigidbody>().velocity = transform.parent.GetComponent<Rigidbody>().velocity / 3;
            /*print("I gave force");
            print(player);
            print(cs.immediateturn);
            print(diff * 400 + Vector3.up * 200);*/
            lastgiven = Time.time;
        }
    }
}
