//! Frontend plugin for UI/rendering integration //! //! This module provides the FrontendPlugin which handles all frontend communication //! including rendering, input, and UI updates. use bevy_ecs::prelude::*; use bevy_ecs::schedule::common_conditions::resource_exists; use crate::TerrainData; use crate::app::{App, Plugin, Update}; use crate::networking::GameView; use crate::ui::protocol::{BackendMessage, FrontendMessage}; use crate::ui::transport::{FrontendTransport, RenderBridge, emit_backend_messages_system, ingest_frontend_messages_system, send_initial_render_data, stream_territory_deltas}; /// Plugin to add frontend communication and UI systems to Bevy pub struct FrontendPlugin { transport: T, } impl FrontendPlugin { pub fn new(transport: T) -> Self { Self { transport } } } impl Plugin for FrontendPlugin { fn build(&self, app: &mut App) { let _guard = tracing::trace_span!("frontend_plugin_build").entered(); // Register message event types app.add_message::(); app.add_message::(); // Insert the bridge resource app.insert_resource(RenderBridge::new(self.transport.clone())); // Add render systems app.add_systems(Update, (send_initial_render_data::.run_if(resource_exists::).run_if(resource_exists::).run_if(resource_exists::>), stream_territory_deltas::.run_if(resource_exists::).run_if(resource_exists::>)).chain()); // Add communication systems app.add_systems(Update, (emit_backend_messages_system::, ingest_frontend_messages_system::, reset_bridge_on_quit_system::)); } } /// System to reset the render bridge when a game is quit /// This ensures fresh initialization data is sent when starting a new game fn reset_bridge_on_quit_system(game_view: Option>, mut bridge: ResMut>) { // If GameView doesn't exist but bridge is initialized, reset it if game_view.is_none() && bridge.initialized { bridge.reset(); tracing::debug!("RenderBridge reset - ready for next game initialization"); } }