~starkingdoms/starkingdoms

827d7ac625a9afa1a6afee0c2e10e0afc1fcb162 — core 24 days ago 4791b2e
chore: client refactoring
A crates/unified/src/client/input/mod.rs => crates/unified/src/client/input/mod.rs +34 -0
@@ 0,0 1,34 @@
use bevy::app::{App, Update};
use bevy::window::PrimaryWindow;
use crate::ecs::MainCamera;
use crate::prelude::*;


pub fn input_plugin(app: &mut App) {
    app
        .insert_resource(CursorWorldCoordinates(None))
        .add_systems(Update, update_cursor_position);
}


#[derive(Resource, Default)]
pub struct CursorWorldCoordinates(pub Option<Vec2>);

fn update_cursor_position(
    q_windows: Query<&Window, With<PrimaryWindow>>,
    q_camera: Query<(&Camera, &GlobalTransform), With<MainCamera>>,
    mut coords: ResMut<CursorWorldCoordinates>,
) {
    let (camera, camera_transform) = q_camera.single().unwrap();
    let window = q_windows.single().unwrap();

    if let Some(world_position) = window
        .cursor_position()
        .and_then(|cursor| camera.viewport_to_world(camera_transform, cursor).ok())
        .map(|ray| ray.origin.truncate())
    {
        coords.0 = Some(world_position);
    } else {
        coords.0 = None;
    }
}

M crates/unified/src/client/mod.rs => crates/unified/src/client/mod.rs +9 -53
@@ 1,18 1,13 @@
use crate::client::key_input::key_input_plugin;
use crate::client::net::set_config;
use crate::client::parts::parts_plugin;
use crate::client::planet::indicators::indicators_plugin;
use crate::client::starfield::starfield_plugin;
use crate::client::ui::ui_plugin;
use crate::client::zoom::zoom_plugin;
use crate::ecs::{CursorWorldCoordinates, MainCamera, Part, Player};
use crate::ecs::{Part, Player};
use aeronet_websocket::client::WebSocketClient;
use bevy::anti_alias::fxaa::Fxaa;
use bevy::core_pipeline::tonemapping::DebandDither;
use bevy::dev_tools::picking_debug::DebugPickingMode;
use bevy::post_process::bloom::Bloom;
use crate::prelude::*;
use bevy::window::PrimaryWindow;
use planet::incoming_planets::incoming_planets_plugin;
use crate::client::ship::attachment::client_attachment_plugin;
use crate::ecs::Me;


@@ 27,6 22,8 @@ pub mod starfield;
pub mod ui;
pub mod zoom;
pub mod ship;
mod rendering;
mod input;

pub struct ClientPlugin {
    pub server: Option<String>,


@@ 35,7 32,7 @@ impl Plugin for ClientPlugin {
    fn build(&self, app: &mut App) {
        let server = self.server.clone();
        app
            .insert_resource(CursorWorldCoordinates(None))

            .add_systems(Startup, move |mut commands: Commands| {
                let Some(server) = server.as_ref() else { return };
                let config = net::websocket_config();


@@ 44,11 41,10 @@ impl Plugin for ClientPlugin {
                    .spawn(Name::new("default-session"))
                    .queue(WebSocketClient::connect(config, server.clone()));
            })
            .add_systems(Startup, setup_graphics)
            .add_systems(Update, update_cursor_position)
            .add_systems(Update, follow_camera)
            .add_plugins(rendering::render_plugin)
            .add_plugins(input::input_plugin)
            .add_systems(Update, find_me)
            .add_systems(Update, set_config)
            .add_systems(Update, net::set_config)
            .add_plugins((incoming_planets_plugin, indicators_plugin))
            .add_plugins(parts_plugin)
            .add_plugins(key_input_plugin)


@@ 57,6 53,8 @@ impl Plugin for ClientPlugin {
            .add_plugins(zoom_plugin)
            .add_plugins(client_attachment_plugin)
            .insert_resource(DebugPickingMode::Disabled);

        // These are only needed if we're actually doing network things
        if self.server.is_some() {
            app.add_observer(net::on_connecting)
                .add_observer(net::on_connected)


@@ 92,45 90,3 @@ fn find_me(
    }
}

fn setup_graphics(mut commands: Commands) {
    commands
        .spawn(Camera2d)
        .insert(Camera {
            clear_color: ClearColorConfig::Custom(Color::BLACK),
            ..default()
        })
        .insert(Bloom::default())
        .insert(DebandDither::Enabled)
        .insert(Fxaa::default())
        .insert(MainCamera);
}

fn follow_camera(
    mut camera: Query<&mut Transform, (With<MainCamera>, Without<Me>)>,
    player: Query<&Transform, With<Me>>,
) {
    let mut camera = camera.single_mut().unwrap();
    let Ok(player) = player.single() else {
        return;
    };
    camera.translation = player.translation;
}

fn update_cursor_position(
    q_windows: Query<&Window, With<PrimaryWindow>>,
    q_camera: Query<(&Camera, &GlobalTransform), With<MainCamera>>,
    mut coords: ResMut<CursorWorldCoordinates>,
) {
    let (camera, camera_transform) = q_camera.single().unwrap();
    let window = q_windows.single().unwrap();

    if let Some(world_position) = window
        .cursor_position()
        .and_then(|cursor| camera.viewport_to_world(camera_transform, cursor).ok())
        .map(|ray| ray.origin.truncate())
    {
        coords.0 = Some(world_position);
    } else {
        coords.0 = None;
    }
}

M crates/unified/src/client/parts.rs => crates/unified/src/client/parts.rs +2 -1
@@ 3,7 3,8 @@ use std::f32::consts::PI;
use crate::attachment::{Joint, JointOf, Joints, PartInShip, Peer, SnapOf, SnapOfJoint};
use crate::ecs::Me;
use crate::client::colors::GREEN;
use crate::ecs::{CursorWorldCoordinates, DragRequestEvent, Part};
use crate::ecs::{DragRequestEvent, Part};
use crate::client::input::CursorWorldCoordinates;
use bevy::color::palettes::css::{ORANGE, PURPLE, RED, YELLOW};
use crate::client::ship::attachment::AttachmentDebugRes;
use crate::prelude::*;

A crates/unified/src/client/rendering/mod.rs => crates/unified/src/client/rendering/mod.rs +37 -0
@@ 0,0 1,37 @@
use bevy::anti_alias::fxaa::Fxaa;
use bevy::app::{App, Startup};
use bevy::core_pipeline::tonemapping::DebandDither;
use bevy::post_process::bloom::Bloom;
use crate::ecs::{MainCamera, Me};
use crate::prelude::*;

pub fn render_plugin(app: &mut App) {
    app
        .add_systems(Startup, setup_graphics)
        .add_systems(Update, follow_camera);
}


fn setup_graphics(mut commands: Commands) {
    commands
        .spawn(Camera2d)
        .insert(Camera {
            clear_color: ClearColorConfig::Custom(Color::BLACK),
            ..default()
        })
        .insert(Bloom::default())
        .insert(DebandDither::Enabled)
        .insert(Fxaa::default())
        .insert(MainCamera);
}

fn follow_camera(
    mut camera: Query<&mut Transform, (With<MainCamera>, Without<Me>)>,
    player: Query<&Transform, With<Me>>,
) {
    let mut camera = camera.single_mut().unwrap();
    let Ok(player) = player.single() else {
        return;
    };
    camera.translation = player.translation;
}
\ No newline at end of file

M crates/unified/src/ecs.rs => crates/unified/src/ecs.rs +0 -3
@@ 20,9 20,6 @@ pub struct FuelText;
#[derive(Component)]
pub struct PowerText;

#[derive(Resource, Default)]
pub struct CursorWorldCoordinates(pub Option<Vec2>);

#[derive(Debug, Deserialize, Message, Serialize)]
pub enum ThrustEvent {
    Up(bool),