Exemplo n.º 1
0
  public EntityRef newInstance(BlockFamily blockFamily, EntityRef blockEntity) {
    if (blockFamily == null) {
      return EntityRef.NULL;
    }

    EntityBuilder builder = entityManager.newBuilder("engine:blockItemBase");
    if (blockFamily.getArchetypeBlock().getLuminance() > 0) {
      builder.addComponent(new LightComponent());
    }

    // Copy the components from block prefab into the block item
    for (Component component : blockEntity.iterateComponents()) {
      if (component.getClass().getAnnotation(AddToBlockBasedItem.class) != null) {
        builder.addComponent(entityManager.getComponentLibrary().copy(component));
      }
    }

    ItemComponent item = builder.getComponent(ItemComponent.class);
    if (blockFamily.getArchetypeBlock().isStackable()) {
      item.stackId = "block:" + blockFamily.getURI().toString();
      item.stackCount = (byte) 1;
    }

    BlockItemComponent blockItem = builder.getComponent(BlockItemComponent.class);
    blockItem.blockFamily = blockFamily;

    return builder.build();
  }
Exemplo n.º 2
0
  /**
   * Use this method instead of {@link #newInstance(BlockFamily)} to modify entity properties like
   * the persistence flag before it gets created.
   *
   * @param blockFamily must not be null
   */
  public EntityBuilder newBuilder(BlockFamily blockFamily, int quantity) {
    EntityBuilder builder = entityManager.newBuilder("engine:blockItemBase");
    if (blockFamily.getArchetypeBlock().getLuminance() > 0) {
      builder.addComponent(new LightComponent());
    }

    // Copy the components from block prefab into the block item
    Prefab prefab = Assets.getPrefab(blockFamily.getArchetypeBlock().getPrefab());
    if (prefab != null) {
      for (Component component : prefab.iterateComponents()) {
        if (component.getClass().getAnnotation(AddToBlockBasedItem.class) != null) {
          builder.addComponent(entityManager.getComponentLibrary().copy(component));
        }
      }
    }

    DisplayNameComponent displayNameComponent = builder.getComponent(DisplayNameComponent.class);
    displayNameComponent.name = blockFamily.getDisplayName();

    ItemComponent item = builder.getComponent(ItemComponent.class);
    if (blockFamily.getArchetypeBlock().isStackable()) {
      item.stackId = "block:" + blockFamily.getURI().toString();
      item.stackCount = (byte) quantity;
    }

    BlockItemComponent blockItem = builder.getComponent(BlockItemComponent.class);
    blockItem.blockFamily = blockFamily;

    return builder;
  }
  private EntityRef createResultItem(String itemResult) {
    String resultText = itemResult;
    int count = 1;
    int starIndex = resultText.indexOf("*");
    if (starIndex > -1) {
      count = Integer.parseInt(resultText.substring(0, starIndex));
      resultText = resultText.substring(starIndex + 1);
    }

    EntityManager entityManager = CoreRegistry.get(EntityManager.class);
    BlockManager blockManager = CoreRegistry.get(BlockManager.class);
    PrefabManager prefabManager = CoreRegistry.get(PrefabManager.class);
    Prefab prefab = prefabManager.getPrefab(resultText);

    EntityRef result;
    if (prefab != null) {
      result = entityManager.create(prefab);
      ItemComponent item = result.getComponent(ItemComponent.class);
      item.stackCount = (byte) count;
      result.saveComponent(item);
    } else {
      BlockItemFactory blockItemFactory = new BlockItemFactory(entityManager);
      BlockFamily blockFamily = blockManager.getBlockFamily(resultText);
      result = blockItemFactory.newInstance(blockFamily, count);
    }
    return result;
  }
  @Override
  public boolean step() {
    EntityManager entityManager = context.get(EntityManager.class);
    WorldRenderer worldRenderer = context.get(WorldRenderer.class);

    Iterator<EntityRef> worldEntityIterator =
        entityManager.getEntitiesWith(WorldComponent.class).iterator();
    // TODO: Move the world renderer bits elsewhere
    if (worldEntityIterator.hasNext()) {
      EntityRef worldEntity = worldEntityIterator.next();
      worldRenderer.getChunkProvider().setWorldEntity(worldEntity);

      // get the world generator config from the world entity
      // replace the world generator values from the components in the world entity
      WorldGenerator worldGenerator = context.get(WorldGenerator.class);
      WorldConfigurator worldConfigurator = worldGenerator.getConfigurator();
      Map<String, Component> params = worldConfigurator.getProperties();
      for (Map.Entry<String, Component> entry : params.entrySet()) {
        Class<? extends Component> clazz = entry.getValue().getClass();
        Component comp = worldEntity.getComponent(clazz);
        if (comp != null) {
          worldConfigurator.setProperty(entry.getKey(), comp);
        }
      }
    } else {
      EntityRef worldEntity = entityManager.create();
      worldEntity.addComponent(new WorldComponent());
      worldRenderer.getChunkProvider().setWorldEntity(worldEntity);

      // transfer all world generation parameters from Config to WorldEntity
      WorldGenerator worldGenerator = context.get(WorldGenerator.class);
      SimpleUri generatorUri = worldGenerator.getUri();
      Config config = context.get(Config.class);

      // get the map of properties from the world generator.
      // Replace its values with values from the config set by the UI.
      // Also set all the components to the world entity.
      WorldConfigurator worldConfigurator = worldGenerator.getConfigurator();
      Map<String, Component> params = worldConfigurator.getProperties();
      for (Map.Entry<String, Component> entry : params.entrySet()) {
        Class<? extends Component> clazz = entry.getValue().getClass();
        Component comp = config.getModuleConfig(generatorUri, entry.getKey(), clazz);
        if (comp != null) {
          worldEntity.addComponent(comp);
          worldConfigurator.setProperty(entry.getKey(), comp);
        } else {
          worldEntity.addComponent(entry.getValue());
        }
      }
    }

    return true;
  }
  private void handlePressurePlateEvents() {
    Set<Vector3i> toRemoveSignal = Sets.newHashSet(activatedPressurePlates);

    Iterable<EntityRef> players =
        entityManager.getEntitiesWith(CharacterComponent.class, LocationComponent.class);
    for (EntityRef player : players) {
      Vector3f playerLocation = player.getComponent(LocationComponent.class).getWorldPosition();
      Vector3i locationBeneathPlayer =
          new Vector3i(playerLocation.x + 0.5f, playerLocation.y - 0.5f, playerLocation.z + 0.5f);
      Block blockBeneathPlayer = worldProvider.getBlock(locationBeneathPlayer);
      if (blockBeneathPlayer == signalPressurePlate) {
        EntityRef entityBeneathPlayer = blockEntityRegistry.getBlockEntityAt(locationBeneathPlayer);
        SignalProducerComponent signalProducer =
            entityBeneathPlayer.getComponent(SignalProducerComponent.class);
        if (signalProducer != null) {
          if (signalProducer.signalStrength == 0) {
            startProducingSignal(entityBeneathPlayer, -1);
            activatedPressurePlates.add(locationBeneathPlayer);
          } else {
            toRemoveSignal.remove(locationBeneathPlayer);
          }
        }
      }
    }

    for (Vector3i pressurePlateLocation : toRemoveSignal) {
      EntityRef pressurePlate = blockEntityRegistry.getBlockEntityAt(pressurePlateLocation);
      SignalProducerComponent signalProducer =
          pressurePlate.getComponent(SignalProducerComponent.class);
      if (signalProducer != null) {
        stopProducingSignal(pressurePlate);
        activatedPressurePlates.remove(pressurePlateLocation);
      }
    }
  }
  @Override
  public void update(float delta) {
    for (EntityRef entity : entityManager.getEntitiesWith(HealthComponent.class)) {
      HealthComponent health = entity.getComponent(HealthComponent.class);
      if (health.currentHealth <= 0) {
        continue;
      }

      if (health.currentHealth == health.maxHealth || health.regenRate == 0) {
        continue;
      }

      int healAmount = 0;
      healAmount = regenerateHealth(health, healAmount);

      checkHealed(entity, health, healAmount);
    }
  }
Exemplo n.º 7
0
  @ReceiveEvent
  public void onActivate(
      ActivateEvent event, EntityRef entity, TunnelActionComponent tunnelActionComponent) {

    Vector3f dir = new Vector3f(event.getDirection());
    dir.scale(4.0f);
    Vector3f origin = new Vector3f(event.getOrigin());
    origin.add(dir);
    Vector3i blockPos = new Vector3i();

    int particleEffects = 0;
    int blockCounter = tunnelActionComponent.maxDestroyedBlocks;
    for (int s = 0; s <= tunnelActionComponent.maxTunnelDepth; s++) {
      origin.add(dir);
      if (!worldProvider.isBlockRelevant(origin)) {
        break;
      }

      for (int i = 0; i < tunnelActionComponent.maxRaysCast; i++) {
        Vector3f direction = random.nextVector3f();
        Vector3f impulse = new Vector3f(direction);
        impulse.scale(tunnelActionComponent.explosiveForce);

        for (int j = 0; j < 3; j++) {
          Vector3f target = new Vector3f(origin);

          target.x += direction.x * j;
          target.y += direction.y * j;
          target.z += direction.z * j;

          blockPos.set((int) target.x, (int) target.y, (int) target.z);

          Block currentBlock = worldProvider.getBlock(blockPos);

          if (currentBlock.isDestructible()) {
            if (particleEffects < tunnelActionComponent.maxParticalEffects) {
              EntityBuilder builder = entityManager.newBuilder("engine:smokeExplosion");
              builder.getComponent(LocationComponent.class).setWorldPosition(target);
              builder.build();
              particleEffects++;
            }
            if (random.nextFloat() < tunnelActionComponent.thoroughness) {
              EntityRef blockEntity = blockEntityRegistry.getEntityAt(blockPos);
              blockEntity.send(
                  new DoDamageEvent(
                      tunnelActionComponent.damageAmount, tunnelActionComponent.damageType));
            }

            blockCounter--;
          }

          if (blockCounter <= 0) {
            return;
          }
        }
      }
    }
    // No blocks were destroyed, so cancel the event
    if (blockCounter == tunnelActionComponent.maxDestroyedBlocks) {
      event.consume();
    }
  }