feat: implemented singleton fully for game state

This commit is contained in:
Sara 2025-06-24 14:20:06 +02:00
parent 1828ebbe21
commit 8d07ee03fa
5 changed files with 40 additions and 18 deletions

View file

@ -1,11 +1,25 @@
#include "game_state.h" #include "game_state.h"
#include "macros.h" #include "macros.h"
GameState *GameState::singleton_instance{ nullptr };
void GameState::_bind_methods() { void GameState::_bind_methods() {
BIND_PROPERTY(Variant::STRING, locale_uid); BIND_PROPERTY(Variant::STRING, locale_uid);
BIND_PROPERTY(Variant::STRING, locale_entrance_path); BIND_PROPERTY(Variant::STRING, locale_entrance_path);
} }
GameState::GameState() {
self_type::singleton_instance = this;
}
GameState::~GameState() {
self_type::singleton_instance = nullptr;
}
GameState *GameState::get_singleton() {
return self_type::singleton_instance;
}
void GameState::set_locale_uid(String const &value) { void GameState::set_locale_uid(String const &value) {
this->locale_uid = value; this->locale_uid = value;
} }
@ -21,4 +35,3 @@ void GameState::set_locale_entrance_path(String const &value) {
String GameState::get_locale_entrance_path() const { String GameState::get_locale_entrance_path() const {
return this->locale_uid; return this->locale_uid;
} }

View file

@ -1,18 +1,23 @@
#ifndef GAME_STATE_H #ifndef GAME_STATE_H
#define GAME_STATE_H #define GAME_STATE_H
#include "core/object/object.h"
#include "core/object/class_db.h" #include "core/object/class_db.h"
#include "core/object/object.h"
class GameState : public Object { class GameState : public Object {
GDCLASS(GameState, Object); GDCLASS(GameState, Object);
static void _bind_methods(); static void _bind_methods();
static GameState *singleton_instance;
public: public:
GameState();
virtual ~GameState();
static GameState *get_singleton(); static GameState *get_singleton();
void set_locale_uid(String const &value); void set_locale_uid(String const &value);
String get_locale_uid() const; String get_locale_uid() const;
void set_locale_entrance_path(String const &value); void set_locale_entrance_path(String const &value);
String get_locale_entrance_path() const; String get_locale_entrance_path() const;
private: private:
String locale_uid{ "" }; String locale_uid{ "" };
String locale_entrance_path{ "" }; String locale_entrance_path{ "" };

View file

@ -13,7 +13,8 @@ void LocaleMarker::_notification(int what) {
return; return;
} }
switch (what) { switch (what) {
default: return; default:
return;
case NOTIFICATION_READY: case NOTIFICATION_READY:
this->ready(); this->ready();
return; return;
@ -27,8 +28,9 @@ void LocaleMarker::ready() {
void LocaleMarker::on_input_event(Node *camera, Ref<InputEvent> event, Vector3 position, Vector3 normal, int shape_ind) { void LocaleMarker::on_input_event(Node *camera, Ref<InputEvent> event, Vector3 position, Vector3 normal, int shape_ind) {
Ref<InputEventMouseButton> clicked{ event }; Ref<InputEventMouseButton> clicked{ event };
if (clicked.is_valid() && clicked->get_button_index() == MouseButton::LEFT) { if (clicked.is_valid() && clicked->get_button_index() == MouseButton::LEFT) {
GameState::get_singleton()->set_locale_uid(this->locale_scene);
GameState::get_singleton()->set_locale_entrance_path(this->entrance_path); GameState::get_singleton()->set_locale_entrance_path(this->entrance_path);
GameState::get_singleton()->set_locale_uid(locale_scene); get_tree()->change_scene_to_file(this->locale_scene);
} }
} }

View file

@ -11,11 +11,13 @@ class LocaleMarker : public Area3D {
void _notification(int what); void _notification(int what);
void ready(); void ready();
void on_input_event(Node *camera, Ref<InputEvent> event, Vector3 position, Vector3 normal, int shape_ind); void on_input_event(Node *camera, Ref<InputEvent> event, Vector3 position, Vector3 normal, int shape_ind);
public: public:
void set_locale_scene(String const &value); void set_locale_scene(String const &value);
String get_locale_scene() const; String get_locale_scene() const;
void set_entrance_path(String const &value); void set_entrance_path(String const &value);
String get_entrance_path() const; String get_entrance_path() const;
private: private:
String locale_scene{ "res://" }; String locale_scene{ "res://" };
String entrance_path{ "%" }; String entrance_path{ "%" };