Character* CharacterManager::GetNearestCharacter(Character* c, int angle) { float angle_rad = angle * 3.1415f / 180.0f; float x, y, w, h; c->GetPosition(x, y); //1 unidade = 32 pixels //Sprites têm 32 pixels (geralmente) = 1 unidades. //TODO: Character::Get/SetSize() w = 32; h = 32; Character* destiny = NULL; for (auto it = _chars.begin(); it != _chars.end(); it++) { float destx, desty; (*it)->GetPosition(destx, desty); Sprite* s; (*it)->GetSprite(&s); w = (s->GetFrameWidth() / 32.0f) * s->GetZoomFactor(); h = (s->GetFrameHeight() / 32.0f) * s->GetZoomFactor(); if (destx >= (x - w) && destx <= (x + w)) { if (desty >= (y - h) && desty <= (y + h)) { if (c->GetID() != (*it)->GetID()){ /* Otimizado para ângulos retos TODO: Otimizar de 45 em 45, não é necessário agora*/ bool isOK = false; switch (angle) { case 0: isOK = (destx > x); break; case 90: isOK = (desty > y); break; case 180: isOK = (x > destx); break; case 270: isOK = (y > desty); break; case 360: isOK = true; } if (isOK) destiny = (Character*)(*it); } } } } return destiny; }