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>
#define TERRAIN(x, y) (_terrain[(x) + (y) * _width])
#define NODES(x, y) (_nodes[(x) + (y) * _width])
using namespace godot;
@@ -19,6 +18,7 @@ Node::Node() { }
Node::Node(int x, int y)
{
this->hasUnit = false;
this->terrainType = BLOCKED;
this->openPass = 0;
this->closedPass = 0;
@@ -152,7 +152,6 @@ void MultilevelAStarEx::init(const Rect2i &region)
_height = region.get_size().height;
_trans = region.get_position();
_terrain.resize(_width * _height, BLOCKED);
_nodes.resize(_width * _height);
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));
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(_region.has_point(cell));
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)
@@ -214,8 +213,8 @@ bool MultilevelAStarEx::can_move(const Node *current, int x, int y) const
{
if (NODES(x, y).hasUnit) return false;
TerrainType tc = TERRAIN(current->x, current->y);
TerrainType td = TERRAIN(x, y);
TerrainType tc = NODES(current->x, current->y).terrainType;
TerrainType td = NODES(x, y).terrainType;
if (td == BLOCKED)
{

View File

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