public int compareTo(BlockRigidBody blockRigidBody) { if (blockRigidBody.calcAgeInMs() == calcAgeInMs()) { return 0; } if (blockRigidBody.calcAgeInMs() > calcAgeInMs()) return 1; else return -1; }
public void render() { FloatBuffer mBuffer = BufferUtils.createFloatBuffer(16); float[] mFloat = new float[16]; GL11.glPushMatrix(); Vector3d cameraPosition = _parent.getActiveCamera().getPosition(); GL11.glTranslated(-cameraPosition.x, -cameraPosition.y, -cameraPosition.z); List<CollisionObject> collisionObjects = _discreteDynamicsWorld.getCollisionObjectArray(); for (CollisionObject co : collisionObjects) { if (co.getClass().equals(BlockRigidBody.class)) { BlockRigidBody br = (BlockRigidBody) co; Block block = BlockManager.getInstance().getBlock(br.getType()); Transform t = new Transform(); br.getMotionState().getWorldTransform(t); t.getOpenGLMatrix(mFloat); mBuffer.put(mFloat); mBuffer.flip(); GL11.glPushMatrix(); GL11.glMultMatrix(mBuffer); if (br.getCollisionShape() == _blockShapeHalf) GL11.glScalef(0.5f, 0.5f, 0.5f); else if (br.getCollisionShape() == _blockShapeQuarter) GL11.glScalef(0.25f, 0.25f, 0.25f); block.renderWithLightValue(_parent.getRenderingLightValueAt(new Vector3d(t.origin))); GL11.glPopMatrix(); } } GL11.glPopMatrix(); }
/** * Adds a new physics block to be rendered as a rigid body. Translucent blocks are ignored. * * @param position The position * @param type The block type * @param impulse An impulse * @param size The size of the block * @return The created rigid body (if any) */ public synchronized BlockRigidBody addBlock( Vector3f position, byte type, Vector3f impulse, BLOCK_SIZE size, boolean temporary) { if (temporary && _blocks.size() > MAX_TEMP_BLOCKS) return null; BoxShape shape = _blockShape; Block block = BlockManager.getInstance().getBlock(type); if (block.isTranslucent()) return null; if (size == BLOCK_SIZE.HALF_SIZE) shape = _blockShapeHalf; else if (size == BLOCK_SIZE.QUARTER_SIZE) shape = _blockShapeQuarter; Matrix3f rot = new Matrix3f(); rot.setIdentity(); DefaultMotionState blockMotionState = new DefaultMotionState(new Transform(new Matrix4f(rot, position, 1.0f))); Vector3f fallInertia = new Vector3f(); shape.calculateLocalInertia(block.getMass(), fallInertia); RigidBodyConstructionInfo blockCI = new RigidBodyConstructionInfo(block.getMass(), blockMotionState, shape, fallInertia); BlockRigidBody rigidBlock = new BlockRigidBody(blockCI, type); rigidBlock.setRestitution(0.0f); rigidBlock.setAngularFactor(0.5f); rigidBlock.setFriction(0.5f); rigidBlock._temporary = temporary; // Apply impulse rigidBlock.applyImpulse(impulse, new Vector3f(0.0f, 0.0f, 0.0f)); _insertionQueue.add(rigidBlock); return rigidBlock; }
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); } } } }