SHARE
TWEET
Untitled
a guest
Nov 6th, 2016
67
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- public Steering nearestEntity(AISteering aiSteering, float maxSearchRadius)
- {
- IntArray ignoredIndex = new IntArray();
- float compareX,compareY;
- float compareSum;
- float smallestSum = 0;
- int index = 0;
- for(int k = 0; k < EntityManager.getEntity().size; k ++)
- {
- if(EntityManager.getEntity(k) == aiSteering.getEntity())
- {
- ignoredIndex.add(k); //index of the entity that is searching for the nearest entity. Ignore so it doesn't find itself
- }
- else
- {
- if(!aiSteering.getHostilityList().contains(hostilityMap.get(EntityManager.getEntity(k)).hostile, false))
- {
- ignoredIndex.add(k);
- }
- }
- }
- compareX = Math.abs(positionMap.get(EntityManager.getSteering(0).getEntity()).getX() - aiSteering.getPosition().x);
- compareY = Math.abs(positionMap.get(EntityManager.getSteering(0).getEntity()).getY() - aiSteering.getPosition().y);
- compareSum = (float) Math.sqrt(Math.pow(compareX,2) + Math.pow(compareY,2));
- if(compareSum > maxSearchRadius)
- {
- ignoredIndex.add(0);
- }
- if(!ignoredIndex.contains(0))
- {
- smallestSum = compareSum;
- index = 0;
- }
- else if(EntityManager.getEntity().size > 1) //to prevent out of bounds
- {
- int ignoree = 1;
- if(!ignoredIndex.contains(ignoree))
- {
- compareX = Math.abs(positionMap.get(EntityManager.getSteering(ignoree).getEntity()).getX() - aiSteering.getPosition().x);
- compareY = Math.abs(positionMap.get(EntityManager.getSteering(ignoree).getEntity()).getY() - aiSteering.getPosition().y);
- compareSum = (float) Math.sqrt(Math.pow(compareX,2) + Math.pow(compareY,2));
- if(compareSum > maxSearchRadius)
- {
- ignoredIndex.add(ignoree);
- }
- else
- {
- smallestSum = compareSum;
- index = ignoree;
- }
- }
- while(ignoredIndex.contains(ignoree))
- {
- ignoree++;
- if(ignoree >= EntityManager.getSteering().size)
- {
- ignoree = EntityManager.getSteering().size-1;
- break;
- }
- if(!ignoredIndex.contains(ignoree))
- {
- compareX = Math.abs(positionMap.get(EntityManager.getSteering(ignoree).getEntity()).getX() - aiSteering.getPosition().x);
- compareY = Math.abs(positionMap.get(EntityManager.getSteering(ignoree).getEntity()).getY() - aiSteering.getPosition().y);
- compareSum = (float) Math.sqrt(Math.pow(compareX,2) + Math.pow(compareY,2));
- if(compareSum > maxSearchRadius)
- {
- ignoredIndex.add(ignoree);
- }
- else
- {
- smallestSum = compareSum;
- index = ignoree;
- }
- }
- }
- }
- for(int k = 0; k < EntityManager.getEntity().size; k ++)
- {
- if(k == 0 && (ignoredIndex.contains(0) || ignoredIndex.contains(1)))
- {
- k = 2;
- }
- if((!ignoredIndex.contains(0) && !ignoredIndex.contains(1)) && k == 0)
- {
- k = 1;
- }
- while(ignoredIndex.contains(k))
- {
- k++;
- }
- if(k < EntityManager.getEntity().size)
- {
- compareX = Math.abs(positionMap.get(EntityManager.getSteering(k).getEntity()).getX() - aiSteering.getPosition().x);
- compareY = Math.abs(positionMap.get(EntityManager.getSteering(k).getEntity()).getY() - aiSteering.getPosition().y);
- compareSum = (float) Math.sqrt(Math.pow(compareX,2) + Math.pow(compareY,2));
- if(compareSum < smallestSum && compareSum <= maxSearchRadius)
- {
- smallestSum = compareSum;
- index = k;
- }
- }
- }
- if(ignoredIndex.contains(index))
- {
- return null;
- }
- return EntityManager.getSteering(index);
- }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

