use bevy states
despawn plugin buiding plugin
This commit is contained in:
@@ -8,6 +8,8 @@ edition = "2021"
|
||||
[dependencies]
|
||||
bevy = "0.13.2"
|
||||
world_generation = {path = "../../engine/world_generation"}
|
||||
shared = {path = "../shared"}
|
||||
bevy_rapier3d = "0.26.0"
|
||||
|
||||
[features]
|
||||
tracing = []
|
||||
|
||||
68
game/buildings/src/building_plugin.rs
Normal file
68
game/buildings/src/building_plugin.rs
Normal file
@@ -0,0 +1,68 @@
|
||||
use bevy::{prelude::*, window::PrimaryWindow};
|
||||
use bevy_rapier3d::{pipeline::QueryFilter, plugin::RapierContext};
|
||||
use shared::{despawn::Despawn, states::GameplayState, tags::MainCamera};
|
||||
use world_generation::{hex_utils::HexCoord, map::map::Map};
|
||||
|
||||
pub struct BuildingPugin;
|
||||
|
||||
impl Plugin for BuildingPugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Startup, init);
|
||||
app.add_systems(Update, hq_placement.run_if(in_state(GameplayState::PlaceHQ)));
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Resource)]
|
||||
struct IndicatorCube(Handle<Mesh>, Handle<StandardMaterial>);
|
||||
|
||||
fn init(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>, mut materials: ResMut<Assets<StandardMaterial>>) {
|
||||
let cube = Cuboid::from_size(Vec3::splat(1.));
|
||||
let mesh_handle = meshes.add(cube);
|
||||
let mat_handle = materials.add(Color::WHITE);
|
||||
commands.insert_resource(IndicatorCube(mesh_handle, mat_handle));
|
||||
}
|
||||
|
||||
fn hq_placement(
|
||||
cam_query: Query<(&GlobalTransform, &Camera), With<MainCamera>>,
|
||||
mut commands: Commands,
|
||||
window: Query<&Window, With<PrimaryWindow>>,
|
||||
mouse: Res<ButtonInput<MouseButton>>,
|
||||
rapier_context: Res<RapierContext>,
|
||||
map: Res<Map>,
|
||||
indicator: Res<IndicatorCube>,
|
||||
) {
|
||||
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);
|
||||
let positions = map.hex_select(&contact_coord, 3, true, |pos, h, _| pos.to_world(h));
|
||||
for p in positions {
|
||||
commands.spawn((
|
||||
PbrBundle {
|
||||
mesh: indicator.0.clone(),
|
||||
material: indicator.1.clone(),
|
||||
transform: Transform::from_translation(p),
|
||||
..default()
|
||||
},
|
||||
Despawn,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1 +1,4 @@
|
||||
pub mod buildings_database;
|
||||
pub mod building_plugin;
|
||||
|
||||
pub use building_plugin::*;
|
||||
Reference in New Issue
Block a user