package collisions;

import actors.MyActor;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import java.util.HashMap;
import java.util.LinkedList;


public class WorldCollector {

    private HashMap<Vector2,LinkedList<MyActor>> collection ;
    public WorldCollector() {
        this.collection = new HashMap<Vector2, LinkedList<MyActor>>();
    }

    public void add(MyActor actor){
        LinkedList<MyActor> keyList;
        float x = (float)Math.floor(actor.getX()/64);
        float y = (float)Math.floor(actor.getY()/64);
        float l = 0;
        float w = 0;
        Vector2 v = new Vector2();
        for (int i = 0 ; i< Math.ceil(actor.getWidth()/64); i++){
            x += l;
            for (int j = 0 ; j< Math.ceil(actor.getHeight()/64); j++) {
                y += w;
                v.set(x,y);
                keyList = this.collection.get(v);
                if (keyList != null) {
                    if (!keyList.contains(actor)) {
                        keyList.add(actor);
                        this.collection.remove(v);
                        this.collection.put(v, keyList);
                    }
                } else {
                    keyList = new LinkedList<MyActor>();
                    keyList.add(actor);
                    this.collection.put(v, keyList);
                }
                w+=64;
            }
            l+=64;
        }
    }

    public boolean isHit (Vector2 v){
        return this.collection.containsKey(v);
    }


    private Vector2 getHashFromVector(MyActor actor){
        float x = (float)Math.floor(actor.getX()/64);
        float y = (float)Math.floor(actor.getY()/64);
        return new Vector2(x,y);
    }

    /**
     * version 1
     * @param actor
     * @return the actor that hit the actor
     */
    public MyActor hit(MyActor actor){
        Vector2 vector2 = getHashFromVector(actor);
        if (isHit(vector2)){
            LinkedList<MyActor> keyList = this.collection.get(vector2);
            Rectangle rect =  new Rectangle();
            for (MyActor aKeyList : keyList) {
                if (Intersector.intersectRectangles(actor.getHitbox(), aKeyList.getHitbox(), rect)) {
                    return aKeyList;
                }
            }
        }
        return null;
    }
}

@@ -245,16 +245,53 @@
 ctor %7B%0A%0A
+    public static int hashSize = 64;%0A
     priv
@@ -566,34 +566,40 @@
 or(actor.getX()/
-64
+hashSize
 );%0A        float
@@ -626,34 +626,40 @@
 or(actor.getY()/
-64
+hashSize
 );%0A        float
@@ -777,18 +777,24 @@
 Width()/
-64
+hashSize
 ); i++)%7B
@@ -874,18 +874,24 @@
 eight()/
-64
+hashSize
 ); j++) 
@@ -1471,18 +1471,24 @@
      w+=
-64
+hashSize
 ;%0A      
@@ -1510,18 +1510,24 @@
      l+=
-64
+hashSize
 ;%0A      
@@ -1733,18 +1733,24 @@
 .getX()/
-64
+hashSize
 );%0A     
@@ -1793,18 +1793,24 @@
 .getY()/
-64
+hashSize
 );%0A     

refactor de constante