@ReceiveEvent(components = LocalPlayerComponent.class) public void onSelectedItemChanged(ChangedComponentEvent event, EntityRef entity) { for (UIItemCell cell : toolbar.getCells()) { cell.setSelectionRectangleEnable(false); } LocalPlayer localPlayer = CoreRegistry.get(LocalPlayer.class); LocalPlayerComponent localPlayerComp = localPlayer.getEntity().getComponent(LocalPlayerComponent.class); toolbar.getCells().get(localPlayerComp.selectedTool).setSelectionRectangleEnable(true); }
private void checkForLootedBlocks() { LocalPlayer player = CoreRegistry.get(LocalPlayer.class); // to send blocks to minions, some needed classes EntityRef playerent = player.getEntity(); LocalPlayerComponent localPlayerComp = playerent.getComponent(LocalPlayerComponent.class); MinionBarComponent inventory = null; EntityRef closestminion = null; if (localPlayerComp.minionMode) { inventory = playerent.getComponent(MinionBarComponent.class); } for (int i = _blocks.size() - 1; i >= 0; i--) { BlockRigidBody b = _blocks.get(i); if (b._temporary) continue; float closestDist = 99999; if (inventory != null) { // check for the closest minion Iterator<EntityRef> it = inventory.MinionSlots.iterator(); while (it.hasNext()) { EntityRef minion = it.next(); if (b.distanceToEntity(minion) < closestDist) { closestDist = b.distanceToEntity(minion); closestminion = minion; } } } if (localPlayerComp.minionMode) { if (closestDist < 8 && !b._picked) { b._picked = true; } } else { closestDist = b.distanceToPlayer(); // Check if the block is close enough to the player if (b.distanceToPlayer() < 8.0f && !b._picked) { // Mark it as picked and remove it from the simulation b._picked = true; } } // Block was marked as being picked if (b._picked && closestDist < 32.0f) { // Animate the movement in direction of the player if (closestDist > 1.0) { Transform t = new Transform(); b.getMotionState().getWorldTransform(t); Matrix4f tMatrix = new Matrix4f(); t.getMatrix(tMatrix); Vector3f blockPlayer = new Vector3f(); tMatrix.get(blockPlayer); if (localPlayerComp.minionMode && closestminion != null) { LocationComponent minionloc = closestminion.getComponent(LocationComponent.class); if (minionloc != null) blockPlayer.sub(minionloc.getWorldPosition()); else blockPlayer.sub(new Vector3f()); } else { blockPlayer.sub(new Vector3f(player.getPosition())); } blockPlayer.normalize(); blockPlayer.scale(-16000f); b.applyCentralImpulse(blockPlayer); } else { // TODO: Handle full inventories // TODO: Loot blocks should be entities // Block was looted (and reached the player) Block block = BlockManager.getInstance().getBlock(b.getType()); EntityRef blockItem = _blockItemFactory.newInstance(block.getBlockFamily()); if (localPlayerComp.minionMode) { if (closestminion != null) { closestminion.send(new ReceiveItemEvent(blockItem)); } } else { playerent.send(new ReceiveItemEvent(blockItem)); } ItemComponent itemComp = blockItem.getComponent(ItemComponent.class); if (itemComp != null && !itemComp.container.exists()) { blockItem.destroy(); } AudioManager.play(new AssetUri(AssetType.SOUND, "engine:Loot")); _blocks.remove(i); _discreteDynamicsWorld.removeRigidBody(b); } } } }