diff --git a/client/src/plugins/camera/systems/camera_system.rs b/client/src/plugins/camera/systems/camera_system.rs index 24aa07f..01c0300 100644 --- a/client/src/plugins/camera/systems/camera_system.rs +++ b/client/src/plugins/camera/systems/camera_system.rs @@ -6,7 +6,7 @@ use bevy_render::camera::{Exposure, PhysicalCameraParameters, Projection}; use bevy_window::CursorGrabMode; use rand::Rng; use random_word::Lang; -use crate::module_bindings::{set_name, spawn_entity, DbVector3}; +use crate::module_bindings::{set_name, set_position, spawn_entity, DbVector3}; use crate::plugins::network::systems::database::DbConnectionResource; #[derive(Component)] @@ -28,7 +28,10 @@ impl Default for CameraController { } } -pub fn setup(mut commands: Commands) { +pub fn setup(mut commands: Commands, + db_resource: Res,) { + + commands.spawn(( Transform::from_xyz(0.0, 0.0, 10.0), // initial f32 GlobalTransform::default(), @@ -45,6 +48,7 @@ pub fn setup(mut commands: Commands) { sensitivity_iso: 100.0, sensor_height: 0.01866, }), + )); } @@ -159,6 +163,13 @@ pub fn camera_controller_system( let distance = controller.speed as f64 * delta_seconds; transform.translation += direction * distance as f32; + ctx.0.reducers.set_position(DbVector3{ + x: transform.translation.x, + y: transform.translation.y, + z: transform.translation.z, + }).expect("TODO: panic message"); + + // ========================= // 4) Lock/Unlock Mouse (L) // ========================= diff --git a/client/src/plugins/environment/environment_plugin.rs b/client/src/plugins/environment/environment_plugin.rs index c3a0b34..0731a95 100644 --- a/client/src/plugins/environment/environment_plugin.rs +++ b/client/src/plugins/environment/environment_plugin.rs @@ -1,12 +1,6 @@ -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::*; +use bevy::app::{App, Plugin}; pub struct EnvironmentPlugin; impl Plugin for EnvironmentPlugin { fn build(&self, app: &mut App) { - app.add_systems(Startup, init); - app.add_systems(Update, sync_entities_system); } } diff --git a/client/src/plugins/environment/systems/environment_system.rs b/client/src/plugins/environment/systems/environment_system.rs index a99b6b8..f3101db 100644 --- a/client/src/plugins/environment/systems/environment_system.rs +++ b/client/src/plugins/environment/systems/environment_system.rs @@ -1,93 +1,9 @@ use bevy::prelude::*; -use spacetimedb_sdk::Table; -use crate::module_bindings::{entity_table, DbVector3, EntityTableAccess}; -use crate::plugins::network::systems::database::DbConnectionResource; - -#[derive(Component)] -pub struct EntityDto { - - pub entity_id: u32, - pub position: DbVector3, -} - -pub fn init(mut commands: Commands, - ctx: Res, - mut meshes: ResMut>, - mut materials: ResMut>,) { - - let debug_material = materials.add(StandardMaterial { ..default() }); - - for entity in ctx.0.db.entity().iter() { - commands.spawn(( - Mesh3d(meshes.add(Cuboid::default()),), - MeshMaterial3d(debug_material.clone ()), - Transform::from_xyz( - entity.position.x, - entity.position.y, - entity.position.z, - ), - EntityDto{ - entity_id: entity.entity_id, - position: entity.position, - } - )); - - } - - -} - - - -pub fn sync_entities_system( - mut commands: Commands, - db_resource: Res, - mut query: Query<(Entity, &mut Transform, &mut EntityDto)>, - mut meshes: ResMut>, - mut materials: ResMut>, -) { - let ctx = &db_resource.0.db; - - // For each entity record in DB, see if it exists in ECS: - for db_entity in ctx.entity().iter() { - // Try to find a matching entity in ECS by comparing `entity_id` - if let Some((entity, mut transform, mut dto)) = query - .iter_mut() - .find(|(_, _, dto)| dto.entity_id == db_entity.entity_id) - { - // It already exists. Perhaps update ECS data to match DB: - dto.position = db_entity.position; - transform.translation = Vec3::new( - dto.position.x, - dto.position.y, - dto.position.z, - ); - // ...do any other sync logic - } else { - - - let debug_material = materials.add(StandardMaterial { ..default() }); - // Not found in ECS, so spawn a new entity - commands.spawn(( - EntityDto { - entity_id: db_entity.entity_id, - position: db_entity.position.clone(), - }, - // Create an initial transform using DB data - Transform::from_xyz( - db_entity.position.x, - db_entity.position.y, - db_entity.position.z, - ), - GlobalTransform::default(), - Mesh3d(meshes.add(Cuboid::default()),), - MeshMaterial3d(debug_material.clone ()), - )); - } - } +pub fn setup(mut commands: Commands) { + } pub fn update(time: Res