Skip to content

Commit

Permalink
Merge pull request #34 from GsLogiMaker/feature/enitty-new-method
Browse files Browse the repository at this point in the history
Use `new()` as constructors in all objects
  • Loading branch information
GsLogiMaker authored Dec 4, 2024
2 parents 5ee7c7e + a9ef95a commit 43ec38f
Show file tree
Hide file tree
Showing 50 changed files with 476 additions and 460 deletions.
49 changes: 27 additions & 22 deletions cpp/src/component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "component_builder.h"
#include "entity.h"
#include "godot_cpp/classes/wrapped.hpp"
#include "godot_cpp/core/error_macros.hpp"
#include "godot_cpp/variant/array.hpp"
#include "godot_cpp/variant/dictionary.hpp"
#include "godot_cpp/variant/packed_int64_array.hpp"
Expand All @@ -19,37 +20,40 @@

using namespace godot;

GFComponent::GFComponent() {
}
GFComponent::~GFComponent() {
}

Ref<GFComponent> GFComponent::spawn(GFWorld* world_) {
ERR(NULL,
"Could not instantiate ", get_class_static(), "\n",
"Use ", get_class_static(), ".from or ",
get_class_static(), ".from_id instead."
);
}
Ref<GFComponent> GFComponent::from(Variant comp, Variant entity, GFWorld* world) {
return from_id(world->coerce_id(comp), world->coerce_id(entity), world);
}
Ref<GFComponent> GFComponent::from_id(ecs_entity_t comp, ecs_entity_t entity, GFWorld* world) {
const EcsComponent* comp_ptr = GFComponent::get_component_ptr(world, comp);
if (comp_ptr == nullptr) {
const EcsComponent* comp_data = GFComponent::get_component_ptr(world, comp);
if (comp_data == nullptr) {
ERR(nullptr,
"Could not instantiate ", get_class_static(), "\n",
" Entity ", world->id_to_text(comp), " is not a component"
);
}
Ref<GFComponent> component = from_id_template<GFComponent>(comp, world);
component->set_source_id(entity);
return component;
Ref<GFComponent> comp_ref = memnew(GFComponent(entity, comp, world));
return setup_template<GFComponent>(comp_ref);
}

Ref<GFComponent> GFComponent::from_id_no_source(ecs_entity_t comp, GFWorld* world) {
const EcsComponent* comp_data = GFComponent::get_component_ptr(world, comp);
if (comp_data == nullptr) {
ERR(nullptr,
"Could not instantiate ", get_class_static(), "\n",
" Entity ", world->id_to_text(comp), " is not a component"
);
}
Ref<GFComponent> comp_ref = Ref(memnew(GFComponent(0, comp, world)));
comp_ref->update_script();
return comp_ref;
}

void GFComponent::_register_internal() {
// Build component
Ref<GFComponentBuilder> b = get_world()->component_builder();
Ref<GFComponentBuilder> b = memnew(GFComponentBuilder(get_world()));
if (GDVIRTUAL_IS_OVERRIDDEN(_build)) {
b->set_entity(get_id());
GDVIRTUAL_CALL(_build, b);
Expand Down Expand Up @@ -231,6 +235,13 @@ Variant GFComponent::member_value_as_type(
throw "Unreachable";
}

String GFComponent::to_string() {
return String("[#")
+ String::num_int64(get_source_id())
+ "--" + String::num_int64(get_id())
+ "]";
}

Ref<GFEntity> GFComponent::get_source_entity() {
return GFEntity::from(get_source_id(), get_world());
}
Expand Down Expand Up @@ -354,15 +365,10 @@ void GFComponent::set_source_id(ecs_entity_t id) {
source_entity_id = id;
}

Ref<GFRegisterableEntity> GFComponent::new_internal() {
return Ref(memnew(GFComponent));
}

void GFComponent::_bind_methods() {
GDVIRTUAL_BIND(_build, "b");
godot::ClassDB::bind_method(D_METHOD("_register_internal"), &GFComponent::_register_internal);

godot::ClassDB::bind_static_method(GFComponent::get_class_static(), D_METHOD("spawn", "world"), &GFComponent::spawn, nullptr);
godot::ClassDB::bind_static_method(GFComponent::get_class_static(), D_METHOD("from", "component", "world"), &GFComponent::from, nullptr);
godot::ClassDB::bind_static_method(GFComponent::get_class_static(), D_METHOD("from_id", "id", "world"), &GFComponent::from_id, nullptr);

Expand All @@ -374,6 +380,5 @@ void GFComponent::_bind_methods() {
godot::ClassDB::bind_method(D_METHOD("get_data_size"), &GFComponent::get_data_size);
godot::ClassDB::bind_method(D_METHOD("get_data_alignment"), &GFComponent::get_data_alignment);
godot::ClassDB::bind_method(D_METHOD("is_alive"), &GFComponent::is_alive);

godot::ClassDB::bind_static_method(get_class_static(), D_METHOD("_new_internal"), &GFComponent::new_internal);
godot::ClassDB::bind_method(D_METHOD("_to_string"), &GFComponent::to_string);
}
16 changes: 12 additions & 4 deletions cpp/src/component.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@ namespace godot {
GDCLASS(GFComponent, GFRegisterableEntity)

public:
GFComponent();
GFComponent(ecs_entity_t component, GFWorld* world):
GFRegisterableEntity(component, world),
source_entity_id(0)
{}
GFComponent(ecs_entity_t entity, ecs_entity_t component, GFWorld* world):
source_entity_id(entity),
GFRegisterableEntity(component, world) {}
GFRegisterableEntity(component, world)
{}
GFComponent():
source_entity_id(0),
GFRegisterableEntity(0, GFWorld::singleton())
{}
~GFComponent();

// --------------------------------------
Expand All @@ -30,9 +38,9 @@ namespace godot {

GDVIRTUAL1(_build, Ref<GFComponentBuilder>)

static Ref<GFComponent> spawn(GFWorld*);
static Ref<GFComponent> from(Variant c, Variant e, GFWorld*);
static Ref<GFComponent> from_id(ecs_entity_t c, ecs_entity_t e, GFWorld*);
static Ref<GFComponent> from_id_no_source(ecs_entity_t comp, GFWorld* world);

Variant getm(String);
void setm(String, Variant);
Expand All @@ -43,6 +51,7 @@ namespace godot {
int get_data_alignment();

bool is_alive();
String to_string();

// --------------------------------------
// --- Unexposed
Expand All @@ -56,7 +65,6 @@ namespace godot {
static const EcsStruct* get_struct_ptr(GFWorld*, ecs_entity_t);
void set_source_id(ecs_entity_t id);

static Ref<GFRegisterableEntity> new_internal();

protected:
static void _bind_methods();
Expand Down
22 changes: 11 additions & 11 deletions cpp/src/component_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@
#include "component_builder.h"
#include "world.h"
#include "utils.h"
#include "entity.h"

#include <stdlib.h>
#include <flecs.h>

using namespace godot;

GFComponentBuilder::GFComponentBuilder() {
component_desc = {0};
struct_desc = {0};
member_names = Array();
world = {0};
built = {0};
}
GFComponentBuilder::~GFComponentBuilder() {
}

Ref<GFComponentBuilder> GFComponentBuilder::new_in_world(GFWorld* world) {
return memnew(GFComponentBuilder(world));
}

Ref<GFComponentBuilder> GFComponentBuilder::add_member(
String member,
Variant::Type type
Expand Down Expand Up @@ -73,15 +71,15 @@ Ref<GFComponentBuilder> GFComponentBuilder::set_name(
return Ref(this);
}

void GFComponentBuilder::build() {
Ref<GFEntity> GFComponentBuilder::build() {
if (built) {
ERR(/**/,
ERR(nullptr,
"Failed to build component \"" + name + "\".\n",
" Component is already built."
);
}
if (get_member_count() == 0) {
ERR(/**/,
ERR(nullptr,
"Failed to build component \"" + name + "\".\n",
" No members were defined. Specify at least one member."
);
Expand Down Expand Up @@ -126,6 +124,8 @@ void GFComponentBuilder::build() {
}; ecs_set_hooks_id(raw, component_id, &hooks);

ecs_add_path(raw, component_id, 0, component_desc.type.name);

return memnew(GFEntity(component_id, world));
}

void GFComponentBuilder::set_world(GFWorld* world_) {
Expand All @@ -137,11 +137,11 @@ void GFComponentBuilder::set_world(GFWorld* world_) {
// **********************************************

void GFComponentBuilder::_bind_methods() {
godot::ClassDB::bind_static_method(get_class_static(), D_METHOD("new_in_world", "world"), &GFComponentBuilder::new_in_world);
godot::ClassDB::bind_method(D_METHOD("add_member", "member", "type"), &GFComponentBuilder::add_member);
godot::ClassDB::bind_method(D_METHOD("is_built"), &GFComponentBuilder::is_built);
godot::ClassDB::bind_method(D_METHOD("set_name", "name"), &GFComponentBuilder::set_name);
godot::ClassDB::bind_method(D_METHOD("build"), &GFComponentBuilder::build);

}

// **********************************************
Expand Down
17 changes: 15 additions & 2 deletions cpp/src/component_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#ifndef COMPONENT_Builder_H
#define COMPONENT_Builder_H

#include "world.h"
#include <flecs.h>
#include <godot_cpp/classes/ref_counted.hpp>
#include <godot_cpp/variant/string.hpp>
Expand All @@ -15,20 +16,32 @@ namespace godot {
GDCLASS(GFComponentBuilder, RefCounted)

public:
GFComponentBuilder();
GFComponentBuilder(GFWorld* world):
component_desc({0}),
struct_desc({0}),
name(""),
member_names(Array()),
world(world),
built(false)
{}
GFComponentBuilder():
GFComponentBuilder(GFWorld::singleton())
{}
~GFComponentBuilder();

// **************************************
// *** Exposed ***
// **************************************

static Ref<GFComponentBuilder> new_in_world(GFWorld*);

Ref<GFComponentBuilder> add_member(String, Variant::Type);
int get_member_count();
GFWorld* get_world();
bool is_built();
Ref<GFComponentBuilder> set_entity(Variant);
Ref<GFComponentBuilder> set_name(String);
void build();
Ref<GFEntity> build();

// **************************************
// *** Unexposed ***
Expand Down
11 changes: 5 additions & 6 deletions cpp/src/doc_classes/GFComponent.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GFComponent" inherits="GFRegisterableEntity"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd">
<class name="GFComponent" inherits="GFRegisterableEntity" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd">
<brief_description>
A reference to a Glecs component from a [GFWorld] that is attatached to an entity.
</brief_description>
Expand Down Expand Up @@ -30,7 +29,7 @@
Returns a [GFEntity] reference to the entity this component's data is from.
[codeblock]
var Vector2C = GFEntity.from("glecs/meta/Vector2")
var entity = GFEntity.spawn()
var entity = GFEntity.new()
entity.add_component(Vector2C)
var vec2 = entity.get_component(Vector2C)

Expand All @@ -44,7 +43,7 @@
Returns the ID of the entity that this component's data is from.
[codeblock]
var Vector2C = GFEntity.from("glecs/meta/Vector2")
var entity = GFEntity.spawn()
var entity = GFEntity.new()
entity.add_component(Vector2C)
var vec2 = entity.get_component(Vector2C)

Expand All @@ -58,7 +57,7 @@
<description>
Returns the value of a component's member.
[codeblock]
var entity = GFEntity.spawn()
var entity = GFEntity.new()
entity.add_component("glecs/meta/Vector2")
var vec2 = entity.get_component("glecs/meta/Vector2")

Expand All @@ -73,7 +72,7 @@
<description>
Sets the value of a component's member.
[codeblock]
var entity = GFEntity.spawn()
var entity = GFEntity.new()
entity.add_component("glecs/meta/Vector2")
var vec2 = entity.get_component("glecs/meta/Vector2")

Expand Down
18 changes: 12 additions & 6 deletions cpp/src/doc_classes/GFComponentBuilder.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
<description>
This example shows the creation of a [i]Jump[/i] component:
[codeblock]
var world:= GFWorld.new()
world.component_builder() \
.set_name("Jump") \
.add_member("jump_velocity", TYPE_FLOAT) \
.build()
GFComponentBuilder.new() \
.set_name("Jump") \
.add_member("jump_velocity", TYPE_FLOAT) \
.build()
[/codeblock]
</description>
<tutorials>
Expand All @@ -26,7 +25,7 @@
</description>
</method>
<method name="build">
<return type="void" />
<return type="GFEntity" />
<description>
Creates the new component from the settings of the builder.
</description>
Expand All @@ -37,6 +36,13 @@
Returns [code]true[/code] if this builder has already been built.
</description>
</method>
<method name="new_in_world" qualifiers="static">
<return type="GFComponentBuilder" />
<param index="0" name="world" type="GFWorld" />
<description>
Returns a new builder in [param world].
</description>
</method>
<method name="set_name">
<return type="GFComponentBuilder" />
<param index="0" name="name" type="String" />
Expand Down
Loading

0 comments on commit 43ec38f

Please sign in to comment.