feat: fixed global fetching, added get_player_character/position to global world state
This commit is contained in:
parent
7d227d105b
commit
8d3484163c
|
@ -1,10 +1,13 @@
|
||||||
#include "global_world_state.hpp"
|
#include "global_world_state.hpp"
|
||||||
#include "character_actor.hpp"
|
#include "character_actor.hpp"
|
||||||
#include "utils/game_root.hpp"
|
#include "utils/game_root.hpp"
|
||||||
|
#include "utils/godot_macros.h"
|
||||||
|
#include <godot_cpp/variant/utility_functions.hpp>
|
||||||
|
|
||||||
namespace godot::goap {
|
namespace godot::goap {
|
||||||
void GlobalWorldState::_bind_methods() {
|
void GlobalWorldState::_bind_methods() {
|
||||||
#define CLASSNAME GlobalWorldState
|
#define CLASSNAME GlobalWorldState
|
||||||
|
GDFUNCTION(get_player_character);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GlobalWorldState::has_singleton() {
|
bool GlobalWorldState::has_singleton() {
|
||||||
|
@ -15,29 +18,31 @@ GlobalWorldState *GlobalWorldState::get_singleton() {
|
||||||
return GlobalWorldState::singleton_instance;
|
return GlobalWorldState::singleton_instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GlobalWorldState::_enter_tree() {
|
void GlobalWorldState::_enter_tree() { GDGAMEONLY();
|
||||||
if(GlobalWorldState::singleton_instance == nullptr)
|
if(GlobalWorldState::singleton_instance == nullptr) {
|
||||||
GlobalWorldState::singleton_instance = this;
|
GlobalWorldState::singleton_instance = this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GlobalWorldState::_ready() {
|
void GlobalWorldState::_exit_tree() { GDGAMEONLY();
|
||||||
this->game_mode = GameRoot::get_singleton()->get_game_mode();
|
|
||||||
}
|
|
||||||
|
|
||||||
void GlobalWorldState::_exit_tree() {
|
|
||||||
if(GlobalWorldState::singleton_instance == this)
|
if(GlobalWorldState::singleton_instance == this)
|
||||||
GlobalWorldState::singleton_instance = nullptr;
|
GlobalWorldState::singleton_instance = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GlobalWorldState::_process(double delta_time) {
|
void GlobalWorldState::_process(double delta_time) { GDGAMEONLY();
|
||||||
global_state_cache.clear(); // invalidate cache
|
global_state_cache.clear(); // invalidate cache
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3 GlobalWorldState::get_player_position() {
|
Vector3 GlobalWorldState::get_player_position() const {
|
||||||
return this->game_mode->get_player_instance()->get_character()->get_global_position();
|
return this->get_player_character()->get_global_position();
|
||||||
|
}
|
||||||
|
|
||||||
|
CharacterActor *GlobalWorldState::get_player_character() const {
|
||||||
|
return Ref<TunnelsGameMode>(GameRoot::get_singleton()->get_game_mode())->get_player_instance()->get_character();
|
||||||
}
|
}
|
||||||
|
|
||||||
Variant GlobalWorldState::get_world_property(StringName prop_key) {
|
Variant GlobalWorldState::get_world_property(StringName prop_key) {
|
||||||
|
UtilityFunctions::print("fetching: ", prop_key);
|
||||||
// check if prop key corresponds to a global key
|
// check if prop key corresponds to a global key
|
||||||
if(!prop_key.begins_with("g_"))
|
if(!prop_key.begins_with("g_"))
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -45,13 +50,14 @@ Variant GlobalWorldState::get_world_property(StringName prop_key) {
|
||||||
else if(global_state_cache.has(prop_key))
|
else if(global_state_cache.has(prop_key))
|
||||||
return global_state_cache.get(prop_key);
|
return global_state_cache.get(prop_key);
|
||||||
// fetch by function name
|
// fetch by function name
|
||||||
StringName const fn = "get_" + prop_key.right(prop_key.length() - 2);
|
StringName const fn = "get_" + prop_key.erase(0, 2);
|
||||||
if(this->has_method(fn)) {
|
if(this->has_method(fn)) {
|
||||||
Variant result = this->call(fn);
|
Variant result = this->call(fn);
|
||||||
// cache and return
|
// cache and return
|
||||||
this->global_state_cache.insert(prop_key, result);
|
this->global_state_cache.insert(prop_key, result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
abort();
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,16 +14,15 @@ public:
|
||||||
static GlobalWorldState *get_singleton();
|
static GlobalWorldState *get_singleton();
|
||||||
|
|
||||||
virtual void _enter_tree() override;
|
virtual void _enter_tree() override;
|
||||||
virtual void _ready() override;
|
|
||||||
virtual void _exit_tree() override;
|
virtual void _exit_tree() override;
|
||||||
virtual void _process(double delta_time) override;
|
virtual void _process(double delta_time) override;
|
||||||
|
|
||||||
Vector3 get_player_position();
|
Vector3 get_player_position() const;
|
||||||
|
CharacterActor *get_player_character() const;
|
||||||
|
|
||||||
Variant get_world_property(StringName prop_key);
|
Variant get_world_property(StringName prop_key);
|
||||||
private:
|
private:
|
||||||
WorldState global_state_cache{};
|
WorldState global_state_cache{};
|
||||||
Ref<TunnelsGameMode> game_mode{};
|
|
||||||
static GlobalWorldState *singleton_instance;
|
static GlobalWorldState *singleton_instance;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue