Prevented pickups with full inventory

This commit is contained in:
Joshua Barretto 2019-09-25 23:53:43 +01:00
parent 57a2313348
commit a6d5b82ef5
4 changed files with 24 additions and 8 deletions

View File

@ -45,6 +45,10 @@ impl Inventory {
}
}
pub fn is_full(&self) -> bool {
self.slots.iter().all(|slot| slot.is_some())
}
/// Get content of a slot
pub fn get(&self, cell: usize) -> Option<&Item> {
self.slots.get(cell).and_then(Option::as_ref)

View File

@ -1035,7 +1035,14 @@ impl Server {
ClientMsg::CollectBlock(pos) => {
let block = state.terrain().get(pos).ok().copied();
if let Some(block) = block {
if block.is_collectible() {
if block.is_collectible()
&& state
.ecs()
.read_storage::<comp::Inventory>()
.get(entity)
.map(|inv| !inv.is_full())
.unwrap_or(false)
{
if state.try_set_block(pos, Block::empty()).is_some() {
comp::Item::try_reclaim_from_block(block)
.map(|item| state.give_item(entity, item));
@ -1409,17 +1416,22 @@ impl Drop for Server {
}
trait StateExt {
fn give_item(&mut self, entity: EcsEntity, item: comp::Item);
fn give_item(&mut self, entity: EcsEntity, item: comp::Item) -> bool;
fn apply_effect(&mut self, entity: EcsEntity, effect: Effect);
}
impl StateExt for State {
fn give_item(&mut self, entity: EcsEntity, item: comp::Item) {
self.ecs()
fn give_item(&mut self, entity: EcsEntity, item: comp::Item) -> bool {
let success = self
.ecs()
.write_storage::<comp::Inventory>()
.get_mut(entity)
.map(|inv| inv.push(item));
self.write_component(entity, comp::InventoryUpdate);
.map(|inv| inv.push(item).is_none())
.unwrap_or(false);
if success {
self.write_component(entity, comp::InventoryUpdate);
}
success
}
fn apply_effect(&mut self, entity: EcsEntity, effect: Effect) {

View File

@ -445,7 +445,7 @@ impl<'a> ZCache<'a> {
}
});
let ground_max = (self.sample.alt + 3.0 + warp + rocks).max(cliff);
let ground_max = (self.sample.alt + 4.0 + warp + rocks).max(cliff);
let min = min + structure_min;
let max = (ground_max + structure_max)

View File

@ -128,7 +128,7 @@ impl WorldSim {
cave_0_nz: SuperSimplex::new().set_seed(rng.gen()),
cave_1_nz: SuperSimplex::new().set_seed(rng.gen()),
structure_gen: StructureGen2d::new(rng.gen(), 32, 24),
structure_gen: StructureGen2d::new(rng.gen(), 32, 16),
region_gen: StructureGen2d::new(rng.gen(), 400, 96),
cliff_gen: StructureGen2d::new(rng.gen(), 80, 56),
humid_nz: Billow::new()