›_ebskola.lv
sākt stundu
Prog II · 4. tēma · 6 stundas - C++ · AI · Shooter

Datu struktūras //
un AI

STL vector un map, ienaidnieku AI ar NavigationAgent2D un A* algoritmu. Rezultāts - Top-down šautene ar gudru AI.

6 stundas STL · vector · map A* pathfinding
# 01 stundu plāns

6 stundas - STL, AI un Top-down Shooter

noslēguma projekts
# 02 špikeris

STL un NavigationAgent2D špikeris

STL datu struktūras - galvenās operācijas

// std::vector - dinamisks masīvs
#include <vector>
std::vector<Bullet*> bullets;
bullets.push_back(new Bullet());     // pievieno
bullets.erase(bullets.begin() + i); // izdzēš
for (auto* b : bullets) b->update(); // iterē

// std::map - atslēga → vērtība
#include <map>
std::map<String, int> scores;
scores["Anna"] = 500;
scores["Janis"]++;
if (scores.count("Anna")) { ... }  // pārbauda vai eksistē

NavigationAgent2D A* pathfinding

// enemy_ai.cpp
void EnemyAI::_physics_process(double delta) {
    auto* nav = get_node<NavigationAgent2D>("NavigationAgent2D");
    nav->set_target_position(player->get_global_position());

    if (nav->is_navigation_finished()) return;

    Vector2 next = nav->get_next_path_position();
    Vector2 dir  = (next - get_global_position()).normalized();
    set_velocity(dir * SPEED);
    move_and_slide();
}
C++ nav->get_next_path_position(); // A* aprēķina ceļu automātiski # NavigationAgent2D + std::vector<Enemy*> = gudra, efektīva AI sistēma