feat:洋流图,预烘焙寻路实现(缓存怪物前进路线)

This commit is contained in:
sjm 2024-06-11 18:37:56 +08:00
parent 3c10a01c69
commit 7f5129c48c
4 changed files with 108 additions and 4 deletions

View File

@ -137,6 +137,7 @@
<ClInclude Include="game_manager.h" />
<ClInclude Include="manager.h" />
<ClInclude Include="map.h" />
<ClInclude Include="route.h" />
<ClInclude Include="tile.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

View File

@ -35,5 +35,8 @@
<ClInclude Include="map.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="route.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -3,14 +3,19 @@
#define _MAP_H_
#include "tile.h"
#include "route.h"
#include <SDL.h>
#include <string>
#include <fstream>
#include <sstream>
#include <unordered_map> //哈希表,无序关联容器
class Map
{
public:
typedef std::unordered_map<int, Route> SpawnerRoutePool;
public:
Map() = default;
~Map() = default;
@ -54,9 +59,13 @@ public:
tile_map = tile_map_temp;
generate_map_cache();
return true;
}
size_t get_width() const //const²»»á¶ÔÀàÄÚ½øÐÐÐÞ¸Ä
{
if (tile_map.empty())
@ -72,7 +81,8 @@ public:
private:
TileMap tile_map;
SDL_Point idx_home = { 0 };
SDL_Point idx_home = { 0 };//房屋点
SpawnerRoutePool spawner_route_pool;//哈希表构建的路径池
private:
@ -118,7 +128,7 @@ private:
}
//生成地图缓存
//生成地图缓存,附带生成路径
void generate_map_cache()
{
for (int y = 0; y < get_height(); y++)
@ -126,14 +136,18 @@ private:
for (int x = 0; x < get_width(); x++)
{
const Tile& tile = tile_map[y][x];
if (tile.special_flag < 0)
if (tile.special_flag < 0)//普通单元格是-1
continue;
if (tile.special_flag == 0)
if (tile.special_flag == 0)//代表是房屋其余1234是刷怪点
{
idx_home.x == x;
idx_home.y == y;
}
else
{
spawner_route_pool[tile.special_flag] = Route(tile_map, { x, y });
}
}
}
}

86
TdGame/route.h Normal file
View File

@ -0,0 +1,86 @@
#pragma once
#ifndef _ROUTE_H_
#define _ROUTE_H_
#include "tile.h"
#include <SDL.h>
#include <vector>
class Route
{
public:
typedef std::vector<SDL_Point> Idxlist;
public:
Route() = default;
//Óвι¹Ô캯Êý
Route(const TileMap& map,SDL_Point& idx_origin)
{
size_t width_map = map[0].size();
size_t height_map = map.size();
SDL_Point idx_next = idx_origin;
while (true)
{
if (idx_next.x >= width_map || idx_next.y >= height_map)
break;
if (check_duplicate_idx(idx_next))
break;
else
idx_list.push_back(idx_next);
bool is_next_dir_exist = true;
const Tile& tile = map[idx_next.y][idx_next.x];
if (tile.special_flag == 0)
break;
switch (tile.direction)
{
case Tile::Direction::Up:
idx_next.y--;
break;
case Tile::Direction::Down:
idx_next.y++;
break;
case Tile::Direction::Left:
idx_next.x--;
break;
case Tile::Direction::Right:
idx_next.x++;
break;
default:
is_next_dir_exist = false;
break;
}
if (!is_next_dir_exist)
break;
}
}
~Route() = default;
const Idxlist& get_idx_list() const
{
return idx_list;
}
private:
Idxlist idx_list;
private:
bool check_duplicate_idx(const SDL_Point& target_idx)
{
for (const SDL_Point& idx : idx_list)
{
if (idx.x == target_idx.x && idx.y == target_idx.y)
return true;
}
return false;
}
};
#endif