Ejemplo n.º 1
0
  public void update(float delta) {
    for (EntityRef entity :
        entityManager.iteratorEntities(
            SimpleAIComponent.class, CharacterMovementComponent.class, LocationComponent.class)) {
      LocationComponent location = entity.getComponent(LocationComponent.class);
      SimpleAIComponent ai = entity.getComponent(SimpleAIComponent.class);
      CharacterMovementComponent moveComp = entity.getComponent(CharacterMovementComponent.class);

      Vector3f worldPos = location.getWorldPosition();
      moveComp.getDrive().set(0, 0, 0);
      // TODO: shouldn't use local player, need some way to find nearest player
      LocalPlayer localPlayer = CoreRegistry.get(LocalPlayer.class);
      if (localPlayer != null) {
        Vector3f dist = new Vector3f(worldPos);
        dist.sub(localPlayer.getPosition());
        double distanceToPlayer = dist.lengthSquared();

        if (distanceToPlayer > 6 && distanceToPlayer < 16) {
          // Head to player
          ai.movementTarget.set(localPlayer.getPosition());
          ai.followingPlayer = true;
          entity.saveComponent(ai);
        } else {
          // Random walk
          if (Terasology.getInstance().getTimeInMs() - ai.lastChangeOfDirectionAt > 12000
              || ai.followingPlayer) {
            ai.movementTarget.set(
                worldPos.x + random.randomFloat() * 500,
                worldPos.y,
                worldPos.z + random.randomFloat() * 500);
            ai.lastChangeOfDirectionAt = Terasology.getInstance().getTimeInMs();
            ai.followingPlayer = false;
            entity.saveComponent(ai);
          }
        }

        Vector3f targetDirection = new Vector3f();
        targetDirection.sub(ai.movementTarget, worldPos);
        targetDirection.normalize();
        moveComp.setDrive(targetDirection);

        float yaw = (float) Math.atan2(targetDirection.x, targetDirection.z);
        AxisAngle4f axisAngle = new AxisAngle4f(0, 1, 0, yaw);
        location.getLocalRotation().set(axisAngle);
        entity.saveComponent(moveComp);
        entity.saveComponent(location);
      }
    }
  }
Ejemplo n.º 2
0
  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);
        }
      }
    }
  }