| 1 |
use std::collections::HashSet; |
| 2 |
|
| 3 |
use bevy::prelude::*; |
| 4 |
|
| 5 |
use crate::{ |
| 6 |
selection::{Cursor, update_cursor}, |
| 7 |
world::{TILE_SIZE, WorldMap}, |
| 8 |
worldgen::{WorldGenerated, spawn_tiles}, |
| 9 |
}; |
| 10 |
|
| 11 |
pub struct ZonesPlugin; |
| 12 |
|
| 13 |
impl Plugin for ZonesPlugin { |
| 14 |
fn build(&self, app: &mut App) { |
| 15 |
app.init_resource::<Zones>() |
| 16 |
.init_resource::<ZoneTool>() |
| 17 |
.add_systems( |
| 18 |
Update, |
| 19 |
place_starting_stockpile.run_if(on_message::<WorldGenerated>), |
| 20 |
) |
| 21 |
.add_systems( |
| 22 |
PostUpdate, |
| 23 |
(zone_tool, draw_zone_preview).chain().after(update_cursor), |
| 24 |
); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
const STARTING_STOCKPILE_OFFSET: IVec2 = IVec2::new(2, -2); |
| 29 |
const STARTING_STOCKPILE_SIZE: IVec2 = IVec2::new(4, 4); |
| 30 |
const PREVIEW_ADD_COLOR: Color = Color::srgba(0.4, 0.7, 1.0, 0.9); |
| 31 |
const PREVIEW_REMOVE_COLOR: Color = Color::srgba(1.0, 0.4, 0.4, 0.9); |
| 32 |
|
| 33 |
#[derive(Resource, Default, Debug, Clone, PartialEq)] |
| 34 |
pub struct Zones { |
| 35 |
stockpile: HashSet<UVec2>, |
| 36 |
} |
| 37 |
|
| 38 |
impl Zones { |
| 39 |
pub fn is_stockpile(&self, tile: UVec2) -> bool { |
| 40 |
self.stockpile.contains(&tile) |
| 41 |
} |
| 42 |
|
| 43 |
#[cfg(test)] |
| 44 |
pub fn stockpile_tiles(&self) -> impl Iterator<Item = UVec2> + '_ { |
| 45 |
self.stockpile.iter().copied() |
| 46 |
} |
| 47 |
|
| 48 |
pub fn add_stockpile(&mut self, map: &WorldMap, a: IVec2, b: IVec2) { |
| 49 |
for tile in rect_tiles(a, b) { |
| 50 |
if can_zone(map, tile) { |
| 51 |
self.stockpile.insert(tile.as_uvec2()); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
pub fn remove_stockpile(&mut self, a: IVec2, b: IVec2) { |
| 57 |
for tile in rect_tiles(a, b) { |
| 58 |
if tile.x >= 0 && tile.y >= 0 { |
| 59 |
self.stockpile.remove(&tile.as_uvec2()); |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
pub fn can_zone(map: &WorldMap, tile: IVec2) -> bool { |
| 66 |
map.move_cost(tile).is_some() && !map.get(tile).is_some_and(|t| t.is_water()) |
| 67 |
} |
| 68 |
|
| 69 |
fn rect_tiles(a: IVec2, b: IVec2) -> impl Iterator<Item = IVec2> { |
| 70 |
let (min, max) = (a.min(b), a.max(b)); |
| 71 |
(min.y..=max.y).flat_map(move |y| (min.x..=max.x).map(move |x| IVec2::new(x, y))) |
| 72 |
} |
| 73 |
|
| 74 |
#[derive(Resource, Default, Debug)] |
| 75 |
pub struct ZoneTool { |
| 76 |
pub active: bool, |
| 77 |
drag_start: Option<UVec2>, |
| 78 |
} |
| 79 |
|
| 80 |
fn place_starting_stockpile(map: Res<WorldMap>, mut zones: ResMut<Zones>) { |
| 81 |
*zones = Zones::default(); |
| 82 |
if let Some(&spawn) = spawn_tiles(&map, 1).first() { |
| 83 |
let corner = spawn.as_ivec2() + STARTING_STOCKPILE_OFFSET; |
| 84 |
zones.add_stockpile(&map, corner, corner + STARTING_STOCKPILE_SIZE - 1); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
fn removing(keys: &ButtonInput<KeyCode>) -> bool { |
| 89 |
keys.any_pressed([KeyCode::ShiftLeft, KeyCode::ShiftRight]) |
| 90 |
} |
| 91 |
|
| 92 |
fn zone_tool( |
| 93 |
keys: Res<ButtonInput<KeyCode>>, |
| 94 |
buttons: Res<ButtonInput<MouseButton>>, |
| 95 |
cursor: Res<Cursor>, |
| 96 |
map: Res<WorldMap>, |
| 97 |
mut tool: ResMut<ZoneTool>, |
| 98 |
mut zones: ResMut<Zones>, |
| 99 |
) { |
| 100 |
if keys.just_pressed(KeyCode::KeyZ) { |
| 101 |
tool.active = !tool.active; |
| 102 |
tool.drag_start = None; |
| 103 |
return; |
| 104 |
} |
| 105 |
if keys.just_pressed(KeyCode::Escape) { |
| 106 |
tool.active = false; |
| 107 |
tool.drag_start = None; |
| 108 |
} |
| 109 |
if !tool.active { |
| 110 |
return; |
| 111 |
} |
| 112 |
if buttons.just_pressed(MouseButton::Left) { |
| 113 |
tool.drag_start = cursor.tile; |
| 114 |
} |
| 115 |
if !buttons.just_released(MouseButton::Left) { |
| 116 |
return; |
| 117 |
} |
| 118 |
if let (Some(start), Some(end)) = (tool.drag_start.take(), cursor.tile) { |
| 119 |
if removing(&keys) { |
| 120 |
zones.remove_stockpile(start.as_ivec2(), end.as_ivec2()); |
| 121 |
} else { |
| 122 |
zones.add_stockpile(&map, start.as_ivec2(), end.as_ivec2()); |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
fn draw_zone_preview( |
| 128 |
keys: Res<ButtonInput<KeyCode>>, |
| 129 |
cursor: Res<Cursor>, |
| 130 |
tool: Res<ZoneTool>, |
| 131 |
mut gizmos: Gizmos, |
| 132 |
) { |
| 133 |
let (Some(start), Some(end)) = (tool.drag_start, cursor.tile) else { |
| 134 |
return; |
| 135 |
}; |
| 136 |
let (min, max) = (start.min(end), start.max(end)); |
| 137 |
let center = (WorldMap::tile_center(min) + WorldMap::tile_center(max)) / 2.0; |
| 138 |
let size = (max - min + 1).as_vec2() * TILE_SIZE; |
| 139 |
let color = if removing(&keys) { |
| 140 |
PREVIEW_REMOVE_COLOR |
| 141 |
} else { |
| 142 |
PREVIEW_ADD_COLOR |
| 143 |
}; |
| 144 |
gizmos.rect_2d(center, size, color); |
| 145 |
} |
| 146 |
|
| 147 |
#[cfg(test)] |
| 148 |
mod tests { |
| 149 |
use super::*; |
| 150 |
use crate::world::Terrain; |
| 151 |
|
| 152 |
#[test] |
| 153 |
fn stockpiles_only_cover_walkable_dry_land() { |
| 154 |
let mut map = WorldMap::filled(UVec2::new(4, 1), Terrain::Grass); |
| 155 |
map.set(UVec2::new(1, 0), Terrain::Stone); |
| 156 |
map.set(UVec2::new(2, 0), Terrain::ShallowWater); |
| 157 |
let mut zones = Zones::default(); |
| 158 |
zones.add_stockpile(&map, IVec2::new(3, 0), IVec2::new(-1, 0)); |
| 159 |
let mut tiles: Vec<_> = zones.stockpile_tiles().collect(); |
| 160 |
tiles.sort_by_key(|t| t.x); |
| 161 |
assert_eq!(tiles, vec![UVec2::new(0, 0), UVec2::new(3, 0)]); |
| 162 |
} |
| 163 |
|
| 164 |
#[test] |
| 165 |
fn removing_clears_a_rectangle() { |
| 166 |
let map = WorldMap::filled(UVec2::new(4, 4), Terrain::Grass); |
| 167 |
let mut zones = Zones::default(); |
| 168 |
zones.add_stockpile(&map, IVec2::ZERO, IVec2::splat(3)); |
| 169 |
zones.remove_stockpile(IVec2::new(1, 1), IVec2::new(2, 2)); |
| 170 |
assert_eq!(zones.stockpile_tiles().count(), 12); |
| 171 |
assert!(!zones.is_stockpile(UVec2::new(1, 2))); |
| 172 |
assert!(zones.is_stockpile(UVec2::new(0, 2))); |
| 173 |
} |
| 174 |
|
| 175 |
fn tool_app() -> App { |
| 176 |
let mut app = App::new(); |
| 177 |
app.insert_resource(WorldMap::filled(UVec2::splat(8), Terrain::Grass)) |
| 178 |
.init_resource::<Zones>() |
| 179 |
.init_resource::<ButtonInput<KeyCode>>() |
| 180 |
.init_resource::<ButtonInput<MouseButton>>() |
| 181 |
.insert_resource(ZoneTool { |
| 182 |
active: true, |
| 183 |
drag_start: None, |
| 184 |
}) |
| 185 |
.init_resource::<Cursor>() |
| 186 |
.add_systems(Update, zone_tool); |
| 187 |
app |
| 188 |
} |
| 189 |
|
| 190 |
fn drag(app: &mut App, from: UVec2, to: UVec2) { |
| 191 |
app.world_mut().resource_mut::<Cursor>().tile = Some(from); |
| 192 |
app.world_mut() |
| 193 |
.resource_mut::<ButtonInput<MouseButton>>() |
| 194 |
.press(MouseButton::Left); |
| 195 |
app.update(); |
| 196 |
app.world_mut().resource_mut::<Cursor>().tile = Some(to); |
| 197 |
let mut buttons = app.world_mut().resource_mut::<ButtonInput<MouseButton>>(); |
| 198 |
buttons.clear(); |
| 199 |
buttons.release(MouseButton::Left); |
| 200 |
app.update(); |
| 201 |
app.world_mut() |
| 202 |
.resource_mut::<ButtonInput<MouseButton>>() |
| 203 |
.clear(); |
| 204 |
} |
| 205 |
|
| 206 |
#[test] |
| 207 |
fn dragging_adds_and_shift_dragging_removes() { |
| 208 |
let mut app = tool_app(); |
| 209 |
drag(&mut app, UVec2::new(1, 1), UVec2::new(3, 2)); |
| 210 |
assert_eq!(app.world().resource::<Zones>().stockpile_tiles().count(), 6); |
| 211 |
app.world_mut() |
| 212 |
.resource_mut::<ButtonInput<KeyCode>>() |
| 213 |
.press(KeyCode::ShiftLeft); |
| 214 |
drag(&mut app, UVec2::new(3, 2), UVec2::new(3, 1)); |
| 215 |
assert_eq!(app.world().resource::<Zones>().stockpile_tiles().count(), 4); |
| 216 |
} |
| 217 |
|
| 218 |
#[test] |
| 219 |
fn dragging_does_nothing_when_the_tool_is_off() { |
| 220 |
let mut app = tool_app(); |
| 221 |
app.world_mut().resource_mut::<ZoneTool>().active = false; |
| 222 |
drag(&mut app, UVec2::new(1, 1), UVec2::new(3, 2)); |
| 223 |
assert_eq!(app.world().resource::<Zones>().stockpile_tiles().count(), 0); |
| 224 |
} |
| 225 |
|
| 226 |
#[test] |
| 227 |
fn new_worlds_start_with_a_stockpile() { |
| 228 |
let mut app = App::new(); |
| 229 |
app.insert_resource(crate::worldgen::generate(UVec2::new(96, 64), 1)) |
| 230 |
.init_resource::<Zones>() |
| 231 |
.add_message::<WorldGenerated>() |
| 232 |
.add_systems( |
| 233 |
Update, |
| 234 |
place_starting_stockpile.run_if(on_message::<WorldGenerated>), |
| 235 |
); |
| 236 |
app.world_mut().write_message(WorldGenerated); |
| 237 |
app.update(); |
| 238 |
assert!(app.world().resource::<Zones>().stockpile_tiles().count() > 8); |
| 239 |
} |
| 240 |
} |