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,18 +1,23 @@
#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{ "" };

View file

@ -13,7 +13,8 @@ void LocaleMarker::_notification(int what) {
return;
}
switch (what) {
default: return;
default:
return;
case NOTIFICATION_READY:
this->ready();
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) {
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,11 +11,13 @@ 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{ "%" };