Example #1
0
 /**
  * destroys a minion at the end of their dying animation this implies that setting their animation
  * to die will destroy them.
  */
 @ReceiveEvent(components = {SkeletalMeshComponent.class, AnimationComponent.class})
 public void onAnimationEnd(AnimEndEvent event, EntityRef entity) {
   AnimationComponent animcomp = entity.getComponent(AnimationComponent.class);
   if (animcomp != null && event.getAnimation().equals(animcomp.dieAnim)) {
     entity.destroy();
   }
 }
  @Override
  public void update(float delta) {
    int processed = 0;
    PerformanceMonitor.startActivity("BlockChangedEventQueue");
    BlockChangedEvent event = eventQueue.poll();
    while (event != null) {
      logger.finer(
          String.format(
              "%s: %s -> %s",
              event.getBlockPosition(),
              event.getOldType().getBlockFamily(),
              event.getNewType().getBlockFamily()));
      getOrCreateEntityAt(event.getBlockPosition()).send(event);
      if (processed++ >= 4) {
        break;
      }
      event = eventQueue.poll();
    }
    PerformanceMonitor.endActivity();
    PerformanceMonitor.startActivity("Temp Blocks Cleanup");
    for (EntityRef entity : tempBlocks) {
      BlockComponent blockComp = entity.getComponent(BlockComponent.class);
      if (blockComp == null || !blockComp.temporary) continue;

      HealthComponent healthComp = entity.getComponent(HealthComponent.class);
      if (healthComp == null || healthComp.currentHealth == healthComp.maxHealth) {
        entity.destroy();
      }
    }
    tempBlocks.clear();
    PerformanceMonitor.endActivity();
  }
  @Override
  public void initialise() {
    this.entityManager = CoreRegistry.get(EntityManager.class);
    for (EntityRef blockComp : entityManager.iteratorEntities(BlockComponent.class)) {
      BlockComponent comp = blockComp.getComponent(BlockComponent.class);
      blockComponentLookup.put(new Vector3i(comp.getPosition()), blockComp);
    }

    for (EntityRef entity : entityManager.iteratorEntities(BlockComponent.class)) {
      BlockComponent blockComp = entity.getComponent(BlockComponent.class);
      if (blockComp.temporary) {
        HealthComponent health = entity.getComponent(HealthComponent.class);
        if (health == null
            || health.currentHealth == health.maxHealth
            || health.currentHealth == 0) {
          entity.destroy();
        }
      }
    }
  }
  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);
        }
      }
    }
  }