-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from GsLogiMaker:feature/tag-class
Add GFTag class and related tests
- Loading branch information
Showing
9 changed files
with
218 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<class name="GFTag" 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 an entity with no data. | ||
</brief_description> | ||
<description> | ||
Tags are a kind of entity that can be attached to other entities to mark them without adding data, unlike components which will add data when attatched. | ||
</description> | ||
<tutorials> | ||
<link title="More on Flecs Tags">https://www.flecs.dev/flecs/md_docs_2Quickstart.html#tag</link> | ||
</tutorials> | ||
<methods> | ||
<method name="from" qualifiers="static"> | ||
<return type="GFTag" /> | ||
<param index="0" name="tag" type="Variant" /> | ||
<param index="1" name="world" type="GFWorld" default="null" /> | ||
<description> | ||
Returns an entity from an ID coerced from a [Variant]. | ||
If no world is specified, a default world is used. | ||
To learn more about [Variant] coercion see [method GFWorld.coerce_id]. | ||
[codeblock] | ||
var entity:= GFTag.from("flecs/core/Trait") | ||
[/codeblock] | ||
</description> | ||
</method> | ||
<method name="from_id" qualifiers="static"> | ||
<return type="GFTag" /> | ||
<param index="0" name="tag_id" type="int" /> | ||
<param index="1" name="world" type="GFWorld" default="null" /> | ||
<description> | ||
Returns an tag from an ID. | ||
If no world is specified, a default world is used. | ||
[codeblock] | ||
var entity:= GFEntity.from_id(255) | ||
[/codeblock] | ||
</description> | ||
</method> | ||
<method name="new_in_world" qualifiers="static"> | ||
<return type="GFTag" /> | ||
<param index="0" name="world" type="GFWorld" /> | ||
<description> | ||
Returns a reference to a new entity created within [param world]. | ||
[codeblock] | ||
var world:= GFWorld.new() | ||
var entity:= GFEntity.new_in_world(world) | ||
[/codeblock] | ||
</description> | ||
</method> | ||
</methods> | ||
</class> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
#include "tag.h" | ||
#include "utils.h" | ||
#include <stdint.h> | ||
#include <flecs.h> | ||
#include "godot_cpp/classes/wrapped.hpp" | ||
#include "godot_cpp/variant/array.hpp" | ||
#include "world.h" | ||
#include <godot_cpp/core/class_db.hpp> | ||
#include <godot_cpp/variant/utility_functions.hpp> | ||
|
||
using namespace godot; | ||
|
||
GFTag::~GFTag() { | ||
} | ||
|
||
Ref<GFTag> GFTag::new_in_world(GFWorld* world) { | ||
return memnew(GFTag(world)); | ||
} | ||
|
||
Ref<GFTag> GFTag::from(Variant tag, GFWorld* world) { | ||
ecs_entity_t tag_id = world->coerce_id(tag); | ||
return from_id(tag_id, world); | ||
} | ||
|
||
Ref<GFTag> GFTag::from_id(ecs_entity_t tag_id, GFWorld* world_) { | ||
GFWorld* world = GFWorld::world_or_singleton(world_); | ||
Ref<GFTag> ett = memnew(GFTag( | ||
tag_id, | ||
world | ||
)); | ||
if (!ett->is_alive()) { | ||
ERR(nullptr, | ||
"Could not instantiate tag from ID\n", | ||
"World/ID is not valid/alive" | ||
); | ||
} | ||
return ett; | ||
} | ||
|
||
|
||
void GFTag::_bind_methods() { | ||
godot::ClassDB::bind_static_method(get_class_static(), D_METHOD("new_in_world", "world"), &GFTag::new_in_world); | ||
godot::ClassDB::bind_static_method(get_class_static(), D_METHOD("from", "tag", "world"), &GFTag::from, nullptr); | ||
godot::ClassDB::bind_static_method(get_class_static(), D_METHOD("from_id", "tag_id", "world"), &GFTag::from_id, nullptr); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
|
||
#ifndef GF_TAG_H | ||
#define GF_TAG_H | ||
|
||
#include "registerable_entity.h" | ||
#include "world.h" | ||
|
||
#include <flecs.h> | ||
#include <godot_cpp/classes/ref_counted.hpp> | ||
#include <godot_cpp/variant/string.hpp> | ||
|
||
namespace godot { | ||
|
||
class GFTag : public GFRegisterableEntity { | ||
GDCLASS(GFTag, GFRegisterableEntity) | ||
|
||
public: | ||
/// Create a new named module | ||
GFTag(GFWorld* world): | ||
GFRegisterableEntity( | ||
ecs_new( | ||
world->raw() | ||
), | ||
world | ||
) | ||
{} | ||
/// Create from existing ID | ||
GFTag(ecs_entity_t id, GFWorld* world): | ||
GFRegisterableEntity( | ||
id, | ||
world | ||
) | ||
{} | ||
GFTag(): | ||
GFRegisterableEntity(GFWorld::singleton()) | ||
{} | ||
|
||
~GFTag(); | ||
|
||
// -------------------------------------- | ||
// --- Exposed | ||
// -------------------------------------- | ||
|
||
static Ref<GFTag> new_in_world(GFWorld* world); | ||
static Ref<GFTag> new_named_in_world(String name, GFWorld*); | ||
static Ref<GFTag> from(Variant module, GFWorld*); | ||
static Ref<GFTag> from_id(ecs_entity_t, GFWorld*); | ||
|
||
// -------------------------------------- | ||
// --- Unexposed | ||
// -------------------------------------- | ||
|
||
protected: | ||
static void _bind_methods(); | ||
|
||
|
||
private: | ||
|
||
}; | ||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
extends GutTest | ||
|
||
var world:GFWorld = null | ||
|
||
func before_each(): | ||
world = GFWorld.new() | ||
|
||
func after_each(): | ||
world.free() | ||
|
||
#region Tests | ||
|
||
func _test_tag_creation(): | ||
var enemy:= GFTag.new_in_world(world) | ||
var robot:=GFEntity.new_in_world(world) \ | ||
.add(enemy) | ||
|
||
assert_true(robot.has_entity(enemy), "Expected `robot` to have the `enemy` tag") | ||
|
||
#endregion | ||
|
||
#region Classes | ||
|
||
class Eats extends GFTag: pass | ||
|
||
class Grass extends GFTag: pass | ||
|
||
#endregion |