@@ -12,11 +12,11 @@ use crate::{
|
||||
resource_gathering::ResourceGatheringBuildingInfo,
|
||||
},
|
||||
footprint::BuildingFootprint,
|
||||
prelude::Building,
|
||||
};
|
||||
|
||||
#[derive(Asset, TypePath, Debug, Serialize, Deserialize)]
|
||||
pub struct BuildingAsset {
|
||||
pub struct BuildingAsset
|
||||
{
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub footprint: BuildingFootprint,
|
||||
@@ -35,7 +35,9 @@ pub struct BuildingAsset {
|
||||
// pub components: Option<Vec<ComponentDefination>>,
|
||||
}
|
||||
|
||||
impl BuildingAsset {
|
||||
impl BuildingAsset
|
||||
{
|
||||
#[allow(unused)]
|
||||
pub fn spawn(
|
||||
&self,
|
||||
pos: Vec3,
|
||||
@@ -44,7 +46,8 @@ impl BuildingAsset {
|
||||
commands: &mut Commands,
|
||||
meshes: &Assets<GltfMesh>,
|
||||
nodes: &Assets<GltfNode>,
|
||||
) -> Option<Entity> {
|
||||
) -> Option<Entity>
|
||||
{
|
||||
todo!("Update building spawning");
|
||||
// let base_node = &gltf.named_nodes[&self.base_mesh_path.clone().into_boxed_str()];
|
||||
// if let Some(node) = nodes.get(base_node.id()) {
|
||||
@@ -120,7 +123,8 @@ impl BuildingAsset {
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, TypePath)]
|
||||
pub enum BuildingType {
|
||||
pub enum BuildingType
|
||||
{
|
||||
Basic,
|
||||
Gathering(ResourceGatheringBuildingInfo),
|
||||
FactoryBuildingInfo(FactoryBuildingInfo),
|
||||
|
||||
@@ -1,26 +1,19 @@
|
||||
use std::f32::consts::E;
|
||||
|
||||
use bevy::{
|
||||
ecs::world::CommandQueue,
|
||||
gltf::{GltfMesh, GltfNode},
|
||||
prelude::*,
|
||||
window::PrimaryWindow,
|
||||
};
|
||||
use bevy_asset_loader::loading_state::{
|
||||
config::{ConfigureLoadingState, LoadingStateConfig},
|
||||
LoadingStateAppExt,
|
||||
};
|
||||
use bevy_rapier3d::{parry::transformation::utils::transform, pipeline::QueryFilter, plugin::RapierContext};
|
||||
use shared::{
|
||||
despawn::Despawn,
|
||||
events::TileModifiedEvent,
|
||||
resources::TileUnderCursor,
|
||||
states::{AssetLoadState, GameplayState},
|
||||
tags::MainCamera,
|
||||
};
|
||||
use world_generation::{
|
||||
heightmap, hex_utils::HexCoord, map::map::Map, prelude::GenerationConfig, states::GeneratorState,
|
||||
};
|
||||
use world_generation::{map::map::Map, prelude::GenerationConfig, states::GeneratorState};
|
||||
|
||||
use crate::{
|
||||
assets::{
|
||||
@@ -34,8 +27,10 @@ use crate::{
|
||||
|
||||
pub struct BuildingPugin;
|
||||
|
||||
impl Plugin for BuildingPugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
impl Plugin for BuildingPugin
|
||||
{
|
||||
fn build(&self, app: &mut App)
|
||||
{
|
||||
app.insert_resource(BuildQueue::default());
|
||||
app.add_plugins(BuildingAssetPlugin);
|
||||
|
||||
@@ -62,12 +57,15 @@ impl Plugin for BuildingPugin {
|
||||
}
|
||||
}
|
||||
|
||||
fn prepare_building_map(mut commands: Commands, cfg: Res<GenerationConfig>) {
|
||||
fn prepare_building_map(mut commands: Commands, cfg: Res<GenerationConfig>)
|
||||
{
|
||||
commands.insert_resource(BuildingMap::new(cfg.size));
|
||||
}
|
||||
|
||||
fn regernerate(mut commands: Commands, buildings: Query<Entity, With<Building>>, cfg: Res<GenerationConfig>) {
|
||||
for e in buildings.iter() {
|
||||
fn regernerate(mut commands: Commands, buildings: Query<Entity, With<Building>>, cfg: Res<GenerationConfig>)
|
||||
{
|
||||
for e in buildings.iter()
|
||||
{
|
||||
commands.entity(e).despawn();
|
||||
}
|
||||
commands.insert_resource(BuildingMap::new(cfg.size));
|
||||
@@ -76,7 +74,8 @@ fn regernerate(mut commands: Commands, buildings: Query<Entity, With<Building>>,
|
||||
#[derive(Resource)]
|
||||
struct IndicatorCube(Handle<Mesh>, Handle<StandardMaterial>);
|
||||
|
||||
fn init(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>, mut materials: ResMut<Assets<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);
|
||||
@@ -91,12 +90,15 @@ fn hq_placement(
|
||||
indicator: Res<IndicatorCube>,
|
||||
mut build_queue: ResMut<BuildQueue>,
|
||||
mut next_state: ResMut<NextState<GameplayState>>,
|
||||
) {
|
||||
if let Some(contact) = tile_under_cursor.0 {
|
||||
)
|
||||
{
|
||||
if let Some(contact) = tile_under_cursor.0
|
||||
{
|
||||
let positions = map.hex_select(&contact.tile, 3, true, |pos, h, _| pos.to_world(h));
|
||||
show_indicators(positions, &mut commands, &indicator);
|
||||
|
||||
if mouse.just_pressed(MouseButton::Left) {
|
||||
if mouse.just_pressed(MouseButton::Left)
|
||||
{
|
||||
build_queue.queue.push(QueueEntry {
|
||||
building: 0.into(),
|
||||
pos: contact.tile,
|
||||
@@ -107,8 +109,10 @@ fn hq_placement(
|
||||
}
|
||||
}
|
||||
|
||||
fn show_indicators(positions: Vec<Vec3>, commands: &mut Commands, indicator: &IndicatorCube) {
|
||||
for p in positions {
|
||||
fn show_indicators(positions: Vec<Vec3>, commands: &mut Commands, indicator: &IndicatorCube)
|
||||
{
|
||||
for p in positions
|
||||
{
|
||||
commands.spawn((
|
||||
Mesh3d(indicator.0.clone()),
|
||||
MeshMaterial3d(indicator.1.clone()),
|
||||
@@ -128,13 +132,17 @@ fn process_build_queue(
|
||||
gltf_nodes: Res<Assets<GltfNode>>,
|
||||
mut building_map: ResMut<BuildingMap>,
|
||||
heightmap: Res<Map>,
|
||||
) {
|
||||
for item in &queue.queue {
|
||||
)
|
||||
{
|
||||
for item in &queue.queue
|
||||
{
|
||||
let handle = &db.buildings[item.building.0];
|
||||
if let Some(building) = building_assets.get(handle.id()) {
|
||||
if let Some(building) = building_assets.get(handle.id())
|
||||
{
|
||||
let h = heightmap.sample_height(&item.pos);
|
||||
println!("Spawning {} at {}", building.name, item.pos);
|
||||
if let Some(gltf) = gltf_assets.get(building.prefab.id()) {
|
||||
if let Some(gltf) = gltf_assets.get(building.prefab.id())
|
||||
{
|
||||
let e = building.spawn(
|
||||
item.pos.to_world(h),
|
||||
Quat::IDENTITY,
|
||||
@@ -143,10 +151,13 @@ fn process_build_queue(
|
||||
&gltf_meshes,
|
||||
&gltf_nodes,
|
||||
);
|
||||
if let Some(b) = e {
|
||||
if let Some(b) = e
|
||||
{
|
||||
building_map.add_building(BuildingEntry::new(item.pos, b));
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
warn!("Failed to spawn building");
|
||||
}
|
||||
}
|
||||
@@ -158,17 +169,23 @@ fn update_building_heights(
|
||||
mut tile_updates: MessageReader<TileModifiedEvent>,
|
||||
building_map: Res<BuildingMap>,
|
||||
mut commands: Commands,
|
||||
) {
|
||||
for event in tile_updates.read() {
|
||||
match event {
|
||||
TileModifiedEvent::HeightChanged(coord, new_height) => {
|
||||
if let Some(building) = building_map.get_building(coord) {
|
||||
)
|
||||
{
|
||||
for event in tile_updates.read()
|
||||
{
|
||||
match event
|
||||
{
|
||||
TileModifiedEvent::HeightChanged(coord, new_height) =>
|
||||
{
|
||||
if let Some(building) = building_map.get_building(coord)
|
||||
{
|
||||
let mut queue = CommandQueue::default();
|
||||
let e = building.entity.clone();
|
||||
let h = *new_height;
|
||||
queue.push(move |world: &mut World| {
|
||||
let mut emut = world.entity_mut(e);
|
||||
if let Some(mut transform) = emut.get_mut::<Transform>() {
|
||||
if let Some(mut transform) = emut.get_mut::<Transform>()
|
||||
{
|
||||
transform.translation.y = h;
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user