public cCritter pickClosestAhead(cLine sightline, cCritter pcrittercenter, float visionangle) { float closestdistance = 1000000000.0f, testdistance, angle; cCritter closest = null; foreach (cCritter c in this) { cCritter ppossible = ElementAt(); angle = sightline._tangent.angleBetween( ppossible.Position.sub(pcrittercenter.Position)); if (ppossible == pcrittercenter || Math.Abs(angle) > visionangle / 2.0f) //Only go for those ahead. { continue; //Skip this critter. } testdistance = ppossible.distanceTo(sightline); //Pick closest to sightline // testdistance = ppossible->distanceTo(pcrittercenter); //OR Pick closest to pcrittercenter if (testdistance < closestdistance) { closestdistance = testdistance; closest = ppossible; } } return(closest); }
/* looks along the sightline from pcrittercenter and finds candidate critters * of the desired pruntimeclass and which are within the visionangle field around * sighline. Of these candidates, the critter that's actually closest to the * sightline is chosen.*/ public LinkedList <cCritter> touchList(cLine sightline, cCritter pcritterignore = null) { LinkedList <cCritter> touchlist = new LinkedList <cCritter>( delegate(out cCritter c1, cCritter c2) { c1 = c2; } ); foreach (cCritter c in this) { if (ElementAt().touch(sightline) && ElementAt() != pcritterignore) { touchlist.Add(ElementAt()); } } return(touchlist); }
public cCritter pickClosestTouched(cLine sightline, cCritter pcritterignore) { LinkedList <cCritter> touchlist = touchList(sightline, pcritterignore); if (touchlist.Size == 0) { return(null); } float closestdistance = 1000000000.0f, testdistance; cCritter closest = touchlist.ElementAt(0); foreach (cCritter c in touchlist) { testdistance = touchlist.ElementAt().distanceTo(sightline); if (testdistance < closestdistance) { closestdistance = testdistance; closest = touchlist.ElementAt(); } } return(closest); }
public cCritter pickTopTouched(cLine sightline, cCritter pcritterignore) { /*Same code as pickClosestTouched but with a different sorting critereon; * we use lineCoord(sightline) instead of distanceTo(sightline). */ LinkedList <cCritter> touchlist = touchList(sightline, pcritterignore); if (touchlist.Size == 0) { return(null); } float closestdistance = 1000000000.0f, testdistance; cCritter closest = touchlist.ElementAt(0); //Everyone in the list is acceptable, so start with 0 is ok. foreach (cCritter c in touchlist) { testdistance = sightline.lineCoord(touchlist.ElementAt().Position); if (testdistance < closestdistance) { closestdistance = testdistance; closest = touchlist.ElementAt(); } } return(closest); }