Skip to content

Commit 7309186

Browse files
committed
C73 - fixes return issues in player.rs.
1 parent 2d035c8 commit 7309186

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

chapter-73-systems/src/player.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) -> RunState
133133
if pos.x + delta_x < 1 || pos.x + delta_x > map.width-1 || pos.y + delta_y < 1 || pos.y + delta_y > map.height-1 { return RunState::AwaitingInput; }
134134
let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y);
135135

136-
crate::spatial::for_each_tile_content(destination_idx, |potential_target| {
136+
result = crate::spatial::for_each_tile_content_with_gamemode(destination_idx, |potential_target| {
137137
if let Some(_vendor) = vendors.get(potential_target) {
138-
result = RunState::ShowVendor{ vendor: potential_target, mode : VendorMode::Sell }
138+
return Some(RunState::ShowVendor{ vendor: potential_target, mode : VendorMode::Sell });
139139
}
140140

141141
let mut hostile = true;
@@ -162,7 +162,7 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) -> RunState
162162
let mut ppos = ecs.write_resource::<Point>();
163163
ppos.x = pos.x;
164164
ppos.y = pos.y;
165-
result = RunState::Ticking;
165+
return Some(RunState::Ticking);
166166
} else {
167167
let target = combat_stats.get(potential_target);
168168
if let Some(_target) = target {
@@ -178,8 +178,9 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) -> RunState
178178
let glyph = renderables.get_mut(potential_target).unwrap();
179179
glyph.glyph = rltk::to_cp437('/');
180180
viewshed.dirty = true;
181-
result = RunState::Ticking;
181+
return Some(RunState::Ticking);
182182
}
183+
None
183184
});
184185

185186
if !crate::spatial::is_blocked(destination_idx) {

chapter-73-systems/src/spatial/mod.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::sync::Mutex;
22
use specs::prelude::*;
3-
use crate::{ Map, tile_walkable };
3+
use crate::{ Map, tile_walkable, RunState };
44

55
struct SpatialMap {
66
blocked : Vec<(bool, bool)>,
@@ -68,6 +68,19 @@ where F : FnMut(Entity)
6868
}
6969
}
7070

71+
pub fn for_each_tile_content_with_gamemode<F>(idx: usize, mut f: F) -> RunState
72+
where F : FnMut(Entity)->Option<RunState>
73+
{
74+
let lock = SPATIAL_MAP.lock().unwrap();
75+
for entity in lock.tile_content[idx].iter() {
76+
if let Some(rs) = f(entity.0) {
77+
return rs;
78+
}
79+
}
80+
81+
RunState::AwaitingInput
82+
}
83+
7184
pub fn get_tile_content_clone(idx:usize) -> Vec<Entity> {
7285
let lock = SPATIAL_MAP.lock().unwrap();
7386
lock.tile_content[idx].iter().map(|(e,_)| *e).collect()

0 commit comments

Comments
 (0)