#define HEXMAP_H
#include <vector>
#include <godot_cpp/classes/node3d.hpp>
#include <godot_cpp/classes/window.hpp>
#include <godot_cpp/classes/scene_tree.hpp>
#include <godot_cpp/classes/packed_scene.hpp>
#include <godot_cpp/classes/resource_loader.hpp>
#include "hexcell.h"
namespace godot {
class HexMap : public Node3D {
GDCLASS(HexMap, Node3D)
protected:
static void _bind_methods();
private:
float hexSize { 1.0 };
double hexCellWidth { hexSize * sqrt(3) };
Vector2 mapSize;
std::vector<std::vector<HexCell>> hexCells;
Node3D* cameraRig { nullptr };
public:
HexMap();
~HexMap();
void setNumCol(int p_numCol);
int getNumCol();
void setNumRow(int p_numRow);
int getNumRow();
void _ready();
void _process(float delta);
void generateHexMap();
void checkAndWrapHex(HexCell hexCell);
};
}
#endif // HEXMAP_H```
#Need help with _bind_methods() in GDExtension
1 messages · Page 1 of 1 (latest)
void HexMap::_bind_methods() {
ClassDB::bind_method(D_METHOD("setNumCol", "p_numCol"), &HexMap::setNumCol);
ClassDB::bind_method(D_METHOD("getNumCol"), &HexMap::getNumCol);
ADD_PROPERTY(PropertyInfo(Variant::INT, "num_col"), "setNumCol", "getNumCol");
ClassDB::bind_method(D_METHOD("setNumRow", "p_numRow"), &HexMap::setNumRow);
ClassDB::bind_method(D_METHOD("getNumRow"), &HexMap::getNumRow);
ADD_PROPERTY(PropertyInfo(Variant::INT, "num_row"), "setNumRow", "getNumRow");
ClassDB::bind_method(D_METHOD("generateHexMap"), &HexMap::generateHexMap);
ClassDB::bind_method(D_METHOD("checkAndWrapHex", "hex"), &HexMap::checkAndWrapHex);
}
void HexMap::generateHexMap() {
// Clear existing array
hexCells.clear();
// For hexCell in hexCells, instantiate and place hex.
for (int q = 0; q < mapSize.x; q++) {
std::vector<HexCell> column;
for (int r = 0; r < mapSize.y; r++) {
HexCell hexCell;
// Set hexCell location
hexCell.set_q(q);
hexCell.set_r(r);
hexCell.placeHex();
// Add hexCell to column
column.push_back(hexCell);
}
hexCells.push_back(column);
}
}
void HexMap::checkAndWrapHex(HexCell hexCell) {
// Check the distance between the tile and the camera in world coordinates (X-axis only)
Vector3 tilePosition = hexCell.get_transform().origin;
Vector3 cameraPosition = cameraRig->get_transform().origin;
float distanceX = abs(tilePosition.x - cameraPosition.x);
// Calculate how many map widths away the tile is from the camera along the X-axis
float mapWidthsAwayX = distanceX / (hexCellWidth * mapSize.x);
// If the tile is more than 0.5 map widths away from the camera along the X-axis, wrap it to the other side
if (mapWidthsAwayX > 0.5) {
// Calculate the new position of the tile to wrap it along the X-axis
Vector3 wrappedPosition = tilePosition;
if (tilePosition.x - cameraPosition.x > 0) {
wrappedPosition.x -= hexCellWidth * mapSize.x;
} else {
wrappedPosition.x += hexCellWidth * mapSize.x;
}
// Set the new position for the tile
Transform3D transform = hexCell.get_transform();
transform.origin.x = wrappedPosition.x;
hexCell.set_transform(transform);
}
}
#define HEXCELL_H
#include <godot_cpp/classes/node3d.hpp>
#include <godot_cpp/classes/mesh_instance3d.hpp>
namespace godot {
class HexCell : public Node3D {
GDCLASS(HexCell, Node3D)
protected:
static void _bind_methods() {
ClassDB::bind_method(D_METHOD("get_q"), &HexCell::get_q);
ClassDB::bind_method(D_METHOD("set_q", "pQ"), &HexCell::set_q);
ADD_PROPERTY(PropertyInfo(Variant::INT, "q"), "set_q", "get_q");
ClassDB::bind_method(D_METHOD("get_r"), &HexCell::get_r);
ClassDB::bind_method(D_METHOD("set_r", "pR"), &HexCell::set_r);
ADD_PROPERTY(PropertyInfo(Variant::INT, "r"), "set_r", "get_r");
}
float hexSize { 1.0 };
int q;
int r;
public:
HexCell() {}
~HexCell() {}
void set_q(int pQ) { q = pQ; }
int get_q() { return q; }
void set_r(int pR) { r = pR; }
int get_r() { return r; }
Vector3 axialToCube(Vector2 axial) {
float x = axial.x;
float z = axial.y;
float y = -x - z;
return Vector3(x, y, z);
}
Vector3 cubeToWorld(Vector3 cube) {
float x = hexSize * sqrt(3) * (cube.x + cube.z / 2);
float y = 0;
float z = hexSize * 3/2 * cube.z;
return Vector3(x, y, z);
}
void placeHex() {
Vector2 axial = Vector2(q, r);
Vector3 cube = axialToCube(axial);
Vector3 worldPosition = cubeToWorld(cube);
set_global_transform(Transform3D(Basis(), worldPosition));
}
};
}
#endif // HEXCELL_H```
You have to use pointers with nodes being passed to/from GDScript. Use HexCell* for your parameter types.