Merged terrain type into Node.

This commit is contained in:
2024-06-06 02:52:39 +02:00
parent 4918c0319a
commit a94ce85cfa
2 changed files with 15 additions and 17 deletions

View File

@@ -6,7 +6,6 @@
#include <set> #include <set>
#define TERRAIN(x, y) (_terrain[(x) + (y) * _width])
#define NODES(x, y) (_nodes[(x) + (y) * _width]) #define NODES(x, y) (_nodes[(x) + (y) * _width])
using namespace godot; using namespace godot;
@@ -19,6 +18,7 @@ Node::Node() { }
Node::Node(int x, int y) Node::Node(int x, int y)
{ {
this->hasUnit = false; this->hasUnit = false;
this->terrainType = BLOCKED;
this->openPass = 0; this->openPass = 0;
this->closedPass = 0; this->closedPass = 0;
@@ -152,7 +152,6 @@ void MultilevelAStarEx::init(const Rect2i &region)
_height = region.get_size().height; _height = region.get_size().height;
_trans = region.get_position(); _trans = region.get_position();
_terrain.resize(_width * _height, BLOCKED);
_nodes.resize(_width * _height); _nodes.resize(_width * _height);
std::vector<Node>::iterator iter = _nodes.begin(); std::vector<Node>::iterator iter = _nodes.begin();
@@ -180,16 +179,16 @@ void MultilevelAStarEx::set_terrain(const Vector2i &cell, TerrainType type)
DEV_ASSERT(_region.has_point(cell)); DEV_ASSERT(_region.has_point(cell));
Vector2i cell2 = cell - _trans; Vector2i cell2 = cell - _trans;
TERRAIN(cell2.x, cell2.y) = type; NODES(cell2.x, cell2.y).terrainType = type;
} }
MultilevelAStarEx::TerrainType MultilevelAStarEx::get_terrain(const Vector2i &cell) const TerrainType MultilevelAStarEx::get_terrain(const Vector2i &cell) const
{ {
DEV_ASSERT(_init); DEV_ASSERT(_init);
DEV_ASSERT(_region.has_point(cell)); DEV_ASSERT(_region.has_point(cell));
Vector2i cell2 = cell - _trans; Vector2i cell2 = cell - _trans;
return TERRAIN(cell2.x, cell2.y); return NODES(cell2.x, cell2.y).terrainType;
} }
void MultilevelAStarEx::set_unit(const Vector2i &cell, bool blocked) void MultilevelAStarEx::set_unit(const Vector2i &cell, bool blocked)
@@ -214,8 +213,8 @@ bool MultilevelAStarEx::can_move(const Node *current, int x, int y) const
{ {
if (NODES(x, y).hasUnit) return false; if (NODES(x, y).hasUnit) return false;
TerrainType tc = TERRAIN(current->x, current->y); TerrainType tc = NODES(current->x, current->y).terrainType;
TerrainType td = TERRAIN(x, y); TerrainType td = NODES(x, y).terrainType;
if (td == BLOCKED) if (td == BLOCKED)
{ {

View File

@@ -10,6 +10,13 @@
namespace godot { namespace godot {
enum TerrainType : int
{
STAIRS = -1,
BLOCKED = 0,
GROUND = 1,
};
class Node { class Node {
friend class MultilevelAStarEx; friend class MultilevelAStarEx;
@@ -21,6 +28,7 @@ public:
private: private:
bool hasUnit; bool hasUnit;
TerrainType terrainType;
int openPass, closedPass; int openPass, closedPass;
int x, y; int x, y;
@@ -47,18 +55,9 @@ class MultilevelAStarEx : public RefCounted
{ {
GDCLASS(MultilevelAStarEx, RefCounted) GDCLASS(MultilevelAStarEx, RefCounted)
public:
enum TerrainType
{
STAIRS = -1,
BLOCKED = 0,
GROUND = 1,
};
private: private:
bool _init; bool _init;
int _pass; int _pass;
std::vector<TerrainType> _terrain;
std::vector<Node> _nodes; std::vector<Node> _nodes;
Rect2i _region; Rect2i _region;
@@ -86,6 +85,6 @@ public:
} }
VARIANT_ENUM_CAST(MultilevelAStarEx::TerrainType); VARIANT_ENUM_CAST(TerrainType);
#endif #endif