Removed a lot of things that overcomplicate things. I will first try to focus on basic networking.

This commit is contained in:
Elias Stepanik 2025-04-03 11:07:42 +02:00
parent 720ffc743a
commit 0d5a94da23
10 changed files with 164 additions and 41 deletions

View File

@ -0,0 +1,6 @@
https://spacetimedb.com/docs/getting-started
https://spacetimedb.com/docs/modules/rust/quickstart
https://spacetimedb.com/docs/sdks/rust/quickstart
https://winter.dev/articles/physics-engine
https://spacetimedb.com/docs/unity/part-4

View File

@ -3,8 +3,12 @@ use crate::helper::egui_dock::{
reset_camera_viewport, set_camera_viewport, set_gizmo_mode, show_ui_system, UiState,
};
use bevy::prelude::*;
use crate::helper::*;
use bevy_egui::EguiSet;
use bevy_render::extract_resource::ExtractResourcePlugin;
use spacetimedb_sdk::{credentials, DbContext, Error, Event, Identity, Status, Table, TableWithPrimaryKey};
use crate::helper::database::setup_database;
use crate::module_bindings::DbConnection;
pub struct AppPlugin;
@ -23,8 +27,9 @@ impl Plugin for AppPlugin {
app.add_plugins(crate::plugins::camera::camera_plugin::CameraPlugin);
app.add_plugins(crate::plugins::ui::ui_plugin::UiPlugin);
app.add_plugins(crate::plugins::environment::environment_plugin::EnvironmentPlugin);
app.add_systems(Startup, setup_database);
app.add_systems(Update, (debug_gizmos, toggle_ui_system));
app.add_systems(

View File

@ -0,0 +1,119 @@
use bevy::prelude::{Commands, Resource};
use bevy::utils::info;
use spacetimedb_sdk::{credentials, DbContext, Error, Event, Identity, Status, Table, TableWithPrimaryKey};
use crate::module_bindings::*;
/// The URI of the SpacetimeDB instance hosting our chat module.
const HOST: &str = "http://192.168.178.10:3000";
/// The database name we chose when we published our module.
const DB_NAME: &str = "horror-game";
#[derive(Resource)]
pub struct DbConnectionResource(pub(crate) DbConnection);
pub fn setup_database(mut commands: Commands) {
// Call your connection function and insert the connection as a resource.
let ctx = connect_to_db();
register_callbacks(&ctx);
// Subscribe to SQL queries in order to construct a local partial replica of the database.
subscribe_to_tables(&ctx);
// Spawn a thread, where the connection will process messages and invoke callbacks.
ctx.run_threaded();
commands.insert_resource(DbConnectionResource(ctx));
}
/// Register subscriptions for all rows of both tables.
fn subscribe_to_tables(ctx: &DbConnection) {
ctx.subscription_builder()
.on_applied(on_sub_applied)
.on_error(on_sub_error)
.subscribe(["SELECT * FROM physics_world"]);
}
/// Our `on_subscription_applied` callback:
/// sort all past messages and print them in timestamp order.
fn on_sub_applied(ctx: &SubscriptionEventContext) {
println!("Fully connected and all subscriptions applied.");
println!("Use /name to set your name, or type a message!");
}
/// Or `on_error` callback:
/// print the error, then exit the process.
fn on_sub_error(_ctx: &ErrorContext, err: Error) {
eprintln!("Subscription failed: {}", err);
std::process::exit(1);
}
fn connect_to_db() -> DbConnection {
DbConnection::builder()
// Register our `on_connect` callback, which will save our auth token.
.on_connect(on_connected)
// Register our `on_connect_error` callback, which will print a message, then exit the process.
/*.on_connect_error(on_connect_error)*/
// Our `on_disconnect` callback, which will print a message, then exit the process.
.on_disconnect( on_disconnected)
// If the user has previously connected, we'll have saved a token in the `on_connect` callback.
// In that case, we'll load it and pass it to `with_token`,
// so we can re-authenticate as the same `Identity`.
.with_token(creds_store().load().expect("Error loading credentials"))
// Set the database name we chose when we called `spacetime publish`.
.with_module_name(DB_NAME)
// Set the URI of the SpacetimeDB host that's running our database.
.with_uri(HOST)
// Finalize configuration and connect!
.build()
.expect("Failed to connect")
}
/// Register all the callbacks our app will use to respond to database events.
fn register_callbacks(ctx: &DbConnection) {
/*// When a new message is received, print it.
ctx.db.message().on_insert(on_message_inserted);*/
/*// When we fail to set our name, print a warning.
ctx.reducers.on_set_name(on_name_set);*/
// When we fail to send a message, print a warning.
/*ctx.reducers.on_send_message(on_message_sent);*/
}
fn creds_store() -> credentials::File {
credentials::File::new("quickstart-chat")
}
/// Our `on_connect` callback: save our credentials to a file.
fn on_connected(_ctx: &DbConnection, _identity: Identity, token: &str) {
if let Err(e) = creds_store().save(token) {
eprintln!("Failed to save credentials: {:?}", e);
}
}
/// Our `on_connect_error` callback: print the error, then exit the process.
fn on_connect_error(_ctx: &ErrorContext, err: Error) {
eprintln!("Connection error: {:?}", err);
std::process::exit(1);
}
/// Our `on_disconnect` callback: print a note, then exit the process.
fn on_disconnected(_ctx: &ErrorContext, err: Option<Error>) {
if let Some(err) = err {
eprintln!("Disconnected: {}", err);
std::process::exit(1);
} else {
println!("Disconnected.");
std::process::exit(0);
}
}

View File

@ -1,2 +1,3 @@
pub mod debug_gizmos;
pub mod egui_dock;
pub mod database;

View File

@ -1,6 +1,8 @@
mod app;
mod helper;
mod plugins;
mod module_bindings;
use crate::app::AppPlugin;
use bevy::gizmos::{AppGizmoBuilder, GizmoPlugin};
use bevy::log::info;

View File

@ -2,8 +2,12 @@ use bevy::app::{App, Plugin, Startup};
use bevy::color::palettes::basic::{GREEN, YELLOW};
use bevy::color::palettes::css::RED;
use bevy::prelude::*;
use crate::plugins::environment::systems::environment_system::*;
pub struct EnvironmentPlugin;
impl Plugin for EnvironmentPlugin {
fn build(&self, app: &mut App) {}
fn build(&self, app: &mut App) {
app.add_systems(Startup, init);
app.add_systems(FixedUpdate, fixed_update);
app.add_systems(Update, fixed_update);
}
}

View File

@ -1,10 +1,18 @@
use bevy::color::palettes::basic::*;
use bevy::color::palettes::css::{BEIGE, MIDNIGHT_BLUE, ORANGE, ORANGE_RED, SEA_GREEN};
use bevy::math::*;
use bevy::pbr::{CascadeShadowConfigBuilder, NotShadowCaster};
use bevy::prelude::*;
use crate::helper::database::DbConnectionResource;
pub fn setup(mut commands: Commands) {
// Insert the octree into the ECS
pub fn init(mut commands: Commands, db_connection: Res<DbConnectionResource>) {
}
pub fn fixed_update(time: Res<Time>, db_connection: Res<DbConnectionResource>) {
}
pub fn update(time: Res<Time>, db_connection: Res<DbConnectionResource>) {
}

View File

@ -1,3 +1,3 @@
pub mod camera;
pub mod environment;
pub mod ui;
pub mod ui;

View File

@ -2,4 +2,6 @@
@echo off
REM Script to publish the horror-game project using spacetime
spacetime publish -c --project-path server horror-game
spacetime publish -c --project-path server horror-game -y
rm client\src\module_bindings\*
spacetime generate --lang rust --out-dir client/src/module_bindings --project-path server

View File

@ -1,34 +1,10 @@
use spacetimedb::{ReducerContext, Table};
#[spacetimedb::table(name = person)]
pub struct Person {
name: String
}
use std::time::Duration;
use spacetimedb::{ReducerContext, ScheduleAt, Table};
#[spacetimedb::reducer(init)]
pub fn init(_ctx: &ReducerContext) {
// Called when the module is initially published
}
pub fn init(ctx: &ReducerContext) {
log::info!("Initializing...");
#[spacetimedb::reducer(client_connected)]
pub fn identity_connected(_ctx: &ReducerContext) {
// Called everytime a new client connects
}
#[spacetimedb::reducer(client_disconnected)]
pub fn identity_disconnected(_ctx: &ReducerContext) {
// Called everytime a client disconnects
}
#[spacetimedb::reducer]
pub fn add(ctx: &ReducerContext, name: String) {
ctx.db.person().insert(Person { name });
}
#[spacetimedb::reducer]
pub fn say_hello(ctx: &ReducerContext) {
for person in ctx.db.person().iter() {
log::info!("Hello, {}!", person.name);
}
log::info!("Hello, World!");
}
}