tiledmap 分类读取
This commit is contained in:
parent
15e11666b5
commit
15bc086a4d
118
cpp/tiledmap.cc
118
cpp/tiledmap.cc
@ -1,5 +1,5 @@
|
||||
#include <a8/a8.h>
|
||||
|
||||
#include <a8/stringlist.h>
|
||||
#include "framework/cpp/tiledmap.h"
|
||||
|
||||
a8::XValue TiledObject::GetProperty(const std::string& prop_name)
|
||||
@ -38,6 +38,7 @@ bool TiledMap::LoadTmxFile(const std::string& filename)
|
||||
tile_count = tileset_node->At("attrs.tilecount")->AsXValue();
|
||||
tile_width = tileset_node->At("attrs.tilewidth")->AsXValue();
|
||||
tile_height = tileset_node->At("attrs.tileheight")->AsXValue();
|
||||
tile_columns = tileset_node->At("attrs.columns")->AsXValue();
|
||||
for (int i = 0; i < xobj.At("child_node.layer")->Size(); ++i) {
|
||||
std::shared_ptr<a8::XObject> layer_node = xobj.At("child_node.layer")->At(i);
|
||||
|
||||
@ -93,7 +94,7 @@ bool TiledMap::LoadTmxFile(const std::string& filename)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -104,8 +105,121 @@ std::list<TiledObject>* TiledMap::GetObjectGroup(const std::string& object_class
|
||||
return itr != object_group_hash.end() ? &itr->second : nullptr;
|
||||
}
|
||||
|
||||
void TiledMap::Init()
|
||||
{
|
||||
grid_cell_list.reserve(tile_count);
|
||||
grid_cell_list.assign(tile_count,GridCell{});
|
||||
|
||||
for (auto& pair : layer_hash) {
|
||||
for (auto& layer : pair.second) {
|
||||
|
||||
std::string name = layer.GetProperty("name").GetString();
|
||||
bool has_speed = layer.HasProperty("speed");
|
||||
int speed = layer.GetProperty("speed");
|
||||
|
||||
bool has_trigger = layer.HasProperty("isTrigger");
|
||||
bool istrigger = layer.GetProperty("isTrigger");;
|
||||
std::string trigger_point = layer.GetProperty("objName").GetString();
|
||||
|
||||
bool has_num = layer.HasProperty("num");
|
||||
//int num = layer.GetProperty("num");
|
||||
|
||||
std::vector<GridCell*> grid_cell_path_list;
|
||||
a8::StringList str_list;
|
||||
str_list.SetText(layer.data.GetString().c_str());
|
||||
for (int i = 2; i < str_list.Count(); ++i) {
|
||||
std::string str_line = str_list.String(i);
|
||||
std::vector<std::string> split_list;
|
||||
a8::Split(str_line, split_list);
|
||||
int split_count = split_list.size()-1;
|
||||
|
||||
for(int j = 0; j < split_count; ++j){
|
||||
int value = a8::XValue(split_list[j]);
|
||||
if(value > 0){
|
||||
int index = (i-2)*tile_columns + j;
|
||||
if (has_speed) {
|
||||
grid_cell_list[index].x = i-1;
|
||||
grid_cell_list[index].y = j+1;
|
||||
grid_cell_list[index].speed = speed;
|
||||
grid_cell_list[index].value = value;
|
||||
}
|
||||
if (has_trigger) {
|
||||
TriggerPoint tp;
|
||||
{
|
||||
tp.x = i;
|
||||
tp.y = j+1;
|
||||
tp.point = trigger_point;
|
||||
tp.istrigger = istrigger;
|
||||
trigger_list.push_back(tp);
|
||||
}
|
||||
}
|
||||
if (has_num) {
|
||||
grid_cell_path_list.push_back(&grid_cell_list[index]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (grid_cell_path_list.size() > 0) {
|
||||
stage_path_hash[name] = grid_cell_path_list;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (auto& pair : object_group_hash) {
|
||||
for (auto& object : pair.second) {
|
||||
StagePoint sp;
|
||||
sp.x = object.GetProperty("tile_x");
|
||||
sp.y = object.GetProperty("tile_y");
|
||||
sp.point = object.GetProperty("location").GetString();
|
||||
std::string str_relation = object.GetProperty("relation").GetString();
|
||||
std::vector<std::string> split_list;
|
||||
a8::Split(str_relation, split_list, ',');
|
||||
for (auto& point : split_list) {
|
||||
sp.relation_list.push_back(point);
|
||||
}
|
||||
std::string name = object.GetProperty("name").GetString();
|
||||
stage_object_hash[name] = sp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TiledMap::Dump()
|
||||
{
|
||||
Init();
|
||||
#if 0
|
||||
for (auto& pair : stage_path_hash) {
|
||||
a8::XPrintf("stage$:%s\n", {pair.first});
|
||||
for (auto& gc : pair.second) {
|
||||
a8::XPrintf("path$x:%d,y:%d,speed:%d,value:%d\n", {gc->x, gc->y, gc->speed, gc->value});
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#if 0
|
||||
for (auto& tp : trigger_list) {
|
||||
a8::XPrintf("x:%d,y:%d,point:%d,istrigger:%d\n", {tp.x, tp.y, tp.point, a8::XValue(tp.istrigger)});
|
||||
}
|
||||
#endif
|
||||
#if 0
|
||||
int len = grid_cell_list.size();
|
||||
a8::XPrintf("grid_cell_size:%s\n", {len});
|
||||
for (int i= 0; i < len; ++i) {
|
||||
auto gc = grid_cell_list[i];
|
||||
a8::XPrintf("i:%d, x:%d,y:%d,speed:%d,value:%d\n", {i, gc.x, gc.y, gc.speed, gc.value});
|
||||
}
|
||||
#endif
|
||||
#if 0
|
||||
for (auto& pair : stage_object_hash) {
|
||||
a8::XPrintf("object$:%s\n", {pair.first});
|
||||
a8::XPrintf("value$x:%d,y:%d,p:%s,relation:", {pair.second.x, pair.second.y, pair.second.point});
|
||||
for (auto& relation : pair.second.relation_list) {
|
||||
a8::XPrintf("%s,", {relation});
|
||||
}
|
||||
a8::XPrintf("\n",{});
|
||||
}
|
||||
#endif
|
||||
return;
|
||||
|
||||
a8::XPrintf("map$tile_count:%d\n", {tile_count});
|
||||
a8::XPrintf("map$tile_width:%d\n", { tile_width });
|
||||
a8::XPrintf("map$tile_height:%d\n", { tile_height });
|
||||
|
@ -22,6 +22,31 @@ class TiledLayer
|
||||
a8::XValue data;
|
||||
};
|
||||
|
||||
//格子对象
|
||||
struct GridCell
|
||||
{
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
int speed = 0;
|
||||
int value = 0;
|
||||
};
|
||||
|
||||
struct StagePoint
|
||||
{
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
std::string point;
|
||||
std::vector<std::string> relation_list;
|
||||
};
|
||||
|
||||
struct TriggerPoint
|
||||
{
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
std::string point;
|
||||
bool istrigger = false;
|
||||
};
|
||||
|
||||
class TiledMap
|
||||
{
|
||||
public:
|
||||
@ -30,13 +55,21 @@ class TiledMap
|
||||
int tile_count = 0; //瓦片地图的尺寸。(以瓦片数量为单位)
|
||||
int tile_width = 0; //瓦片的宽。(以像素点为单位)
|
||||
int tile_height = 0; //瓦片的高。(以像素点为单位)
|
||||
int tile_columns = 0; //列
|
||||
|
||||
bool LoadTmxFile(const std::string& filename);
|
||||
std::list<TiledObject>* GetObjectGroup(const std::string& object_class_name);
|
||||
void Dump();
|
||||
void Init();
|
||||
|
||||
private:
|
||||
std::map<std::string, std::list<TiledLayer>> layer_hash;
|
||||
std::map<std::string, std::list<TiledObject>> object_group_hash;
|
||||
|
||||
std::map<std::string, StagePoint> stage_object_hash; //key point
|
||||
std::vector<TriggerPoint> trigger_list;
|
||||
std::map<std::string, std::vector<GridCell*>> stage_path_hash; //key A->B AB
|
||||
std::vector<GridCell> grid_cell_list; //所有的格子数组 size = x * y
|
||||
|
||||
std::map<std::string, std::vector<GridCell>> grid_cell_hash; //key 11|2 坐标
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user