bool AISystem::MoveUnitTowardsEntity(Entity* selectedUnit, Entity* entity) { World* world = engine->GetContext()->GetWorld(); Pathfinder* pathfinder = engine->GetContext()->GetPathfinder(); vector<Grid> path = pathfinder->FindPath(selectedUnit, entity); if (path.size() > 0) { PositionComponent* upc = dynamic_cast<PositionComponent*>(selectedUnit->GetComponent(Component::POSITION)); Grid lastStep = *(path.end() - 1); SelectTarget(Graphics::Instance().ToPx(lastStep)); engine->GetContext()->SetGameState(Context::PL_MOVE); selectedUnit->Add(new AnimationComponent(path, upc->pos)); engine->UpdateEntity(selectedUnit); return true; } else { return false; } }
void SelectionSystem::CreateCurrentPath(Entity* unit, Entity* tile) { World* world = engine->GetContext()->GetWorld(); Pathfinder* pathfinder = engine->GetContext()->GetPathfinder(); int remainingAP = dynamic_cast<UnitComponent*>(unit->GetComponent(Component::UNIT))->ap; std::vector<Grid> path = pathfinder->FindPath(unit, tile); ClearCurrentPath(); // make new Entities with the selection sprite and add them to the engine int counter = 1; for (unsigned int i = 0; i < path.size(); i++) { Entity* step = new Entity(); if (counter <= remainingAP) { step->Add(new TextureComponent({ Graphics::SPRITE_PATH })); } else { step->Add(new TextureComponent({ Graphics::SPRITE_PATH_FAR })); } step->Add(new PositionComponent(path[i], 1)); engine->AddEntity(step); currentPath.push_back(step); counter++; } }
Entity* AISystem::FilterNearestEntity(Entity* selectedUnit, set<Entity*> entities) { Pathfinder* pathfinder = engine->GetContext()->GetPathfinder(); Entity* nearest = nullptr; int pathSize; int shortestPathSize = INT_MAX; for (Entity* entity : entities) { vector<Grid> path = pathfinder->FindPath(selectedUnit, entity); pathSize = static_cast<int>(path.size()); if (pathSize > 0 && pathSize < shortestPathSize) { shortestPathSize = pathSize; nearest = entity; } } return nearest; }