feat: implemented ClueDB, ClueData and ClueID
This commit is contained in:
parent
3ff4935e02
commit
46d8749c2c
10 changed files with 165 additions and 27 deletions
|
|
@ -1,11 +1,13 @@
|
|||
#include "client_node.h"
|
||||
#include "ydi_client.h"
|
||||
#include <core/config/engine.h>
|
||||
|
||||
ClientNode *ClientNode::singleton_instance{ nullptr };
|
||||
String const ClientNode::sig_connection_changed{ "connection_changed" };
|
||||
|
||||
void ClientNode::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("connect_to_server", "server_url"), &self_type::connect_to_server);
|
||||
|
||||
ADD_SIGNAL(MethodInfo(sig_connection_changed, PropertyInfo(Variant::INT, "connected", PROPERTY_HINT_ENUM, "Disconnected,Connected,Authenticated")));
|
||||
}
|
||||
|
||||
|
|
@ -37,25 +39,12 @@ void ClientNode::_notification(int what) {
|
|||
if (Engine::get_singleton()->is_editor_hint()) {
|
||||
return;
|
||||
}
|
||||
switch (what) {
|
||||
case NOTIFICATION_ENTER_TREE:
|
||||
set_process(true);
|
||||
return;
|
||||
case NOTIFICATION_PROCESS:
|
||||
process();
|
||||
return;
|
||||
case NOTIFICATION_EXIT_TREE:
|
||||
exit_tree();
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
ClientNode *ClientNode::get_singleton() {
|
||||
return singleton_instance;
|
||||
}
|
||||
|
||||
void ClientNode::connect_to_server(String const &server) {
|
||||
ydi::client::connect(server);
|
||||
void ClientNode::connect_to_server(String const &url) {
|
||||
ydi::client::connect(url);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,7 +29,9 @@ void ClueData::reveal() {
|
|||
return;
|
||||
}
|
||||
this->revealed = true;
|
||||
ydi::client::send::reveal_clue(this->id);
|
||||
if (ydi::client::status() == NetworkData::CONNECTION_AUTHENTICATED) {
|
||||
ydi::client::send::reveal_clue(this->id);
|
||||
}
|
||||
}
|
||||
|
||||
bool ClueData::get_revealed() const {
|
||||
|
|
|
|||
62
modules/you_done_it/clue_db.cpp
Normal file
62
modules/you_done_it/clue_db.cpp
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
#include "clue_db.h"
|
||||
#include "core/io/resource_loader.h"
|
||||
#include "you_done_it/clue_data.h"
|
||||
#include "you_done_it/ydi_networking.h"
|
||||
|
||||
Ref<ClueDB> ClueDB::singleton_instance{ nullptr };
|
||||
|
||||
void ClueDB::_bind_methods() {
|
||||
BIND_HPROPERTY(Variant::ARRAY, clues, PROPERTY_HINT_ARRAY_TYPE, vformat("%s/%s:ClueData", Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE));
|
||||
}
|
||||
|
||||
void ClueDB::ensure_data_valid() {
|
||||
this->clues.resize_initialized(this->clues.capacity());
|
||||
for (int i{ 0 }; i < NetworkData::CLUE_MAX; ++i) {
|
||||
if (!this->clues[i].is_valid()) {
|
||||
this->clues[i].instantiate();
|
||||
}
|
||||
this->clues[i]->set_id((NetworkData::ClueID)i);
|
||||
}
|
||||
}
|
||||
|
||||
ClueDB::~ClueDB() {
|
||||
if (singleton_instance == this) {
|
||||
singleton_instance = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
Ref<ClueDB> &ClueDB::get_singleton() {
|
||||
if (!singleton_instance.is_valid()) {
|
||||
singleton_instance = Ref<ClueDB>(ResourceLoader::load("res://clue_db.tres"));
|
||||
}
|
||||
return singleton_instance;
|
||||
}
|
||||
|
||||
void ClueDB::set_clues(Array data) {
|
||||
for (Variant value : data) {
|
||||
Ref<ClueData> clue{ Object::cast_to<ClueData>(value) };
|
||||
if (clue.is_valid()) {
|
||||
NetworkData::ClueID id{ clue->get_id() };
|
||||
if (id >= 0 && id < NetworkData::CLUE_MAX) {
|
||||
this->clues[clue->get_id()] = clue;
|
||||
} else {
|
||||
print_error(vformat("attempt to insert clue of id %s into db (min 0 max %s)", id, NetworkData::CLUE_MAX));
|
||||
}
|
||||
}
|
||||
}
|
||||
ensure_data_valid();
|
||||
}
|
||||
|
||||
Array ClueDB::get_clues() {
|
||||
ensure_data_valid();
|
||||
Array data{};
|
||||
for (int i{ 0 }; i < NetworkData::CLUE_MAX; ++i) {
|
||||
data.push_back(this->clues[i]);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
Ref<ClueData> ClueDB::get_clue(NetworkData::ClueID id) {
|
||||
ensure_data_valid();
|
||||
return this->clues[id];
|
||||
}
|
||||
23
modules/you_done_it/clue_db.h
Normal file
23
modules/you_done_it/clue_db.h
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#pragma once
|
||||
|
||||
#include "core/io/resource.h"
|
||||
#include "core/templates/fixed_vector.h"
|
||||
#include "ydi_networking.h"
|
||||
#include "you_done_it/clue_data.h"
|
||||
|
||||
class ClueDB : public Resource {
|
||||
GDCLASS(ClueDB, Resource);
|
||||
static void _bind_methods();
|
||||
static Ref<ClueDB> singleton_instance;
|
||||
void ensure_data_valid();
|
||||
|
||||
public:
|
||||
virtual ~ClueDB();
|
||||
static Ref<ClueDB> &get_singleton();
|
||||
void set_clues(Array data);
|
||||
Array get_clues();
|
||||
Ref<ClueData> get_clue(NetworkData::ClueID id);
|
||||
|
||||
private:
|
||||
FixedVector<Ref<ClueData>, NetworkData::CLUE_MAX> clues{};
|
||||
};
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
#include "clue_finder.h"
|
||||
#include "scene/3d/xr/xr_nodes.h"
|
||||
#include "ydi_client.h"
|
||||
#include "you_done_it/client_node.h"
|
||||
#include "you_done_it/clue_db.h"
|
||||
#include <core/config/engine.h>
|
||||
#include <scene/main/node.h>
|
||||
|
||||
|
|
@ -16,6 +18,9 @@ void ClueFinder::enter_tree() {
|
|||
} else {
|
||||
queue_free();
|
||||
}
|
||||
if (XRController3D * controller{ cast_to<XRController3D>(get_parent()) }) {
|
||||
controller->connect("button_pressed", callable_mp(this, &self_type::on_button_pressed));
|
||||
}
|
||||
}
|
||||
|
||||
void ClueFinder::exit_tree() {
|
||||
|
|
@ -24,6 +29,12 @@ void ClueFinder::exit_tree() {
|
|||
}
|
||||
}
|
||||
|
||||
void ClueFinder::on_button_pressed(String button) {
|
||||
if (button == "trigger_click") {
|
||||
take_photo();
|
||||
}
|
||||
}
|
||||
|
||||
void ClueFinder::_notification(int what) {
|
||||
if (Engine::get_singleton()->is_editor_hint()) {
|
||||
return;
|
||||
|
|
@ -57,8 +68,14 @@ void ClueFinder::register_clue_marker(ClueMarker *marker) {
|
|||
this->clue_markers.insert(marker);
|
||||
}
|
||||
|
||||
void ClueFinder::remove_clue_marker(ClueMarker *marker) {
|
||||
this->clue_markers.erase(marker);
|
||||
}
|
||||
|
||||
void ClueFinder::take_photo() {
|
||||
print_line("TAKING PHOTO");
|
||||
if (ClueMarker * found{ find_current_clue() }) {
|
||||
ClientNode::get_singleton()->get_clue(found->get_clue_id())->reveal();
|
||||
found->reveal();
|
||||
print_line("FOUND MARKER: ", found->get_path());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ class ClueFinder : public Node3D {
|
|||
static ClueFinder *singleton_instance;
|
||||
void enter_tree();
|
||||
void exit_tree();
|
||||
void on_button_pressed(String button);
|
||||
|
||||
protected:
|
||||
void _notification(int what);
|
||||
|
|
@ -18,6 +19,7 @@ public:
|
|||
static ClueFinder *get_singleton();
|
||||
ClueMarker *find_current_clue();
|
||||
void register_clue_marker(ClueMarker *marker);
|
||||
void remove_clue_marker(ClueMarker *marker);
|
||||
void take_photo();
|
||||
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
#include "clue_marker.h"
|
||||
#include "core/config/engine.h"
|
||||
#include "macros.h"
|
||||
#include "you_done_it/clue_finder.h"
|
||||
#include "you_done_it/ydi_networking.h"
|
||||
|
|
@ -7,6 +8,29 @@ void ClueMarker::_bind_methods() {
|
|||
BIND_HPROPERTY(Variant::INT, clue_id, PROPERTY_HINT_ENUM, NetworkData::ClueID_hint());
|
||||
}
|
||||
|
||||
void ClueMarker::enter_tree() {
|
||||
ClueFinder::get_singleton()->register_clue_marker(this);
|
||||
}
|
||||
|
||||
void ClueMarker::exit_tree() {
|
||||
ClueFinder::get_singleton()->remove_clue_marker(this);
|
||||
}
|
||||
|
||||
void ClueMarker::_notification(int what) {
|
||||
if (Engine::get_singleton()->is_editor_hint()) {
|
||||
return;
|
||||
}
|
||||
switch (what) {
|
||||
case NOTIFICATION_ENTER_TREE:
|
||||
enter_tree();
|
||||
return;
|
||||
case NOTIFICATION_EXIT_TREE:
|
||||
exit_tree();
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
bool ClueMarker::is_visible() const {
|
||||
Transform3D const viewpoint{ ClueFinder::get_singleton()->get_global_transform() };
|
||||
Basis const basis{ viewpoint.get_basis() };
|
||||
|
|
@ -16,13 +40,11 @@ bool ClueMarker::is_visible() const {
|
|||
return false;
|
||||
}
|
||||
Vector3 const transformed_dir{ pos_transformed.normalized() };
|
||||
if (Math::abs(transformed_dir.signed_angle_to({ 0, 0, 1 }, { 0, 1, 0 })) > Math::PI / 4.0) {
|
||||
return false;
|
||||
}
|
||||
if (Math::abs(transformed_dir.signed_angle_to({ 0, 0, 1 }, { 1, 0, 0 })) > Math::PI / 4.0) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
return Math::abs(transformed_dir.x) < 0.5 || Math::abs(transformed_dir.y) < 0.5;
|
||||
}
|
||||
|
||||
void ClueMarker::reveal() {
|
||||
ClueDB::get_singleton()->get_clue(this->id)->reveal();
|
||||
}
|
||||
|
||||
void ClueMarker::set_clue_id(NetworkData::ClueID id) {
|
||||
|
|
|
|||
|
|
@ -1,14 +1,21 @@
|
|||
#pragma once
|
||||
|
||||
#include "clue_db.h"
|
||||
#include "ydi_networking.h"
|
||||
#include <scene/3d/node_3d.h>
|
||||
#include <scene/3d/marker_3d.h>
|
||||
|
||||
class ClueMarker : public Node3D {
|
||||
GDCLASS(ClueMarker, Node3D);
|
||||
class ClueMarker : public Marker3D {
|
||||
GDCLASS(ClueMarker, Marker3D);
|
||||
static void _bind_methods();
|
||||
void enter_tree();
|
||||
void exit_tree();
|
||||
|
||||
protected:
|
||||
void _notification(int what);
|
||||
|
||||
public:
|
||||
bool is_visible() const;
|
||||
void reveal();
|
||||
void set_clue_id(NetworkData::ClueID id);
|
||||
NetworkData::ClueID get_clue_id() const;
|
||||
|
||||
|
|
|
|||
|
|
@ -20,10 +20,14 @@ void initialize_you_done_it_module(ModuleInitializationLevel p_level) {
|
|||
ClassDB::register_class<ClueMarker>();
|
||||
ClassDB::register_class<ClueFinder>();
|
||||
ClassDB::register_class<ClueData>();
|
||||
ClassDB::register_class<ClueDB>();
|
||||
}
|
||||
|
||||
void uninitialize_you_done_it_module(ModuleInitializationLevel p_level) {
|
||||
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
|
||||
return;
|
||||
}
|
||||
if (ClueDB::get_singleton().is_valid()) {
|
||||
ClueDB::get_singleton().unref();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
10
vr-project/clue_db.tres
Normal file
10
vr-project/clue_db.tres
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
[gd_resource type="ClueDB" load_steps=3 format=3 uid="uid://dlf8dxiter8b8"]
|
||||
|
||||
[sub_resource type="ClueData" id="ClueData_kxjsf"]
|
||||
id = 0
|
||||
|
||||
[sub_resource type="ClueData" id="ClueData_du6rq"]
|
||||
id = 1
|
||||
|
||||
[resource]
|
||||
clues = [SubResource("ClueData_kxjsf"), SubResource("ClueData_du6rq")]
|
||||
Loading…
Add table
Add a link
Reference in a new issue