units testing
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
use asset_loader::create_asset_loader;
|
use asset_loader::create_asset_loader;
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use shared::resource::ResourceIdentifier;
|
use shared::identifiers::ResourceIdentifier;
|
||||||
|
|
||||||
use crate::footprint::BuildingFootprint;
|
use crate::footprint::BuildingFootprint;
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ use bevy_rapier3d::{parry::transformation::utils::transform, pipeline::QueryFilt
|
|||||||
use shared::{
|
use shared::{
|
||||||
despawn::Despawn,
|
despawn::Despawn,
|
||||||
events::TileModifiedEvent,
|
events::TileModifiedEvent,
|
||||||
|
resources::TileUnderCursor,
|
||||||
states::{AssetLoadState, GameplayState},
|
states::{AssetLoadState, GameplayState},
|
||||||
tags::MainCamera,
|
tags::MainCamera,
|
||||||
};
|
};
|
||||||
@@ -78,47 +79,23 @@ fn init(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>, mut materials:
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn hq_placement(
|
fn hq_placement(
|
||||||
cam_query: Query<(&GlobalTransform, &Camera), With<MainCamera>>,
|
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
window: Query<&Window, With<PrimaryWindow>>,
|
|
||||||
mouse: Res<ButtonInput<MouseButton>>,
|
mouse: Res<ButtonInput<MouseButton>>,
|
||||||
rapier_context: Res<RapierContext>,
|
tile_under_cursor: Res<TileUnderCursor>,
|
||||||
map: Res<Map>,
|
map: Res<Map>,
|
||||||
indicator: Res<IndicatorCube>,
|
indicator: Res<IndicatorCube>,
|
||||||
mut build_queue: ResMut<BuildQueue>,
|
mut build_queue: ResMut<BuildQueue>,
|
||||||
mut next_state: ResMut<NextState<GameplayState>>,
|
mut next_state: ResMut<NextState<GameplayState>>,
|
||||||
) {
|
) {
|
||||||
let win = window.single();
|
|
||||||
let (cam_transform, camera) = cam_query.single();
|
|
||||||
let Some(cursor_pos) = win.cursor_position() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
let Some(cam_ray) = camera.viewport_to_world(cam_transform, cursor_pos) else {
|
if let Some(contact) = tile_under_cursor.0 {
|
||||||
return;
|
let positions = map.hex_select(&contact.tile, 3, true, |pos, h, _| pos.to_world(h));
|
||||||
};
|
|
||||||
|
|
||||||
let collision = rapier_context.cast_ray(
|
|
||||||
cam_ray.origin,
|
|
||||||
cam_ray.direction.into(),
|
|
||||||
500.,
|
|
||||||
true,
|
|
||||||
QueryFilter::only_fixed(),
|
|
||||||
);
|
|
||||||
|
|
||||||
if let Some((_e, dist)) = collision {
|
|
||||||
let contact_point = cam_ray.get_point(dist);
|
|
||||||
let contact_coord = HexCoord::from_world_pos(contact_point);
|
|
||||||
if !map.is_in_bounds(&contact_coord) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let positions = map.hex_select(&contact_coord, 3, true, |pos, h, _| pos.to_world(h));
|
|
||||||
show_indicators(positions, &mut commands, &indicator);
|
show_indicators(positions, &mut commands, &indicator);
|
||||||
|
|
||||||
if mouse.just_pressed(MouseButton::Left) {
|
if mouse.just_pressed(MouseButton::Left) {
|
||||||
build_queue.queue.push(QueueEntry {
|
build_queue.queue.push(QueueEntry {
|
||||||
building: 0.into(),
|
building: 0.into(),
|
||||||
pos: contact_coord,
|
pos: contact.tile,
|
||||||
});
|
});
|
||||||
|
|
||||||
next_state.set(GameplayState::Playing);
|
next_state.set(GameplayState::Playing);
|
||||||
|
|||||||
@@ -107,6 +107,7 @@ fn setup_materials(
|
|||||||
base: StandardMaterial {
|
base: StandardMaterial {
|
||||||
base_color: Color::srgb(0., 0.878, 1.),
|
base_color: Color::srgb(0., 0.878, 1.),
|
||||||
alpha_mode: AlphaMode::Blend,
|
alpha_mode: AlphaMode::Blend,
|
||||||
|
metallic: 1.0,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
extension: WaterMaterial {
|
extension: WaterMaterial {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ use bevy::{prelude::*, utils::hashbrown::HashSet, window::PrimaryWindow};
|
|||||||
use bevy_rapier3d::{pipeline::QueryFilter, plugin::RapierContext};
|
use bevy_rapier3d::{pipeline::QueryFilter, plugin::RapierContext};
|
||||||
use shared::{
|
use shared::{
|
||||||
events::{ChunkModifiedEvent, TileModifiedEvent},
|
events::{ChunkModifiedEvent, TileModifiedEvent},
|
||||||
|
resources::TileUnderCursor,
|
||||||
states::GameplayState,
|
states::GameplayState,
|
||||||
};
|
};
|
||||||
use world_generation::{hex_utils::HexCoord, prelude::Map, states::GeneratorState};
|
use world_generation::{hex_utils::HexCoord, prelude::Map, states::GeneratorState};
|
||||||
@@ -25,13 +26,11 @@ impl Plugin for TerraFormingTestPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn deform(
|
fn deform(
|
||||||
cam_query: Query<(&GlobalTransform, &Camera), With<PhosCamera>>,
|
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
window: Query<&Window, With<PrimaryWindow>>,
|
|
||||||
mouse: Res<ButtonInput<MouseButton>>,
|
mouse: Res<ButtonInput<MouseButton>>,
|
||||||
rapier_context: Res<RapierContext>,
|
|
||||||
mut heightmap: ResMut<Map>,
|
mut heightmap: ResMut<Map>,
|
||||||
chunks: Res<PhosChunkRegistry>,
|
chunks: Res<PhosChunkRegistry>,
|
||||||
|
tile_under_cursor: Res<TileUnderCursor>,
|
||||||
mut chunk_modified: EventWriter<ChunkModifiedEvent>,
|
mut chunk_modified: EventWriter<ChunkModifiedEvent>,
|
||||||
mut tile_modified: EventWriter<TileModifiedEvent>,
|
mut tile_modified: EventWriter<TileModifiedEvent>,
|
||||||
) {
|
) {
|
||||||
@@ -46,33 +45,10 @@ fn deform(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let win = window.single();
|
if let Some(contact) = tile_under_cursor.0 {
|
||||||
let (cam_transform, camera) = cam_query.single();
|
|
||||||
let Some(cursor_pos) = win.cursor_position() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
let Some(cam_ray) = camera.viewport_to_world(cam_transform, cursor_pos) else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
let collision = rapier_context.cast_ray(
|
|
||||||
cam_ray.origin,
|
|
||||||
cam_ray.direction.into(),
|
|
||||||
500.,
|
|
||||||
true,
|
|
||||||
QueryFilter::only_fixed(),
|
|
||||||
);
|
|
||||||
|
|
||||||
if let Some((_, dist)) = collision {
|
|
||||||
#[cfg(feature = "tracing")]
|
#[cfg(feature = "tracing")]
|
||||||
let span = info_span!("Deform Mesh").entered();
|
let span = info_span!("Deform Mesh").entered();
|
||||||
let contact_point = cam_ray.get_point(dist);
|
let modified_tiles = heightmap.create_crater(&contact.tile, 5, 5. * multi);
|
||||||
let contact_coord = HexCoord::from_world_pos(contact_point);
|
|
||||||
if !heightmap.is_in_bounds(&contact_coord) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let modified_tiles = heightmap.create_crater(&contact_coord, 5, 5. * multi);
|
|
||||||
let mut chunk_set: HashSet<usize> = HashSet::new();
|
let mut chunk_set: HashSet<usize> = HashSet::new();
|
||||||
for (tile, height) in modified_tiles {
|
for (tile, height) in modified_tiles {
|
||||||
let chunk = tile.to_chunk_index(heightmap.width);
|
let chunk = tile.to_chunk_index(heightmap.width);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ use crate::camera_system::components::PhosCamera;
|
|||||||
use crate::map_rendering::map_init::MapInitPlugin;
|
use crate::map_rendering::map_init::MapInitPlugin;
|
||||||
use crate::utlis::editor_plugin::EditorPlugin;
|
use crate::utlis::editor_plugin::EditorPlugin;
|
||||||
use crate::utlis::render_distance_system::RenderDistancePlugin;
|
use crate::utlis::render_distance_system::RenderDistancePlugin;
|
||||||
|
use crate::utlis::tile_selection_plugin::TileSelectionPlugin;
|
||||||
use crate::{camera_system::camera_plugin::PhosCameraPlugin, utlis::debug_plugin::DebugPlugin};
|
use crate::{camera_system::camera_plugin::PhosCameraPlugin, utlis::debug_plugin::DebugPlugin};
|
||||||
use bevy::{
|
use bevy::{
|
||||||
pbr::{wireframe::WireframeConfig, CascadeShadowConfig},
|
pbr::{wireframe::WireframeConfig, CascadeShadowConfig},
|
||||||
@@ -13,8 +14,11 @@ use bevy_rapier3d::geometry::Collider;
|
|||||||
use bevy_rapier3d::plugin::{NoUserData, RapierPhysicsPlugin};
|
use bevy_rapier3d::plugin::{NoUserData, RapierPhysicsPlugin};
|
||||||
use buildings::BuildingPugin;
|
use buildings::BuildingPugin;
|
||||||
use iyes_perf_ui::prelude::*;
|
use iyes_perf_ui::prelude::*;
|
||||||
|
use shared::sets::GameplaySet;
|
||||||
use shared::states::{GameplayState, MenuState};
|
use shared::states::{GameplayState, MenuState};
|
||||||
use shared::{despawn::DespawnPuglin, states::AssetLoadState};
|
use shared::{despawn::DespawnPuglin, states::AssetLoadState};
|
||||||
|
use units::units_plugin::UnitsPlugin;
|
||||||
|
use world_generation::states::GeneratorState;
|
||||||
|
|
||||||
pub struct PhosGamePlugin;
|
pub struct PhosGamePlugin;
|
||||||
|
|
||||||
@@ -33,13 +37,17 @@ impl Plugin for PhosGamePlugin {
|
|||||||
MapInitPlugin,
|
MapInitPlugin,
|
||||||
RenderDistancePlugin,
|
RenderDistancePlugin,
|
||||||
BuildingPugin,
|
BuildingPugin,
|
||||||
|
UnitsPlugin,
|
||||||
DespawnPuglin,
|
DespawnPuglin,
|
||||||
|
TileSelectionPlugin,
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
EditorPlugin,
|
EditorPlugin,
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
DebugPlugin,
|
DebugPlugin,
|
||||||
));
|
));
|
||||||
|
|
||||||
|
configure_gameplay_set(app);
|
||||||
|
|
||||||
//Systems - Startup
|
//Systems - Startup
|
||||||
app.add_systems(Startup, init_game);
|
app.add_systems(Startup, init_game);
|
||||||
|
|
||||||
@@ -63,6 +71,34 @@ impl Plugin for PhosGamePlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn configure_gameplay_set(app: &mut App) {
|
||||||
|
app.configure_sets(
|
||||||
|
Update,
|
||||||
|
GameplaySet.run_if(in_state(GeneratorState::Idle).and_then(in_state(MenuState::InGame))),
|
||||||
|
);
|
||||||
|
app.configure_sets(
|
||||||
|
PreUpdate,
|
||||||
|
GameplaySet.run_if(in_state(GeneratorState::Idle).and_then(in_state(MenuState::InGame))),
|
||||||
|
);
|
||||||
|
app.configure_sets(
|
||||||
|
PostUpdate,
|
||||||
|
GameplaySet.run_if(in_state(GeneratorState::Idle).and_then(in_state(MenuState::InGame))),
|
||||||
|
);
|
||||||
|
|
||||||
|
app.configure_sets(
|
||||||
|
FixedUpdate,
|
||||||
|
GameplaySet.run_if(in_state(GeneratorState::Idle).and_then(in_state(MenuState::InGame))),
|
||||||
|
);
|
||||||
|
app.configure_sets(
|
||||||
|
FixedPreUpdate,
|
||||||
|
GameplaySet.run_if(in_state(GeneratorState::Idle).and_then(in_state(MenuState::InGame))),
|
||||||
|
);
|
||||||
|
app.configure_sets(
|
||||||
|
FixedPostUpdate,
|
||||||
|
GameplaySet.run_if(in_state(GeneratorState::Idle).and_then(in_state(MenuState::InGame))),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
fn init_game(mut commands: Commands, mut materials: ResMut<Assets<StandardMaterial>>) {
|
fn init_game(mut commands: Commands, mut materials: ResMut<Assets<StandardMaterial>>) {
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
PerfUiRoot::default(),
|
PerfUiRoot::default(),
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ use bevy::{prelude::*, window::PrimaryWindow};
|
|||||||
use bevy_inspector_egui::bevy_egui::{systems::InputEvents, EguiContexts};
|
use bevy_inspector_egui::bevy_egui::{systems::InputEvents, EguiContexts};
|
||||||
use bevy_inspector_egui::egui;
|
use bevy_inspector_egui::egui;
|
||||||
use bevy_rapier3d::prelude::*;
|
use bevy_rapier3d::prelude::*;
|
||||||
|
use shared::resources::TileUnderCursor;
|
||||||
use shared::states::GameplayState;
|
use shared::states::GameplayState;
|
||||||
use shared::tags::MainCamera;
|
use shared::tags::MainCamera;
|
||||||
use world_generation::{
|
use world_generation::{
|
||||||
@@ -66,62 +67,32 @@ fn regenerate_map(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn show_tile_heights(
|
fn show_tile_heights(map: Res<Map>, mut gizmos: Gizmos, shape: Res<Shape>, tile_under_cursor: Res<TileUnderCursor>) {
|
||||||
cam_query: Query<(&GlobalTransform, &Camera), With<MainCamera>>,
|
if let Some(contact) = tile_under_cursor.0 {
|
||||||
window: Query<&Window, With<PrimaryWindow>>,
|
let height = map.sample_height(&contact.tile);
|
||||||
rapier_context: Res<RapierContext>,
|
|
||||||
map: Res<Map>,
|
|
||||||
mut gizmos: Gizmos,
|
|
||||||
shape: Res<Shape>,
|
|
||||||
) {
|
|
||||||
let win = window.single();
|
|
||||||
let (cam_transform, camera) = cam_query.single();
|
|
||||||
let Some(cursor_pos) = win.cursor_position() else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
let Some(cam_ray) = camera.viewport_to_world(cam_transform, cursor_pos) else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
let collision = rapier_context.cast_ray(
|
|
||||||
cam_ray.origin,
|
|
||||||
cam_ray.direction.into(),
|
|
||||||
500.,
|
|
||||||
true,
|
|
||||||
QueryFilter::only_fixed(),
|
|
||||||
);
|
|
||||||
|
|
||||||
if let Some((_e, dist)) = collision {
|
|
||||||
let contact_point = cam_ray.get_point(dist);
|
|
||||||
let contact_coord = HexCoord::from_world_pos(contact_point);
|
|
||||||
if !map.is_in_bounds(&contact_coord) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let height = map.sample_height(&contact_coord);
|
|
||||||
gizmos.primitive_3d(
|
gizmos.primitive_3d(
|
||||||
&shape.0,
|
&shape.0,
|
||||||
contact_coord.to_world(height + 0.01),
|
contact.tile.to_world(height + 0.01),
|
||||||
Quat::IDENTITY,
|
Quat::IDENTITY,
|
||||||
Color::WHITE,
|
Color::WHITE,
|
||||||
);
|
);
|
||||||
let nbors = map.get_neighbors(&contact_coord);
|
let nbors = map.get_neighbors(&contact.tile);
|
||||||
let contact_tile_pos = contact_coord.to_world(map.sample_height(&contact_coord));
|
let contact_tile_pos = contact.tile.to_world(map.sample_height(&contact.tile));
|
||||||
|
|
||||||
for i in 0..6 {
|
// for i in 0..6 {
|
||||||
if let Some(s) = nbors[i] {
|
// if let Some(s) = nbors[i] {
|
||||||
let coord = contact_coord.get_neighbor(i);
|
// let coord = contact.tile.get_neighbor(i);
|
||||||
let p = coord.to_world(s);
|
// let p = coord.to_world(s);
|
||||||
gizmos.arrow(p, p + Vec3::Y * (i as f32 + 1.0), Color::WHITE);
|
// gizmos.arrow(p, p + Vec3::Y * (i as f32 + 1.0), Color::WHITE);
|
||||||
}
|
// }
|
||||||
|
|
||||||
let p = HEX_CORNERS[i] + contact_tile_pos;
|
// let p = HEX_CORNERS[i] + contact_tile_pos;
|
||||||
gizmos.arrow(p, p + Vec3::Y * (i as f32 + 1.0), LinearRgba::rgb(1.0, 0.0, 0.5));
|
// gizmos.arrow(p, p + Vec3::Y * (i as f32 + 1.0), LinearRgba::rgb(1.0, 0.0, 0.5));
|
||||||
}
|
// }
|
||||||
|
|
||||||
gizmos.line(contact_point, contact_point + Vec3::X, LinearRgba::RED);
|
gizmos.line(contact.point, contact.point + Vec3::X, LinearRgba::RED);
|
||||||
gizmos.line(contact_point, contact_point + Vec3::Y, LinearRgba::GREEN);
|
gizmos.line(contact.point, contact.point + Vec3::Y, LinearRgba::GREEN);
|
||||||
gizmos.line(contact_point, contact_point + Vec3::Z, LinearRgba::BLUE);
|
gizmos.line(contact.point, contact.point + Vec3::Z, LinearRgba::BLUE);
|
||||||
//gizmos.sphere(contact_point, Quat::IDENTITY, 0.1, LinearRgba::rgb(1., 0., 0.5));
|
//gizmos.sphere(contact_point, Quat::IDENTITY, 0.1, LinearRgba::rgb(1., 0., 0.5));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,3 +2,4 @@ pub mod chunk_utils;
|
|||||||
pub mod render_distance_system;
|
pub mod render_distance_system;
|
||||||
pub mod debug_plugin;
|
pub mod debug_plugin;
|
||||||
pub mod editor_plugin;
|
pub mod editor_plugin;
|
||||||
|
pub mod tile_selection_plugin;
|
||||||
|
|||||||
62
game/main/src/utlis/tile_selection_plugin.rs
Normal file
62
game/main/src/utlis/tile_selection_plugin.rs
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
use bevy::{prelude::*, window::PrimaryWindow};
|
||||||
|
use bevy_rapier3d::{plugin::RapierContext, prelude::QueryFilter};
|
||||||
|
use shared::{
|
||||||
|
resources::{TileContact, TileUnderCursor},
|
||||||
|
tags::MainCamera,
|
||||||
|
};
|
||||||
|
use world_generation::{hex_utils::HexCoord, prelude::Map, states::GeneratorState};
|
||||||
|
pub struct TileSelectionPlugin;
|
||||||
|
|
||||||
|
impl Plugin for TileSelectionPlugin {
|
||||||
|
fn build(&self, app: &mut App) {
|
||||||
|
app.init_resource::<TileUnderCursor>();
|
||||||
|
app.add_systems(
|
||||||
|
PreUpdate,
|
||||||
|
update_tile_under_cursor.run_if(in_state(GeneratorState::Idle)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_tile_under_cursor(
|
||||||
|
cam_query: Query<(&GlobalTransform, &Camera), With<MainCamera>>,
|
||||||
|
window: Query<&Window, With<PrimaryWindow>>,
|
||||||
|
rapier_context: Res<RapierContext>,
|
||||||
|
map: Res<Map>,
|
||||||
|
mut tile_under_cursor: ResMut<TileUnderCursor>,
|
||||||
|
) {
|
||||||
|
let win = window.single();
|
||||||
|
let (cam_transform, camera) = cam_query.single();
|
||||||
|
let Some(cursor_pos) = win.cursor_position() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let Some(cam_ray) = camera.viewport_to_world(cam_transform, cursor_pos) else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let collision = rapier_context.cast_ray(
|
||||||
|
cam_ray.origin,
|
||||||
|
cam_ray.direction.into(),
|
||||||
|
500.,
|
||||||
|
true,
|
||||||
|
QueryFilter::only_fixed(),
|
||||||
|
);
|
||||||
|
|
||||||
|
if let Some((_e, dist)) = collision {
|
||||||
|
let contact_point = cam_ray.get_point(dist);
|
||||||
|
let contact_coord = HexCoord::from_world_pos(contact_point);
|
||||||
|
//todo: handle correct tile detection when contacting a tile from the side
|
||||||
|
if !map.is_in_bounds(&contact_coord) {
|
||||||
|
tile_under_cursor.0 = None;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let surface = map.sample_height(&contact_coord);
|
||||||
|
tile_under_cursor.0 = Some(TileContact::new(
|
||||||
|
contact_coord,
|
||||||
|
contact_point,
|
||||||
|
contact_coord.to_world(surface),
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
tile_under_cursor.0 = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
|
use bevy::prelude::Resource;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use world_generation::hex_utils::HexCoord;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct ResourceIdentifier {
|
pub struct ResourceIdentifier {
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
pub mod building;
|
pub mod building;
|
||||||
pub mod despawn;
|
pub mod despawn;
|
||||||
pub mod resource;
|
pub mod identifiers;
|
||||||
pub mod states;
|
pub mod states;
|
||||||
pub mod tags;
|
pub mod tags;
|
||||||
pub mod events;
|
pub mod events;
|
||||||
pub mod sets;
|
pub mod sets;
|
||||||
|
pub mod resources;
|
||||||
|
|||||||
22
game/shared/src/resources.rs
Normal file
22
game/shared/src/resources.rs
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
use bevy::prelude::*;
|
||||||
|
use world_generation::hex_utils::HexCoord;
|
||||||
|
|
||||||
|
#[derive(Resource, Default)]
|
||||||
|
pub struct TileUnderCursor(pub Option<TileContact>);
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
pub struct TileContact {
|
||||||
|
pub tile: HexCoord,
|
||||||
|
pub point: Vec3,
|
||||||
|
pub surface: Vec3,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TileContact {
|
||||||
|
pub fn new(tile: HexCoord, contact: Vec3, surface: Vec3) -> Self {
|
||||||
|
return Self {
|
||||||
|
tile,
|
||||||
|
point: contact,
|
||||||
|
surface,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,7 +2,7 @@ use asset_loader::create_asset_loader;
|
|||||||
use bevy::{ecs::world::CommandQueue, prelude::*};
|
use bevy::{ecs::world::CommandQueue, prelude::*};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::components::{Unit, UnitDomain};
|
use crate::components::{AirUnit, LandUnit, NavalUnit, Unit, UnitDomain};
|
||||||
|
|
||||||
#[derive(Asset, TypePath, Debug, Serialize, Deserialize)]
|
#[derive(Asset, TypePath, Debug, Serialize, Deserialize)]
|
||||||
pub struct UnitAsset {
|
pub struct UnitAsset {
|
||||||
@@ -26,10 +26,15 @@ impl UnitAsset {
|
|||||||
..default()
|
..default()
|
||||||
},
|
},
|
||||||
Unit,
|
Unit,
|
||||||
self.domain.clone(),
|
|
||||||
);
|
);
|
||||||
|
let domain = self.domain.clone();
|
||||||
commands.push(move |world: &mut World| {
|
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!();
|
todo!();
|
||||||
|
|||||||
@@ -4,9 +4,19 @@ use serde::{Deserialize, Serialize};
|
|||||||
#[derive(Component, Debug)]
|
#[derive(Component, Debug)]
|
||||||
pub struct Unit;
|
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 {
|
pub enum UnitDomain {
|
||||||
Land,
|
Land,
|
||||||
Air,
|
Air,
|
||||||
Navy,
|
Naval,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Debug)]
|
||||||
|
pub struct Target(pub Vec3);
|
||||||
|
|||||||
@@ -1,18 +1,66 @@
|
|||||||
|
use std::f32::consts::E;
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use shared::states::GameplayState;
|
use shared::{resources::TileUnderCursor, sets::GameplaySet, states::AssetLoadState};
|
||||||
use world_generation::states::GeneratorState;
|
use world_generation::{heightmap, prelude::Map};
|
||||||
|
|
||||||
|
use crate::components::{LandUnit, Target, Unit};
|
||||||
|
|
||||||
pub struct UnitsDebugPlugin;
|
pub struct UnitsDebugPlugin;
|
||||||
|
|
||||||
impl Plugin for UnitsDebugPlugin {
|
impl Plugin for UnitsDebugPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_systems(
|
app.add_systems(Update, init.run_if(in_state(AssetLoadState::Loading)));
|
||||||
Update,
|
|
||||||
spawn_test_unit.run_if(in_state(GeneratorState::Idle).and_then(in_state(GameplayState::Playing))),
|
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},
|
config::{ConfigureLoadingState, LoadingStateConfig},
|
||||||
LoadingStateAppExt,
|
LoadingStateAppExt,
|
||||||
};
|
};
|
||||||
use shared::states::{AssetLoadState, GameplayState};
|
use shared::{sets::GameplaySet, states::AssetLoadState};
|
||||||
use world_generation::states::GeneratorState;
|
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;
|
pub struct UnitsPlugin;
|
||||||
|
|
||||||
@@ -14,20 +18,16 @@ impl Plugin for UnitsPlugin {
|
|||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_plugins(UnitAssetPlugin);
|
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(
|
app.add_systems(Update, units_control.in_set(GameplaySet));
|
||||||
Update,
|
app.add_systems(Update, move_unit.in_set(GameplaySet));
|
||||||
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>>) {
|
fn units_control(input: Res<ButtonInput<KeyCode>>, window: Query<&Window, With<PrimaryWindow>>) {
|
||||||
let win = window.single();
|
let win = window.single();
|
||||||
|
|
||||||
@@ -35,3 +35,14 @@ fn units_control(input: Res<ButtonInput<KeyCode>>, window: Query<&Window, With<P
|
|||||||
return;
|
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