courier
Clone
https://git.tahinli.com/Tahinli/courier.git
0 watching·0 forks·Rust·AGPL-3.0·5.3 MiB·6d9eeb9·Tahinli·21 days ago·3 commits
| 1 | use crate::game::*; |
| 2 | use crate::iso::*; |
| 3 | use crate::sheets::CharSheets; |
| 4 | use crate::sprites::{self, Spr}; |
| 5 | use bevy::prelude::*; |
| 6 | use std::collections::HashSet; |
| 7 | |
| 8 | pub const ASHFORD: Vec2 = Vec2::new(0.0, 0.0); |
| 9 | pub const MILLBROOK: Vec2 = Vec2::new(110.0, -110.0); |
| 10 | pub const COALBRIDGE: Vec2 = Vec2::new(220.0, -220.0); |
| 11 | pub const TOWN_RADIUS: f32 = 17.0; |
| 12 | const BANDS: [Vec2; 3] = [ASHFORD, MILLBROOK, COALBRIDGE]; |
| 13 | |
| 14 | const OAKS: [&Spr; 4] = [&sprites::OAK0, &sprites::OAK1, &sprites::OAK2, &sprites::BIRCH]; |
| 15 | const PINES: [&Spr; 2] = [&sprites::PINE, &sprites::BIRCH]; |
| 16 | const GRASS: [&Spr; 4] = [&sprites::GRASS0, &sprites::GRASS1, &sprites::GRASS2, &sprites::GRASS3]; |
| 17 | const ROAD: [&Spr; 4] = [&sprites::ROAD0, &sprites::ROAD1, &sprites::ROAD2, &sprites::ROAD3]; |
| 18 | const COBBLE: [&Spr; 4] = [&sprites::COBBLE0, &sprites::COBBLE1, &sprites::COBBLE2, &sprites::COBBLE3]; |
| 19 | const FLAG: [&Spr; 4] = [&sprites::FLAG0, &sprites::FLAG1, &sprites::FLAG2, &sprites::FLAG3]; |
| 20 | const SOOT: [&Spr; 4] = [&sprites::SOOT0, &sprites::SOOT1, &sprites::SOOT2, &sprites::SOOT3]; |
| 21 | const MUD: [&Spr; 2] = [&sprites::MUD0, &sprites::MUD1]; |
| 22 | const FARMLAND: [&Spr; 2] = [&sprites::FARMLAND0, &sprites::FARMLAND1]; |
| 23 | const ROCKS: [&Spr; 2] = [&sprites::ROCK0, &sprites::ROCK1]; |
| 24 | const BUSHES: [&Spr; 2] = [&sprites::BUSH0, &sprites::BUSH1]; |
| 25 | |
| 26 | #[derive(Resource, Default)] |
| 27 | pub struct Map { |
| 28 | pub ground: HashSet<IVec2>, |
| 29 | pub blocked: HashSet<IVec2>, |
| 30 | pub legs: Vec<Vec<Vec2>>, |
| 31 | } |
| 32 | |
| 33 | impl Map { |
| 34 | pub fn walkable(&self, p: Vec2) -> bool { |
| 35 | let c = cell(p); |
| 36 | self.ground.contains(&c) && !self.blocked.contains(&c) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | #[derive(Component)] |
| 41 | pub struct Occluder { |
| 42 | pub min: Vec2, |
| 43 | pub max: Vec2, |
| 44 | pub z: f32, |
| 45 | } |
| 46 | |
| 47 | #[derive(Component)] |
| 48 | pub struct Interact { |
| 49 | pub talk: Talk, |
| 50 | pub pos: Vec2, |
| 51 | } |
| 52 | |
| 53 | #[derive(Component)] |
| 54 | pub struct NpcTag(pub Npc); |
| 55 | |
| 56 | #[derive(Component)] |
| 57 | pub struct Glow; |
| 58 | |
| 59 | #[derive(Component)] |
| 60 | pub struct Emissive; |
| 61 | |
| 62 | #[derive(Component)] |
| 63 | pub struct Lamp(pub Vec2); |
| 64 | |
| 65 | #[derive(Component)] |
| 66 | pub struct Ground; |
| 67 | |
| 68 | #[derive(Component)] |
| 69 | pub struct Water; |
| 70 | |
| 71 | #[derive(Component)] |
| 72 | pub struct Chimney { |
| 73 | pub at: Vec3, |
| 74 | pub t: f32, |
| 75 | pub rate: f32, |
| 76 | } |
| 77 | |
| 78 | #[derive(Component)] |
| 79 | pub struct Puff { |
| 80 | pub age: f32, |
| 81 | pub drift: f32, |
| 82 | } |
| 83 | |
| 84 | #[derive(Component)] |
| 85 | pub struct EventSpot { |
| 86 | pub ev: Ev, |
| 87 | pub pos: Vec2, |
| 88 | } |
| 89 | |
| 90 | pub fn zone_at(p: Vec2) -> Zone { |
| 91 | for (z, o) in [(Zone::Ashford, ASHFORD), (Zone::Millbrook, MILLBROOK), (Zone::Coalbridge, COALBRIDGE)] { |
| 92 | if (p - o).length() < TOWN_RADIUS { |
| 93 | return z; |
| 94 | } |
| 95 | } |
| 96 | Zone::Road |
| 97 | } |
| 98 | |
| 99 | pub fn town_origin(z: Zone) -> Vec2 { |
| 100 | match z { |
| 101 | Zone::Ashford => ASHFORD, |
| 102 | Zone::Millbrook => MILLBROOK, |
| 103 | Zone::Coalbridge => COALBRIDGE, |
| 104 | Zone::Road => ASHFORD, |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | pub struct WorldPlugin; |
| 109 | |
| 110 | impl Plugin for WorldPlugin { |
| 111 | fn build(&self, app: &mut App) { |
| 112 | app.init_resource::<Map>() |
| 113 | .add_systems(Startup, build_world.after(crate::sheets::load_sheets)) |
| 114 | .add_systems(Update, (smoke, water)); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | pub struct Spawner<'a, 'w, 's> { |
| 119 | pub commands: &'a mut Commands<'w, 's>, |
| 120 | pub assets: &'a AssetServer, |
| 121 | pub map: &'a mut Map, |
| 122 | } |
| 123 | |
| 124 | impl Spawner<'_, '_, '_> { |
| 125 | fn art(&mut self, s: &Spr, at: Vec2, z: f32) -> Entity { |
| 126 | let e = self |
| 127 | .commands |
| 128 | .spawn((Sprite::from_image(self.assets.load(s.path)), anchor(s), Transform::from_translation(to_screen(at).extend(z)).with_scale(Vec3::splat(PX)))) |
| 129 | .id(); |
| 130 | if let Some(g) = s.glow { |
| 131 | let mut sprite = Sprite::from_image(self.assets.load(g)); |
| 132 | sprite.color = Color::srgba(1.0, 1.0, 1.0, 0.0); |
| 133 | self.commands.spawn((sprite, anchor(s), Transform::from_translation(to_screen(at).extend(z + 0.0005)).with_scale(Vec3::splat(PX)), Emissive)); |
| 134 | } |
| 135 | e |
| 136 | } |
| 137 | |
| 138 | pub fn ground(&mut self, s: &Spr, c: IVec2) { |
| 139 | let e = self.art(s, c.as_vec2(), -10.0 + depth(c.as_vec2()) * 0.01); |
| 140 | self.commands.entity(e).insert(Ground); |
| 141 | self.map.ground.insert(c); |
| 142 | } |
| 143 | |
| 144 | pub fn water(&mut self, c: IVec2) { |
| 145 | let e = self.art(&sprites::WATER0, c.as_vec2(), -10.0 + depth(c.as_vec2()) * 0.01); |
| 146 | self.commands.entity(e).insert(Water); |
| 147 | self.map.blocked.insert(c); |
| 148 | } |
| 149 | |
| 150 | pub fn prop_fp(&mut self, s: &Spr, c: IVec2, size: IVec2, block: bool) -> Entity { |
| 151 | if block { |
| 152 | for x in 0..size.x { |
| 153 | for y in 0..size.y { |
| 154 | self.map.blocked.insert(c + IVec2::new(x, y)); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | let center = c.as_vec2() + size.as_vec2() * 0.5; |
| 159 | self.art(s, c.as_vec2(), depth(center)) |
| 160 | } |
| 161 | |
| 162 | pub fn prop(&mut self, s: &Spr, c: IVec2, block: bool) -> Entity { |
| 163 | self.prop_fp(s, c, IVec2::from(s.foot), block) |
| 164 | } |
| 165 | |
| 166 | pub fn tree(&mut self, s: &Spr, c: IVec2) { |
| 167 | self.map.blocked.insert(c); |
| 168 | let center = c.as_vec2() + Vec2::splat(0.5); |
| 169 | self.art(s, c.as_vec2(), depth(center)); |
| 170 | } |
| 171 | |
| 172 | pub fn building(&mut self, s: &Spr, at: IVec2, talk: Option<Talk>) -> Vec2 { |
| 173 | let size = IVec2::from(s.foot); |
| 174 | for x in 0..size.x { |
| 175 | for y in 0..size.y { |
| 176 | self.map.blocked.insert(at + IVec2::new(x, y)); |
| 177 | } |
| 178 | } |
| 179 | let min = at.as_vec2(); |
| 180 | let max = min + size.as_vec2(); |
| 181 | let z = depth((min + max) * 0.5); |
| 182 | let e = self.art(s, min, z); |
| 183 | self.commands.entity(e).insert(Occluder { min, max, z }); |
| 184 | let base = to_screen(min); |
| 185 | let rate = if size.x >= 6 { 0.12 } else { 0.45 }; |
| 186 | for (i, (dx, dy)) in s.smoke.iter().enumerate() { |
| 187 | self.commands.spawn(Chimney { at: Vec3::new(base.x + dx, base.y + dy, 8.0), t: i as f32 * 0.3, rate }); |
| 188 | } |
| 189 | let door = Vec2::new(at.x as f32 + 1.5, max.y + 0.6); |
| 190 | if let Some(t) = talk { |
| 191 | self.commands.spawn((Interact { talk: t, pos: door }, Transform::default())); |
| 192 | } |
| 193 | door |
| 194 | } |
| 195 | |
| 196 | pub fn npc(&mut self, sheets: &CharSheets, n: Npc, sheet: &str, pos: Vec2, facing: usize) -> Entity { |
| 197 | self.map.blocked.insert(cell(pos)); |
| 198 | let spr = sheets.spr(sheet); |
| 199 | self.commands |
| 200 | .spawn(( |
| 201 | sheets.sprite(sheet, facing * 9), |
| 202 | anchor(spr), |
| 203 | Transform::from_translation(to_screen(pos).extend(depth(pos))).with_scale(Vec3::splat(PX)), |
| 204 | NpcTag(n), |
| 205 | Interact { talk: Talk::Npc(n), pos }, |
| 206 | crate::player::Mover::new(pos, facing), |
| 207 | )) |
| 208 | .id() |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | fn band_of(c: IVec2) -> Option<(usize, IVec2)> { |
| 213 | for (i, o) in BANDS.iter().enumerate() { |
| 214 | let l = c - cell(*o); |
| 215 | if (-20..=20).contains(&l.x) && (-18..=18).contains(&l.y) { |
| 216 | return Some((i, l)); |
| 217 | } |
| 218 | } |
| 219 | None |
| 220 | } |
| 221 | |
| 222 | fn street(l: IVec2) -> bool { |
| 223 | (-16..=15).contains(&l.x) && (0..=1).contains(&l.y) |
| 224 | } |
| 225 | |
| 226 | fn river(l: IVec2) -> bool { |
| 227 | (9..=11).contains(&l.x) |
| 228 | } |
| 229 | |
| 230 | fn field(l: IVec2) -> bool { |
| 231 | (10..=17).contains(&l.x) && (7..=14).contains(&l.y) |
| 232 | } |
| 233 | |
| 234 | fn rail(l: IVec2) -> bool { |
| 235 | l.y == 10 && (-16..=15).contains(&l.x) |
| 236 | } |
| 237 | |
| 238 | fn reserved(c: IVec2) -> bool { |
| 239 | match band_of(c) { |
| 240 | Some((0, l)) => street(l) || field(l), |
| 241 | Some((_, l)) => street(l), |
| 242 | None => false, |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | fn leg1_t(p: Vec2) -> f32 { |
| 247 | let d = COALBRIDGE - MILLBROOK; |
| 248 | (p - MILLBROOK).dot(d) / d.length_squared() |
| 249 | } |
| 250 | |
| 251 | fn tree_kind(s: f32, k: usize) -> &'static Spr { |
| 252 | if s < 0.4 { |
| 253 | OAKS[k % 4] |
| 254 | } else if s < 0.75 { |
| 255 | PINES[k % 2] |
| 256 | } else { |
| 257 | &sprites::DEAD |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | fn vnoise(c: IVec2, s: f32, salt: u32) -> f32 { |
| 262 | let p = c.as_vec2() / s; |
| 263 | let i = p.floor(); |
| 264 | let f = p - i; |
| 265 | let f = f * f * (Vec2::splat(3.0) - 2.0 * f); |
| 266 | let (x, y) = (i.x as i32, i.y as i32); |
| 267 | let a = hash(x, y, salt) + (hash(x + 1, y, salt) - hash(x, y, salt)) * f.x; |
| 268 | let b = hash(x, y + 1, salt) + (hash(x + 1, y + 1, salt) - hash(x, y + 1, salt)) * f.x; |
| 269 | a + (b - a) * f.y |
| 270 | } |
| 271 | |
| 272 | fn biome(c: IVec2, k: usize) -> &'static Spr { |
| 273 | let pm = ((leg1_t(c.as_vec2()) - 0.2) / 0.6).clamp(0.0, 1.0); |
| 274 | if vnoise(c, 5.0, 11) < pm * 0.9 { |
| 275 | MUD[k % 2] |
| 276 | } else if vnoise(c, 4.0, 12) < pm * 0.35 { |
| 277 | SOOT[k] |
| 278 | } else { |
| 279 | GRASS[k] |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | fn dense(points: &[Vec2]) -> Vec<Vec2> { |
| 284 | let mut out = Vec::new(); |
| 285 | for w in points.windows(2) { |
| 286 | let (a, b) = (w[0], w[1]); |
| 287 | let n = ((b - a).length() / 0.25).ceil() as i32; |
| 288 | for i in 0..n { |
| 289 | out.push(a.lerp(b, i as f32 / n as f32)); |
| 290 | } |
| 291 | } |
| 292 | out.push(*points.last().unwrap()); |
| 293 | out |
| 294 | } |
| 295 | |
| 296 | fn leg(from: Vec2, wiggle: [Vec2; 5]) -> Vec<Vec2> { |
| 297 | let start = from + Vec2::new(15.0, 0.5); |
| 298 | let end = from + Vec2::new(110.0 - 15.0, -110.0 + 0.5); |
| 299 | let mut pts = vec![start, start + Vec2::new(6.0, 0.0)]; |
| 300 | for (i, w) in wiggle.iter().enumerate() { |
| 301 | let t = (i as f32 + 1.0) / 6.0; |
| 302 | pts.push(start.lerp(end, t) + *w); |
| 303 | } |
| 304 | pts.push(end - Vec2::new(6.0, 0.0)); |
| 305 | pts.push(end); |
| 306 | dense(&pts) |
| 307 | } |
| 308 | |
| 309 | pub fn build_world(mut commands: Commands, assets: Res<AssetServer>, mut map: ResMut<Map>, sheets: Res<CharSheets>) { |
| 310 | let legs = vec![ |
| 311 | leg(ASHFORD, [Vec2::new(-4.0, -10.0), Vec2::new(6.0, 4.0), Vec2::new(-8.0, -6.0), Vec2::new(5.0, 6.0), Vec2::new(-3.0, -4.0)]), |
| 312 | leg(MILLBROOK, [Vec2::new(6.0, 6.0), Vec2::new(-6.0, -8.0), Vec2::new(4.0, 7.0), Vec2::new(-7.0, -3.0), Vec2::new(3.0, 5.0)]), |
| 313 | ]; |
| 314 | let mut sp = Spawner { commands: &mut commands, assets: &assets, map: &mut map }; |
| 315 | |
| 316 | let mut road_cells: HashSet<IVec2> = HashSet::new(); |
| 317 | let mut near: HashSet<IVec2> = HashSet::new(); |
| 318 | for l in &legs { |
| 319 | for p in l.iter().step_by(2) { |
| 320 | for dx in -1..=1 { |
| 321 | for dy in -1..=1 { |
| 322 | let q = *p + Vec2::new(dx as f32 * 0.5, dy as f32 * 0.5); |
| 323 | road_cells.insert(cell(q)); |
| 324 | } |
| 325 | } |
| 326 | let c = cell(*p); |
| 327 | for dx in -14..=14 { |
| 328 | for dy in -14..=14 { |
| 329 | if dx * dx + dy * dy <= 196 { |
| 330 | near.insert(c + IVec2::new(dx, dy)); |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | for o in BANDS { |
| 337 | let oc = cell(o); |
| 338 | for dx in -20..=20 { |
| 339 | for dy in -18..=18 { |
| 340 | near.insert(oc + IVec2::new(dx, dy)); |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | for c in &near { |
| 346 | let k = (hash(c.x, c.y, 1) * 4.0) as usize; |
| 347 | let road = road_cells.contains(c); |
| 348 | if let Some((1, l)) = band_of(*c) { |
| 349 | if river(l) { |
| 350 | if street(l) { |
| 351 | sp.ground(&sprites::PLANKS, *c); |
| 352 | } else { |
| 353 | sp.water(*c); |
| 354 | } |
| 355 | continue; |
| 356 | } |
| 357 | } |
| 358 | let tile: &Spr = match band_of(*c) { |
| 359 | Some((0, l)) if street(l) => FLAG[k], |
| 360 | Some((0, l)) if field(l) => FARMLAND[k % 2], |
| 361 | Some((0, _)) if road => ROAD[k], |
| 362 | Some((0, _)) => GRASS[k], |
| 363 | Some((1, l)) if street(l) => COBBLE[k], |
| 364 | Some((1, _)) => ROAD[k], |
| 365 | Some((2, l)) if rail(l) => &sprites::RAIL_X, |
| 366 | Some((2, _)) => SOOT[k], |
| 367 | None if road => ROAD[k], |
| 368 | None => biome(*c, k), |
| 369 | Some(_) => GRASS[k], |
| 370 | }; |
| 371 | sp.ground(tile, *c); |
| 372 | } |
| 373 | |
| 374 | ashford(&mut sp, &sheets); |
| 375 | millbrook(&mut sp, &sheets); |
| 376 | coalbridge(&mut sp, &sheets); |
| 377 | |
| 378 | let mut taken: HashSet<IVec2> = sp.map.blocked.clone(); |
| 379 | for c in &near { |
| 380 | if taken.contains(c) || road_cells.contains(c) || reserved(*c) { |
| 381 | continue; |
| 382 | } |
| 383 | let b = band_of(*c); |
| 384 | let road_d = road_dist(&legs, c.as_vec2()); |
| 385 | let mut edge = 99; |
| 386 | for dx in -15..=15 { |
| 387 | for dy in -15..=15 { |
| 388 | if !near.contains(&(*c + IVec2::new(dx, dy))) { |
| 389 | edge = edge.min(dx.abs().max(dy.abs())); |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | let chance = match b { |
| 394 | Some((0, _)) => 0.012, |
| 395 | Some((1, _)) => 0.002, |
| 396 | Some(_) => 0.004, |
| 397 | None => { |
| 398 | if edge <= 3 { |
| 399 | 0.55 |
| 400 | } else if road_d < 3.0 { |
| 401 | 0.0 |
| 402 | } else { |
| 403 | 0.02 + 0.06 * ((road_d - 3.0) / 8.0).min(1.0) |
| 404 | } |
| 405 | } |
| 406 | }; |
| 407 | let k = (hash(c.x, c.y, 3) * 6.0) as usize; |
| 408 | if hash(c.x, c.y, 7) < chance { |
| 409 | sp.tree(tree_kind(leg1_t(c.as_vec2()), k), *c); |
| 410 | taken.insert(*c); |
| 411 | } else if road_d > 2.0 && hash(c.x, c.y, 21) < 0.012 { |
| 412 | sp.prop(ROCKS[k % 2], *c, true); |
| 413 | taken.insert(*c); |
| 414 | } else if road_d > 1.5 && hash(c.x, c.y, 22) < 0.02 { |
| 415 | sp.prop(BUSHES[k % 2], *c, false); |
| 416 | taken.insert(*c); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | for (i, (ev, t)) in [(Ev::BrokenCart, 0.32f32), (Ev::Highwayman, 0.62), (Ev::Peddler, 1.35), (Ev::Storm, 1.7), (Ev::Lost, 0.85)].iter().enumerate() { |
| 421 | let l = &legs[(*t as usize).min(1)]; |
| 422 | let f = t.fract(); |
| 423 | let p = l[((l.len() - 1) as f32 * f) as usize]; |
| 424 | sp.commands.spawn((EventSpot { ev: *ev, pos: p }, Transform::default(), Name::new(format!("event{i}")))); |
| 425 | if *ev == Ev::Lost { |
| 426 | let pos = p + Vec2::new(1.2, 1.2); |
| 427 | let e = sp.npc(&sheets, Npc::Peddler, "boy", pos, 1); |
| 428 | sp.commands.entity(e).insert(Interact { talk: Talk::Ev(Ev::Lost), pos }); |
| 429 | } |
| 430 | if *ev == Ev::BrokenCart { |
| 431 | let c = cell(p) + IVec2::new(2, 2); |
| 432 | sp.prop(&sprites::CRATES, c, true); |
| 433 | sp.prop(&sprites::BARREL, c + IVec2::new(1, 0), true); |
| 434 | sp.prop(&sprites::SACK, c + IVec2::new(0, 1), false); |
| 435 | let pos = c.as_vec2() + Vec2::new(-0.5, 1.5); |
| 436 | let e = sp.npc(&sheets, Npc::Peddler, "worker", pos, 1); |
| 437 | sp.commands.entity(e).insert(Interact { talk: Talk::Ev(Ev::BrokenCart), pos }); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | sp.map.legs = legs; |
| 442 | } |
| 443 | |
| 444 | fn road_dist(legs: &[Vec<Vec2>], p: Vec2) -> f32 { |
| 445 | let mut d = f32::MAX; |
| 446 | for l in legs { |
| 447 | for q in l.iter().step_by(4) { |
| 448 | d = d.min((*q - p).length()); |
| 449 | } |
| 450 | } |
| 451 | d |
| 452 | } |
| 453 | |
| 454 | fn at(o: Vec2, x: i32, y: i32) -> IVec2 { |
| 455 | cell(o) + IVec2::new(x, y) |
| 456 | } |
| 457 | |
| 458 | fn lamp(sp: &mut Spawner, c: IVec2) { |
| 459 | let e = sp.prop(&sprites::LAMP, c, true); |
| 460 | let g = c.as_vec2() + Vec2::splat(0.5); |
| 461 | sp.commands.entity(e).insert(Lamp(g)); |
| 462 | let mut light = Sprite::from_image(sp.assets.load(sprites::LIGHT.path)); |
| 463 | light.color = Color::srgba(1.0, 0.75, 0.4, 0.0); |
| 464 | sp.commands.spawn((light, anchor(&sprites::LIGHT), Transform::from_translation(to_screen(g).extend(7.0)).with_scale(Vec3::splat(PX * 2.2)), Glow)); |
| 465 | } |
| 466 | |
| 467 | fn ashford(sp: &mut Spawner, sh: &CharSheets) { |
| 468 | let o = ASHFORD; |
| 469 | sp.building(&sprites::A_POST, at(o, -11, -2), Some(Talk::Npc(Npc::Hale))); |
| 470 | sp.building(&sprites::A_SHOP, at(o, -6, -2), Some(Talk::Npc(Npc::Pike))); |
| 471 | sp.building(&sprites::A_INN, at(o, 0, -2), Some(Talk::Npc(Npc::Bess))); |
| 472 | sp.building(&sprites::A_SCHOOL, at(o, 6, -2), Some(Talk::Npc(Npc::Edwin))); |
| 473 | sp.building(&sprites::A_HOME, at(o, -9, 4), Some(Talk::Obj(Obj::HomeBed))); |
| 474 | sp.building(&sprites::A_COTTAGE1, at(o, -3, 4), None); |
| 475 | sp.building(&sprites::A_COTTAGE2, at(o, 3, 4), None); |
| 476 | sp.building(&sprites::A_BARN, at(o, 9, 4), None); |
| 477 | |
| 478 | sp.npc(sh, Npc::Hale, "gent", o + Vec2::new(-7.5, 0.3), 3); |
| 479 | sp.npc(sh, Npc::Pike, "grocer", o + Vec2::new(-3.5, 0.3), 3); |
| 480 | sp.npc(sh, Npc::Bess, "barmaid", o + Vec2::new(3.5, 0.3), 3); |
| 481 | sp.npc(sh, Npc::Edwin, "teacher", o + Vec2::new(9.5, 0.3), 3); |
| 482 | sp.npc(sh, Npc::OldTom, "old", o + Vec2::new(-1.5, 7.5), 4); |
| 483 | sp.npc(sh, Npc::Annie, "boy", o + Vec2::new(10.5, -4.5), 2); |
| 484 | |
| 485 | sp.prop(&sprites::WELL, at(o, 12, -4), true); |
| 486 | sp.prop(&sprites::BENCH, at(o, 13, -2), true); |
| 487 | sp.tree(&sprites::OAK0, at(o, 15, -6)); |
| 488 | sp.tree(&sprites::OAK2, at(o, -15, -6)); |
| 489 | sp.tree(&sprites::OAK1, at(o, -16, 9)); |
| 490 | |
| 491 | for x in -13..=-6 { |
| 492 | sp.prop(&sprites::FENCE_X, at(o, x, -5), true); |
| 493 | } |
| 494 | sp.prop(&sprites::FENCE_Y, at(o, -13, -4), true); |
| 495 | sp.prop(&sprites::FENCE_Y, at(o, -13, -3), true); |
| 496 | sp.prop(&sprites::FENCE_Y, at(o, -6, -4), true); |
| 497 | sp.prop(&sprites::FENCE_Y, at(o, -6, -3), true); |
| 498 | for (x, y) in [(-12, -4), (-10, -4), (-11, -3), (-7, -3)] { |
| 499 | sp.prop(&sprites::FLOWERS0, at(o, x, y), false); |
| 500 | } |
| 501 | for (x, y) in [(-8, -4), (-9, -3)] { |
| 502 | sp.prop(&sprites::FLOWERS1, at(o, x, y), false); |
| 503 | } |
| 504 | for x in -12..=-8 { |
| 505 | sp.prop(&sprites::FENCE_X, at(o, x, 9), true); |
| 506 | } |
| 507 | for (x, y) in [(-11, 8), (-9, 8), (-7, 8)] { |
| 508 | sp.prop(&sprites::FLOWERS0, at(o, x, y), false); |
| 509 | } |
| 510 | |
| 511 | sp.prop(&sprites::HAYSTACK, at(o, 16, 10), true); |
| 512 | sp.prop(&sprites::SIGNPOST, at(o, 17, 3), true); |
| 513 | |
| 514 | for (x, y) in [(-12, 2), (-2, 2), (5, 2), (11, -1)] { |
| 515 | lamp(sp, at(o, x, y)); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | fn millbrook(sp: &mut Spawner, sh: &CharSheets) { |
| 520 | let o = MILLBROOK; |
| 521 | sp.building(&sprites::M_SHOP, at(o, -14, -2), Some(Talk::Npc(Npc::Quill))); |
| 522 | sp.building(&sprites::M_INN, at(o, -8, -2), Some(Talk::Obj(Obj::MillInn))); |
| 523 | sp.building(&sprites::M_MILL, at(o, 3, -3), Some(Talk::Obj(Obj::BaleDrop))); |
| 524 | sp.building(&sprites::M_WAREHOUSE, at(o, 12, -2), None); |
| 525 | sp.building(&sprites::M_TERRACE, at(o, -13, 6), None); |
| 526 | sp.building(&sprites::M_TERRACE, at(o, 1, 6), None); |
| 527 | |
| 528 | let bale = sp.prop(&sprites::BALE, at(o, -2, -2), true); |
| 529 | sp.commands.entity(bale).insert(Interact { talk: Talk::Obj(Obj::BalePile), pos: o + Vec2::new(-0.5, -1.0) }); |
| 530 | sp.prop(&sprites::BALE, at(o, -1, -2), true); |
| 531 | sp.prop(&sprites::BALE, at(o, -2, -3), true); |
| 532 | sp.prop(&sprites::BARRELS, at(o, -5, -3), true); |
| 533 | sp.prop(&sprites::CRATES, at(o, -13, -3), true); |
| 534 | |
| 535 | sp.prop_fp(&sprites::STALL, at(o, -11, 3), IVec2::new(2, 1), true); |
| 536 | sp.prop(&sprites::SACKS, at(o, -13, 3), true); |
| 537 | |
| 538 | sp.npc(sh, Npc::Foreman, "worker", o + Vec2::new(5.5, 2.5), 3); |
| 539 | sp.npc(sh, Npc::Quill, "gent", o + Vec2::new(-10.5, 0.3), 3); |
| 540 | sp.npc(sh, Npc::Weaver, "woman", o + Vec2::new(-11.5, 4.5), 3); |
| 541 | sp.npc(sh, Npc::Peddler, "peddler", o + Vec2::new(-7.5, 3.5), 2); |
| 542 | |
| 543 | sp.tree(&sprites::BIRCH, at(o, 8, -7)); |
| 544 | sp.tree(&sprites::BIRCH, at(o, 13, -6)); |
| 545 | |
| 546 | sp.prop(&sprites::SIGNPOST, at(o, 17, 3), true); |
| 547 | |
| 548 | for (x, y) in [(-5, 2), (2, 2), (7, 2), (13, 2)] { |
| 549 | lamp(sp, at(o, x, y)); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | fn coalbridge(sp: &mut Spawner, sh: &CharSheets) { |
| 554 | let o = COALBRIDGE; |
| 555 | sp.building(&sprites::C_HEADFRAME, at(o, -14, -3), None); |
| 556 | sp.building(&sprites::C_ENGINE, at(o, -9, -2), None); |
| 557 | sp.building(&sprites::C_PAWN, at(o, -4, -2), Some(Talk::Npc(Npc::Pawn))); |
| 558 | sp.building(&sprites::C_INN, at(o, 0, -2), Some(Talk::Obj(Obj::CoalInn))); |
| 559 | sp.building(&sprites::C_ROW, at(o, 6, -2), None); |
| 560 | sp.building(&sprites::C_ROW, at(o, -6, 4), None); |
| 561 | |
| 562 | sp.npc(sh, Npc::Jack, "miner", o + Vec2::new(-10.5, 0.5), 3); |
| 563 | sp.npc(sh, Npc::Miner, "miner", o + Vec2::new(-12.5, 2.5), 2); |
| 564 | sp.npc(sh, Npc::Pawn, "grocer", o + Vec2::new(-1.5, 0.3), 3); |
| 565 | |
| 566 | for (x, y) in [(-12, 8), (-7, 8), (2, 8), (9, 8)] { |
| 567 | sp.prop(&sprites::TUB, at(o, x, y), true); |
| 568 | } |
| 569 | for (x, y) in [(-15, 7), (12, 6), (12, 8)] { |
| 570 | sp.prop(&sprites::COALPILE, at(o, x, y), true); |
| 571 | } |
| 572 | sp.prop_fp(&sprites::SLAG, at(o, 18, -7), IVec2::new(2, 2), true); |
| 573 | sp.prop_fp(&sprites::SLAG, at(o, -18, -11), IVec2::new(2, 2), true); |
| 574 | sp.tree(&sprites::DEAD, at(o, 14, -6)); |
| 575 | sp.tree(&sprites::DEAD, at(o, -16, -7)); |
| 576 | sp.prop(&sprites::FLAME1, at(o, -6, -3), false); |
| 577 | sp.prop(&sprites::SIGNPOST, at(o, -17, 3), true); |
| 578 | |
| 579 | for (x, y) in [(-4, 2), (4, 2), (10, 2)] { |
| 580 | lamp(sp, at(o, x, y)); |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | fn smoke(mut commands: Commands, time: Res<Time>, assets: Res<AssetServer>, mut chims: Query<&mut Chimney>, mut puffs: Query<(Entity, &mut Puff, &mut Transform, &mut Sprite)>) { |
| 585 | let dt = time.delta_secs(); |
| 586 | for mut c in &mut chims { |
| 587 | c.t += dt; |
| 588 | if c.t >= c.rate { |
| 589 | c.t = 0.0; |
| 590 | let k = hash((c.at.x * 7.0) as i32, (time.elapsed_secs() * 100.0) as i32, 5); |
| 591 | commands.spawn(( |
| 592 | Sprite { image: assets.load("gfx/puff.png"), color: Color::srgba(0.55, 0.53, 0.5, 0.0), custom_size: Some(Vec2::splat(22.0)), ..default() }, |
| 593 | Transform::from_translation(c.at + Vec3::new(0.0, 4.0, k * 0.01)), |
| 594 | Puff { age: 0.0, drift: 10.0 + k * 14.0 }, |
| 595 | )); |
| 596 | } |
| 597 | } |
| 598 | for (e, mut p, mut t, mut s) in &mut puffs { |
| 599 | p.age += dt; |
| 600 | let life = 6.0; |
| 601 | if p.age > life { |
| 602 | commands.entity(e).despawn(); |
| 603 | continue; |
| 604 | } |
| 605 | let f = p.age / life; |
| 606 | t.translation.y += dt * 26.0; |
| 607 | t.translation.x += dt * p.drift; |
| 608 | t.scale = Vec3::splat(1.0 + f * 3.5); |
| 609 | s.color.set_alpha(0.55 * (1.0 - f) * (f * 8.0).min(1.0)); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | fn water(time: Res<Time>, assets: Res<AssetServer>, mut tiles: Query<&mut Sprite, With<Water>>) { |
| 614 | let frame = (time.elapsed_secs() / 0.7) as u32 % 2; |
| 615 | let img = assets.load(if frame == 0 { sprites::WATER0.path } else { sprites::WATER1.path }); |
| 616 | for mut s in &mut tiles { |
| 617 | if s.image != img { |
| 618 | s.image = img.clone(); |
| 619 | } |
| 620 | } |
| 621 | } |