Skip to content

Commit

Permalink
Structure spawns and other components
Browse files Browse the repository at this point in the history
This splits up the spawning components quite a bit so that it's in
multiple modules instead of 1, which means it's a bit easier to read.

In addition to that I'm shuffling some other code around and adding a
new spawnable for structures, which are _slightly_ different from the
tdlg ones, which I might want to fix up at some point in time.
  • Loading branch information
derrickp committed Mar 29, 2023
1 parent 5fc53f7 commit e37d599
Show file tree
Hide file tree
Showing 26 changed files with 217 additions and 76 deletions.
3 changes: 2 additions & 1 deletion src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod zones;
pub use grid_box::GridBox;
pub use map::Map;
pub use spawns::{
CharacterSpawnable, CharacterSpawns, MapSpawnable, MapSpawns, SpawnCoordinate, ZoneSpawnable,
CharacterSpawnable, CharacterSpawns, MapSpawns, SpawnCoordinate, StructureSpawnType,
StructureSpawnable, StructureSpawns, TdlgSpawnable, ZoneSpawnable,
};
pub use world::World;
43 changes: 0 additions & 43 deletions src/components/spawns.rs

This file was deleted.

21 changes: 21 additions & 0 deletions src/components/spawns/character_spawns.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use bevy::prelude::Component;

use crate::components::characters::CreatureType;

use super::SpawnCoordinate;

#[derive(Component)]
pub struct CharacterSpawns {
pub spawnables: Vec<CharacterSpawnable>,
}

impl CharacterSpawns {
pub fn clear(&mut self) {
self.spawnables.clear();
}
}

pub struct CharacterSpawnable {
pub spawn_type: CreatureType,
pub coordinate: SpawnCoordinate,
}
16 changes: 16 additions & 0 deletions src/components/spawns/map_spawns.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use bevy::prelude::Component;

use super::{TdlgSpawnable, ZoneSpawnable};

#[derive(Component)]
pub struct MapSpawns {
pub tdlg_spawnables: Vec<TdlgSpawnable>,
pub zone_spawnables: Vec<ZoneSpawnable>,
}

impl MapSpawns {
pub fn clear(&mut self) {
self.tdlg_spawnables.clear();
self.zone_spawnables.clear();
}
}
13 changes: 13 additions & 0 deletions src/components/spawns/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mod character_spawns;
mod map_spawns;
mod spawn_coordinate;
mod structure_spawns;
mod tdlg_spawnable;
mod zone_spawnable;

pub use character_spawns::{CharacterSpawnable, CharacterSpawns};
pub use map_spawns::MapSpawns;
pub use spawn_coordinate::SpawnCoordinate;
pub use structure_spawns::{StructureSpawnType, StructureSpawnable, StructureSpawns};
pub use tdlg_spawnable::TdlgSpawnable;
pub use zone_spawnable::ZoneSpawnable;
7 changes: 7 additions & 0 deletions src/components/spawns/spawn_coordinate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use tdlg::map::cells::Coordinate;

#[derive(Clone, Debug)]
pub struct SpawnCoordinate {
pub coordinate: Coordinate,
pub z_level: f32,
}
26 changes: 26 additions & 0 deletions src/components/spawns/structure_spawns.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use bevy::prelude::{Component, Visibility};

use super::SpawnCoordinate;

#[derive(Component, Debug, Clone)]
pub struct StructureSpawns {
pub spawnables: Vec<StructureSpawnable>,
}

impl StructureSpawns {
pub fn clear(&mut self) {
self.spawnables.clear();
}
}

#[derive(Clone, Debug)]
pub struct StructureSpawnable {
pub spawn_type: StructureSpawnType,
pub spawn_coordinate: SpawnCoordinate,
pub visibility: Visibility,
}

#[derive(Clone, Copy, Debug)]
pub enum StructureSpawnType {
StorageArea,
}
10 changes: 10 additions & 0 deletions src/components/spawns/tdlg_spawnable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use bevy::prelude::Visibility;
use tdlg::map::layers::LayerType;

use super::SpawnCoordinate;

pub struct TdlgSpawnable {
pub layer_type: LayerType,
pub spawn_coordinate: SpawnCoordinate,
pub visibility: Visibility,
}
8 changes: 8 additions & 0 deletions src/components/spawns/zone_spawnable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::components::zones::ZoneType;

use super::SpawnCoordinate;

pub struct ZoneSpawnable {
pub spawn_coordinate: SpawnCoordinate,
pub zone_type: ZoneType,
}
6 changes: 2 additions & 4 deletions src/components/structures/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
mod body;
mod mineable;
mod mining_target;
mod setup_storage_area;
mod storage_area;
mod structure;

pub use body::GridBody;
pub use mineable::Mineable;
pub use mining_target::MiningTarget;
pub use setup_storage_area::SetupStorageArea;
pub use storage_area::StorageArea;
pub use structure::Structure;
4 changes: 4 additions & 0 deletions src/components/structures/storage_area.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use bevy::prelude::Component;

#[derive(Clone, Component, Debug)]
pub struct StorageArea {}
File renamed without changes.
4 changes: 4 additions & 0 deletions src/components/tasks/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
mod mining_target;
mod setup_storage_area;
mod task;
mod todo;

pub use mining_target::MiningTarget;
pub use setup_storage_area::SetupStorageArea;
pub use task::Task;
pub use todo::Todo;
File renamed without changes.
2 changes: 1 addition & 1 deletion src/components/tasks/task.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::components::{
movement::{ExplorationTarget, Path},
structures::{MiningTarget, SetupStorageArea},
tasks::{MiningTarget, SetupStorageArea},
};

#[derive(Clone)]
Expand Down
1 change: 0 additions & 1 deletion src/components/zones/zone_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ use bevy::prelude::Component;
pub enum ZoneType {
Exploration,
SetupStorageArea,
StorageArea,
}
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ fn main() {
let finishing_set = (
systems::characters::show_in_visible_area,
systems::spawns::characters,
systems::spawns::structures,
systems::spawns::map,
systems::spawns::clear
.after(systems::spawns::characters)
Expand All @@ -68,7 +69,7 @@ fn main() {

let starting_spawns = (
systems::targets::spawn,
systems::map::spawn_starting,
systems::init::spawn_starting,
systems::camera::spawn_camera,
)
.after(StartupSets::TextureAtlas)
Expand Down
17 changes: 12 additions & 5 deletions src/systems/map.rs → src/systems/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bevy::prelude::{Commands, Query, ResMut, Visibility};
use crate::{
components::{
jobs::ExplorationHistory, movement::CameraMoveTimer, CharacterSpawnable, CharacterSpawns,
Map, MapSpawnable, MapSpawns, SpawnCoordinate, World,
Map, MapSpawns, SpawnCoordinate, StructureSpawns, TdlgSpawnable, World,
},
resources::config::GameConfiguration,
};
Expand All @@ -13,6 +13,7 @@ pub fn spawn_starting(
mut game_config: ResMut<GameConfiguration>,
mut character_spawns_query: Query<&mut CharacterSpawns>,
mut map_spawns_query: Query<&mut MapSpawns>,
structure_spawns_query: Query<&StructureSpawns>,
) {
commands.spawn(World::default());
commands.spawn(CameraMoveTimer {
Expand All @@ -22,10 +23,10 @@ pub fn spawn_starting(
commands.spawn(ExplorationHistory::default());

let top_down_map = game_config.generate_top_down_map();
let mut map_spawnables: Vec<MapSpawnable> = Vec::new();
let mut tdlg_spawnables: Vec<TdlgSpawnable> = Vec::new();
for cell in top_down_map.grid().cells() {
for (index, layer) in cell.layers().iter().enumerate() {
map_spawnables.push(MapSpawnable {
tdlg_spawnables.push(TdlgSpawnable {
layer_type: *layer,
spawn_coordinate: SpawnCoordinate {
coordinate: *cell.coordinate(),
Expand Down Expand Up @@ -53,15 +54,21 @@ pub fn spawn_starting(
};

match map_spawns_query.get_single_mut() {
Ok(mut map_spawns) => map_spawns.map_spawnables.append(&mut map_spawnables),
Ok(mut map_spawns) => map_spawns.tdlg_spawnables.append(&mut tdlg_spawnables),
Err(_) => {
commands.spawn(MapSpawns {
map_spawnables,
tdlg_spawnables,
zone_spawnables: Vec::new(),
});
}
};

if let Err(_) = structure_spawns_query.get_single() {
commands.spawn(StructureSpawns {
spawnables: Vec::new(),
});
}

commands.spawn(Map {
current: top_down_map,
tile_size: game_config.tile_size(),
Expand Down
2 changes: 1 addition & 1 deletion src/systems/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod camera;
pub mod characters;
pub mod init;
pub mod jobs;
pub mod map;
pub mod spawns;
pub mod targets;
pub mod tasks;
Expand Down
9 changes: 7 additions & 2 deletions src/systems/spawns/clear.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
use bevy::prelude::Query;

use crate::components::{CharacterSpawns, MapSpawns};
use crate::components::{CharacterSpawns, MapSpawns, StructureSpawns};

pub fn clear(
mut character_spawns_query: Query<&mut CharacterSpawns>,
mut map_spawns_query: Query<&mut MapSpawns>,
mut structure_spawns_query: Query<&mut StructureSpawns>,
) {
if let Ok(mut character_spawns) = character_spawns_query.get_single_mut() {
character_spawns.spawnables.clear();
character_spawns.clear();
}

if let Ok(mut map_spawns) = map_spawns_query.get_single_mut() {
map_spawns.clear();
}

if let Ok(mut structure_spawns) = structure_spawns_query.get_single_mut() {
structure_spawns.clear();
}
}
6 changes: 2 additions & 4 deletions src/systems/spawns/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ pub fn map(
let key = match spawnable.zone_type {
ZoneType::Exploration => "exploration",
ZoneType::SetupStorageArea => "setup_storage",
ZoneType::StorageArea => "storage",
};

if let Some(zone_config) = game_config.zone_config(key) {
Expand All @@ -63,12 +62,11 @@ pub fn map(
})
.insert(GridBody {
center_coordinate: spawnable.spawn_coordinate.coordinate,
})
.insert(ZoneType::StorageArea);
});
}
}

for spawnable in map_spawns.map_spawnables.iter() {
for spawnable in map_spawns.tdlg_spawnables.iter() {
let coordinate = world_coordinate_from_grid(
&spawnable.spawn_coordinate.coordinate,
map.grid_size,
Expand Down
2 changes: 2 additions & 0 deletions src/systems/spawns/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod characters;
mod clear;
mod map;
mod structures;

pub use characters::characters;
pub use clear::clear;
pub use map::map;
pub use structures::structures;
Loading

0 comments on commit e37d599

Please sign in to comment.