1
This commit is contained in:
parent
a3200f0ea5
commit
2a4fe90981
@ -1,8 +0,0 @@
|
||||
package im;
|
||||
|
||||
//消息id定义
|
||||
enum IMMessageId_e
|
||||
{
|
||||
_IMSocketConnect = 1;
|
||||
_IMSocketDisconnect = 2;
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
package im;
|
||||
|
||||
//常量
|
||||
enum Constant_e
|
||||
{
|
||||
MaxIMMsgId = 7;
|
||||
}
|
||||
|
||||
message IMSocketConnect
|
||||
{
|
||||
optional bool is_websocket = 1;
|
||||
optional string host = 2;
|
||||
optional string url = 3;
|
||||
optional string query_str = 4;
|
||||
}
|
||||
|
||||
message IMSocketDisconnect
|
||||
{
|
||||
|
||||
}
|
272
f8/inputgeom.cc
272
f8/inputgeom.cc
@ -1,272 +0,0 @@
|
||||
#include "precompile.h"
|
||||
|
||||
#include "Recast.h"
|
||||
|
||||
#include "inputgeom.h"
|
||||
|
||||
struct BoundsItem
|
||||
{
|
||||
float bmin[2];
|
||||
float bmax[2];
|
||||
int i;
|
||||
};
|
||||
|
||||
static int compareItemX(const void* va, const void* vb)
|
||||
{
|
||||
const BoundsItem* a = (const BoundsItem*)va;
|
||||
const BoundsItem* b = (const BoundsItem*)vb;
|
||||
if (a->bmin[0] < b->bmin[0])
|
||||
return -1;
|
||||
if (a->bmin[0] > b->bmin[0])
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int compareItemY(const void* va, const void* vb)
|
||||
{
|
||||
const BoundsItem* a = (const BoundsItem*)va;
|
||||
const BoundsItem* b = (const BoundsItem*)vb;
|
||||
if (a->bmin[1] < b->bmin[1])
|
||||
return -1;
|
||||
if (a->bmin[1] > b->bmin[1])
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline int longestAxis(float x, float y)
|
||||
{
|
||||
return y > x ? 1 : 0;
|
||||
}
|
||||
|
||||
static void calcExtends(const BoundsItem* items, const int /*nitems*/,
|
||||
const int imin, const int imax,
|
||||
float* bmin, float* bmax)
|
||||
{
|
||||
bmin[0] = items[imin].bmin[0];
|
||||
bmin[1] = items[imin].bmin[1];
|
||||
|
||||
bmax[0] = items[imin].bmax[0];
|
||||
bmax[1] = items[imin].bmax[1];
|
||||
|
||||
for (int i = imin+1; i < imax; ++i) {
|
||||
const BoundsItem& it = items[i];
|
||||
if (it.bmin[0] < bmin[0]) bmin[0] = it.bmin[0];
|
||||
if (it.bmin[1] < bmin[1]) bmin[1] = it.bmin[1];
|
||||
|
||||
if (it.bmax[0] > bmax[0]) bmax[0] = it.bmax[0];
|
||||
if (it.bmax[1] > bmax[1]) bmax[1] = it.bmax[1];
|
||||
}
|
||||
}
|
||||
|
||||
static void subdivide(BoundsItem* items,
|
||||
int nitems,
|
||||
int imin,
|
||||
int imax,
|
||||
int trisPerChunk,
|
||||
int& curNode,
|
||||
rcChunkyTriMeshNode* nodes,
|
||||
const int maxNodes,
|
||||
int& curTri,
|
||||
int* outTris,
|
||||
const int* inTris)
|
||||
{
|
||||
int inum = imax - imin;
|
||||
int icur = curNode;
|
||||
|
||||
if (curNode > maxNodes) {
|
||||
abort();
|
||||
return;
|
||||
}
|
||||
|
||||
rcChunkyTriMeshNode& node = nodes[curNode++];
|
||||
|
||||
if (inum <= trisPerChunk) {
|
||||
// Leaf
|
||||
calcExtends(items, nitems, imin, imax, node.bmin, node.bmax);
|
||||
|
||||
// Copy triangles.
|
||||
node.i = curTri;
|
||||
node.n = inum;
|
||||
|
||||
for (int i = imin; i < imax; ++i) {
|
||||
const int* src = &inTris[items[i].i*3];
|
||||
int* dst = &outTris[curTri*3];
|
||||
curTri++;
|
||||
dst[0] = src[0];
|
||||
dst[1] = src[1];
|
||||
dst[2] = src[2];
|
||||
}
|
||||
} else {
|
||||
// Split
|
||||
calcExtends(items, nitems, imin, imax, node.bmin, node.bmax);
|
||||
|
||||
int axis = longestAxis(node.bmax[0] - node.bmin[0],
|
||||
node.bmax[1] - node.bmin[1]);
|
||||
|
||||
if (axis == 0) {
|
||||
// Sort along x-axis
|
||||
qsort(items+imin, static_cast<size_t>(inum), sizeof(BoundsItem), compareItemX);
|
||||
} else if (axis == 1) {
|
||||
// Sort along y-axis
|
||||
qsort(items+imin, static_cast<size_t>(inum), sizeof(BoundsItem), compareItemY);
|
||||
}
|
||||
|
||||
int isplit = imin+inum/2;
|
||||
|
||||
// Left
|
||||
subdivide(items, nitems, imin, isplit, trisPerChunk, curNode, nodes, maxNodes, curTri, outTris, inTris);
|
||||
// Right
|
||||
subdivide(items, nitems, isplit, imax, trisPerChunk, curNode, nodes, maxNodes, curTri, outTris, inTris);
|
||||
|
||||
int iescape = curNode - icur;
|
||||
// Negative index means escape.
|
||||
node.i = -iescape;
|
||||
}
|
||||
}
|
||||
|
||||
static bool rcCreateChunkyTriMesh(const float* verts,
|
||||
const int* tris,
|
||||
int ntris,
|
||||
int trisPerChunk,
|
||||
rcChunkyTriMesh* cm)
|
||||
{
|
||||
int nchunks = (ntris + trisPerChunk-1) / trisPerChunk;
|
||||
|
||||
cm->nodes = new rcChunkyTriMeshNode[nchunks*4];
|
||||
if (!cm->nodes) {
|
||||
abort();
|
||||
return false;
|
||||
}
|
||||
|
||||
cm->tris = new int[ntris*3];
|
||||
if (!cm->tris) {
|
||||
abort();
|
||||
return false;
|
||||
}
|
||||
|
||||
cm->ntris = ntris;
|
||||
|
||||
// Build tree
|
||||
BoundsItem* items = new BoundsItem[ntris];
|
||||
if (!items) {
|
||||
abort();
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < ntris; i++) {
|
||||
const int* t = &tris[i*3];
|
||||
BoundsItem& it = items[i];
|
||||
it.i = i;
|
||||
// Calc triangle XZ bounds.
|
||||
it.bmin[0] = it.bmax[0] = verts[t[0]*3+0];
|
||||
it.bmin[1] = it.bmax[1] = verts[t[0]*3+2];
|
||||
for (int j = 1; j < 3; ++j) {
|
||||
const float* v = &verts[t[j]*3];
|
||||
if (v[0] < it.bmin[0]) it.bmin[0] = v[0];
|
||||
if (v[2] < it.bmin[1]) it.bmin[1] = v[2];
|
||||
|
||||
if (v[0] > it.bmax[0]) it.bmax[0] = v[0];
|
||||
if (v[2] > it.bmax[1]) it.bmax[1] = v[2];
|
||||
}
|
||||
}
|
||||
|
||||
int curTri = 0;
|
||||
int curNode = 0;
|
||||
subdivide(items, ntris, 0, ntris, trisPerChunk, curNode, cm->nodes, nchunks*4, curTri, cm->tris, tris);
|
||||
|
||||
delete [] items;
|
||||
|
||||
cm->nnodes = curNode;
|
||||
|
||||
// Calc max tris per node.
|
||||
cm->maxTrisPerChunk = 0;
|
||||
for (int i = 0; i < cm->nnodes; ++i) {
|
||||
rcChunkyTriMeshNode& node = cm->nodes[i];
|
||||
const bool isLeaf = node.i >= 0;
|
||||
if (!isLeaf) continue;
|
||||
if (node.n > cm->maxTrisPerChunk) {
|
||||
cm->maxTrisPerChunk = node.n;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool checkOverlapRect(const float amin[2], const float amax[2],
|
||||
const float bmin[2], const float bmax[2])
|
||||
{
|
||||
bool overlap = true;
|
||||
overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ? false : overlap;
|
||||
overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ? false : overlap;
|
||||
return overlap;
|
||||
}
|
||||
|
||||
int rcGetChunksOverlappingRect(const rcChunkyTriMesh* cm,
|
||||
float bmin[2], float bmax[2],
|
||||
int* ids, const int maxIds)
|
||||
{
|
||||
// Traverse tree
|
||||
int i = 0;
|
||||
int n = 0;
|
||||
while (i < cm->nnodes) {
|
||||
const rcChunkyTriMeshNode* node = &cm->nodes[i];
|
||||
const bool overlap = checkOverlapRect(bmin, bmax, node->bmin, node->bmax);
|
||||
const bool isLeafNode = node->i >= 0;
|
||||
|
||||
if (isLeafNode && overlap) {
|
||||
if (n < maxIds) {
|
||||
ids[n] = i;
|
||||
n++;
|
||||
}
|
||||
}
|
||||
|
||||
if (overlap || isLeafNode)
|
||||
i++;
|
||||
else {
|
||||
const int escapeIndex = -node->i;
|
||||
i += escapeIndex;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
namespace f8
|
||||
{
|
||||
|
||||
void InputGeom::Init(float width, float height)
|
||||
{
|
||||
{
|
||||
verts_.reserve(3 * 4);
|
||||
|
||||
verts_.push_back(0);
|
||||
verts_.push_back(0);
|
||||
verts_.push_back(0);
|
||||
|
||||
verts_.push_back(0);
|
||||
verts_.push_back(0);
|
||||
verts_.push_back(-height);
|
||||
|
||||
verts_.push_back(-width);
|
||||
verts_.push_back(0);
|
||||
verts_.push_back(-height);
|
||||
|
||||
verts_.push_back(-width);
|
||||
verts_.push_back(0);
|
||||
verts_.push_back(0);
|
||||
}
|
||||
{
|
||||
tris_.push_back(0);
|
||||
tris_.push_back(1);
|
||||
tris_.push_back(2);
|
||||
|
||||
tris_.push_back(0);
|
||||
tris_.push_back(2);
|
||||
tris_.push_back(3);
|
||||
}
|
||||
rcCalcBounds(GetVerts(), GetVertCount(), min_, max_);
|
||||
if (!rcCreateChunkyTriMesh(GetVerts(), GetTris(), GetTriCount(), 256, &chunky_mesh_)) {
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,64 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
struct rcChunkyTriMeshNode
|
||||
{
|
||||
float bmin[2];
|
||||
float bmax[2];
|
||||
int i;
|
||||
int n;
|
||||
};
|
||||
|
||||
struct rcChunkyTriMesh
|
||||
{
|
||||
inline rcChunkyTriMesh() : nodes(0), nnodes(0), tris(0), ntris(0), maxTrisPerChunk(0) {};
|
||||
inline ~rcChunkyTriMesh() { delete [] nodes; delete [] tris; }
|
||||
|
||||
rcChunkyTriMeshNode* nodes;
|
||||
int nnodes;
|
||||
int* tris;
|
||||
int ntris;
|
||||
int maxTrisPerChunk;
|
||||
|
||||
private:
|
||||
// Explicitly disabled copy constructor and copy assignment operator.
|
||||
rcChunkyTriMesh(const rcChunkyTriMesh&);
|
||||
rcChunkyTriMesh& operator=(const rcChunkyTriMesh&);
|
||||
};
|
||||
|
||||
namespace f8
|
||||
{
|
||||
|
||||
class InputGeom
|
||||
{
|
||||
public:
|
||||
void Init(float width, float height);
|
||||
|
||||
const float* GetMeshBoundsMin() const { return min_; }
|
||||
const float* GetMeshBoundsMax() const { return max_; }
|
||||
const float* GetVerts() const { return !verts_.empty() ? &verts_[0] : nullptr; }
|
||||
int GetVertCount() const { return verts_.size() / 3; }
|
||||
const int* GetTris() const { return !tris_.empty() ? &tris_[0] : nullptr; }
|
||||
int GetTriCount() const { return tris_.size() / 3; }
|
||||
const rcChunkyTriMesh* GetChunkyMesh() { return &chunky_mesh_; }
|
||||
int GetOffMeshConnectionCount() const { return 0; }
|
||||
const float* GetOffMeshConnectionVerts() const { return nullptr; }
|
||||
const float* GetOffMeshConnectionRads() const { return nullptr; }
|
||||
const unsigned char* GetOffMeshConnectionDirs() const { return nullptr; }
|
||||
const unsigned char* GetOffMeshConnectionAreas() const { return nullptr; }
|
||||
const unsigned short* GetOffMeshConnectionFlags() const { return nullptr; }
|
||||
const unsigned int* GetOffMeshConnectionId() const { return 0; }
|
||||
|
||||
private:
|
||||
float min_[3] = {0};
|
||||
float max_[3] = {0};
|
||||
std::vector<float> verts_;
|
||||
std::vector<int> tris_;
|
||||
rcChunkyTriMesh chunky_mesh_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
int rcGetChunksOverlappingRect(const rcChunkyTriMesh* cm,
|
||||
float bmin[2], float bmax[2],
|
||||
int* ids, const int maxIds);
|
||||
|
@ -1,54 +0,0 @@
|
||||
#include "precompile.h"
|
||||
|
||||
#include "behaviac/behaviac.h"
|
||||
#include "navigation.h"
|
||||
#include "navigation_handle.h"
|
||||
|
||||
namespace f8
|
||||
{
|
||||
void Navigation::Init()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Navigation::UnInit()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
f8::NavigationHandle* Navigation::NewNavigation(std::string& unique_str)
|
||||
{
|
||||
if (HasNavigation(unique_str)) {
|
||||
abort();
|
||||
}
|
||||
f8::NavigationHandle* handle = new f8::NavigationHandle;
|
||||
navhandles_[unique_str] = handle;
|
||||
return handle;
|
||||
}
|
||||
|
||||
f8::NavigationHandle* Navigation::LoadNavigation(const std::string& res_path,
|
||||
const std::map<int, std::string>& params)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool Navigation::HasNavigation(const std::string& res_path)
|
||||
{
|
||||
return navhandles_.find(res_path) != navhandles_.end();
|
||||
}
|
||||
|
||||
void Navigation::RemoveNavigation(const std::string& res_path)
|
||||
{
|
||||
auto itr = navhandles_.find(res_path);
|
||||
if (itr != navhandles_.end()) {
|
||||
navhandles_.erase(itr);
|
||||
}
|
||||
}
|
||||
|
||||
f8::NavigationHandle* Navigation::FindNavigation(const std::string& res_path)
|
||||
{
|
||||
auto itr = navhandles_.find(res_path);
|
||||
return itr != navhandles_.end() ? itr->second : nullptr;
|
||||
}
|
||||
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
namespace f8
|
||||
{
|
||||
class NavigationHandle;
|
||||
class Navigation : public a8::Singleton<Navigation>
|
||||
{
|
||||
private:
|
||||
Navigation() {};
|
||||
friend class a8::Singleton<Navigation>;
|
||||
|
||||
public:
|
||||
void Init();
|
||||
void UnInit();
|
||||
|
||||
f8::NavigationHandle* NewNavigation(std::string& unique_str);
|
||||
f8::NavigationHandle* LoadNavigation(const std::string& res_path,
|
||||
const std::map<int, std::string>& params);
|
||||
bool HasNavigation(const std::string& res_path);
|
||||
void RemoveNavigation(const std::string& res_path);
|
||||
f8::NavigationHandle* FindNavigation(const std::string& res_path);
|
||||
|
||||
private:
|
||||
std::map<std::string, f8::NavigationHandle*> navhandles_;
|
||||
};
|
||||
}
|
@ -1,103 +0,0 @@
|
||||
#include "precompile.h"
|
||||
|
||||
#include "navigation_handle.h"
|
||||
|
||||
namespace f8
|
||||
{
|
||||
|
||||
int NavMeshLayer::FindStraightPath(const a8::Vec3& start, const a8::Vec3& end,
|
||||
std::vector<a8::Vec3>& path_list)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int NavMeshLayer::FindRandomPointAroundCircle(const a8::Vec3& center_pos,
|
||||
std::vector<a8::Vec3>& points,
|
||||
int max_points, float max_radius)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int NavMeshLayer::Raycast(const a8::Vec3& start, const a8::Vec3& end,
|
||||
std::vector<a8::Vec3>& hit_points)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool NavMeshLayer::AddObstacle(a8::Vec3& pos, const float radius, const float height,
|
||||
ObstacleRef* obstacle_ref)
|
||||
{
|
||||
if (obstacle_ref) {
|
||||
*obstacle_ref = 0;
|
||||
}
|
||||
float p[3];
|
||||
p[0] = pos.x;
|
||||
p[1] = pos.y;
|
||||
p[2] = pos.z;
|
||||
dtStatus status = tile_cache_->addObstacle(p, radius, height, obstacle_ref);
|
||||
return status == DT_SUCCESS || status == DT_IN_PROGRESS;
|
||||
}
|
||||
|
||||
bool NavMeshLayer::AddBoxObstacle(a8::Vec3& bmin, a8::Vec3& bmax,
|
||||
ObstacleRef* obstacle_ref)
|
||||
{
|
||||
if (obstacle_ref) {
|
||||
*obstacle_ref = 0;
|
||||
}
|
||||
float pmin[3];
|
||||
pmin[0] = bmin.x;
|
||||
pmin[1] = bmin.y;
|
||||
pmin[2] = bmin.z;
|
||||
|
||||
float pmax[3];
|
||||
pmax[0] = bmax.x;
|
||||
pmax[1] = bmax.y;
|
||||
pmax[2] = bmax.z;
|
||||
|
||||
dtStatus status = tile_cache_->addBoxObstacle(pmin, pmax, obstacle_ref);
|
||||
return status == DT_SUCCESS || status == DT_IN_PROGRESS;
|
||||
}
|
||||
|
||||
bool NavMeshLayer::RemoveObstacle(const ObstacleRef& obstacle_ref)
|
||||
{
|
||||
dtStatus status = tile_cache_->removeObstacle(obstacle_ref);
|
||||
return status == DT_SUCCESS || status == DT_IN_PROGRESS;
|
||||
}
|
||||
|
||||
void NavMeshLayer::WaitRebuildOk()
|
||||
{
|
||||
while (!Update()) {}
|
||||
}
|
||||
|
||||
bool NavMeshLayer::Update()
|
||||
{
|
||||
bool up_to_date = false;
|
||||
tile_cache_->update(0, navmesh_, &up_to_date);
|
||||
return up_to_date;
|
||||
}
|
||||
|
||||
NavMeshLayer* NavigationHandle::GetLayer(int layer)
|
||||
{
|
||||
auto itr = navmesh_layers_.find(layer);
|
||||
return itr != navmesh_layers_.end() ? &itr->second : nullptr;
|
||||
}
|
||||
|
||||
void NavigationHandle::WaitRebuildOk()
|
||||
{
|
||||
for (auto& pair : navmesh_layers_) {
|
||||
pair.second.WaitRebuildOk();
|
||||
}
|
||||
}
|
||||
|
||||
bool NavigationHandle::Update()
|
||||
{
|
||||
bool up_to_date = true;
|
||||
for (auto& pair : navmesh_layers_) {
|
||||
if (!pair.second.Update() && up_to_date) {
|
||||
up_to_date = false;
|
||||
}
|
||||
}
|
||||
return up_to_date;
|
||||
}
|
||||
|
||||
}
|
@ -1,52 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <a8/vec3.h>
|
||||
|
||||
#include "DetourTileCache.h"
|
||||
|
||||
class dtTileCache;
|
||||
class dtNavMesh;
|
||||
class dtNavMeshQuery;
|
||||
namespace f8
|
||||
{
|
||||
|
||||
typedef dtObstacleRef ObstacleRef;
|
||||
|
||||
class NavMeshLayer
|
||||
{
|
||||
public:
|
||||
int FindStraightPath(const a8::Vec3& start, const a8::Vec3& end,
|
||||
std::vector<a8::Vec3>& path_list);
|
||||
int FindRandomPointAroundCircle(const a8::Vec3& center_pos,
|
||||
std::vector<a8::Vec3>& points, int max_points, float max_radius);
|
||||
int Raycast(const a8::Vec3& start, const a8::Vec3& end,
|
||||
std::vector<a8::Vec3>& hit_points);
|
||||
|
||||
bool AddObstacle(a8::Vec3& pos, const float radius, const float height, ObstacleRef* obstacle_ref);
|
||||
bool AddBoxObstacle(a8::Vec3& bmin, a8::Vec3& bmax, ObstacleRef* obstacle_ref);
|
||||
bool RemoveObstacle(const ObstacleRef& obstacle_ref);
|
||||
void WaitRebuildOk();
|
||||
bool Update();
|
||||
|
||||
private:
|
||||
|
||||
dtNavMesh* navmesh_ = nullptr;
|
||||
dtTileCache* tile_cache_ = nullptr;
|
||||
dtNavMeshQuery* navmesh_query_ = nullptr;
|
||||
};
|
||||
|
||||
class NavigationHandle
|
||||
{
|
||||
public:
|
||||
NavigationHandle() {};
|
||||
~NavigationHandle() {};
|
||||
|
||||
NavMeshLayer* GetLayer(int layer);
|
||||
void WaitRebuildOk();
|
||||
bool Update();
|
||||
|
||||
private:
|
||||
std::map<int, NavMeshLayer> navmesh_layers_;
|
||||
};
|
||||
|
||||
}
|
503
f8/tiledmap.cc
503
f8/tiledmap.cc
@ -1,503 +0,0 @@
|
||||
#include <a8/a8.h>
|
||||
#include <cmath>
|
||||
#include <a8/stringlist.h>
|
||||
#include "f8/tiledmap.h"
|
||||
|
||||
namespace f8
|
||||
{
|
||||
a8::XValue TiledObject::GetProperty(const std::string& prop_name)
|
||||
{
|
||||
auto itr = prop_hash.find(prop_name);
|
||||
return itr != prop_hash.end() ? itr->second : a8::XValue();
|
||||
}
|
||||
|
||||
bool TiledObject::HasProperty(const std::string& prop_name)
|
||||
{
|
||||
auto itr = prop_hash.find(prop_name);
|
||||
return itr != prop_hash.end();
|
||||
}
|
||||
|
||||
|
||||
a8::XValue TiledLayer::GetProperty(const std::string& prop_name)
|
||||
{
|
||||
auto itr = prop_hash.find(prop_name);
|
||||
return itr != prop_hash.end() ? itr->second : a8::XValue();
|
||||
}
|
||||
|
||||
bool TiledLayer::HasProperty(const std::string& prop_name)
|
||||
{
|
||||
auto itr = prop_hash.find(prop_name);
|
||||
return itr != prop_hash.end();
|
||||
}
|
||||
|
||||
bool TiledMap::LoadTmxFile(const std::string& filename)
|
||||
{
|
||||
a8::XObject xobj;
|
||||
if (!xobj.ReadFromXmlFile(filename)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::shared_ptr<a8::XObject> tileset_node = xobj.At("child_node.tileset")->At(0);
|
||||
tile_width = tileset_node->At("attrs.tilewidth")->AsXValue();
|
||||
tile_height = tileset_node->At("attrs.tileheight")->AsXValue();
|
||||
#if 0
|
||||
tile_count = tileset_node->At("attrs.tilecount")->AsXValue();
|
||||
tile_columns = tileset_node->At("attrs.columns")->AsXValue();
|
||||
tile_rows = tile_count/tile_columns + 1;
|
||||
tile_columns += 1;
|
||||
tile_count = tile_rows * tile_columns;
|
||||
#endif
|
||||
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);
|
||||
|
||||
TiledLayer layer;
|
||||
{
|
||||
for (int ii = 0; ii < layer_node->At("attr_names")->Size(); ++ii) {
|
||||
std::string attr_name = layer_node->At("attr_names")->At(ii)->AsXValue();
|
||||
layer.prop_hash[attr_name] = layer_node->At("attrs." + attr_name)->AsXValue();
|
||||
}
|
||||
std::shared_ptr<a8::XObject> prop_nodes = layer_node->At("child_node.properties")->At(0)->At("child_node.property");
|
||||
for (int j = 0; j < prop_nodes->Size(); ++j) {
|
||||
std::shared_ptr<a8::XObject> prop_node = prop_nodes->At(j);
|
||||
layer.prop_hash[prop_node->At("attrs.name")->AsXValue()] = prop_node->At("attrs.value")->AsXValue();
|
||||
}
|
||||
|
||||
std::shared_ptr<a8::XObject> data_node = layer_node->At("child_node.data")->At(0);
|
||||
layer.data = data_node->At("node_value")->AsXValue();
|
||||
}
|
||||
|
||||
std::string layer_name = layer_node->At("attrs.name")->AsXValue();
|
||||
if (layer_name.compare("ground") == 0) {
|
||||
tile_columns = layer_node->At("attrs.width")->AsXValue();
|
||||
tile_rows = layer_node->At("attrs.height")->AsXValue();
|
||||
tile_count = tile_rows * tile_columns;
|
||||
}
|
||||
auto itr = layer_hash.find(layer_name);
|
||||
if (itr != layer_hash.end()) {
|
||||
itr->second.push_back(layer);
|
||||
} else {
|
||||
layer_hash[layer_name] = std::list<TiledLayer>({layer});
|
||||
}
|
||||
}
|
||||
|
||||
for (int group_id = 0; group_id < xobj.At("child_node.objectgroup")->Size(); group_id++) {
|
||||
std::shared_ptr<a8::XObject> objgroup_node = xobj.At("child_node.objectgroup")->At(group_id);
|
||||
for (int i = 0; i < objgroup_node->At("child_node.object")->Size(); i++) {
|
||||
std::shared_ptr<a8::XObject> object_node = objgroup_node->At("child_node.object")->At(i);
|
||||
TiledObject object;
|
||||
{
|
||||
for (int ii = 0; ii < object_node->At("attr_names")->Size(); ii++) {
|
||||
std::string attr_name = object_node->At("attr_names")->At(ii)->AsXValue();
|
||||
object.prop_hash[attr_name] = object_node->At("attrs." + attr_name)->AsXValue();
|
||||
}
|
||||
std::shared_ptr<a8::XObject> prop_nodes = object_node->At("child_node.properties")->At(0)->At("child_node.property");
|
||||
for (int j = 0; j < prop_nodes->Size(); j++) {
|
||||
std::shared_ptr<a8::XObject> prop_node = prop_nodes->At(j);
|
||||
object.prop_hash[prop_node->At("attrs.name")->AsXValue()] = prop_node->At("attrs.value")->AsXValue();
|
||||
}
|
||||
}
|
||||
|
||||
std::string object_name = object_node->At("attrs.name")->AsXValue();
|
||||
auto itr = object_group_hash.find(object_name);
|
||||
if (itr != object_group_hash.end()) {
|
||||
itr->second.push_back(object);
|
||||
}
|
||||
else
|
||||
{
|
||||
object_group_hash[object_name] = std::list<TiledObject>({ object });
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::list<TiledObject>* TiledMap::GetObjectGroup(const std::string& object_class_name)
|
||||
{
|
||||
auto itr = object_group_hash.find(object_class_name);
|
||||
return itr != object_group_hash.end() ? &itr->second : nullptr;
|
||||
}
|
||||
|
||||
StagePoint* TiledMap::GetStageObject(int point)
|
||||
{
|
||||
auto itr = stage_object_hash.find(point);
|
||||
return itr != stage_object_hash.end() ? &itr->second : nullptr;
|
||||
}
|
||||
|
||||
bool TiledMap::HasStageObject(int point)
|
||||
{
|
||||
auto itr = stage_object_hash.find(point);
|
||||
return itr != stage_object_hash.end();
|
||||
}
|
||||
|
||||
std::vector<GridCell*>* TiledMap::GetStagePath(const std::string& path_name)
|
||||
{
|
||||
auto itr = stage_path_hash.find(path_name);
|
||||
return itr != stage_path_hash.end() ? &itr->second : nullptr;
|
||||
}
|
||||
|
||||
bool TiledMap::HasStagePath(const std::string& path_name)
|
||||
{
|
||||
auto itr = stage_path_hash.find(path_name);
|
||||
return itr != stage_path_hash.end();
|
||||
}
|
||||
|
||||
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("area_id");
|
||||
int speed = layer.GetProperty("area_id");
|
||||
|
||||
bool has_trigger = layer.HasProperty("isTrigger");
|
||||
bool istrigger = layer.GetProperty("isTrigger");;
|
||||
int trigger_point = layer.GetProperty("objName");
|
||||
|
||||
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 = 1; 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();
|
||||
|
||||
for(int j = 0; j < split_count; ++j){
|
||||
int value = a8::XValue(split_list[j]);
|
||||
if(value > 0){
|
||||
int index = (i-1)*tile_columns + j;
|
||||
if (has_speed) {
|
||||
grid_cell_list[index].x = j;
|
||||
grid_cell_list[index].y = i-1;
|
||||
|
||||
grid_cell_list[index].speed = speed;
|
||||
grid_cell_list[index].value = value;
|
||||
}
|
||||
if (has_trigger) {
|
||||
TriggerPoint tp;
|
||||
{
|
||||
tp.x = j;
|
||||
tp.y = i-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("name");
|
||||
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(a8::XValue(point));
|
||||
}
|
||||
int point = object.GetProperty("name");
|
||||
stage_object_hash[point] = 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 });
|
||||
a8::XPrintf("layer$layer_hash.size:%d\n", {layer_hash.size()});
|
||||
for (auto& pair : layer_hash) {
|
||||
a8::XPrintf(" layer$layer_type:%s\n", {pair.first});
|
||||
for (auto& layer : pair.second) {
|
||||
a8::XPrintf("data$data:%s\n",{layer.data});
|
||||
for (auto& pair2 : layer.prop_hash) {
|
||||
a8::XPrintf(" layer$%s:%s\n", {pair2.first, pair2.second});
|
||||
}
|
||||
}
|
||||
}
|
||||
a8::XPrintf("object$object_group_hash.size:%d\n", {object_group_hash.size()});
|
||||
for (auto& pair : object_group_hash) {
|
||||
a8::XPrintf(" object$layer_type:%s\n", {pair.first});
|
||||
for (auto& layer : pair.second) {
|
||||
for (auto& pair2 : layer.prop_hash) {
|
||||
a8::XPrintf(" object$%s:%s\n", {pair2.first, pair2.second});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool TiledMap::CalcCurrPos(std::vector<int>& path_points, int old_pos_x, int old_pos_y, int time_ms,
|
||||
int& curr_pos_x, int& curr_pos_y)
|
||||
{
|
||||
if (path_points.size() < 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
StagePoint* sp_terminal = GetStageObject(path_points[path_points.size()-1]);
|
||||
if (sp_terminal) {
|
||||
if (old_pos_x == sp_terminal->x && old_pos_y == sp_terminal->y) {
|
||||
curr_pos_x = old_pos_x;
|
||||
curr_pos_y = old_pos_y;
|
||||
return true;
|
||||
}
|
||||
//报错放终点
|
||||
curr_pos_x = sp_terminal->x;
|
||||
curr_pos_y = sp_terminal->y;
|
||||
}
|
||||
|
||||
std::vector<std::string> stage_path_list;
|
||||
{
|
||||
std::string path_point;
|
||||
bool path_start = true;
|
||||
for (auto& point : path_points) {
|
||||
if (path_start) {
|
||||
path_point = a8::XValue(point).GetString();
|
||||
path_start = !path_start;
|
||||
} else {
|
||||
std::string point_str = a8::XValue(point).GetString();
|
||||
path_point += "-" + point_str;
|
||||
stage_path_list.push_back(path_point);
|
||||
path_point = point_str;
|
||||
}
|
||||
}
|
||||
}
|
||||
std::vector<GridCell*> grid_all_list;
|
||||
{
|
||||
bool exc_once = true;
|
||||
for (auto& stage_path : stage_path_list) {
|
||||
std::vector<GridCell*>* grid_list = nullptr;
|
||||
|
||||
std::string stage_path_reverse;
|
||||
std::vector<std::string> split_list;
|
||||
a8::Split(stage_path,split_list,'-');
|
||||
if (split_list.size() != 2) {
|
||||
return false;
|
||||
}
|
||||
StagePoint* sp = GetStageObject(a8::XValue(split_list.at(0)));
|
||||
if (sp == nullptr) {
|
||||
return false;
|
||||
}
|
||||
grid_list = GetStagePath(stage_path);
|
||||
if (grid_list == nullptr) {
|
||||
stage_path_reverse = split_list[1] + "-" + split_list[0];
|
||||
grid_list = GetStagePath(stage_path_reverse);
|
||||
if (grid_list == nullptr) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<GridCell*> grid_sort_list = SortGridList(grid_list,sp);
|
||||
if (grid_sort_list.size() < 2) {
|
||||
return false;
|
||||
}
|
||||
if (exc_once) {
|
||||
grid_all_list.insert(grid_all_list.end(),grid_sort_list.begin(),grid_sort_list.end());
|
||||
exc_once = false;
|
||||
} else {
|
||||
grid_all_list.insert(grid_all_list.end(),grid_sort_list.begin()+1,grid_sort_list.end());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (grid_all_list.empty()) {
|
||||
return false;
|
||||
}
|
||||
{
|
||||
bool has_index = false;
|
||||
int index = 0;
|
||||
for (auto& gc : grid_all_list) {
|
||||
if (gc->x == old_pos_x && gc->y == old_pos_y) {
|
||||
has_index = true;
|
||||
break;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
if (!has_index) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int grid_all_count = grid_all_list.size();
|
||||
for (int i = index; i < grid_all_count-1; ++i) {
|
||||
GridCell* gc_first = grid_all_list[i];
|
||||
GridCell* gc_second = grid_all_list[i+1];
|
||||
int speed = gc_first->speed;
|
||||
int time_use = 0;
|
||||
if (gc_second->x == gc_first->x) {
|
||||
time_use = a8::XValue((((float)tile_height / speed)*1000));
|
||||
} else if (gc_second->y == gc_first->y) {
|
||||
time_use = a8::XValue((((float)tile_width / speed)*1000));
|
||||
} else {
|
||||
float width_pow = std::pow(tile_width,2.0);
|
||||
float height_pow = std::pow(tile_height,2.0);
|
||||
float value_sqrt = std::sqrt(width_pow+height_pow);
|
||||
time_use = a8::XValue(((value_sqrt / speed)*1000));
|
||||
}
|
||||
time_ms -= time_use;
|
||||
curr_pos_x = gc_second->x;
|
||||
curr_pos_y = gc_second->y;
|
||||
if (time_ms < 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (time_ms > 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<GridCell*> TiledMap::SortGridList(std::vector<GridCell*>* grid_list, StagePoint* sp)
|
||||
{
|
||||
std::vector<GridCell*> grid_sort_list;
|
||||
std::map<GridCell*, bool> grid_tag_hash;
|
||||
int grid_list_count = grid_list->size();
|
||||
int old_x = 0;
|
||||
int old_y = 0;
|
||||
for (auto& gc : *grid_list) {
|
||||
grid_tag_hash[gc] = false;
|
||||
}
|
||||
for (auto& gc : *grid_list) {
|
||||
if (gc->x == sp->x && gc->y == sp->y) {
|
||||
grid_sort_list.push_back(gc);
|
||||
old_x = gc->x;
|
||||
old_y = gc->y;
|
||||
grid_tag_hash[gc] = true;
|
||||
}
|
||||
}
|
||||
|
||||
for (int count = 1; count < grid_list_count; ++count) {
|
||||
GridCell* gc_next = nullptr;
|
||||
for (int i = 0; i < grid_list_count; ++i) {
|
||||
GridCell* gc = grid_list->at(i);
|
||||
for (int x = old_x-1; x <= old_x+1; ++x) {
|
||||
for(int y = old_y-1; y <= old_y+1; ++y) {
|
||||
if (gc->x == x && gc->y == y && !grid_tag_hash[gc]) {
|
||||
grid_tag_hash[gc] = true;
|
||||
gc_next = gc;
|
||||
old_x = gc->x;
|
||||
old_y = gc->y;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (gc_next != nullptr) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (gc_next != nullptr) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (gc_next != nullptr) {
|
||||
grid_sort_list.push_back(gc_next);
|
||||
}
|
||||
}
|
||||
return grid_sort_list;
|
||||
}
|
||||
|
||||
void TiledMap::SetGridCellSpeedByAreaId(std::map<int,int>& area_speed_hash)
|
||||
{
|
||||
for (auto& pair : stage_path_hash) {
|
||||
for (auto& grid_cell : pair.second) {
|
||||
auto itr = area_speed_hash.find(grid_cell->speed);
|
||||
if (itr != area_speed_hash.end()) {
|
||||
grid_cell->speed = itr->second;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TiledMap::LoadWayPointsData(const std::string& filename)
|
||||
{
|
||||
a8::XObject waypoints_json;
|
||||
if (!waypoints_json.ReadFromJsonFile(filename)) {
|
||||
abort();
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto& pair : stage_object_hash) {
|
||||
int stage_id = pair.first;
|
||||
StagePoint& stage = pair.second;
|
||||
for (int relation_point : stage.relation_list) {
|
||||
std::string path_name = a8::Format("%d-%d", {stage_id, relation_point});
|
||||
std::shared_ptr<a8::XObject> points_xobj = waypoints_json.At(path_name);
|
||||
if (!points_xobj) {
|
||||
path_name = a8::Format("%d-%d", {relation_point, stage_id});
|
||||
points_xobj = waypoints_json.At(path_name);
|
||||
if (!points_xobj) {
|
||||
continue;
|
||||
// abort();
|
||||
}
|
||||
}
|
||||
if (stage_path_hash.find(path_name) != stage_path_hash.end()) {
|
||||
continue;
|
||||
}
|
||||
stage_path_hash[path_name] = std::vector<GridCell*>();
|
||||
std::vector<GridCell*>& path_points = stage_path_hash[path_name];
|
||||
for (int i = 0; i < points_xobj->Size(); ++i) {
|
||||
int x = points_xobj->At(i)->At(0)->AsXValue();
|
||||
int y = points_xobj->At(i)->At(1)->AsXValue();
|
||||
int index = y*tile_columns + x;
|
||||
path_points.push_back(&grid_cell_list[index]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,91 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
namespace f8
|
||||
{
|
||||
class TiledObject
|
||||
{
|
||||
public:
|
||||
a8::XValue GetProperty(const std::string& prop_name);
|
||||
bool HasProperty(const std::string& prop_name);
|
||||
|
||||
public:
|
||||
std::map<std::string, a8::XValue> prop_hash;
|
||||
};
|
||||
|
||||
class TiledLayer
|
||||
{
|
||||
public:
|
||||
|
||||
a8::XValue GetProperty(const std::string& prop_name);
|
||||
bool HasProperty(const std::string& prop_name);
|
||||
|
||||
public:
|
||||
std::map<std::string, a8::XValue> prop_hash;
|
||||
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;
|
||||
int point = 0;
|
||||
std::vector<int> relation_list;
|
||||
};
|
||||
|
||||
struct TriggerPoint
|
||||
{
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
int point = 0;
|
||||
bool istrigger = false;
|
||||
};
|
||||
|
||||
class TiledMap
|
||||
{
|
||||
public:
|
||||
TiledLayer* ground_layer = nullptr;
|
||||
std::list<TiledLayer> speed_layers;
|
||||
int tile_count = 0; //瓦片地图的尺寸。(以瓦片数量为单位)
|
||||
int tile_width = 0; //瓦片的宽。(以像素点为单位)
|
||||
int tile_height = 0; //瓦片的高。(以像素点为单位)
|
||||
int tile_rows = 0; //行
|
||||
int tile_columns = 0; //列
|
||||
|
||||
bool LoadTmxFile(const std::string& filename);
|
||||
void LoadWayPointsData(const std::string& filename);
|
||||
std::list<TiledObject>* GetObjectGroup(const std::string& object_class_name);
|
||||
void Dump();
|
||||
void Init();
|
||||
StagePoint* GetStageObject(int point);
|
||||
bool HasStageObject(int point);
|
||||
|
||||
std::vector<GridCell*>* GetStagePath(const std::string& path_name);
|
||||
bool HasStagePath(const std::string& path_name);
|
||||
|
||||
std::vector<GridCell*> SortGridList(std::vector<GridCell*>* grid_list, StagePoint* sp);
|
||||
bool CalcCurrPos(std::vector<int>& path_points, int old_pos_x, int old_pos_y, int time_ms, int& curr_pos_x, int& curr_pos_y);
|
||||
|
||||
//速度设置
|
||||
void SetGridCellSpeedByAreaId(std::map<int,int>& area_speed_hash);//key:area_id value:speed
|
||||
|
||||
private:
|
||||
std::map<std::string, std::list<TiledLayer>> layer_hash;
|
||||
std::map<std::string, std::list<TiledObject>> object_group_hash;
|
||||
|
||||
std::map<int, StagePoint> stage_object_hash; //key point
|
||||
std::vector<TriggerPoint> trigger_list;
|
||||
std::map<std::string, std::vector<GridCell*>> stage_path_hash; //key 11|2
|
||||
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