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

public class Bounce : MonoBehaviour {

	[SerializeField] Vector2 dir;
	[SerializeField] float effect = 5f;
	Vector2 vel, colVel;
	Vector3 lastPos;
	float charge;

	void Start(){
		lastPos = transform.position;
	}

	void Update(){
		colVel = (transform.position - lastPos);
		colVel *= (1f / Time.deltaTime);
		lastPos = transform.position;
	}


	void OnCollisionEnter2D(Collision2D col){


		vel = col.gameObject.GetComponent<Rigidbody2D> ().velocity;
		col.gameObject.GetComponent<Rigidbody2D> ().AddForce ((new Vector2(((vel.x + 1f / (vel.x + 1f)) * dir.x) + colVel.x * effect, 
			((vel.y + 1f / (vel.y + 1f)) * dir.y)  + colVel.y * effect)) * (1f + charge), ForceMode2D.Impulse);

		col.gameObject.GetComponent<Rigidbody2D> ().mass *= 0.98f;
		charge = 0f;
	}

	public void IncreaseBounce(float intensity){
		if (dir.x < 0) {
			dir.x = dir.x-intensity;
		} else {
			dir.x = dir.x+intensity;
		}
	}

	public void SetCharge(float cha){
		charge = cha;
	}

}
