Skip to content

Commit 3fdddca

Browse files
committed
Apply multi-tile fix to subsequent chapters.
1 parent 08163e2 commit 3fdddca

6 files changed

Lines changed: 114 additions & 24 deletions

File tree

chapter-68-mushrooms/src/map_indexing_system.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
use specs::prelude::*;
2-
use super::{Map, Position, BlocksTile, Pools, spatial};
2+
use super::{Map, Position, BlocksTile, Pools, spatial, TileSize};
33

44
pub struct MapIndexingSystem {}
55

66
impl<'a> System<'a> for MapIndexingSystem {
7+
#[allow(clippy::type_complexity)]
78
type SystemData = ( ReadExpect<'a, Map>,
89
ReadStorage<'a, Position>,
910
ReadStorage<'a, BlocksTile>,
1011
ReadStorage<'a, Pools>,
12+
ReadStorage<'a, TileSize>,
1113
Entities<'a>,);
1214

1315
fn run(&mut self, data : Self::SystemData) {
14-
let (map, position, blockers, pools, entities) = data;
16+
let (map, position, blockers, pools, sizes, entities) = data;
1517

1618
spatial::clear();
1719
spatial::populate_blocked_from_map(&*map);
@@ -23,8 +25,21 @@ impl<'a> System<'a> for MapIndexingSystem {
2325
}
2426
}
2527
if alive {
26-
let idx = map.xy_idx(position.x, position.y);
27-
spatial::index_entity(entity, idx, blockers.get(entity).is_some());
28+
if let Some(size) = sizes.get(entity) {
29+
// Multi-tile
30+
for y in position.y .. position.y + size.y {
31+
for x in position.x .. position.x + size.x {
32+
if x > 0 && x < map.width-1 && y > 0 && y < map.height-1 {
33+
let idx = map.xy_idx(x, y);
34+
spatial::index_entity(entity, idx, blockers.get(entity).is_some());
35+
}
36+
}
37+
}
38+
} else {
39+
// Single tile
40+
let idx = map.xy_idx(position.x, position.y);
41+
spatial::index_entity(entity, idx, blockers.get(entity).is_some());
42+
}
2843
}
2944
}
3045
}

chapter-69-mushrooms2/src/map_indexing_system.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
use specs::prelude::*;
2-
use super::{Map, Position, BlocksTile, Pools, spatial};
2+
use super::{Map, Position, BlocksTile, Pools, spatial, TileSize};
33

44
pub struct MapIndexingSystem {}
55

66
impl<'a> System<'a> for MapIndexingSystem {
7+
#[allow(clippy::type_complexity)]
78
type SystemData = ( ReadExpect<'a, Map>,
89
ReadStorage<'a, Position>,
910
ReadStorage<'a, BlocksTile>,
1011
ReadStorage<'a, Pools>,
12+
ReadStorage<'a, TileSize>,
1113
Entities<'a>,);
1214

1315
fn run(&mut self, data : Self::SystemData) {
14-
let (map, position, blockers, pools, entities) = data;
16+
let (map, position, blockers, pools, sizes, entities) = data;
1517

1618
spatial::clear();
1719
spatial::populate_blocked_from_map(&*map);
@@ -23,8 +25,21 @@ impl<'a> System<'a> for MapIndexingSystem {
2325
}
2426
}
2527
if alive {
26-
let idx = map.xy_idx(position.x, position.y);
27-
spatial::index_entity(entity, idx, blockers.get(entity).is_some());
28+
if let Some(size) = sizes.get(entity) {
29+
// Multi-tile
30+
for y in position.y .. position.y + size.y {
31+
for x in position.x .. position.x + size.x {
32+
if x > 0 && x < map.width-1 && y > 0 && y < map.height-1 {
33+
let idx = map.xy_idx(x, y);
34+
spatial::index_entity(entity, idx, blockers.get(entity).is_some());
35+
}
36+
}
37+
}
38+
} else {
39+
// Single tile
40+
let idx = map.xy_idx(position.x, position.y);
41+
spatial::index_entity(entity, idx, blockers.get(entity).is_some());
42+
}
2843
}
2944
}
3045
}

chapter-70-missiles/src/map_indexing_system.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
use specs::prelude::*;
2-
use super::{Map, Position, BlocksTile, Pools, spatial};
2+
use super::{Map, Position, BlocksTile, Pools, spatial, TileSize};
33

44
pub struct MapIndexingSystem {}
55

66
impl<'a> System<'a> for MapIndexingSystem {
7+
#[allow(clippy::type_complexity)]
78
type SystemData = ( ReadExpect<'a, Map>,
89
ReadStorage<'a, Position>,
910
ReadStorage<'a, BlocksTile>,
1011
ReadStorage<'a, Pools>,
12+
ReadStorage<'a, TileSize>,
1113
Entities<'a>,);
1214

1315
fn run(&mut self, data : Self::SystemData) {
14-
let (map, position, blockers, pools, entities) = data;
16+
let (map, position, blockers, pools, sizes, entities) = data;
1517

1618
spatial::clear();
1719
spatial::populate_blocked_from_map(&*map);
@@ -23,8 +25,21 @@ impl<'a> System<'a> for MapIndexingSystem {
2325
}
2426
}
2527
if alive {
26-
let idx = map.xy_idx(position.x, position.y);
27-
spatial::index_entity(entity, idx, blockers.get(entity).is_some());
28+
if let Some(size) = sizes.get(entity) {
29+
// Multi-tile
30+
for y in position.y .. position.y + size.y {
31+
for x in position.x .. position.x + size.x {
32+
if x > 0 && x < map.width-1 && y > 0 && y < map.height-1 {
33+
let idx = map.xy_idx(x, y);
34+
spatial::index_entity(entity, idx, blockers.get(entity).is_some());
35+
}
36+
}
37+
}
38+
} else {
39+
// Single tile
40+
let idx = map.xy_idx(position.x, position.y);
41+
spatial::index_entity(entity, idx, blockers.get(entity).is_some());
42+
}
2843
}
2944
}
3045
}

chapter-71-logging/src/map_indexing_system.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
use specs::prelude::*;
2-
use super::{Map, Position, BlocksTile, Pools, spatial};
2+
use super::{Map, Position, BlocksTile, Pools, spatial, TileSize};
33

44
pub struct MapIndexingSystem {}
55

66
impl<'a> System<'a> for MapIndexingSystem {
7+
#[allow(clippy::type_complexity)]
78
type SystemData = ( ReadExpect<'a, Map>,
89
ReadStorage<'a, Position>,
910
ReadStorage<'a, BlocksTile>,
1011
ReadStorage<'a, Pools>,
12+
ReadStorage<'a, TileSize>,
1113
Entities<'a>,);
1214

1315
fn run(&mut self, data : Self::SystemData) {
14-
let (map, position, blockers, pools, entities) = data;
16+
let (map, position, blockers, pools, sizes, entities) = data;
1517

1618
spatial::clear();
1719
spatial::populate_blocked_from_map(&*map);
@@ -23,8 +25,21 @@ impl<'a> System<'a> for MapIndexingSystem {
2325
}
2426
}
2527
if alive {
26-
let idx = map.xy_idx(position.x, position.y);
27-
spatial::index_entity(entity, idx, blockers.get(entity).is_some());
28+
if let Some(size) = sizes.get(entity) {
29+
// Multi-tile
30+
for y in position.y .. position.y + size.y {
31+
for x in position.x .. position.x + size.x {
32+
if x > 0 && x < map.width-1 && y > 0 && y < map.height-1 {
33+
let idx = map.xy_idx(x, y);
34+
spatial::index_entity(entity, idx, blockers.get(entity).is_some());
35+
}
36+
}
37+
}
38+
} else {
39+
// Single tile
40+
let idx = map.xy_idx(position.x, position.y);
41+
spatial::index_entity(entity, idx, blockers.get(entity).is_some());
42+
}
2843
}
2944
}
3045
}

chapter-72-textlayers/src/map_indexing_system.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
use specs::prelude::*;
2-
use super::{Map, Position, BlocksTile, Pools, spatial};
2+
use super::{Map, Position, BlocksTile, Pools, spatial, TileSize};
33

44
pub struct MapIndexingSystem {}
55

66
impl<'a> System<'a> for MapIndexingSystem {
7+
#[allow(clippy::type_complexity)]
78
type SystemData = ( ReadExpect<'a, Map>,
89
ReadStorage<'a, Position>,
910
ReadStorage<'a, BlocksTile>,
1011
ReadStorage<'a, Pools>,
12+
ReadStorage<'a, TileSize>,
1113
Entities<'a>,);
1214

1315
fn run(&mut self, data : Self::SystemData) {
14-
let (map, position, blockers, pools, entities) = data;
16+
let (map, position, blockers, pools, sizes, entities) = data;
1517

1618
spatial::clear();
1719
spatial::populate_blocked_from_map(&*map);
@@ -23,8 +25,21 @@ impl<'a> System<'a> for MapIndexingSystem {
2325
}
2426
}
2527
if alive {
26-
let idx = map.xy_idx(position.x, position.y);
27-
spatial::index_entity(entity, idx, blockers.get(entity).is_some());
28+
if let Some(size) = sizes.get(entity) {
29+
// Multi-tile
30+
for y in position.y .. position.y + size.y {
31+
for x in position.x .. position.x + size.x {
32+
if x > 0 && x < map.width-1 && y > 0 && y < map.height-1 {
33+
let idx = map.xy_idx(x, y);
34+
spatial::index_entity(entity, idx, blockers.get(entity).is_some());
35+
}
36+
}
37+
}
38+
} else {
39+
// Single tile
40+
let idx = map.xy_idx(position.x, position.y);
41+
spatial::index_entity(entity, idx, blockers.get(entity).is_some());
42+
}
2843
}
2944
}
3045
}

chapter-73-systems/src/systems/map_indexing_system.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
use specs::prelude::*;
2-
use crate::{Map, Position, BlocksTile, Pools, spatial};
2+
use crate::{Map, Position, BlocksTile, Pools, spatial, TileSize};
33

44
pub struct MapIndexingSystem {}
55

66
impl<'a> System<'a> for MapIndexingSystem {
7+
#[allow(clippy::type_complexity)]
78
type SystemData = ( ReadExpect<'a, Map>,
89
ReadStorage<'a, Position>,
910
ReadStorage<'a, BlocksTile>,
1011
ReadStorage<'a, Pools>,
12+
ReadStorage<'a, TileSize>,
1113
Entities<'a>,);
1214

1315
fn run(&mut self, data : Self::SystemData) {
14-
let (map, position, blockers, pools, entities) = data;
16+
let (map, position, blockers, pools, sizes, entities) = data;
1517

1618
spatial::clear();
1719
spatial::populate_blocked_from_map(&*map);
@@ -23,8 +25,21 @@ impl<'a> System<'a> for MapIndexingSystem {
2325
}
2426
}
2527
if alive {
26-
let idx = map.xy_idx(position.x, position.y);
27-
spatial::index_entity(entity, idx, blockers.get(entity).is_some());
28+
if let Some(size) = sizes.get(entity) {
29+
// Multi-tile
30+
for y in position.y .. position.y + size.y {
31+
for x in position.x .. position.x + size.x {
32+
if x > 0 && x < map.width-1 && y > 0 && y < map.height-1 {
33+
let idx = map.xy_idx(x, y);
34+
spatial::index_entity(entity, idx, blockers.get(entity).is_some());
35+
}
36+
}
37+
}
38+
} else {
39+
// Single tile
40+
let idx = map.xy_idx(position.x, position.y);
41+
spatial::index_entity(entity, idx, blockers.get(entity).is_some());
42+
}
2843
}
2944
}
3045
}

0 commit comments

Comments
 (0)