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::*;
|
||||
@@ -12,9 +12,10 @@ bevy-inspector-egui = "0.23.4"
|
||||
iyes_perf_ui = "0.2.3"
|
||||
noise = "0.8.2"
|
||||
world_generation ={path="../../engine/world_generation"}
|
||||
bevy_rapier3d = { version = "0.25.0", features = [ "simd-stable","parallel" ] }
|
||||
bevy_rapier3d = { version = "0.26.0", features = [ "simd-stable","parallel" ] }
|
||||
rayon = "1.10.0"
|
||||
buildings = {path="../buildings"}
|
||||
shared = {path="../shared"}
|
||||
|
||||
[features]
|
||||
tracing = ["bevy/trace_tracy", "world_generation/tracing", "buildings/tracing"]
|
||||
@@ -2,6 +2,8 @@ use bevy::core_pipeline::experimental::taa::{TemporalAntiAliasBundle, TemporalAn
|
||||
use bevy::input::mouse::{MouseMotion, MouseScrollUnit, MouseWheel};
|
||||
use bevy::prelude::*;
|
||||
use bevy::window::CursorGrabMode;
|
||||
use shared::states::GameState;
|
||||
use shared::tags::MainCamera;
|
||||
use world_generation::hex_utils::HexCoord;
|
||||
use world_generation::prelude::Map;
|
||||
|
||||
@@ -15,7 +17,7 @@ impl Plugin for PhosCameraPlugin {
|
||||
|
||||
app.add_systems(PreStartup, setup);
|
||||
|
||||
app.add_systems(Update, rts_camera_system);
|
||||
app.add_systems(Update, rts_camera_system.run_if(in_state(GameState::Playing)));
|
||||
app.add_systems(PostUpdate, limit_camera_bounds);
|
||||
//Free Cam
|
||||
//app.add_systems(Update, (grab_mouse, (update_camera, update_camera_mouse).chain()));
|
||||
@@ -32,6 +34,7 @@ fn setup(mut commands: Commands, mut msaa: ResMut<Msaa>) {
|
||||
..default()
|
||||
},
|
||||
PhosCamera::default(),
|
||||
MainCamera,
|
||||
PhosCameraTargets::default(),
|
||||
))
|
||||
.insert(TemporalAntiAliasBundle::default());
|
||||
|
||||
@@ -3,6 +3,7 @@ use bevy::log::*;
|
||||
use bevy::{asset::LoadState, pbr::ExtendedMaterial, prelude::*};
|
||||
use bevy_inspector_egui::quick::ResourceInspectorPlugin;
|
||||
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
|
||||
use shared::states::{GameState, GameplayState};
|
||||
use world_generation::{
|
||||
biome_painter::*,
|
||||
heightmap::generate_heightmap,
|
||||
@@ -14,10 +15,10 @@ use world_generation::{
|
||||
|
||||
use crate::{
|
||||
camera_system::components::*,
|
||||
prelude::{ChunkAtlas, PhosChunk, PhosChunkRegistry, PhosMap},
|
||||
prelude::{ChunkAtlas, PhosChunk, PhosChunkRegistry},
|
||||
shader_extensions::chunk_material::ChunkMaterial,
|
||||
utlis::{
|
||||
chunk_utils::{paint_map, prepare_chunk_mesh, prepare_chunk_mesh_with_collider},
|
||||
chunk_utils::{paint_map, prepare_chunk_mesh_with_collider},
|
||||
render_distance_system::RenderDistanceVisibility,
|
||||
},
|
||||
};
|
||||
@@ -30,23 +31,46 @@ pub struct MapInitPlugin;
|
||||
|
||||
impl Plugin for MapInitPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.insert_state(GeneratorState::GenerateHeightmap);
|
||||
app.insert_state(AssetLoadState::StartLoading);
|
||||
|
||||
app.add_plugins((
|
||||
ResourceInspectorPlugin::<PhosMap>::default(),
|
||||
ResourceInspectorPlugin::<GenerationConfig>::default(),
|
||||
ChunkRebuildPlugin,
|
||||
TerraFormingTestPlugin,
|
||||
));
|
||||
|
||||
app.add_systems(Startup, (load_textures, load_tiles));
|
||||
app.add_systems(PostStartup, create_map);
|
||||
app.add_systems(Startup, (load_textures, load_tiles).in_set(AssetLoaderSet));
|
||||
app.add_systems(Startup, create_heightmap.in_set(GeneratorSet));
|
||||
|
||||
app.configure_sets(Startup, AssetLoaderSet.run_if(in_state(AssetLoadState::StartLoading)));
|
||||
app.configure_sets(
|
||||
Startup,
|
||||
GeneratorSet.run_if(in_state(GeneratorState::GenerateHeightmap)),
|
||||
);
|
||||
|
||||
app.add_systems(Update, check_asset_load.run_if(in_state(AssetLoadState::Loading)));
|
||||
app.add_systems(
|
||||
Update,
|
||||
finalize_texture.run_if(in_state(AssetLoadState::FinalizeAssets)),
|
||||
);
|
||||
app.add_systems(Update, despawn_map.run_if(in_state(GeneratorState::Regenerate)));
|
||||
app.add_systems(
|
||||
Update,
|
||||
spawn_map
|
||||
.run_if(in_state(AssetLoadState::LoadComplete))
|
||||
.run_if(in_state(GeneratorState::SpawnMap)),
|
||||
);
|
||||
|
||||
app.add_systems(Update, finalize_texture);
|
||||
app.add_systems(PostUpdate, (despawn_map, spawn_map).chain());
|
||||
app.insert_resource(TileManager::default());
|
||||
app.insert_resource(PhosMap::default());
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
|
||||
struct GeneratorSet;
|
||||
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
|
||||
struct AssetLoaderSet;
|
||||
|
||||
fn load_textures(
|
||||
mut commands: Commands,
|
||||
asset_server: Res<AssetServer>,
|
||||
@@ -67,26 +91,25 @@ fn load_textures(
|
||||
});
|
||||
}
|
||||
|
||||
fn load_tiles(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
fn load_tiles(
|
||||
mut commands: Commands,
|
||||
asset_server: Res<AssetServer>,
|
||||
mut next_state: ResMut<NextState<AssetLoadState>>,
|
||||
) {
|
||||
let handle: Handle<BiomePainterAsset> = asset_server.load("biome_painters/terra.biomes.json");
|
||||
commands.insert_resource(CurrentBiomePainter { handle });
|
||||
next_state.set(AssetLoadState::Loading);
|
||||
}
|
||||
|
||||
fn finalize_texture(
|
||||
fn check_asset_load(
|
||||
asset_server: Res<AssetServer>,
|
||||
mut atlas: ResMut<ChunkAtlas>,
|
||||
mut map: ResMut<PhosMap>,
|
||||
mut images: ResMut<Assets<Image>>,
|
||||
atlas: Res<ChunkAtlas>,
|
||||
painter: Res<CurrentBiomePainter>,
|
||||
painter_load: Res<BiomePainterLoadState>,
|
||||
tile_load: Res<TileAssetLoadState>,
|
||||
mut chunk_materials: ResMut<Assets<ExtendedMaterial<StandardMaterial, ChunkMaterial>>>,
|
||||
mapper_load: Res<TileMapperLoadState>,
|
||||
mut next_state: ResMut<NextState<AssetLoadState>>,
|
||||
) {
|
||||
if atlas.is_loaded {
|
||||
return;
|
||||
}
|
||||
|
||||
if !painter_load.is_all_loaded() || !tile_load.is_all_loaded() || !mapper_load.is_all_loaded() {
|
||||
return;
|
||||
}
|
||||
@@ -97,6 +120,16 @@ fn finalize_texture(
|
||||
if asset_server.load_state(painter.handle.clone()) != LoadState::Loaded {
|
||||
return;
|
||||
}
|
||||
|
||||
next_state.set(AssetLoadState::FinalizeAssets);
|
||||
}
|
||||
|
||||
fn finalize_texture(
|
||||
mut atlas: ResMut<ChunkAtlas>,
|
||||
mut images: ResMut<Assets<Image>>,
|
||||
mut chunk_materials: ResMut<Assets<ExtendedMaterial<StandardMaterial, ChunkMaterial>>>,
|
||||
mut next_state: ResMut<NextState<AssetLoadState>>,
|
||||
) {
|
||||
let image = images.get_mut(&atlas.handle).unwrap();
|
||||
|
||||
let array_layers = 14;
|
||||
@@ -110,11 +143,15 @@ fn finalize_texture(
|
||||
},
|
||||
});
|
||||
atlas.chunk_material_handle = chunk_material;
|
||||
map.ready = true;
|
||||
map.regenerate = true;
|
||||
|
||||
next_state.set(AssetLoadState::LoadComplete);
|
||||
}
|
||||
|
||||
fn create_map(mut commands: Commands, mut cam: Query<(&mut Transform, Entity), With<PhosCamera>>) {
|
||||
fn create_heightmap(
|
||||
mut commands: Commands,
|
||||
mut cam: Query<(&mut Transform, Entity), With<PhosCamera>>,
|
||||
mut next_state: ResMut<NextState<GeneratorState>>,
|
||||
) {
|
||||
let config = GenerationConfig {
|
||||
layers: vec![
|
||||
GeneratorLayer {
|
||||
@@ -193,6 +230,7 @@ fn create_map(mut commands: Commands, mut cam: Query<(&mut Transform, Entity), W
|
||||
|
||||
commands.insert_resource(heightmap);
|
||||
commands.insert_resource(config);
|
||||
next_state.set(GeneratorState::SpawnMap);
|
||||
}
|
||||
|
||||
fn spawn_map(
|
||||
@@ -200,16 +238,15 @@ fn spawn_map(
|
||||
mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
atlas: Res<ChunkAtlas>,
|
||||
mut map: ResMut<PhosMap>,
|
||||
tile_assets: Res<Assets<TileAsset>>,
|
||||
tile_mappers: Res<Assets<TileMapperAsset>>,
|
||||
biome_painters: Res<Assets<BiomePainterAsset>>,
|
||||
painter: Res<CurrentBiomePainter>,
|
||||
mut generator_state: ResMut<NextState<GeneratorState>>,
|
||||
cur_game_state: Res<State<GameState>>,
|
||||
mut game_state: ResMut<NextState<GameState>>,
|
||||
mut gameplay_state: ResMut<NextState<GameplayState>>,
|
||||
) {
|
||||
if !map.ready || !map.regenerate {
|
||||
return;
|
||||
}
|
||||
map.regenerate = false;
|
||||
let b_painter = biome_painters.get(painter.handle.clone());
|
||||
let cur_painter = b_painter.unwrap();
|
||||
paint_map(&mut heightmap, cur_painter, &tile_assets, &tile_mappers);
|
||||
@@ -224,7 +261,6 @@ fn spawn_map(
|
||||
.collect();
|
||||
|
||||
let mut registry = PhosChunkRegistry::new(chunk_meshes.len());
|
||||
|
||||
{
|
||||
#[cfg(feature = "tracing")]
|
||||
let _spawn_span = info_span!("Spawn Chunks").entered();
|
||||
@@ -262,21 +298,24 @@ fn spawn_map(
|
||||
},));
|
||||
|
||||
commands.insert_resource(registry);
|
||||
generator_state.set(GeneratorState::Idle);
|
||||
if cur_game_state.get() != &GameState::Playing {
|
||||
game_state.set(GameState::Playing);
|
||||
gameplay_state.set(GameplayState::PlaceHQ);
|
||||
}
|
||||
}
|
||||
|
||||
fn despawn_map(
|
||||
mut commands: Commands,
|
||||
mut heightmap: ResMut<Map>,
|
||||
cfg: Res<GenerationConfig>,
|
||||
map: Res<PhosMap>,
|
||||
chunks: Query<Entity, With<PhosChunk>>,
|
||||
mut next_state: ResMut<NextState<GeneratorState>>,
|
||||
) {
|
||||
if !map.regenerate {
|
||||
return;
|
||||
}
|
||||
for chunk in chunks.iter() {
|
||||
commands.entity(chunk).despawn();
|
||||
}
|
||||
|
||||
*heightmap = generate_heightmap(&cfg, 4);
|
||||
next_state.set(GeneratorState::SpawnMap);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use bevy::{prelude::*, window::PrimaryWindow};
|
||||
use bevy_rapier3d::{pipeline::QueryFilter, plugin::RapierContext};
|
||||
use world_generation::{hex_utils::HexCoord, prelude::Map};
|
||||
use world_generation::{hex_utils::HexCoord, prelude::Map, states::GeneratorState};
|
||||
|
||||
use crate::{
|
||||
camera_system::components::PhosCamera,
|
||||
@@ -11,7 +11,7 @@ pub struct TerraFormingTestPlugin;
|
||||
|
||||
impl Plugin for TerraFormingTestPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Update, deform);
|
||||
app.add_systems(Update, deform.run_if(in_state(GeneratorState::Idle)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,8 +35,6 @@ fn deform(
|
||||
return;
|
||||
}
|
||||
|
||||
#[cfg(feature = "tracing")]
|
||||
let span = info_span!("Deform Mesh").entered();
|
||||
let win = window.single();
|
||||
let (cam_transform, camera) = cam_query.single();
|
||||
let Some(cursor_pos) = win.cursor_position() else {
|
||||
@@ -50,12 +48,14 @@ fn deform(
|
||||
let collision = rapier_context.cast_ray(
|
||||
cam_ray.origin,
|
||||
cam_ray.direction.into(),
|
||||
100.,
|
||||
500.,
|
||||
true,
|
||||
QueryFilter::only_fixed(),
|
||||
);
|
||||
|
||||
if let Some((e, dist)) = collision {
|
||||
#[cfg(feature = "tracing")]
|
||||
let span = info_span!("Deform Mesh").entered();
|
||||
let contact_point = cam_ray.get_point(dist);
|
||||
let contact_coord = HexCoord::from_world_pos(contact_point);
|
||||
let modified_chunks = heightmap.create_crater(&contact_coord, 5, 5. * multi);
|
||||
|
||||
@@ -12,7 +12,10 @@ use bevy::{
|
||||
use bevy_rapier3d::dynamics::{Ccd, RigidBody, Velocity};
|
||||
use bevy_rapier3d::geometry::Collider;
|
||||
use bevy_rapier3d::plugin::{NoUserData, RapierPhysicsPlugin};
|
||||
use buildings::BuildingPugin;
|
||||
use iyes_perf_ui::prelude::*;
|
||||
use shared::despawn::DespawnPuglin;
|
||||
use shared::states::{GameState, GameplayState};
|
||||
use world_generation::biome_painter::BiomePainterPlugin;
|
||||
use world_generation::tile_manager::TileAssetPlugin;
|
||||
use world_generation::tile_mapper::TileMapperAssetPlugin;
|
||||
@@ -26,8 +29,13 @@ impl Plugin for PhosGamePlugin {
|
||||
MapInitPlugin,
|
||||
MaterialPlugin::<ExtendedMaterial<StandardMaterial, ChunkMaterial>>::default(),
|
||||
RenderDistancePlugin,
|
||||
BuildingPugin,
|
||||
DespawnPuglin,
|
||||
));
|
||||
|
||||
app.insert_state(GameState::Startup);
|
||||
app.insert_state(GameplayState::Waiting);
|
||||
|
||||
//Systems - Startup
|
||||
app.add_systems(Startup, init_game);
|
||||
|
||||
|
||||
@@ -14,13 +14,6 @@ pub struct ChunkAtlas {
|
||||
pub is_loaded: bool,
|
||||
}
|
||||
|
||||
#[derive(Resource, Default, Reflect)]
|
||||
#[reflect(Resource)]
|
||||
pub struct PhosMap {
|
||||
pub ready: bool,
|
||||
pub regenerate: bool,
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct PhosChunk {
|
||||
pub index: usize,
|
||||
|
||||
13
game/shared/Cargo.toml
Normal file
13
game/shared/Cargo.toml
Normal file
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "shared"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
bevy = "0.13.2"
|
||||
|
||||
|
||||
[features]
|
||||
tracing = []
|
||||
47
game/shared/src/despawn.rs
Normal file
47
game/shared/src/despawn.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
use crate::states::GameState;
|
||||
use bevy::prelude::*;
|
||||
|
||||
pub struct DespawnPuglin;
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct DespawnAt(f32);
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct DespawnAfter(Timer);
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Despawn;
|
||||
|
||||
impl Plugin for DespawnPuglin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(PostUpdate, despawn_at);
|
||||
app.add_systems(
|
||||
PreUpdate,
|
||||
(despawn, despawn_after).run_if(not(in_state(GameState::Paused))),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn despawn_at(mut commands: Commands, time: Res<Time>, entities: Query<(Entity, &DespawnAt), Without<DespawnAfter>>) {
|
||||
for (entity, at) in entities.iter() {
|
||||
let d = at.0 - time.elapsed_seconds();
|
||||
commands
|
||||
.entity(entity)
|
||||
.insert(DespawnAfter(Timer::from_seconds(d, TimerMode::Once)));
|
||||
}
|
||||
}
|
||||
|
||||
fn despawn_after(mut commands: Commands, mut entities: Query<(&mut DespawnAfter, Entity)>, time: Res<Time>) {
|
||||
for (mut after, entity) in &mut entities.iter_mut() {
|
||||
after.0.tick(time.delta());
|
||||
if after.0.finished() {
|
||||
commands.entity(entity).despawn();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn despawn(mut commands: Commands, entities: Query<Entity, With<Despawn>>) {
|
||||
for entity in entities.iter() {
|
||||
commands.entity(entity).despawn();
|
||||
}
|
||||
}
|
||||
3
game/shared/src/lib.rs
Normal file
3
game/shared/src/lib.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
pub mod despawn;
|
||||
pub mod states;
|
||||
pub mod tags;
|
||||
17
game/shared/src/states.rs
Normal file
17
game/shared/src/states.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
#[derive(States, Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum GameState {
|
||||
Startup,
|
||||
MainMenu,
|
||||
Loading,
|
||||
Playing,
|
||||
Paused,
|
||||
}
|
||||
|
||||
#[derive(States, Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum GameplayState {
|
||||
Waiting,
|
||||
PlaceHQ,
|
||||
Playing,
|
||||
}
|
||||
3
game/shared/src/tags.rs
Normal file
3
game/shared/src/tags.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
use bevy::prelude::*;
|
||||
#[derive(Component)]
|
||||
pub struct MainCamera;
|
||||
Reference in New Issue
Block a user