Example #1
0
/**
 * @brief Calculates the direction and the speed of the movement
 * depending on the target.
 */
void PathFindingMovement::recompute_movement() {

    PathFinding path_finding(get_entity()->get_map(), *get_entity(), *target);
    std::string path = path_finding.compute_path();

    uint32_t min_delay;
    if (path.size() == 0) {
        // the target is too far or there is no path
        path = create_random_path();

        // no path was found: no need to try again very soon
        // (note that the A* algorithm is very costly when it explores all nodes without finding a solution)
        min_delay = 3000;
    }
    else {
        // a path was found: we need to update it frequently (and the A* algorithm is much faster in general when there is a solution)
        min_delay = 300;
    }
    // compute a new path every random delay to avoid
    // having all path-finding entities of the map compute a path at the same time
    next_recomputation_date = System::now() + min_delay + Random::get_number(200);

    set_path(path);
}
Example #2
0
/**
 * @brief Creates a random walk movement object.
 * @param speed speed of the movement in pixels per second
 */
RandomPathMovement::RandomPathMovement(int speed):
  PathMovement(create_random_path(), speed, false, false, false) {

}