Switch to Transform Positioning

This commit is contained in:
Elias Stepanik 2025-04-17 19:04:27 +07:00
parent 9eabdf1be9
commit 8673ff7c88
14 changed files with 84 additions and 63 deletions

View File

@ -1,17 +0,0 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="PublishServer" type="ShConfigurationType">
<option name="SCRIPT_TEXT" value="" />
<option name="INDEPENDENT_SCRIPT_PATH" value="false" />
<option name="SCRIPT_PATH" value="$PROJECT_DIR$/publish_server.bat" />
<option name="SCRIPT_OPTIONS" value="" />
<option name="INDEPENDENT_SCRIPT_WORKING_DIRECTORY" value="true" />
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="INDEPENDENT_INTERPRETER_PATH" value="false" />
<option name="INTERPRETER_PATH" value="$PROJECT_DIR$/../../../../Windows/System32/WindowsPowerShell/v1.0/powershell.exe" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="EXECUTE_IN_TERMINAL" value="true" />
<option name="EXECUTE_SCRIPT_FILE" value="true" />
<envs />
<method v="2" />
</configuration>
</component>

View File

@ -112,10 +112,10 @@ pub fn camera_controller_system(
let word = random_word::get(Lang::En);
if keyboard_input.just_pressed(KeyCode::Numpad1) {
if keyboard_input.just_pressed(KeyCode::KeyQ) {
ctx.0.reducers.set_name(word.to_string()).unwrap();
}
if keyboard_input.just_pressed(KeyCode::Numpad2) {
if keyboard_input.just_pressed(KeyCode::KeyE) {
let rand_position = random_vec3(-10.0,10.0);
ctx.0.reducers.spawn_entity(DbVector3{
x: rand_position.x,

View File

@ -10,7 +10,7 @@ pub struct NetworkPlugin;
impl Plugin for NetworkPlugin {
fn build(&self, app: &mut App) {
app.add_systems(PreStartup, setup_database);
/*app.add_systems(Startup, init);
app.add_systems(Update, sync_entities_system);*/
app.add_systems(Startup, init);
app.add_systems(Update, sync_entities_system);
}
}

View File

@ -8,10 +8,10 @@ use crate::plugins::network::systems::connection::*;
use crate::plugins::network::systems::subscriptions::*;
/// The URI of the SpacetimeDB instance hosting our chat module.
const HOST: &str = "http://192.168.178.10:3000";
const HOST: &str = "http://100.85.241.101:3000";
/// The database name we chose when we published our module.
const DB_NAME: &str = "horror-game-test";
const DB_NAME: &str = "network-game";
#[derive(Resource)]
pub struct DbConnectionResource(pub(crate) DbConnection);

View File

@ -1,20 +1,25 @@
use std::collections::HashSet;
use bevy::math::Vec3;
use bevy::pbr::{MeshMaterial3d, StandardMaterial};
use bevy::prelude::{default, Commands, Component, Cuboid, DespawnRecursiveExt, Entity, GlobalTransform, Mesh, Query, Res, ResMut, Sphere, Transform};
use bevy::prelude::{default, Bundle, Commands, Component, Cuboid, DespawnRecursiveExt, Entity, GlobalTransform, Mesh, Query, Res, ResMut, Sphere, Transform};
use bevy_asset::Assets;
use bevy_reflect::Reflect;
use bevy_render::mesh::Mesh3d;
use spacetimedb_sdk::Table;
use crate::module_bindings::{DbVector3, EntityTableAccess, EntityType};
use crate::module_bindings::{DbTransform, DbVector3, EntityTableAccess, EntityType};
use crate::plugins::network::systems::database::DbConnectionResource;
#[derive(Component)]
pub struct EntityDto {
pub entity_id: u32,
pub position: DbVector3,
pub transform: DbTransform,
}
pub fn init(mut commands: Commands,
ctx: Res<DbConnectionResource>,
mut meshes: ResMut<Assets<Mesh>>,
@ -29,13 +34,13 @@ pub fn init(mut commands: Commands,
Mesh3d(meshes.add(Cuboid::default()),),
MeshMaterial3d(debug_material.clone ()),
Transform::from_xyz(
entity.position.x,
entity.position.y,
entity.position.z,
entity.transform.position.x,
entity.transform.position.y,
entity.transform.position.z,
),
EntityDto{
entity_id: entity.entity_id,
position: entity.position,
transform: entity.transform
}
));
@ -70,11 +75,11 @@ pub fn sync_entities_system(
query.iter_mut().find(|(_, _, dto)| dto.entity_id == db_entity.entity_id)
{
// Update fields
dto.position = db_entity.position.clone();
dto.transform.position = db_entity.transform.position.clone();
transform.translation = Vec3::new(
db_entity.position.x,
db_entity.position.y,
db_entity.position.z,
db_entity.transform.position.x,
db_entity.transform.position.y,
db_entity.transform.position.z,
);
} else {
@ -92,18 +97,19 @@ pub fn sync_entities_system(
};
commands.spawn((
EntityDto {
entity_id: db_entity.entity_id,
position: db_entity.position.clone(),
},
Transform::from_xyz(
db_entity.position.x,
db_entity.position.y,
db_entity.position.z,
db_entity.transform.position.x,
db_entity.transform.position.y,
db_entity.transform.position.z,
),
GlobalTransform::default(),
entity_type,
MeshMaterial3d(debug_material),
EntityDto {
entity_id: db_entity.entity_id,
transform: db_entity.transform.clone(),
},
));
}

5
publish_server.sh Normal file
View File

@ -0,0 +1,5 @@
spacetime publish -c --project-path server network-game -y
rm client\src\module_bindings\*
spacetime generate --lang rust --out-dir client/src/module_bindings --project-path server

View File

@ -3,7 +3,7 @@ mod types;
use spacetimedb::{reducer, ReducerContext, Table};
use crate::types::entity::{entity, entity__TableHandle, Entity, EntityType};
use crate::types::player::{player, player__TableHandle, Player};
use crate::types::vec3::DbVector3;
use crate::types::types::{DBVector4, DbTransform, DbVector3};
#[spacetimedb::table(name = config, public)]
pub struct Config {
@ -30,11 +30,12 @@ pub fn client_connected(ctx: &ReducerContext) {
// which is online, but hasn't set a name.
let entity = ctx.db.entity().try_insert(Entity{
entity_id: 0,
position: DbVector3 {
x: 0.0,
y: 0.0,
z: 10.0,
transform: DbTransform{
position: DbVector3{x: 0.0, y: 0.0, z: 10.0},
rotation: DBVector4{x: 0.0, y: 0.0, z: 0.0, w: 1.0},
scale: DbVector3{x: 1.0, y: 1.0, z: 1.0},
},
entity_type: EntityType::Sphere,
}).expect("TODO: panic message");

View File

@ -1,6 +1,6 @@
use spacetimedb::{Identity, ReducerContext, SpacetimeType, Table};
use crate::types::vec3::{DbVector3};
use crate::types::types::{DbVector3, DbTransform, DBVector4};
#[spacetimedb::table(name = entity, public)]
#[derive(Debug, Clone, )]
@ -8,7 +8,7 @@ pub struct Entity {
#[auto_inc]
#[primary_key]
pub entity_id: u32,
pub position: DbVector3,
pub transform: DbTransform,
pub entity_type: EntityType,
}
@ -28,7 +28,12 @@ pub fn spawn_entity(ctx: &ReducerContext, position: DbVector3) -> Result<(), Str
ctx.db.entity().try_insert(Entity {
entity_id: 0,
position,
transform: DbTransform{
position: position,
rotation: DBVector4{x: 0.0, y: 0.0, z: 0.0, w: 1.0},
scale: DbVector3 {x: 1.0, y: 1.0, z: 1.0 },
},
entity_type: EntityType::Cube,
}).expect("TODO: panic message");

View File

@ -1,3 +1,3 @@
pub mod vec3;
pub mod types;
pub mod player;
pub mod entity;

View File

@ -1,6 +1,7 @@
use crate::types::entity::*;
use std::io::empty;
use spacetimedb::{reducer, Identity, ReducerContext, Table};
use crate::types::entity::{entity, Entity};
use crate::types::vec3::DbVector3;
use crate::types::types::{DbTransform, DbVector3};
#[spacetimedb::table(name = player, public)]
#[derive(Debug, Clone)]
@ -34,7 +35,10 @@ pub fn set_position(ctx: &ReducerContext, position: DbVector3) -> Result<(), Str
if let Some(entity) = ctx.db.entity().iter().find(|e| e.entity_id == ctx.db.player().identity().find(ctx.sender).unwrap().entity_id) {
ctx.db.entity().entity_id()
.update(Entity{
position,
transform: DbTransform{
position: position,
..entity.transform
},
..entity
});
}

25
server/src/types/types.rs Normal file
View File

@ -0,0 +1,25 @@
use spacetimedb::SpacetimeType;
#[derive(SpacetimeType, Clone, Debug)]
pub struct DbVector3 {
pub x: f32,
pub y: f32,
pub z: f32,
}
#[derive(SpacetimeType, Clone, Debug)]
pub struct DBVector4 {
pub x: f32,
pub y: f32,
pub z: f32,
pub w: f32,
}
#[derive(SpacetimeType, Clone, Debug)]
pub struct DbTransform {
pub position: DbVector3,
pub rotation: DBVector4,
pub scale: DbVector3,
}

View File

@ -1,8 +0,0 @@
use spacetimedb::SpacetimeType;
#[derive(SpacetimeType, Clone, Debug)]
pub struct DbVector3 {
pub x: f32,
pub y: f32,
pub z: f32,
}

View File

@ -1,2 +0,0 @@
for /l %g in () do @( spacetime logs horror-game-test )

2
watch_logs.sh Normal file
View File

@ -0,0 +1,2 @@
watch spacetime logs network-game