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