Skip to content

Commit

Permalink
Add storage area zone type
Browse files Browse the repository at this point in the history
This constitutes both a placeable zone for setting up a storage area,
and the actual storage area itself.

I probably want to split those apart at some point so that the zones
and the targets are not the same thing.
  • Loading branch information
derrickp committed Mar 28, 2023
1 parent 6cdc3f8 commit 5fc53f7
Show file tree
Hide file tree
Showing 25 changed files with 264 additions and 53 deletions.
4 changes: 0 additions & 4 deletions assets/config/game.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
"wait_time": 0.3
}
},
"mouse_target": {
"key": "target",
"path": "sprites/outline.png"
},
"zone": {
"key": "zone",
"path": "sprites/zone.png"
Expand Down
37 changes: 37 additions & 0 deletions assets/config/zones.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"zones": [
{
"key": "exploration",
"target": {
"key": "target",
"path": "sprites/zones/exploration_target.png"
},
"overlay": {
"key": "overlay",
"path": "sprites/zones/exploration_overlay.png"
}
},
{
"key": "setup_storage",
"target": {
"key": "target",
"path": "sprites/zones/setup_storage_target.png"
},
"overlay": {
"key": "overlay",
"path": "sprites/zones/setup_storage_overlay.png"
}
},
{
"key": "storage",
"target": {
"key": "target",
"path": "sprites/zones/storage_target.png"
},
"overlay": {
"key": "overlay",
"path": "sprites/zones/storage_overlay.png"
}
}
]
}
Binary file modified assets/sprites/zones/storage_overlay.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ pub mod zones;

pub use grid_box::GridBox;
pub use map::Map;
pub use spawns::{CharacterSpawnable, CharacterSpawns, MapSpawnable, MapSpawns, SpawnCoordinate};
pub use spawns::{
CharacterSpawnable, CharacterSpawns, MapSpawnable, MapSpawns, SpawnCoordinate, ZoneSpawnable,
};
pub use world::World;
17 changes: 15 additions & 2 deletions src/components/spawns.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bevy::prelude::{Component, Visibility};
use tdlg::map::{cells::Coordinate, layers::LayerType};

use super::characters::CreatureType;
use super::{characters::CreatureType, zones::ZoneType};

#[derive(Component)]
pub struct CharacterSpawns {
Expand All @@ -10,7 +10,20 @@ pub struct CharacterSpawns {

#[derive(Component)]
pub struct MapSpawns {
pub spawnables: Vec<MapSpawnable>,
pub map_spawnables: Vec<MapSpawnable>,
pub zone_spawnables: Vec<ZoneSpawnable>,
}

impl MapSpawns {
pub fn clear(&mut self) {
self.map_spawnables.clear();
self.zone_spawnables.clear();
}
}

pub struct ZoneSpawnable {
pub spawn_coordinate: SpawnCoordinate,
pub zone_type: ZoneType,
}

pub struct SpawnCoordinate {
Expand Down
2 changes: 2 additions & 0 deletions src/components/structures/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
mod body;
mod mineable;
mod mining_target;
mod setup_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 structure::Structure;
7 changes: 7 additions & 0 deletions src/components/structures/setup_storage_area.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use tdlg::map::cells::Coordinate;

#[derive(Clone)]
pub struct SetupStorageArea {
pub done: bool,
pub coordinate: Coordinate,
}
4 changes: 3 additions & 1 deletion src/components/tasks/task.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::components::{
movement::{ExplorationTarget, Path},
structures::MiningTarget,
structures::{MiningTarget, SetupStorageArea},
};

#[derive(Clone)]
pub enum Task {
Walk(Path),
Mine(MiningTarget),
ClearExplorationTarget(ExplorationTarget),
SetupStorageArea(SetupStorageArea),
}

impl Task {
Expand All @@ -19,6 +20,7 @@ impl Task {
.all(|visited_point| visited_point.visited),
Task::Mine(target) => target.entity.is_none(),
Task::ClearExplorationTarget(target) => target.entity.is_none(),
Task::SetupStorageArea(setup) => setup.done,
}
}
}
2 changes: 2 additions & 0 deletions src/components/zones/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod zone;
mod zone_type;

pub use zone::Zone;
pub use zone_type::ZoneType;
3 changes: 3 additions & 0 deletions src/components/zones/zone.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use bevy::prelude::Component;

/// Zones can be placed by the player to mark a place on the map
/// for the characters to go and do something, either clear as an
/// exploration target or set up something.
#[derive(Component)]
pub struct Zone;
8 changes: 8 additions & 0 deletions src/components/zones/zone_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use bevy::prelude::Component;

#[derive(Component, Clone, Debug, Copy)]
pub enum ZoneType {
Exploration,
SetupStorageArea,
StorageArea,
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ fn main() {
systems::camera::process_movement_input,
systems::camera::zoom_camera,
systems::targets::move_target,
systems::targets::swap_targets,
)
.in_set(OnUpdate(AppState::InGame))
.in_set(Sets::Input);
Expand Down
20 changes: 7 additions & 13 deletions src/resources/config/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use tdlg::{generation::Generator, map::TopDownMap};

use super::{
timers::WorldTickTimerConfig, CameraConfig, CharacterConfig, MovementConfig, MovementTimer,
SingleSprite, SpriteGroup, SpriteTileStats, StructureConfig, WorldTickTimer,
SingleSprite, SpriteGroup, SpriteTileStats, StructureConfig, WorldTickTimer, ZoneConfig,
ZonesConfig,
};

#[derive(Debug, Resource)]
Expand All @@ -17,6 +18,7 @@ pub struct GameConfiguration {
pub structures: Vec<StructureConfig>,
pub characters: Vec<CharacterConfig>,
pub camera: CameraConfig,
pub zones_config: ZonesConfig,
generator: Generator,
}

Expand All @@ -27,6 +29,7 @@ impl GameConfiguration {
structures: Vec<StructureConfig>,
characters: Vec<CharacterConfig>,
camera: CameraConfig,
zones: ZonesConfig,
) -> Self {
let generator = tdlg::generation::builder()
.seed(&basics.grid_generation.seed)
Expand All @@ -41,6 +44,7 @@ impl GameConfiguration {
characters,
camera,
basics,
zones_config: zones,
}
}

Expand Down Expand Up @@ -109,12 +113,8 @@ impl GameConfiguration {
self.camera.zoom_in_level(current)
}

pub fn mouse_target(&self) -> &SingleSprite {
&self.basics.mouse_target
}

pub fn zone(&self) -> &SingleSprite {
&self.basics.zone
pub fn zone_config(&self, key: &str) -> Option<&ZoneConfig> {
self.zones_config.zones.iter().find(|zone| zone.key.eq(key))
}
}

Expand All @@ -123,7 +123,6 @@ pub struct GameBasics {
tiles: SpriteTileStats,
grid_generation: GridGeneration,
movement: MovementConfig,
mouse_target: SingleSprite,
zone: SingleSprite,
world: WorldTickTimerConfig,
}
Expand Down Expand Up @@ -171,11 +170,6 @@ mod tests {
movement: MovementConfig {
timer: MovementTimerConfig { wait_time: 0.2 },
},
mouse_target: SingleSprite {
key: "target".to_string(),
path: "outline.png".to_string(),
tile_stats: None,
},
zone: SingleSprite {
key: "zone".to_string(),
path: "zone.png".to_string(),
Expand Down
12 changes: 11 additions & 1 deletion src/resources/config/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::io;

use super::{
game::{GameBasics, GameConfiguration},
CameraConfig, CharacterConfig, SpriteGroup, StructureConfig,
CameraConfig, CharacterConfig, SpriteGroup, StructureConfig, ZonesConfig,
};

#[derive(Debug)]
Expand Down Expand Up @@ -51,13 +51,19 @@ pub fn camera_json() -> &'static str {
include_str!("../../../assets/config/camera.json")
}

#[cfg(feature = "embedded")]
pub fn zones_json() -> &'static str {
include_str!("../../../assets/config/zones.json")
}

#[cfg(feature = "embedded")]
pub fn load_game_configuration() -> Result<GameConfiguration, LoadError> {
let basics: GameBasics = serde_json::from_str(game_json())?;
let floor_sprites: Vec<SpriteGroup> = serde_json::from_str(floors_json())?;
let structures: Vec<StructureConfig> = serde_json::from_str(structures_json())?;
let characters: Vec<CharacterConfig> = serde_json::from_str(characters_json())?;
let camera: CameraConfig = serde_json::from_str(camera_json())?;
let zones: ZonesConfig = serde_json::from_str(zones_json())?;

Ok(GameConfiguration::new(
basics,
Expand Down Expand Up @@ -85,11 +91,15 @@ pub fn load_game_configuration() -> Result<GameConfiguration, LoadError> {
let camera_text = std::fs::read_to_string("./assets/config/camera.json")?;
let camera: CameraConfig = serde_json::from_str(&camera_text)?;

let zone_text = std::fs::read_to_string("./assets/config/zones.json")?;
let zones: ZonesConfig = serde_json::from_str(&zone_text)?;

Ok(GameConfiguration::new(
basics,
floor_sprites,
structures,
characters,
camera,
zones,
))
}
2 changes: 2 additions & 0 deletions src/resources/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod movement;
mod sprites;
mod structures;
mod timers;
mod zones;

pub use camera::{CameraConfig, ZoomLevel};
pub use characters::CharacterConfig;
Expand All @@ -16,3 +17,4 @@ pub use movement::MovementConfig;
pub use sprites::{SingleSprite, SpriteGroup, SpriteLayerType, SpriteTileStats};
pub use structures::{HealthConfig, HealthRange, StructureConfig};
pub use timers::{MovementTimer, MovementTimerConfig, WorldTickTimer, WorldTickTimerConfig};
pub use zones::{ZoneConfig, ZonesConfig};
15 changes: 15 additions & 0 deletions src/resources/config/zones.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use serde::{Deserialize, Serialize};

use super::SingleSprite;

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ZoneConfig {
pub key: String,
pub target: SingleSprite,
pub overlay: SingleSprite,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ZonesConfig {
pub zones: Vec<ZoneConfig>,
}
5 changes: 3 additions & 2 deletions src/systems/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ pub fn spawn_starting(
};

match map_spawns_query.get_single_mut() {
Ok(mut map_spawns) => map_spawns.spawnables.append(&mut map_spawnables),
Ok(mut map_spawns) => map_spawns.map_spawnables.append(&mut map_spawnables),
Err(_) => {
commands.spawn(MapSpawns {
spawnables: map_spawnables,
map_spawnables,
zone_spawnables: Vec::new(),
});
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/systems/spawns/clear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ pub fn clear(
}

if let Ok(mut map_spawns) = map_spawns_query.get_single_mut() {
map_spawns.spawnables.clear();
map_spawns.clear();
}
}
45 changes: 43 additions & 2 deletions src/systems/spawns/map.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use bevy::{
prelude::{default, info, AssetServer, Commands, Query, Res, Transform, Vec3},
prelude::{default, info, AssetServer, Commands, Query, Res, Transform, Vec3, Visibility},
sprite::{SpriteSheetBundle, TextureAtlasSprite},
};
use tdlg::map::layers::{FloorType, LayerType, StructureType};

use crate::{
components::{
structures::{GridBody, Mineable, Structure},
zones::ZoneType,
Map, MapSpawns,
},
resources::{
Expand All @@ -27,7 +28,47 @@ pub fn map(
return;
};

for spawnable in map_spawns.spawnables.iter() {
for spawnable in map_spawns.zone_spawnables.iter() {
let coordinate = world_coordinate_from_grid(
&spawnable.spawn_coordinate.coordinate,
map.grid_size,
map.tile_size,
);
let transform = Transform {
translation: Vec3::new(
coordinate.x,
coordinate.y,
spawnable.spawn_coordinate.z_level,
),
scale: Vec3::splat(game_config.tile_scale()),
..default()
};

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) {
let handle = asset_server.get_handle(&zone_config.overlay.path);
let texture_index = atlas.texture_atlas.get_texture_index(&handle).unwrap();
commands
.spawn(SpriteSheetBundle {
transform,
sprite: TextureAtlasSprite::new(texture_index),
texture_atlas: atlas.atlas_handle.clone(),
visibility: Visibility::Inherited,
..default()
})
.insert(GridBody {
center_coordinate: spawnable.spawn_coordinate.coordinate,
})
.insert(ZoneType::StorageArea);
}
}

for spawnable in map_spawns.map_spawnables.iter() {
let coordinate = world_coordinate_from_grid(
&spawnable.spawn_coordinate.coordinate,
map.grid_size,
Expand Down
Loading

0 comments on commit 5fc53f7

Please sign in to comment.