Spawn Building
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
use crate::prelude::Chunk;
|
use crate::prelude::Chunk;
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
@@ -62,6 +64,12 @@ pub struct HexCoord {
|
|||||||
pub hex: IVec3,
|
pub hex: IVec3,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Display for HexCoord {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
f.write_fmt(format_args!("HexCoord{}", self.hex))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl HexCoord {
|
impl HexCoord {
|
||||||
pub const DIRECTIONS: [IVec3; 6] = [
|
pub const DIRECTIONS: [IVec3; 6] = [
|
||||||
IVec3::new(0, 1, -1),
|
IVec3::new(0, 1, -1),
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use std::path::Display;
|
||||||
|
|
||||||
use bevy::prelude::Resource;
|
use bevy::prelude::Resource;
|
||||||
use shared::building::BuildingIdentifier;
|
use shared::building::BuildingIdentifier;
|
||||||
use world_generation::hex_utils::HexCoord;
|
use world_generation::hex_utils::HexCoord;
|
||||||
|
|||||||
@@ -3,17 +3,21 @@ use bevy_asset_loader::loading_state::{
|
|||||||
config::{ConfigureLoadingState, LoadingStateConfig},
|
config::{ConfigureLoadingState, LoadingStateConfig},
|
||||||
LoadingStateAppExt,
|
LoadingStateAppExt,
|
||||||
};
|
};
|
||||||
use bevy_rapier3d::{pipeline::QueryFilter, plugin::RapierContext};
|
use bevy_rapier3d::{parry::transformation::utils::transform, pipeline::QueryFilter, plugin::RapierContext};
|
||||||
use shared::{
|
use shared::{
|
||||||
despawn::Despawn,
|
despawn::Despawn,
|
||||||
states::{AssetLoadState, GameplayState},
|
states::{AssetLoadState, GameplayState},
|
||||||
tags::MainCamera,
|
tags::MainCamera,
|
||||||
};
|
};
|
||||||
use world_generation::{hex_utils::HexCoord, map::map::Map};
|
use world_generation::{heightmap, hex_utils::HexCoord, map::map::Map};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
assets::{building_asset::BuildingAssetPlugin, building_database::BuildingDatabase},
|
assets::{
|
||||||
|
building_asset::{BuildingAsset, BuildingAssetPlugin},
|
||||||
|
building_database::BuildingDatabase,
|
||||||
|
},
|
||||||
build_queue::{BuildQueue, QueueEntry},
|
build_queue::{BuildQueue, QueueEntry},
|
||||||
|
buildings_map::BuildingMap,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct BuildingPugin;
|
pub struct BuildingPugin;
|
||||||
@@ -21,13 +25,14 @@ pub struct BuildingPugin;
|
|||||||
impl Plugin for BuildingPugin {
|
impl Plugin for BuildingPugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.insert_resource(BuildQueue::default());
|
app.insert_resource(BuildQueue::default());
|
||||||
|
app.init_resource::<BuildingMap>();
|
||||||
app.add_plugins(BuildingAssetPlugin);
|
app.add_plugins(BuildingAssetPlugin);
|
||||||
|
|
||||||
app.configure_loading_state(
|
app.configure_loading_state(
|
||||||
LoadingStateConfig::new(AssetLoadState::Loading).load_collection::<BuildingDatabase>(),
|
LoadingStateConfig::new(AssetLoadState::Loading).load_collection::<BuildingDatabase>(),
|
||||||
);
|
);
|
||||||
|
|
||||||
app.add_systems(Startup, init.run_if(in_state(AssetLoadState::Loading)));
|
app.add_systems(Update, init.run_if(in_state(AssetLoadState::Loading)));
|
||||||
app.add_systems(Update, hq_placement.run_if(in_state(GameplayState::PlaceHQ)));
|
app.add_systems(Update, hq_placement.run_if(in_state(GameplayState::PlaceHQ)));
|
||||||
|
|
||||||
app.add_systems(PreUpdate, process_build_queue.run_if(in_state(GameplayState::Playing)));
|
app.add_systems(PreUpdate, process_build_queue.run_if(in_state(GameplayState::Playing)));
|
||||||
@@ -104,4 +109,24 @@ fn show_indicators(positions: Vec<Vec3>, commands: &mut Commands, indicator: &In
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn process_build_queue(mut queue: ResMut<BuildQueue>) {}
|
fn process_build_queue(
|
||||||
|
mut queue: ResMut<BuildQueue>,
|
||||||
|
mut commands: Commands,
|
||||||
|
db: Res<BuildingDatabase>,
|
||||||
|
building_assets: Res<Assets<BuildingAsset>>,
|
||||||
|
heightmap: Res<Map>,
|
||||||
|
) {
|
||||||
|
for item in &queue.queue {
|
||||||
|
let handle = &db.buildings[item.building.0];
|
||||||
|
if let Some(building) = building_assets.get(handle.id()) {
|
||||||
|
let h = heightmap.sample_height(&item.pos);
|
||||||
|
println!("Spawning {} at {}", building.name, item.pos);
|
||||||
|
commands.spawn(SceneBundle {
|
||||||
|
scene: building.prefab.clone(),
|
||||||
|
transform: Transform::from_translation(item.pos.to_world(h)),
|
||||||
|
..Default::default()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
queue.queue.clear();
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use world_generation::hex_utils::HexCoord;
|
use world_generation::hex_utils::HexCoord;
|
||||||
|
|
||||||
#[derive(Resource)]
|
#[derive(Resource, Default)]
|
||||||
pub struct BuildingMap {
|
pub struct BuildingMap {
|
||||||
pub chunks: Vec<BuildingChunk>,
|
pub chunks: Vec<BuildingChunk>,
|
||||||
}
|
}
|
||||||
@@ -33,6 +33,7 @@ impl BuildingMap {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub struct BuildingChunk {
|
pub struct BuildingChunk {
|
||||||
pub entries: Vec<BuildingChunk>,
|
pub entries: Vec<BuildingChunk>,
|
||||||
pub index: usize,
|
pub index: usize,
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ impl Plugin for PhosGamePlugin {
|
|||||||
PhosCameraPlugin,
|
PhosCameraPlugin,
|
||||||
MapInitPlugin,
|
MapInitPlugin,
|
||||||
RenderDistancePlugin,
|
RenderDistancePlugin,
|
||||||
// BuildingPugin,
|
BuildingPugin,
|
||||||
DespawnPuglin,
|
DespawnPuglin,
|
||||||
));
|
));
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,28 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Default, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
pub struct BuildingIdentifier(u32);
|
pub struct BuildingIdentifier(pub usize);
|
||||||
|
|
||||||
|
impl From<i32> for BuildingIdentifier {
|
||||||
|
fn from(value: i32) -> Self {
|
||||||
|
return BuildingIdentifier(value as usize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<u32> for BuildingIdentifier {
|
impl From<u32> for BuildingIdentifier {
|
||||||
fn from(value: u32) -> Self {
|
fn from(value: u32) -> Self {
|
||||||
|
return BuildingIdentifier(value as usize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<usize> for BuildingIdentifier {
|
||||||
|
fn from(value: usize) -> Self {
|
||||||
return BuildingIdentifier(value);
|
return BuildingIdentifier(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Into<usize> for BuildingIdentifier {
|
||||||
|
fn into(self) -> usize {
|
||||||
|
return self.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user