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 "macros.h"
GameState *GameState::singleton_instance{ nullptr };
void GameState::_bind_methods() {
BIND_PROPERTY(Variant::STRING, locale_uid);
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) {
this->locale_uid = value;
}
@ -21,4 +35,3 @@ void GameState::set_locale_entrance_path(String const &value) {
String GameState::get_locale_entrance_path() const {
return this->locale_uid;
}

View file

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

View file

@ -9,14 +9,15 @@ void LocaleMarker::_bind_methods() {
}
void LocaleMarker::_notification(int what) {
if(Engine::get_singleton()->is_editor_hint()) {
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
switch(what) {
default: return;
case NOTIFICATION_READY:
this->ready();
return;
switch (what) {
default:
return;
case NOTIFICATION_READY:
this->ready();
return;
}
}
@ -25,10 +26,11 @@ void LocaleMarker::ready() {
}
void LocaleMarker::on_input_event(Node *camera, Ref<InputEvent> event, Vector3 position, Vector3 normal, int shape_ind) {
Ref<InputEventMouseButton> clicked{event};
if(clicked.is_valid() && clicked->get_button_index() == MouseButton::LEFT) {
Ref<InputEventMouseButton> clicked{ event };
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_uid(locale_scene);
get_tree()->change_scene_to_file(this->locale_scene);
}
}

View file

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

View file

@ -4,7 +4,7 @@
#include "core/config/engine.h"
#include "core/object/class_db.h"
GameState *game_state{nullptr};
GameState *game_state{ nullptr };
void initialize_authority_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
@ -20,7 +20,7 @@ void uninitialize_authority_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
if(game_state) {
if (game_state) {
memdelete(game_state);
}
}