Skip to content

Commit

Permalink
feat: players get blocks back after destroyed
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgazelka committed Nov 18, 2024
1 parent 93a55c8 commit 0f41061
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
16 changes: 7 additions & 9 deletions crates/hyperion-rank-tree/src/inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ impl Team {
}
}

pub const MAIN_SLOT: u16 = 0;
pub const PICKAXE_SLOT: u16 = 1;
pub const BLOCK_SLOT: u16 = 2;
pub const UPGRADE_START_SLOT: u16 = 3;
pub const GUI_SLOT: u16 = 8;

impl Rank {
pub fn apply_inventory(self, team: Team, inventory: &mut PlayerInventory, world: &World) {
const MAIN_SLOT: u16 = 0;
const PICKAXE_SLOT: u16 = 1;
const BUILD_SLOT: u16 = 2;

const UPGRADE_START_SLOT: u16 = 3;

const GUI_SLOT: u16 = 8;

let upgrade_not_available = ItemBuilder::new(ItemKind::GrayDye);

inventory.clear();
Expand Down Expand Up @@ -63,7 +61,7 @@ impl Rank {
inventory.set_hotbar(PICKAXE_SLOT, default_pickaxe);

let default_build_item = team.build_item().count(16).build();
inventory.set_hotbar(BUILD_SLOT, default_build_item);
inventory.set_hotbar(BLOCK_SLOT, default_build_item);

match self {
Self::Stick => {
Expand Down
38 changes: 30 additions & 8 deletions events/proof-of-concept/src/module/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
};

use flecs_ecs::{
core::{EntityViewGet, QueryBuilderImpl, SystemAPI, TableIter, TermBuilderImpl, World},
core::{Entity, EntityViewGet, QueryBuilderImpl, SystemAPI, TableIter, TermBuilderImpl, World},
macros::{Component, system},
prelude::Module,
};
Expand All @@ -27,6 +27,8 @@ use hyperion::{
text::IntoText,
},
};
use hyperion_inventory::PlayerInventory;
use hyperion_rank_tree::inventory;
use hyperion_scheduled::Scheduled;
use tracing::{error, info_span};

Expand All @@ -41,9 +43,14 @@ pub struct SetLevel {
pub stage: u8,
}

pub struct DestroyValue {
pub position: IVec3,
pub from: Entity,
}

#[derive(Default, Component)]
pub struct PendingDestruction {
pub destroy_at: Scheduled<Instant, IVec3>,
pub destroy_at: Scheduled<Instant, DestroyValue>,
pub set_level_at: Scheduled<Instant, SetLevel>,
}

Expand All @@ -56,6 +63,7 @@ impl Module for BlockModule {
world.set(PendingDestruction::default());

system!("handle_pending_air", world, &mut PendingDestruction($), &mut Blocks($), &Compose($))
.write::<PlayerInventory>()
.multi_threaded()
.each_iter(
move |it: TableIter<'_, false>,
Expand Down Expand Up @@ -87,9 +95,9 @@ impl Module for BlockModule {
.send(&world)
.unwrap();
}
for position in pending_air.destroy_at.pop_until(&now) {
for destroy in pending_air.destroy_at.pop_until(&now) {
// Play particle effect for block destruction
let center_block = position.as_dvec3() + DVec3::splat(0.5);
let center_block = destroy.position.as_dvec3() + DVec3::splat(0.5);

let particle_packet = play::ParticleS2c {
particle: Cow::Owned(Particle::Explosion),
Expand All @@ -116,7 +124,18 @@ impl Module for BlockModule {
.send(&world)
.unwrap();

blocks.set_block(position, BlockState::AIR).unwrap();
destroy.from
.entity_view(world)
.get::<&mut PlayerInventory>(|inventory| {
let stack = inventory
.get_hand_slot_mut(inventory::BLOCK_SLOT)
.unwrap();

stack.count = stack.count.saturating_add(1);
});


blocks.set_block(destroy.position, BlockState::AIR).unwrap();
}
},
);
Expand Down Expand Up @@ -186,7 +205,6 @@ impl Module for BlockModule {
};



let from = event.from;
let from_entity = world.entity_from_id(from);
from_entity.get::<(&NetworkStreamRef, &mut Xp)>(|(&net, xp)| {
Expand Down Expand Up @@ -220,7 +238,6 @@ impl Module for BlockModule {
});

system!("handle_placed_blocks", world, &mut Blocks($), &mut EventQueue<event::PlaceBlock>($), &mut PendingDestruction($))
.multi_threaded()
.each(move |(mc, event_queue, pending_air): (&mut Blocks, &mut EventQueue<event::PlaceBlock>, &mut PendingDestruction)| {
let span = info_span!("handle_placed_blocks");
let _enter = span.enter();
Expand All @@ -229,7 +246,12 @@ impl Module for BlockModule {

mc.set_block(position, event.block).unwrap();

pending_air.destroy_at.schedule(Instant::now() + TOTAL_DESTRUCTION_TIME, position);
let destroy = DestroyValue {
position,
from: event.from,
};

pending_air.destroy_at.schedule(Instant::now() + TOTAL_DESTRUCTION_TIME, destroy);

{
let sequence = fastrand::i32(..);
Expand Down

0 comments on commit 0f41061

Please sign in to comment.