candyapplecorn

entity

Jul 2nd, 2014
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.52 KB | None | 0 0
  1. // Entity uses, takes or wraps around a pathfinder in order to calculate
  2. // its position. entity's location should be a coordinate pair, each element
  3. // a number of pixels.
  4. template <class T>
  5. class entity
  6. {
  7.     pathFinder<T> * pf;
  8.     pair<int, int> pixelCoord; // the current location of the entity in pixels
  9.     pair<int, int> noPixelCoord; // the current element the entity is closest to.
  10.     pair<int, int> pixelDestination;
  11.     pair<int, int> noPixelDestination; // ??
  12.     time_point<system_clock, system_clock::duration> t;
  13.     int pixelsPerSecond = 10;
  14. public:
  15.     entity<T>(pathFinder<T>& p){
  16.         pf = &p;
  17.         pixelCoord.first = p.getBegin().first * p.getLength() - p.getLength() / 2;
  18.         pixelCoord.second = p.getBegin().second * p.getWidth() - p.getWidth() / 2;
  19.         noPixelCoord = p.getBegin();
  20.         t = steady_clock::now();
  21.     }
  22.     int distance(pair<int, int> first, pair<int, int> second){
  23.         int x = first.first, y = first.second;
  24.         int x2 = second.first, y2 = second.second;
  25.         int xdif = x - x2, ydif = y - y2;
  26.         return sqrt(xdif*xdif + ydif*ydif);
  27.     }
  28.     float angle(pair<int, int> first, pair<int, int> second){
  29.         int x = first.first, y = first.second;
  30.         int x2 = second.first, y2 = second.second;
  31.         float numerator = x * x2 + y * y2;
  32.         float denominator = sqrt(x*x + y*y) * sqrt(x2*x2 + y2*y2);
  33.         return (numerator / denominator);
  34.     }
  35.     void update(){
  36.         if (this->findDestination()){
  37.             if (steady_clock::now() - t > milliseconds(1000)){
  38.                 int distanc = distance(noPixelCoord, noPixelDestination);
  39.                 distanc = (distanc > pixelsPerSecond ? pixelsPerSecond : distanc);
  40.                 float angl = angle(noPixelCoord, noPixelDestination);
  41.                 noPixelCoord.first += acos(angl) * distanc;
  42.                 noPixelCoord.second += asin(angl) * distanc;
  43.                 cout << "Updated! ";
  44.             }
  45.         }
  46.     }
  47.     // findDestination gets the closest next square the entity should path to.
  48.     bool findDestination(){
  49.         if (pf->check()){
  50.             if (pf->getEnd() == noPixelCoord) // if the entity has reached the end
  51.                 return false;
  52.             // First, check to see if we're one away from the end.
  53.             else if (pf->oneD(noPixelCoord.first + 1, noPixelCoord.second) == pf->oneD(pf->getEnd()) ||
  54.                 pf->oneD(noPixelCoord.first - 1, noPixelCoord.second) == pf->oneD(pf->getEnd()) ||
  55.                 pf->oneD(noPixelCoord.first, noPixelCoord.second + 1) == pf->oneD(pf->getEnd()) ||
  56.                 pf->oneD(noPixelCoord.first, noPixelCoord.second - 1) == pf->oneD(pf->getEnd()))
  57.                 noPixelDestination = pf->getEnd();
  58.             else{ // Since we aren't one away from the end, find the closest proper element
  59.                 pair<int, int> arr[] = {pair<int, int>(noPixelCoord.first + 1, noPixelCoord.second),
  60.                     pair<int, int>(noPixelCoord.first - 1, noPixelCoord.second),
  61.                     pair<int, int>(noPixelCoord.first, noPixelCoord.second + 1),
  62.                     pair<int, int>(noPixelCoord.first, noPixelCoord.second - 1) };
  63.                 list<pair<int, int> > l(arr, arr + sizeof(arr) / sizeof(pair<int, int>));
  64.                 l.sort([&](pair<int, int> a, pair<int, int> b){ return pf->operator()(a) < pf->operator()(b); });
  65.                 while (pf->operator()(l.front())) // pop all zeros
  66.                     l.pop_front();
  67.                 while (pf->operator()(l.front()) == pf->WALL) // pop all walls
  68.                     l.pop_front();
  69.                 if (!l.empty()) // get the first lowest number that isn't a wall or a zero
  70.                     noPixelDestination = l.front();
  71.             }
  72.  
  73.             // Place the pixelDestination in the middle of the square @ nopixeldestination
  74.             pixelDestination.first = noPixelDestination.first * pf->getLength() - pf->getLength() / 2;
  75.             pixelDestination.second = noPixelDestination.second * pf->getWidth() - pf->getWidth() / 2;
  76.         }
  77.         return true;
  78.     }
  79. };
Advertisement
Add Comment
Please, Sign In to add comment