ESSEntitySpawner is now inherited from Node instead of from Resource. This will make networked spawning code simpler, and more intuitive.

This commit is contained in:
Relintai 2020-09-22 13:47:25 +02:00
parent aaf6ea68d2
commit 7267f327cb
4 changed files with 26 additions and 58 deletions

View File

@ -88,13 +88,17 @@ void ESS::set_resource_db(const Ref<ESSResourceDB> &resource_db) {
_ess_resource_db = resource_db; _ess_resource_db = resource_db;
} }
Ref<ESSEntitySpawner> ESS::get_entity_spawner() { ESSEntitySpawner *ESS::get_entity_spawner() {
return _ess_entity_spawner; return _ess_entity_spawner;
} }
void ESS::set_entity_spawner(const Ref<ESSEntitySpawner> &spawner) { void ESS::set_entity_spawner(ESSEntitySpawner *spawner) {
_ess_entity_spawner = spawner; _ess_entity_spawner = spawner;
} }
void ESS::set_entity_spawner_bind(Node *spawner) {
_ess_entity_spawner = Object::cast_to<ESSEntitySpawner>(spawner);
}
String ESS::get_resource_db_path() { String ESS::get_resource_db_path() {
return _ess_resource_db_path; return _ess_resource_db_path;
} }
@ -102,19 +106,12 @@ void ESS::set_resource_db_path(const String &path) {
_ess_resource_db_path = path; _ess_resource_db_path = path;
} }
String ESS::get_entity_spawner_path() {
return _ess_entity_spawner_path;
}
void ESS::set_entity_spawner_path(const String &path) {
_ess_entity_spawner_path = path;
}
void ESS::request_entity_spawn(Ref<EntityCreateInfo> info) { void ESS::request_entity_spawn(Ref<EntityCreateInfo> info) {
if (_ess_entity_spawner.is_valid()) if (_ess_entity_spawner)
_ess_entity_spawner->request_entity_spawn(info); _ess_entity_spawner->request_entity_spawn(info);
} }
void ESS::request_entity_spawn_deferred(Ref<EntityCreateInfo> info) { void ESS::request_entity_spawn_deferred(Ref<EntityCreateInfo> info) {
if (_ess_entity_spawner.is_valid()) if (_ess_entity_spawner)
_ess_entity_spawner->request_entity_spawn_deferred(info); _ess_entity_spawner->request_entity_spawn_deferred(info);
} }
@ -130,18 +127,6 @@ void ESS::load_resource_db() {
_ess_resource_db = d; _ess_resource_db = d;
} }
void ESS::load_entity_spawner() {
_Directory dir;
ERR_FAIL_COND(_ess_entity_spawner_path == "");
Ref<ESSEntitySpawner> d = load_resource(_ess_entity_spawner_path, "ESSEntitySpawner");
ERR_FAIL_COND(!d.is_valid());
_ess_entity_spawner = d;
}
Ref<Resource> ESS::load_resource(const String &path, const String &type_hint) { Ref<Resource> ESS::load_resource(const String &path, const String &type_hint) {
_ResourceLoader *rl = _ResourceLoader::get_singleton(); _ResourceLoader *rl = _ResourceLoader::get_singleton();
@ -160,16 +145,10 @@ Ref<Resource> ESS::load_resource(const String &path, const String &type_hint) {
void ESS::load_all() { void ESS::load_all() {
load_resource_db(); load_resource_db();
load_entity_spawner();
_ess_resource_db->initialize(); _ess_resource_db->initialize();
} }
void ESS::setup(const Ref<ESSResourceDB> &resource_db, const Ref<ESSEntitySpawner> &entity_spawner) {
_ess_resource_db = resource_db;
_ess_entity_spawner = entity_spawner;
}
//Stats //Stats
void ESS::stat_set_string(const String &stat_enum_string) { void ESS::stat_set_string(const String &stat_enum_string) {
_stat_enum_string = stat_enum_string; _stat_enum_string = stat_enum_string;
@ -564,20 +543,15 @@ void ESS::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "resource_db", PROPERTY_HINT_RESOURCE_TYPE, "ESSResourceDB"), "set_resource_db", "get_resource_db"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "resource_db", PROPERTY_HINT_RESOURCE_TYPE, "ESSResourceDB"), "set_resource_db", "get_resource_db");
ClassDB::bind_method(D_METHOD("get_entity_spawner"), &ESS::get_entity_spawner); ClassDB::bind_method(D_METHOD("get_entity_spawner"), &ESS::get_entity_spawner);
ClassDB::bind_method(D_METHOD("set_entity_spawner"), &ESS::set_entity_spawner); ClassDB::bind_method(D_METHOD("set_entity_spawner"), &ESS::set_entity_spawner_bind);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "entity_spawner", PROPERTY_HINT_RESOURCE_TYPE, "ESSEntitySpawner"), "set_entity_spawner", "get_entity_spawner"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "entity_spawner", PROPERTY_HINT_RESOURCE_TYPE, "ESSEntitySpawner"), "set_entity_spawner", "get_entity_spawner");
ClassDB::bind_method(D_METHOD("get_resource_db_path"), &ESS::get_resource_db_path); ClassDB::bind_method(D_METHOD("get_resource_db_path"), &ESS::get_resource_db_path);
ClassDB::bind_method(D_METHOD("set_resource_db_path", "path"), &ESS::set_resource_db_path); ClassDB::bind_method(D_METHOD("set_resource_db_path", "path"), &ESS::set_resource_db_path);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_db_path"), "set_resource_db_path", "get_resource_db_path"); ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_db_path"), "set_resource_db_path", "get_resource_db_path");
ClassDB::bind_method(D_METHOD("get_entity_spawner_path"), &ESS::get_entity_spawner_path);
ClassDB::bind_method(D_METHOD("set_entity_spawner_path", "path"), &ESS::set_entity_spawner_path);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "entity_spawner_path"), "set_entity_spawner_path", "get_entity_spawner_path");
//load //load
ClassDB::bind_method(D_METHOD("load_resource_db"), &ESS::load_resource_db); ClassDB::bind_method(D_METHOD("load_resource_db"), &ESS::load_resource_db);
ClassDB::bind_method(D_METHOD("load_entity_spawner"), &ESS::load_entity_spawner);
ClassDB::bind_method(D_METHOD("load_resource", "path", "type_hint"), &ESS::load_resource, DEFVAL("")); ClassDB::bind_method(D_METHOD("load_resource", "path", "type_hint"), &ESS::load_resource, DEFVAL(""));
@ -586,8 +560,6 @@ void ESS::_bind_methods() {
ClassDB::bind_method(D_METHOD("load_all"), &ESS::load_all); ClassDB::bind_method(D_METHOD("load_all"), &ESS::load_all);
ClassDB::bind_method(D_METHOD("setup", "resource_db", "entity_spawner"), &ESS::setup);
//Stats //Stats
ClassDB::bind_method(D_METHOD("stat_get_string"), &ESS::stat_get_string); ClassDB::bind_method(D_METHOD("stat_get_string"), &ESS::stat_get_string);
ClassDB::bind_method(D_METHOD("stat_set_string", "stat_enum_string"), &ESS::stat_set_string); ClassDB::bind_method(D_METHOD("stat_set_string", "stat_enum_string"), &ESS::stat_set_string);
@ -682,6 +654,7 @@ void ESS::_bind_methods() {
ESS::ESS() { ESS::ESS() {
instance = this; instance = this;
_ess_entity_spawner = NULL;
_use_spell_points = GLOBAL_DEF("ess/spells/use_spell_points", false); _use_spell_points = GLOBAL_DEF("ess/spells/use_spell_points", false);
_scale_spells_by_default = GLOBAL_DEF("ess/spells/scale_spells_by_default", false); _scale_spells_by_default = GLOBAL_DEF("ess/spells/scale_spells_by_default", false);
@ -696,7 +669,6 @@ ESS::ESS() {
_automatic_load = GLOBAL_DEF("ess/data/automatic_load", false); _automatic_load = GLOBAL_DEF("ess/data/automatic_load", false);
_ess_resource_db_path = GLOBAL_DEF("ess/data/ess_resource_db_path", ""); _ess_resource_db_path = GLOBAL_DEF("ess/data/ess_resource_db_path", "");
_ess_entity_spawner_path = GLOBAL_DEF("ess/data/ess_entity_spawner_path", "");
stat_set_string(GLOBAL_DEF("ess/enums/stats", "Agility,Strength,Stamina,Intellect,Spirit,Health,Speed,Global Cooldown,Haste")); stat_set_string(GLOBAL_DEF("ess/enums/stats", "Agility,Strength,Stamina,Intellect,Spirit,Health,Speed,Global Cooldown,Haste"));
_stat_main_stat_count = GLOBAL_DEF("ess/enums/main_stat_count", 5); _stat_main_stat_count = GLOBAL_DEF("ess/enums/main_stat_count", 5);
@ -727,7 +699,7 @@ ESS::~ESS() {
instance = NULL; instance = NULL;
_ess_resource_db.unref(); _ess_resource_db.unref();
_ess_entity_spawner.unref(); _ess_entity_spawner = NULL;
_stat_id_to_name.clear(); _stat_id_to_name.clear();
_stat_name_to_id.clear(); _stat_name_to_id.clear();

View File

@ -72,26 +72,21 @@ public:
Ref<ESSResourceDB> get_resource_db(); Ref<ESSResourceDB> get_resource_db();
void set_resource_db(const Ref<ESSResourceDB> &resource_db); void set_resource_db(const Ref<ESSResourceDB> &resource_db);
Ref<ESSEntitySpawner> get_entity_spawner(); ESSEntitySpawner *get_entity_spawner();
void set_entity_spawner(const Ref<ESSEntitySpawner> &spawner); void set_entity_spawner(ESSEntitySpawner *spawner);
void set_entity_spawner_bind(Node *spawner);
String get_resource_db_path(); String get_resource_db_path();
void set_resource_db_path(const String &path); void set_resource_db_path(const String &path);
String get_entity_spawner_path();
void set_entity_spawner_path(const String &path);
void request_entity_spawn(Ref<EntityCreateInfo> info); void request_entity_spawn(Ref<EntityCreateInfo> info);
void request_entity_spawn_deferred(Ref<EntityCreateInfo> info); void request_entity_spawn_deferred(Ref<EntityCreateInfo> info);
void load_resource_db(); void load_resource_db();
void load_entity_spawner();
Ref<Resource> load_resource(const String &path, const String &type_hint = ""); Ref<Resource> load_resource(const String &path, const String &type_hint = "");
void load_all(); void load_all();
void setup(const Ref<ESSResourceDB> &resource_db, const Ref<ESSEntitySpawner> &entity_spawner);
//Stats //Stats
String stat_get_string() const; String stat_get_string() const;
void stat_set_string(const String &stat_enum_string); void stat_set_string(const String &stat_enum_string);
@ -188,10 +183,9 @@ private:
bool _automatic_load; bool _automatic_load;
Ref<ESSResourceDB> _ess_resource_db; Ref<ESSResourceDB> _ess_resource_db;
Ref<ESSEntitySpawner> _ess_entity_spawner; ESSEntitySpawner *_ess_entity_spawner;
String _ess_resource_db_path; String _ess_resource_db_path;
String _ess_entity_spawner_path;
static ESS *instance; static ESS *instance;

View File

@ -22,6 +22,8 @@ SOFTWARE.
#include "ess_entity_spawner.h" #include "ess_entity_spawner.h"
#include "../singletons/ess.h"
#include "../utility/entity_create_info.h" #include "../utility/entity_create_info.h"
_FORCE_INLINE_ void ESSEntitySpawner::request_entity_spawn(Ref<EntityCreateInfo> info) { _FORCE_INLINE_ void ESSEntitySpawner::request_entity_spawn(Ref<EntityCreateInfo> info) {
@ -35,17 +37,21 @@ _FORCE_INLINE_ void ESSEntitySpawner::request_entity_spawn_deferred(Ref<EntityCr
} }
ESSEntitySpawner::ESSEntitySpawner() { ESSEntitySpawner::ESSEntitySpawner() {
if (ESS::get_singleton()) {
ESS::get_singleton()->set_entity_spawner(this);
}
} }
ESSEntitySpawner::~ESSEntitySpawner() { ESSEntitySpawner::~ESSEntitySpawner() {
if (ESS::get_singleton() && ESS::get_singleton()->get_entity_spawner() == this) {
ESS::get_singleton()->set_entity_spawner(NULL);
}
} }
void ESSEntitySpawner::_bind_methods() { void ESSEntitySpawner::_bind_methods() {
BIND_VMETHOD(MethodInfo("_request_entity_spawn", PropertyInfo(Variant::OBJECT, "info", PROPERTY_HINT_RESOURCE_TYPE, "EntityCreateInfo"))); BIND_VMETHOD(MethodInfo("_request_entity_spawn", PropertyInfo(Variant::OBJECT, "info", PROPERTY_HINT_RESOURCE_TYPE, "EntityCreateInfo")));
ADD_SIGNAL(MethodInfo("on_entity_spawn", PropertyInfo(Variant::OBJECT, "info", PROPERTY_HINT_RESOURCE_TYPE, "EntityCreateInfo"))); ADD_SIGNAL(MethodInfo("on_entity_spawn", PropertyInfo(Variant::OBJECT, "info", PROPERTY_HINT_RESOURCE_TYPE, "EntityCreateInfo")));
ClassDB::bind_method(D_METHOD("get_scene_tree"), &ESSEntitySpawner::get_scene_tree);
ClassDB::bind_method(D_METHOD("request_entity_spawn", "info"), &ESSEntitySpawner::request_entity_spawn); ClassDB::bind_method(D_METHOD("request_entity_spawn", "info"), &ESSEntitySpawner::request_entity_spawn);
ClassDB::bind_method(D_METHOD("request_entity_spawn_deferred", "info"), &ESSEntitySpawner::request_entity_spawn_deferred); ClassDB::bind_method(D_METHOD("request_entity_spawn_deferred", "info"), &ESSEntitySpawner::request_entity_spawn_deferred);
} }

View File

@ -23,20 +23,16 @@ SOFTWARE.
#ifndef ESS_ENTITY_SPAWNER_H #ifndef ESS_ENTITY_SPAWNER_H
#define ESS_ENTITY_SPAWNER_H #define ESS_ENTITY_SPAWNER_H
#include "core/resource.h" #include "scene/main/node.h"
#include "scene/main/scene_tree.h" #include "scene/main/scene_tree.h"
class EntityCreateInfo; class EntityCreateInfo;
class ESSEntitySpawner : public Resource { class ESSEntitySpawner : public Node {
GDCLASS(ESSEntitySpawner, Resource); GDCLASS(ESSEntitySpawner, Node);
public: public:
_FORCE_INLINE_ SceneTree *get_scene_tree() const {
return SceneTree::get_singleton();
}
void request_entity_spawn(Ref<EntityCreateInfo> info); void request_entity_spawn(Ref<EntityCreateInfo> info);
void request_entity_spawn_deferred(Ref<EntityCreateInfo> info); void request_entity_spawn_deferred(Ref<EntityCreateInfo> info);