Skip to content

Commit

Permalink
Merge pull request #331 from AnthonyTornetta/330-dropped-item-entity
Browse files Browse the repository at this point in the history
330 dropped item entity
  • Loading branch information
AnthonyTornetta authored Sep 12, 2024
2 parents 9ce7ecf + b09145e commit 8cdd042
Show file tree
Hide file tree
Showing 24 changed files with 970 additions and 272 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ See [the issues page](https://github.com/AnthonyTornetta/Cosmos/issues) for the
- [ ] 1 Additional forest
- [ ] Ocean
- [x] On-planet skybox
- [ ] Dropped item entity
- [ ] When storage is broken, drop items on ground
- [ ] If not enough inventory room when player is mining something, drop item
- [x] Dropped item entity
- [x] When storage is broken, drop items on ground
- [x] If not enough inventory room when player is mining something, drop item
- [x] Fix missing chunks on planets

## Release 0.0.6a
Expand Down
34 changes: 29 additions & 5 deletions cosmos_client/src/asset/asset_loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,21 @@ pub enum AssetsSet {
AssetsReady,
}

/// In PostLoading state.
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
pub enum ItemMeshingLoadingSet {
/// Registries [`ItemTextureIndex`] and [`ItemRenderingInfo`] are setup.
LoadItemRenderingInformation,
/// The model files for blocks are loaded.
LoadBlockModels,
/// The model files for items are loaded.
LoadItemModels,
/// Meshes are inserted into the [`ItemRenderingInfo`] registry.
///
/// This MUST happen after both block and item model files are properly loaded.
GenerateMeshes,
}

pub(super) fn register(app: &mut App) {
registry::create_registry::<BlockTextureIndex>(app, "cosmos:block_texture_index");
registry::create_registry::<ItemTextureIndex>(app, "cosmos:item_texture_index");
Expand All @@ -877,6 +892,17 @@ pub(super) fn register(app: &mut App) {

app.configure_sets(Update, (AssetsSet::AssetsLoading, AssetsSet::AssetsReady).chain());

app.configure_sets(
OnExit(GameState::PostLoading),
(
ItemMeshingLoadingSet::LoadItemRenderingInformation,
ItemMeshingLoadingSet::LoadBlockModels,
ItemMeshingLoadingSet::LoadItemModels,
ItemMeshingLoadingSet::GenerateMeshes,
)
.chain(),
);

app.add_event::<AssetsDoneLoadingEvent>()
.add_event::<AllTexturesDoneLoadingEvent>()
.add_systems(
Expand All @@ -892,10 +918,8 @@ pub(super) fn register(app: &mut App) {
.add_systems(OnEnter(GameState::PostLoading), setup_textures)
.add_systems(
OnExit(GameState::PostLoading),
(load_item_rendering_information, load_block_rendering_information).chain(),
(load_item_rendering_information, load_block_rendering_information)
.in_set(ItemMeshingLoadingSet::LoadItemRenderingInformation)
.chain(),
);

// It's probably fine
// app.allow_ambiguous_resource::<Events<AllTexturesDoneLoadingEvent>>();
// app.allow_ambiguous_resource::<Registry<CosmosTextureAtlas>>();
}
6 changes: 4 additions & 2 deletions cosmos_client/src/asset/materials/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use cosmos_core::{

use crate::{rendering::MeshInformation, state::game_state::GameState};

use super::asset_loading::{load_block_rendering_information, AssetsSet, BlockRenderingInfo, ItemRenderingInfo};
use super::asset_loading::{load_block_rendering_information, AssetsSet, BlockRenderingInfo, ItemMeshingLoadingSet, ItemRenderingInfo};

pub mod animated_material;
pub mod block_materials;
Expand Down Expand Up @@ -347,7 +347,9 @@ pub(super) fn register(app: &mut App) {

app.add_systems(
OnExit(GameState::PostLoading),
register_materials.after(load_block_rendering_information),
(load_block_rendering_information, register_materials)
.chain()
.in_set(ItemMeshingLoadingSet::LoadItemRenderingInformation),
)
.add_event::<RemoveAllMaterialsEvent>()
.add_event::<AddMaterialEvent>();
Expand Down
8 changes: 8 additions & 0 deletions cosmos_client/src/input/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ pub enum CosmosInputs {
///
/// This will cause super lag
PanoramaScreenshot,

/// Drops the held item
DropItem,
/// Indicates it should drop the whole stack
BulkDropFlag,
}

fn init_input(mut input_handler: ResMut<CosmosInputHandler>) {
Expand Down Expand Up @@ -171,6 +176,9 @@ fn init_input(mut input_handler: ResMut<CosmosInputHandler>) {
input_handler.set_keycode(CosmosInputs::AlternateInteraction, KeyCode::ShiftLeft);

input_handler.set_keycode(CosmosInputs::PanoramaScreenshot, KeyCode::F9);

input_handler.set_keycode(CosmosInputs::DropItem, KeyCode::KeyG);
input_handler.set_keycode(CosmosInputs::BulkDropFlag, KeyCode::ControlLeft);
}

#[derive(Resource, Default, Debug)]
Expand Down
39 changes: 39 additions & 0 deletions cosmos_client/src/inventory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use cosmos_core::{
},
ecs::NeedsDespawned,
inventory::{
held_item_slot::HeldItemSlot,
itemstack::ItemStack,
netty::{ClientInventoryMessages, InventoryIdentifier},
HeldItemStack, Inventory,
Expand Down Expand Up @@ -395,6 +396,43 @@ fn toggle_inventory_rendering(
}
}

fn drop_item(
input_checker: InputChecker,
q_inventory: Query<(Entity, &Inventory, &HeldItemSlot), With<LocalPlayer>>,
mut client: ResMut<RenetClient>,
network_mapping: Res<NetworkMapping>,
) {
if !input_checker.check_just_pressed(CosmosInputs::DropItem) {
return;
}

let Ok((local_player_entity, inventory, held_item_slot)) = q_inventory.get_single() else {
return;
};

let selected_slot = held_item_slot.slot() as usize;
let Some(is) = inventory.itemstack_at(selected_slot) else {
return;
};

let Some(server_player_ent) = network_mapping.server_from_client(&local_player_entity) else {
return;
};

client.send_message(
NettyChannelClient::Inventory,
cosmos_encoder::serialize(&ClientInventoryMessages::ThrowItemstack {
quantity: if input_checker.check_pressed(CosmosInputs::BulkDropFlag) {
is.quantity()
} else {
1
},
slot: selected_slot as u32,
inventory_holder: InventoryIdentifier::Entity(server_player_ent),
}),
);
}

fn reposition_window_children(
mut q_style: Query<(&StyleOffsets, &mut Style), Without<InventoryTitleBar>>,
q_changed_window_pos: Query<(&Style, &InventoryTitleBar), Changed<Style>>,
Expand Down Expand Up @@ -923,6 +961,7 @@ pub(super) fn register(app: &mut App) {
.add_systems(
Update,
(
drop_item,
(toggle_inventory, close_button_system)
.chain()
.in_set(InventorySet::ToggleInventory),
Expand Down
Loading

0 comments on commit 8cdd042

Please sign in to comment.