Skip to content

Commit 065595e

Browse files
committed
Fix guide for get_available_exits trait
1 parent adcdbbd commit 065595e

1 file changed

Lines changed: 11 additions & 16 deletions

File tree

book/src/chapter_7.md

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,6 @@ fn get_available_exits(&self, idx:usize) -> rltk::SmallVec<[(usize, f32); 10]> {
4242
if self.is_exit_valid(x, y-1) { exits.push((idx-w, 1.0)) };
4343
if self.is_exit_valid(x, y+1) { exits.push((idx+w, 1.0)) };
4444

45-
// Diagonals
46-
if self.is_exit_valid(x-1, y-1) { exits.push(((idx-w)-1, 1.45)); }
47-
if self.is_exit_valid(x+1, y-1) { exits.push(((idx-w)+1, 1.45)); }
48-
if self.is_exit_valid(x-1, y+1) { exits.push(((idx+w)-1, 1.45)); }
49-
if self.is_exit_valid(x+1, y+1) { exits.push(((idx+w)+1, 1.45)); }
50-
5145
exits
5246
}
5347
```
@@ -255,22 +249,23 @@ Since we already put walls into the blocked list, this should take care of the i
255249
It would be nice to be able to bypass the monsters - and diagonal movement is a mainstay of roguelikes. So lets go ahead and support it. In `map.rs`'s `get_available_exits` function, we add them:
256250

257251
```rust
258-
fn get_available_exits(&self, idx:i32) -> Vec<(i32, f32)> {
259-
let mut exits : Vec<(i32, f32)> = Vec::new();
260-
let x = idx % self.width;
261-
let y = idx / self.width;
252+
fn get_available_exits(&self, idx:usize) -> rltk::SmallVec<[(usize, f32); 10]> {
253+
let mut exits = rltk::SmallVec::new();
254+
let x = idx as i32 % self.width;
255+
let y = idx as i32 / self.width;
256+
let w = self.width as usize;
262257

263258
// Cardinal directions
264259
if self.is_exit_valid(x-1, y) { exits.push((idx-1, 1.0)) };
265260
if self.is_exit_valid(x+1, y) { exits.push((idx+1, 1.0)) };
266-
if self.is_exit_valid(x, y-1) { exits.push((idx-self.width, 1.0)) };
267-
if self.is_exit_valid(x, y+1) { exits.push((idx+self.width, 1.0)) };
261+
if self.is_exit_valid(x, y-1) { exits.push((idx-w, 1.0)) };
262+
if self.is_exit_valid(x, y+1) { exits.push((idx+w, 1.0)) };
268263

269264
// Diagonals
270-
if self.is_exit_valid(x-1, y-1) { exits.push(((idx-self.width)-1, 1.45)); }
271-
if self.is_exit_valid(x+1, y-1) { exits.push(((idx-self.width)+1, 1.45)); }
272-
if self.is_exit_valid(x-1, y+1) { exits.push(((idx+self.width)-1, 1.45)); }
273-
if self.is_exit_valid(x+1, y+1) { exits.push(((idx+self.width)+1, 1.45)); }
265+
if self.is_exit_valid(x-1, y-1) { exits.push(((idx-w)-1, 1.45)); }
266+
if self.is_exit_valid(x+1, y-1) { exits.push(((idx-w)+1, 1.45)); }
267+
if self.is_exit_valid(x-1, y+1) { exits.push(((idx+w)-1, 1.45)); }
268+
if self.is_exit_valid(x+1, y+1) { exits.push(((idx+w)+1, 1.45)); }
274269

275270
exits
276271
}

0 commit comments

Comments
 (0)