feat:洋流图,预烘焙寻路实现(缓存怪物前进路线)
This commit is contained in:
parent
3c10a01c69
commit
7f5129c48c
@ -137,6 +137,7 @@
|
|||||||
<ClInclude Include="game_manager.h" />
|
<ClInclude Include="game_manager.h" />
|
||||||
<ClInclude Include="manager.h" />
|
<ClInclude Include="manager.h" />
|
||||||
<ClInclude Include="map.h" />
|
<ClInclude Include="map.h" />
|
||||||
|
<ClInclude Include="route.h" />
|
||||||
<ClInclude Include="tile.h" />
|
<ClInclude Include="tile.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
|
@ -35,5 +35,8 @@
|
|||||||
<ClInclude Include="map.h">
|
<ClInclude Include="map.h">
|
||||||
<Filter>头文件</Filter>
|
<Filter>头文件</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="route.h">
|
||||||
|
<Filter>头文件</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
22
TdGame/map.h
22
TdGame/map.h
@ -3,14 +3,19 @@
|
|||||||
#define _MAP_H_
|
#define _MAP_H_
|
||||||
|
|
||||||
#include "tile.h"
|
#include "tile.h"
|
||||||
|
#include "route.h"
|
||||||
|
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <unordered_map> //哈希表,无序关联容器
|
||||||
|
|
||||||
class Map
|
class Map
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
typedef std::unordered_map<int, Route> SpawnerRoutePool;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Map() = default;
|
Map() = default;
|
||||||
~Map() = default;
|
~Map() = default;
|
||||||
@ -54,9 +59,13 @@ public:
|
|||||||
|
|
||||||
tile_map = tile_map_temp;
|
tile_map = tile_map_temp;
|
||||||
|
|
||||||
|
generate_map_cache();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
size_t get_width() const //const²»»á¶ÔÀàÄÚ½øÐÐÐÞ¸Ä
|
size_t get_width() const //const²»»á¶ÔÀàÄÚ½øÐÐÐÞ¸Ä
|
||||||
{
|
{
|
||||||
if (tile_map.empty())
|
if (tile_map.empty())
|
||||||
@ -72,7 +81,8 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
TileMap tile_map;
|
TileMap tile_map;
|
||||||
SDL_Point idx_home = { 0 };
|
SDL_Point idx_home = { 0 };//房屋点
|
||||||
|
SpawnerRoutePool spawner_route_pool;//哈希表构建的路径池
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
@ -118,7 +128,7 @@ private:
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//生成地图缓存
|
//生成地图缓存,附带生成路径
|
||||||
void generate_map_cache()
|
void generate_map_cache()
|
||||||
{
|
{
|
||||||
for (int y = 0; y < get_height(); y++)
|
for (int y = 0; y < get_height(); y++)
|
||||||
@ -126,14 +136,18 @@ private:
|
|||||||
for (int x = 0; x < get_width(); x++)
|
for (int x = 0; x < get_width(); x++)
|
||||||
{
|
{
|
||||||
const Tile& tile = tile_map[y][x];
|
const Tile& tile = tile_map[y][x];
|
||||||
if (tile.special_flag < 0)
|
if (tile.special_flag < 0)//普通单元格是-1
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (tile.special_flag == 0)
|
if (tile.special_flag == 0)//代表是房屋,其余1234是刷怪点
|
||||||
{
|
{
|
||||||
idx_home.x == x;
|
idx_home.x == x;
|
||||||
idx_home.y == y;
|
idx_home.y == y;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
spawner_route_pool[tile.special_flag] = Route(tile_map, { x, y });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
86
TdGame/route.h
Normal file
86
TdGame/route.h
Normal 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
|
Loading…
Reference in New Issue
Block a user