I know I need to ResourceLoader::get_singleton()
, but I don't know what files to #include, and I'm not keen with using the ResourceLoader. I've got an ArrayMesh saved as "res://Assets/HexMesh.tres"
that I need to set_mesh()
on a MeshInstance3D.
these are the variables I need to set up:
private:
ArrayMesh* mesh;
MeshInstance3D* meshInstance;
here's the source for the HexCell:
#ifndef HEXCELL_H
#define HEXCELL_H
#include <godot_cpp/classes/node3d.hpp>
#include <godot_cpp/classes/array_mesh.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_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");
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_hexSize"), &HexCell::get_hexSize);
ClassDB::bind_method(D_METHOD("set_hexSize", "p_hexSize"), &HexCell::set_hexSize);
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "r"), "set_hexSize", "get_hexSize");
}
private:
ArrayMesh* mesh;
MeshInstance3D* meshInstance;
float hexSize { 1.0 };
int q;
int 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);
}
public:
HexCell() {}
~HexCell() {}
void _ready() {
meshInstance->set_mesh(mesh);
}
void setMesh(ArrayMesh* p_mesh) { mesh = p_mesh; }
ArrayMesh* getMesh() { return mesh; }
void set_hexSize(float p_hexSize) { hexSize = p_hexSize; }
int get_hexSize() { return hexSize; }
void set_q(int pQ) { q = pQ; }
int get_q() { return q; }
void set_r(int pR) { r = pR; }
int get_r() { return r; }
void placeHex() {
Vector2 axial = Vector2(q, r);
Vector3 cube = axialToCube(axial);
Vector3 worldPosition = cubeToWorld(cube);
set_global_transform(Transform3D(Basis(), worldPosition));
}
};
}
#endif // HEXCELL_H