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);
      }
    }
  }
Beispiel #2
0
  /**
   * Init. a new generator with a given seed value.
   *
   * @param seed The seed value
   */
  public PerlinNoise(int seed) {
    FastRandom rand = new FastRandom(seed);

    _noisePermutations = new int[512];
    int[] _noiseTable = new int[256];

    // Init. the noise table
    for (int i = 0; i < 256; i++) _noiseTable[i] = i;

    // Shuffle the array
    for (int i = 0; i < 256; i++) {
      int j = rand.randomInt() % 256;
      j = (j < 0) ? -j : j;

      int swap = _noiseTable[i];
      _noiseTable[i] = _noiseTable[j];
      _noiseTable[j] = swap;
    }

    // Finally replicate the noise permutations in the remaining 256 index positions
    for (int i = 0; i < 256; i++)
      _noisePermutations[i] = _noisePermutations[i + 256] = _noiseTable[i];
  }
  public BlockRigidBody[] addLootableBlocks(Vector3f position, Block block) {
    BlockRigidBody result[] = new BlockRigidBody[8];

    for (int i = 0; i < block.getLootAmount(); i++) {
      // Position the smaller blocks
      Vector3f offsetPossition =
          new Vector3f(
              (float) _random.randomDouble() * 0.5f,
              (float) _random.randomDouble() * 0.5f,
              (float) _random.randomDouble() * 0.5f);
      offsetPossition.add(position);

      result[i] =
          addBlock(
              offsetPossition,
              block.getId(),
              new Vector3f(0.0f, 4000f, 0.0f),
              BLOCK_SIZE.QUARTER_SIZE,
              false);
    }

    return result;
  }
  /** Init. a new random world. */
  public void initWorld(String title, String seed) {
    final FastRandom random = new FastRandom();

    // Get rid of the old world
    if (_worldRenderer != null) {
      _worldRenderer.dispose();
      _worldRenderer = null;
    }

    if (seed == null) {
      seed = random.randomCharacterString(16);
    } else if (seed.isEmpty()) {
      seed = random.randomCharacterString(16);
    }

    Terasology.getInstance()
        .getLogger()
        .log(Level.INFO, "Creating new World with seed \"{0}\"", seed);

    // Init. a new world
    _worldRenderer = new WorldRenderer(title, seed, _entityManager, _localPlayerSys);

    File entityDataFile =
        new File(Terasology.getInstance().getWorldSavePath(title), ENTITY_DATA_FILE);
    _entityManager.clear();
    if (entityDataFile.exists()) {
      try {
        _entityManager.load(entityDataFile, EntityManager.SaveFormat.Binary);
      } catch (IOException e) {
        _logger.log(Level.SEVERE, "Failed to load entity data", e);
      }
    }

    LocalPlayer localPlayer = null;
    Iterator<EntityRef> iterator =
        _entityManager.iteratorEntities(LocalPlayerComponent.class).iterator();
    if (iterator.hasNext()) {
      localPlayer = new LocalPlayer(iterator.next());
    } else {
      PlayerFactory playerFactory = new PlayerFactory(_entityManager);
      localPlayer =
          new LocalPlayer(
              playerFactory.newInstance(
                  new Vector3f(_worldRenderer.getWorldProvider().nextSpawningPoint())));
    }
    _worldRenderer.setPlayer(localPlayer);

    // Create the first Portal if it doesn't exist yet
    _worldRenderer.initPortal();

    fastForwardWorld();
    CoreRegistry.put(WorldRenderer.class, _worldRenderer);
    CoreRegistry.put(IWorldProvider.class, _worldRenderer.getWorldProvider());
    CoreRegistry.put(LocalPlayer.class, _worldRenderer.getPlayer());
    CoreRegistry.put(Camera.class, _worldRenderer.getActiveCamera());
    CoreRegistry.put(BulletPhysicsRenderer.class, _worldRenderer.getBulletRenderer());

    for (ComponentSystem system : _componentSystemManager.iterateAll()) {
      system.initialise();
    }
  }