units wip

This commit is contained in:
2024-08-27 23:04:03 -04:00
parent 25b6ad1099
commit 77fa421bb2
18 changed files with 210 additions and 7 deletions

21
game/units/Cargo.toml Normal file
View File

@@ -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 = []

View File

@@ -0,0 +1,3 @@
pub mod unit_asset;
pub mod unit_database;

View File

@@ -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<Scene>,
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,
}

View File

@@ -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<Handle<UnitAsset>>,
}

View File

@@ -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,
}

5
game/units/src/lib.rs Normal file
View File

@@ -0,0 +1,5 @@
#[cfg(debug_assertions)]
pub mod units_debug_plugin;
pub mod units_plugin;
pub mod components;
pub mod assets;

View File

@@ -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<ButtonInput<KeyCode>>) {
}

View File

@@ -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::<UnitDatabase>());
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<ButtonInput<KeyCode>>, window: Query<&Window, With<PrimaryWindow>>) {
let win = window.single();
let Some(cursor_pos) = win.cursor_position() else {
return;
};
}