units testing
This commit is contained in:
@@ -2,7 +2,7 @@ use asset_loader::create_asset_loader;
|
||||
use bevy::{ecs::world::CommandQueue, prelude::*};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::components::{Unit, UnitDomain};
|
||||
use crate::components::{AirUnit, LandUnit, NavalUnit, Unit, UnitDomain};
|
||||
|
||||
#[derive(Asset, TypePath, Debug, Serialize, Deserialize)]
|
||||
pub struct UnitAsset {
|
||||
@@ -26,10 +26,15 @@ impl UnitAsset {
|
||||
..default()
|
||||
},
|
||||
Unit,
|
||||
self.domain.clone(),
|
||||
);
|
||||
let domain = self.domain.clone();
|
||||
commands.push(move |world: &mut World| {
|
||||
world.spawn(bundle);
|
||||
let mut e = world.spawn(bundle);
|
||||
match domain {
|
||||
UnitDomain::Land => e.insert(LandUnit),
|
||||
UnitDomain::Air => e.insert(AirUnit),
|
||||
UnitDomain::Naval => e.insert(NavalUnit),
|
||||
};
|
||||
});
|
||||
|
||||
todo!();
|
||||
|
||||
@@ -4,9 +4,19 @@ use serde::{Deserialize, Serialize};
|
||||
#[derive(Component, Debug)]
|
||||
pub struct Unit;
|
||||
|
||||
#[derive(Component, Serialize, Deserialize, Debug, Clone, Copy)]
|
||||
#[derive(Component, Debug)]
|
||||
pub struct AirUnit;
|
||||
#[derive(Component, Debug)]
|
||||
pub struct LandUnit;
|
||||
#[derive(Component, Debug)]
|
||||
pub struct NavalUnit;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
|
||||
pub enum UnitDomain {
|
||||
Land,
|
||||
Air,
|
||||
Navy,
|
||||
Naval,
|
||||
}
|
||||
|
||||
#[derive(Component, Debug)]
|
||||
pub struct Target(pub Vec3);
|
||||
|
||||
@@ -1,18 +1,66 @@
|
||||
use std::f32::consts::E;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use shared::states::GameplayState;
|
||||
use world_generation::states::GeneratorState;
|
||||
use shared::{resources::TileUnderCursor, sets::GameplaySet, states::AssetLoadState};
|
||||
use world_generation::{heightmap, prelude::Map};
|
||||
|
||||
use crate::components::{LandUnit, Target, Unit};
|
||||
|
||||
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))),
|
||||
);
|
||||
app.add_systems(Update, init.run_if(in_state(AssetLoadState::Loading)));
|
||||
|
||||
app.add_systems(Update, (spawn_test_unit, set_unit_target).in_set(GameplaySet));
|
||||
}
|
||||
}
|
||||
|
||||
fn spawn_test_unit(mut commands: Commands, input: Res<ButtonInput<KeyCode>>) {
|
||||
|
||||
#[derive(Resource)]
|
||||
struct TestUnit(pub Handle<Mesh>);
|
||||
|
||||
fn init(mut meshes: ResMut<Assets<Mesh>>, mut commands: Commands) {
|
||||
let mesh_handle = meshes.add(Cuboid::from_length(1.0));
|
||||
commands.insert_resource(TestUnit(mesh_handle));
|
||||
}
|
||||
|
||||
fn spawn_test_unit(
|
||||
mut commands: Commands,
|
||||
input: Res<ButtonInput<KeyCode>>,
|
||||
tile_under_cursor: Res<TileUnderCursor>,
|
||||
unit: Res<TestUnit>,
|
||||
) {
|
||||
if !input.just_pressed(KeyCode::KeyT) {
|
||||
return;
|
||||
}
|
||||
if let Some(contact) = tile_under_cursor.0 {
|
||||
info!("Spawning Test Unit");
|
||||
commands.spawn((
|
||||
PbrBundle {
|
||||
transform: Transform::from_translation(contact.surface),
|
||||
mesh: unit.0.clone(),
|
||||
..default()
|
||||
},
|
||||
Unit,
|
||||
LandUnit,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
fn set_unit_target(
|
||||
mut commands: Commands,
|
||||
units: Query<Entity, With<Unit>>,
|
||||
input: Res<ButtonInput<MouseButton>>,
|
||||
tile_under_cursor: Res<TileUnderCursor>,
|
||||
) {
|
||||
if !input.just_pressed(MouseButton::Right) {
|
||||
return;
|
||||
}
|
||||
if let Some(contact) = tile_under_cursor.0 {
|
||||
for e in units.iter() {
|
||||
info!("Setting Target");
|
||||
let mut e = commands.entity(e);
|
||||
e.insert(Target(contact.surface));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,10 +3,14 @@ use bevy_asset_loader::loading_state::{
|
||||
config::{ConfigureLoadingState, LoadingStateConfig},
|
||||
LoadingStateAppExt,
|
||||
};
|
||||
use shared::states::{AssetLoadState, GameplayState};
|
||||
use world_generation::states::GeneratorState;
|
||||
use shared::{sets::GameplaySet, states::AssetLoadState};
|
||||
use world_generation::{hex_utils::HexCoord, prelude::Map};
|
||||
|
||||
use crate::assets::{unit_asset::UnitAssetPlugin, unit_database::UnitDatabase};
|
||||
use crate::{
|
||||
assets::{unit_asset::UnitAssetPlugin, unit_database::UnitDatabase},
|
||||
components::{Target, Unit},
|
||||
units_debug_plugin::UnitsDebugPlugin,
|
||||
};
|
||||
|
||||
pub struct UnitsPlugin;
|
||||
|
||||
@@ -14,20 +18,16 @@ 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>());
|
||||
#[cfg(debug_assertions)]
|
||||
app.add_plugins(UnitsDebugPlugin);
|
||||
|
||||
app.add_systems(Update, units_control.in_set(UnitUpdateSet));
|
||||
// app.configure_loading_state(LoadingStateConfig::new(AssetLoadState::Loading).load_collection::<UnitDatabase>());
|
||||
|
||||
app.configure_sets(
|
||||
Update,
|
||||
UnitUpdateSet.run_if(in_state(GameplayState::Playing).and_then(in_state(GeneratorState::Idle))),
|
||||
);
|
||||
app.add_systems(Update, units_control.in_set(GameplaySet));
|
||||
app.add_systems(Update, move_unit.in_set(GameplaySet));
|
||||
}
|
||||
}
|
||||
|
||||
#[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();
|
||||
|
||||
@@ -35,3 +35,14 @@ fn units_control(input: Res<ButtonInput<KeyCode>>, window: Query<&Window, With<P
|
||||
return;
|
||||
};
|
||||
}
|
||||
|
||||
fn move_unit(mut units: Query<(&mut Transform, &Target), With<Unit>>, time: Res<Time>, map: Res<Map>) {
|
||||
for (mut t, target) in units.iter_mut() {
|
||||
let vel = (target.0 - t.translation).normalize() * 10.0 * time.delta_seconds();
|
||||
t.translation += vel;
|
||||
let coord = HexCoord::from_world_pos(t.translation);
|
||||
if map.is_in_bounds(&coord) {
|
||||
t.translation.y = map.sample_height(&coord);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user