From 77fa421bb27687d031a9e66f29264678aea98e55 Mon Sep 17 00:00:00 2001 From: Amatsugu Date: Tue, 27 Aug 2024 23:04:03 -0400 Subject: [PATCH] units wip --- Cargo.lock | 16 ++++++ Cargo.toml | 2 +- game/buildings/src/building_plugin.rs | 5 +- game/main/Cargo.toml | 1 + game/main/src/map_rendering/chunk_rebuild.rs | 2 +- game/main/src/map_rendering/map_init.rs | 4 +- game/main/src/utlis/debug_plugin.rs | 18 ++++++- game/shared/src/lib.rs | 1 + game/shared/src/sets.rs | 4 ++ game/shared/src/tags.rs | 6 +++ game/units/Cargo.toml | 21 ++++++++ game/units/src/assets/mod.rs | 3 ++ game/units/src/assets/unit_asset.rs | 52 ++++++++++++++++++++ game/units/src/assets/unit_database.rs | 10 ++++ game/units/src/components.rs | 12 +++++ game/units/src/lib.rs | 5 ++ game/units/src/units_debug_plugin.rs | 18 +++++++ game/units/src/units_plugin.rs | 37 ++++++++++++++ 18 files changed, 210 insertions(+), 7 deletions(-) create mode 100644 game/shared/src/sets.rs create mode 100644 game/units/Cargo.toml create mode 100644 game/units/src/assets/mod.rs create mode 100644 game/units/src/assets/unit_asset.rs create mode 100644 game/units/src/assets/unit_database.rs create mode 100644 game/units/src/components.rs create mode 100644 game/units/src/lib.rs create mode 100644 game/units/src/units_debug_plugin.rs create mode 100644 game/units/src/units_plugin.rs diff --git a/Cargo.lock b/Cargo.lock index 0c8b2d8..e4f008c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3668,6 +3668,7 @@ dependencies = [ "rayon", "ron", "shared", + "units", "world_generation", ] @@ -4836,6 +4837,21 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" +[[package]] +name = "units" +version = "0.1.0" +dependencies = [ + "asset_loader", + "bevy", + "bevy_asset_loader", + "bevy_rapier3d", + "ron", + "serde", + "serde_json", + "shared", + "world_generation", +] + [[package]] name = "url" version = "2.5.2" diff --git a/Cargo.toml b/Cargo.toml index a5b56d5..5a441d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ members = [ "game/buildings", "game/shared", "engine/world_generation", - "engine/asset_loader", "game/buildings", "game/shared"] + "engine/asset_loader", "game/buildings", "game/shared", "game/units"] # Enable a small amount of optimization in debug mode [profile.dev] diff --git a/game/buildings/src/building_plugin.rs b/game/buildings/src/building_plugin.rs index cd18a07..2115002 100644 --- a/game/buildings/src/building_plugin.rs +++ b/game/buildings/src/building_plugin.rs @@ -38,7 +38,10 @@ impl Plugin for BuildingPugin { ); app.add_systems(Update, init.run_if(in_state(AssetLoadState::Loading))); - app.add_systems(Update, hq_placement.run_if(in_state(GameplayState::PlaceHQ))); + app.add_systems( + Update, + hq_placement.run_if(in_state(GameplayState::PlaceHQ).and_then(in_state(GeneratorState::Idle))), + ); app.add_systems( PreUpdate, prepare_building_map.run_if(in_state(GeneratorState::SpawnMap)), diff --git a/game/main/Cargo.toml b/game/main/Cargo.toml index c7c66ae..943a7ab 100644 --- a/game/main/Cargo.toml +++ b/game/main/Cargo.toml @@ -15,6 +15,7 @@ world_generation = { path = "../../engine/world_generation" } bevy_rapier3d = { version = "0.27.0", features = ["simd-stable", "parallel"] } rayon = "1.10.0" buildings = { path = "../buildings" } +units = { path = "../units" } shared = { path = "../shared" } bevy_asset_loader = { version = "0.21.0", features = [ "standard_dynamic_assets", diff --git a/game/main/src/map_rendering/chunk_rebuild.rs b/game/main/src/map_rendering/chunk_rebuild.rs index 418aa60..6783b65 100644 --- a/game/main/src/map_rendering/chunk_rebuild.rs +++ b/game/main/src/map_rendering/chunk_rebuild.rs @@ -37,7 +37,7 @@ fn chunk_rebuilder( for (chunk_entity, idx) in &chunk_query { #[cfg(feature = "tracing")] let _spawn_span = info_span!("Rebuild Chunk").entered(); - println!("Rebuilding Chunk"); + info!("Rebuilding Chunk"); let chunk_index = idx.index; let chunk_data = heightmap.get_chunk_mesh_data(chunk_index); let chunk_offset = heightmap.chunks[chunk_index].chunk_offset; diff --git a/game/main/src/map_rendering/map_init.rs b/game/main/src/map_rendering/map_init.rs index 07b2606..10a8ea8 100644 --- a/game/main/src/map_rendering/map_init.rs +++ b/game/main/src/map_rendering/map_init.rs @@ -84,9 +84,7 @@ impl Plugin for MapInitPlugin { app.add_systems(Update, despawn_map.run_if(in_state(GeneratorState::Regenerate))); app.add_systems( Update, - spawn_map - .run_if(in_state(AssetLoadState::LoadComplete)) - .run_if(in_state(GeneratorState::SpawnMap)), + spawn_map.run_if(in_state(AssetLoadState::LoadComplete).and_then(in_state(GeneratorState::SpawnMap))), ); app.insert_resource(TileManager::default()); diff --git a/game/main/src/utlis/debug_plugin.rs b/game/main/src/utlis/debug_plugin.rs index 9437b75..35be8ef 100644 --- a/game/main/src/utlis/debug_plugin.rs +++ b/game/main/src/utlis/debug_plugin.rs @@ -1,8 +1,10 @@ use bevy::{prelude::*, window::PrimaryWindow}; -use bevy_inspector_egui::bevy_egui::EguiContexts; +use bevy_inspector_egui::bevy_egui::{systems::InputEvents, EguiContexts}; use bevy_inspector_egui::egui; use bevy_rapier3d::prelude::*; +use shared::states::GameplayState; use shared::tags::MainCamera; +use units::units_debug_plugin::UnitsDebugPlugin; use world_generation::{ consts::HEX_CORNERS, hex_utils::{HexCoord, INNER_RADIUS}, @@ -14,6 +16,7 @@ pub struct DebugPlugin; impl Plugin for DebugPlugin { fn build(&self, app: &mut App) { + app.add_plugins(UnitsDebugPlugin); app.insert_state(DebugState::Base); app.add_systems( @@ -30,6 +33,8 @@ impl Plugin for DebugPlugin { .run_if(in_state(DebugState::Verbose)), ); + app.add_systems(Update, regenerate_map.run_if(in_state(GeneratorState::Idle))); + app.insert_resource(Shape(Polyline3d::new([ HEX_CORNERS[0], HEX_CORNERS[1], @@ -52,6 +57,17 @@ pub enum DebugState { Verbose, } +fn regenerate_map( + mut generator_state: ResMut>, + mut gameplay_state: ResMut>, + input: Res>, +) { + if input.just_pressed(KeyCode::KeyR) { + generator_state.set(GeneratorState::Regenerate); + gameplay_state.set(GameplayState::PlaceHQ); + } +} + fn show_tile_heights( cam_query: Query<(&GlobalTransform, &Camera), With>, window: Query<&Window, With>, diff --git a/game/shared/src/lib.rs b/game/shared/src/lib.rs index 244a2c5..0d6610b 100644 --- a/game/shared/src/lib.rs +++ b/game/shared/src/lib.rs @@ -4,3 +4,4 @@ pub mod resource; pub mod states; pub mod tags; pub mod events; +pub mod sets; diff --git a/game/shared/src/sets.rs b/game/shared/src/sets.rs new file mode 100644 index 0000000..d5102f3 --- /dev/null +++ b/game/shared/src/sets.rs @@ -0,0 +1,4 @@ +use bevy::prelude::*; + +#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)] +pub struct GameplaySet; diff --git a/game/shared/src/tags.rs b/game/shared/src/tags.rs index 96a24d9..093b86e 100644 --- a/game/shared/src/tags.rs +++ b/game/shared/src/tags.rs @@ -1,3 +1,9 @@ use bevy::prelude::*; #[derive(Component)] pub struct MainCamera; + +#[derive(Component)] +pub enum Faction { + Player, + Phos, +} diff --git a/game/units/Cargo.toml b/game/units/Cargo.toml new file mode 100644 index 0000000..2f716ad --- /dev/null +++ b/game/units/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "units" +version = "0.1.0" +edition = "2021" + +[dependencies] +bevy = "0.14.0" +world_generation = { path = "../../engine/world_generation" } +shared = { path = "../shared" } +bevy_rapier3d = "0.27.0" +serde = { version = "1.0.204", features = ["derive"] } +asset_loader = { path = "../../engine/asset_loader" } +serde_json = "1.0.120" +ron = "0.8.1" +bevy_asset_loader = { version = "0.21.0", features = [ + "standard_dynamic_assets", + "3d", +] } + +[features] +tracing = [] diff --git a/game/units/src/assets/mod.rs b/game/units/src/assets/mod.rs new file mode 100644 index 0000000..77724e5 --- /dev/null +++ b/game/units/src/assets/mod.rs @@ -0,0 +1,3 @@ + +pub mod unit_asset; +pub mod unit_database; diff --git a/game/units/src/assets/unit_asset.rs b/game/units/src/assets/unit_asset.rs new file mode 100644 index 0000000..94c077d --- /dev/null +++ b/game/units/src/assets/unit_asset.rs @@ -0,0 +1,52 @@ +use asset_loader::create_asset_loader; +use bevy::{ecs::world::CommandQueue, prelude::*}; +use serde::{Deserialize, Serialize}; + +use crate::components::{Unit, UnitDomain}; + +#[derive(Asset, TypePath, Debug, Serialize, Deserialize)] +pub struct UnitAsset { + pub name: String, + pub description: String, + pub size: u32, + pub prefab_path: String, + #[serde(skip)] + pub prefab: Handle, + pub unit_type: UnitType, + pub domain: UnitDomain, +} + +impl UnitAsset { + pub fn spawn(&self, transform: Transform) -> CommandQueue { + let mut commands = CommandQueue::default(); + + let bundle = ( + PbrBundle { + transform: transform, + ..default() + }, + Unit, + self.domain.clone(), + ); + commands.push(move |world: &mut World| { + world.spawn(bundle); + }); + + todo!(); + } +} + +create_asset_loader!( + UnitAssetPlugin, + UnitAssetLoader, + UnitAsset, + &["unit", "unit.ron"], + prefab_path -> prefab + ;? +); + +#[derive(Debug, Serialize, Deserialize)] +pub enum UnitType { + Basic, + Turret, +} diff --git a/game/units/src/assets/unit_database.rs b/game/units/src/assets/unit_database.rs new file mode 100644 index 0000000..363d09e --- /dev/null +++ b/game/units/src/assets/unit_database.rs @@ -0,0 +1,10 @@ +use bevy::prelude::*; +use bevy_asset_loader::asset_collection::AssetCollection; + +use super::unit_asset::UnitAsset; + +#[derive(Resource, AssetCollection)] +pub struct UnitDatabase { + #[asset(key = "units", collection(typed))] + pub units: Vec>, +} diff --git a/game/units/src/components.rs b/game/units/src/components.rs new file mode 100644 index 0000000..ff9be19 --- /dev/null +++ b/game/units/src/components.rs @@ -0,0 +1,12 @@ +use bevy::prelude::*; +use serde::{Deserialize, Serialize}; + +#[derive(Component, Debug)] +pub struct Unit; + +#[derive(Component, Serialize, Deserialize, Debug, Clone, Copy)] +pub enum UnitDomain { + Land, + Air, + Navy, +} diff --git a/game/units/src/lib.rs b/game/units/src/lib.rs new file mode 100644 index 0000000..3c8f634 --- /dev/null +++ b/game/units/src/lib.rs @@ -0,0 +1,5 @@ +#[cfg(debug_assertions)] +pub mod units_debug_plugin; +pub mod units_plugin; +pub mod components; +pub mod assets; diff --git a/game/units/src/units_debug_plugin.rs b/game/units/src/units_debug_plugin.rs new file mode 100644 index 0000000..bca2d0c --- /dev/null +++ b/game/units/src/units_debug_plugin.rs @@ -0,0 +1,18 @@ +use bevy::prelude::*; +use shared::states::GameplayState; +use world_generation::states::GeneratorState; + +pub struct UnitsDebugPlugin; + +impl Plugin for UnitsDebugPlugin { + fn build(&self, app: &mut App) { + app.add_systems( + Update, + spawn_test_unit.run_if(in_state(GeneratorState::Idle).and_then(in_state(GameplayState::Playing))), + ); + } +} + +fn spawn_test_unit(mut commands: Commands, input: Res>) { + +} diff --git a/game/units/src/units_plugin.rs b/game/units/src/units_plugin.rs new file mode 100644 index 0000000..a89e0df --- /dev/null +++ b/game/units/src/units_plugin.rs @@ -0,0 +1,37 @@ +use bevy::{prelude::*, window::PrimaryWindow}; +use bevy_asset_loader::loading_state::{ + config::{ConfigureLoadingState, LoadingStateConfig}, + LoadingStateAppExt, +}; +use shared::states::{AssetLoadState, GameplayState}; +use world_generation::states::GeneratorState; + +use crate::assets::{unit_asset::UnitAssetPlugin, unit_database::UnitDatabase}; + +pub struct UnitsPlugin; + +impl Plugin for UnitsPlugin { + fn build(&self, app: &mut App) { + app.add_plugins(UnitAssetPlugin); + + app.configure_loading_state(LoadingStateConfig::new(AssetLoadState::Loading).load_collection::()); + + app.add_systems(Update, units_control.in_set(UnitUpdateSet)); + + app.configure_sets( + Update, + UnitUpdateSet.run_if(in_state(GameplayState::Playing).and_then(in_state(GeneratorState::Idle))), + ); + } +} + +#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)] +struct UnitUpdateSet; + +fn units_control(input: Res>, window: Query<&Window, With>) { + let win = window.single(); + + let Some(cursor_pos) = win.cursor_position() else { + return; + }; +}