Skylicht Engine
Loading...
Searching...
No Matches
CWalkingTileMap.h
1/*
2!@
3MIT License
4
5Copyright (c) 2024 Skylicht Technology CO., LTD
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
8(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
9merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
10subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
20This file is part of the "Skylicht Engine".
21https://github.com/skylicht-lab/skylicht-engine
22!#
23*/
24
25#pragma once
26
27#include "Entity/CEntityPrefab.h"
28#include "RenderMesh/CRenderMeshData.h"
29#include "ObstacleAvoidance/CObstacleAvoidance.h"
30
31namespace Skylicht
32{
33 namespace Graph
34 {
35 struct STile
36 {
37 int Id;
38 int X;
39 int Y;
40 int Z;
41 int AreaId;
42 core::vector3df Position;
43 core::aabbox3df BBox;
45 core::array<STile*> Neighbours;
46 bool Visit;
47
48 STile()
49 {
50 Id = 0;
51 X = 0;
52 Y = 0;
53 Z = 0;
54 AreaId = 0;
55 Visit = false;
56 }
57 };
58
59 struct STriArea
60 {
61 int AreaId;
62 u32 A;
63 u32 B;
64 u32 C;
65 core::array<int> Neighbours;
66 };
67
68 struct STileXYZ
69 {
70 int X;
71 int Y;
72 int Z;
73
74 STileXYZ(int x, int y, int z)
75 {
76 X = x;
77 Y = y;
78 Z = z;
79 }
80 };
81
83 {
84 bool operator()(const STileXYZ& a, const STileXYZ& b) const
85 {
86 if (a.Z < b.Z)
87 return true;
88 else if (a.Z > b.Z)
89 return false;
90
91 if (a.Y < b.Y)
92 return true;
93 else if (a.Y > b.Y)
94 return false;
95
96 if (a.X < b.X)
97 return true;
98
99 return false;
100 }
101 };
102
103 typedef std::map<STileXYZ, STile*, CompareTile> TileValueMap;
104
105 class CWalkingTileMap
106 {
107 protected:
108 enum EGenerateStep
109 {
110 None = 0,
111 CollectTile,
112 RemoveEmptyTile,
113 LinkNeighbours,
114 Finish
115 };
116
117 protected:
118 core::array<STile*> m_tiles;
120
121 TileValueMap m_hashTiles;
122
123 float m_tileWidth;
124 float m_tileHeight;
125
126 core::aabbox3df m_bbox;
127
128 CMesh* m_navMesh;
129 CObstacleAvoidance* m_obstacle;
130
131 EGenerateStep m_generateStep;
132 float m_generatePercent;
133 int m_generateId;
134 int m_generateMax;
135
136 public:
137 CWalkingTileMap();
138
139 virtual ~CWalkingTileMap();
140
141 void generate(float tileWidth, float tileHeight, CMesh* navMesh, CObstacleAvoidance* obstacle);
142
143 void beginGenerate(float tileWidth, float tileHeight, CMesh* navMesh, CObstacleAvoidance* obstacle);
144
145 bool updateGenerate();
146
147 float getGeneratePercent();
148
149 void release();
150
151 void resetVisit();
152
153 STile* getTile(int x, int y, int z);
154
155 STile* getTileByPosition(const core::vector3df& pos);
156
157 inline float getTileWidth()
158 {
159 return m_tileWidth;
160 }
161
162 inline float getTileHeight()
163 {
164 return m_tileHeight;
165 }
166
167 inline core::array<STile*>& getTiles()
168 {
169 return m_tiles;
170 }
171
172 inline u32 getNumTile()
173 {
174 return m_tiles.size();
175 }
176
177 bool save(const char* output);
178
179 bool load(const char* input);
180
181 protected:
182
183 void generate(float tileWidth, float tileHeight, const core::aabbox3df& bbox);
184
185 void updateGenerateTile();
186
187 void updateGenerateRemoveEmptyTile();
188
189 void updateGenerateNeighbours();
190
191 bool hitTris(const core::line3df& line, core::array<core::triangle3df>& tris, core::vector3df& outPoint);
192
193 void fillTrisArea();
194
195 void fillTilesArea();
196 };
197 }
198}
Definition CMesh.h:75
Definition CObstacleAvoidance.h:38
Self reallocating template array (like stl vector) with additional features.
Definition irrArray.h:23
Component that manages the navigation lifecycle, including NavMesh generation and pathfinding.
Definition CGraphComponent.h:41
Everything in the Skylicht Engine. You can start by looking at the topics.
Definition AudioDebugLog.h:29
vector3d< f32 > vector3df
Typedef for a f32 3d vector.
Definition vector3d.h:445
line3d< f32 > line3df
Typedef for an f32 line.
Definition line3d.h:136
aabbox3d< f32 > aabbox3df
Typedef for a f32 3d bounding box.
Definition aabbox3d.h:351
unsigned int u32
32 bit unsigned variable.
Definition irrTypes.h:58
Definition CWalkingTileMap.h:83
Definition CWalkingTileMap.h:36
Definition CWalkingTileMap.h:69
Definition CWalkingTileMap.h:60