units testing

This commit is contained in:
2024-09-10 20:41:33 -04:00
parent 805fb3feb6
commit b156a33a54
15 changed files with 254 additions and 131 deletions

View File

@@ -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));
}
}
}