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

public class GravitySampler
{
    private const int GRAVITYPOINTER_LAYERMASK = 3;

    public static Vector2 Sample(Vector2 position) {
        Collider2D[] colliders = Physics2D.OverlapPointAll(position, 1 << GRAVITYPOINTER_LAYERMASK);

        Vector2 gravity = Vector2.zero;
        foreach(Collider2D collider in colliders) {
            Vector3 pos = collider.transform.position;
            float distance = Vector3.Distance(pos, position);
            float normalizedDistance = distance / collider.transform.localScale.x;

            // Output
            float amount = Mathf.Clamp01(1 - normalizedDistance);
            Vector3 direction = -collider.transform.up;

            gravity += amount*(Vector2)direction;
        }
        return gravity;
    }
}
