private EntityData.Entity serializeEntityDelta(EntityRef entityRef, Prefab prefab) {
    EntityData.Entity.Builder entity = EntityData.Entity.newBuilder();
    entity.setId(entityRef.getId());
    entity.setParentPrefab(prefab.getName());
    for (Component component : entityRef.iterateComponents()) {
      if (component.getClass().equals(EntityInfoComponent.class)) continue;

      Component prefabComponent = prefab.getComponent(component.getClass());
      EntityData.Component componentData;
      if (prefabComponent == null) {
        componentData = serializeComponent(component);
      } else {
        componentData = serializeComponent(prefabComponent, component);
      }

      if (componentData != null) {
        entity.addComponent(componentData);
      }
    }
    for (Component prefabComponent : prefab.listComponents()) {
      if (!entityRef.hasComponent(prefabComponent.getClass())) {
        entity.addRemovedComponent(ComponentUtil.getComponentClassName(prefabComponent.getClass()));
      }
    }
    return entity.build();
  }
Beispiel #2
0
 /** creates a zonelist component used to save all zones (persist) */
 private static void createZoneList() {
   zonelist = CoreRegistry.get(EntityManager.class).create();
   ZoneListComponent zonecomp = new ZoneListComponent();
   zonelist.addComponent(zonecomp);
   zonelist.setPersisted(true);
   zonelist.saveComponent(zonecomp);
 }
  public void renderTransparent() {

    for (EntityRef entity : entityManager.iteratorEntities(MiniaturizerComponent.class)) {
      MiniaturizerComponent min = entity.getComponent(MiniaturizerComponent.class);

      min.blockGrid.render();

      if (min.chunkMesh == null || min.renderPosition == null) continue;

      glPushMatrix();
      Vector3f cameraPosition =
          CoreRegistry.get(WorldRenderer.class).getActiveCamera().getPosition();
      GL11.glTranslated(
          min.renderPosition.x - cameraPosition.x,
          min.renderPosition.y - cameraPosition.y,
          min.renderPosition.z - cameraPosition.z);

      glScalef(
          MiniaturizerComponent.SCALE, MiniaturizerComponent.SCALE, MiniaturizerComponent.SCALE);
      glRotatef(min.orientation, 0, 1, 0);

      ShaderManager.getInstance().enableShader("chunk");
      ShaderManager.getInstance()
          .getShaderProgram("chunk")
          .setFloat("blockScale", MiniaturizerComponent.SCALE);

      min.chunkMesh.render(ChunkMesh.RENDER_PHASE.OPAQUE);
      min.chunkMesh.render(ChunkMesh.RENDER_PHASE.BILLBOARD_AND_TRANSLUCENT);
      min.chunkMesh.render(ChunkMesh.RENDER_PHASE.WATER_AND_ICE);
      glPopMatrix();
    }
  }
  @ReceiveEvent(components = {FenceGateComponent.class})
  public void onActivate(ActivateEvent event, EntityRef entity) {
    FenceGateComponent fenceGateComponent = entity.getComponent(FenceGateComponent.class);
    fenceGateComponent.isClosed = !fenceGateComponent.isClosed;
    entity.saveComponent(fenceGateComponent);

    BlockComponent blockComp = entity.getComponent(BlockComponent.class);
    if (blockComp == null) {
      event.cancel();
      return;
    }

    Vector3i primePos = new Vector3i(blockComp.getPosition());
    Block primeBlock = worldProvider.getBlock(primePos);

    Block newBlock = null;
    if (fenceGateComponent.isClosed) {
      newBlock =
          BlockManager.getInstance()
              .getBlockFamily("fences:FenceGateClosed")
              .getBlockForPlacing(worldProvider, primePos, primeBlock.getDirection(), Side.FRONT);
    } else {
      newBlock =
          BlockManager.getInstance()
              .getBlockFamily("fences:FenceGateOpen")
              .getBlockForPlacing(worldProvider, primePos, primeBlock.getDirection(), Side.FRONT);
    }

    if (newBlock != null) {
      blockEntityRegistry.setBlock(primePos, newBlock, primeBlock, entity);
    }
  }
Beispiel #5
0
 /**
  * adds a new zone to the corresponding zone list
  *
  * @param zone the zone to be added
  */
 public static void addZone(Zone zone) {
   ZoneListComponent zonelistcomp = zonelist.getComponent(ZoneListComponent.class);
   switch (zone.zonetype) {
     case Gather:
       {
         zonelistcomp.Gatherzones.add(zone);
         break;
       }
     case Work:
       {
         zonelistcomp.Workzones.add(zone);
         break;
       }
     case Terraform:
       {
         zonelistcomp.Terrazones.add(zone);
         break;
       }
     case Storage:
       {
         zonelistcomp.Storagezones.add(zone);
         break;
       }
     case OreonFarm:
       {
         zonelistcomp.OreonFarmzones.add(zone);
         break;
       }
   }
   zonelist.saveComponent(zonelistcomp);
 }
Beispiel #6
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 EntityRef getOrCreateEntityAt(Vector3i blockPosition) {
   EntityRef entity = getEntityAt(blockPosition);
   if (!entity.exists()) {
     return getOrCreateBlockEntityAt(blockPosition);
   }
   return entity;
 }
 @ReceiveEvent(components = {SimpleAIComponent.class})
 public void onBump(HorizontalCollisionEvent event, EntityRef entity) {
   CharacterMovementComponent moveComp = entity.getComponent(CharacterMovementComponent.class);
   if (moveComp != null && moveComp.isGrounded) {
     moveComp.jump = true;
     entity.saveComponent(moveComp);
   }
 }
Beispiel #10
0
 /**
  * Ugly way to retrieve a name from a prefab
  *
  * @return a ":" seperated string, with name and flavor text.
  */
 public static String getName() {
   PrefabManager prefMan = CoreRegistry.get(PrefabManager.class);
   Prefab prefab = prefMan.getPrefab("miniion:nameslist");
   EntityRef namelist = CoreRegistry.get(EntityManager.class).create(prefab);
   namelist.hasComponent(namesComponent.class);
   namesComponent namecomp = namelist.getComponent(namesComponent.class);
   Random rand = new Random();
   return namecomp.namelist.get(rand.nextInt(namecomp.namelist.size()));
 }
 public void refresh() {
   // container.fillInventoryCells(this);
   uiminionlist.removeAll();
   EntityManager entMan = CoreRegistry.get(EntityManager.class);
   for (EntityRef minion : entMan.iteratorEntities(MinionComponent.class)) {
     UIListItem listitem = new UIListItem(minion.getComponent(MinionComponent.class).name, minion);
     listitem.setTextColor(Color.black);
     listitem.addClickListener(minionistener);
     uiminionlist.addItem(listitem);
   }
 }
 public void removeMinionFromList(EntityRef minion) {
   for (UIListItem item : uiminionlist.getItems()) {
     EntityRef listminion = (EntityRef) item.getValue();
     if (listminion.getId() == minion.getId()) {
       uiminionlist.removeItem(item);
       // doesn't seem to work
       /*if(uiminionlist.getItemCount() > 0){
       	uiminionlist.select(0);
       	selected.setMinion((EntityRef)uiminionlist.getSelection().getValue());
       }*/
     }
   }
 }
Beispiel #13
0
 /**
  * triggered when a block was destroyed and dropped in the world used to intercept gathering by
  * minions and sending the block to their inventory the droppedblock in the world then gets
  * destroyed, possible duplication exploit
  */
 @ReceiveEvent(components = {MinionComponent.class})
 public void onBlockDropped(BlockDroppedEvent event, EntityRef entity) {
   if (entity.hasComponent(MinionComponent.class)) {
     EntityRef item;
     if (event.getOldBlock().getEntityMode() == BlockEntityMode.PERSISTENT) {
       item = blockItemFactory.newInstance(event.getOldBlock().getBlockFamily(), entity);
     } else {
       item = blockItemFactory.newInstance(event.getOldBlock().getBlockFamily());
     }
     entity.send(new ReceiveItemEvent(item));
     event.getDroppedBlock().destroy();
   }
 }
  private EntityData.Entity serializeEntityFull(EntityRef entityRef) {
    EntityData.Entity.Builder entity = EntityData.Entity.newBuilder();
    entity.setId(entityRef.getId());
    for (Component component : entityRef.iterateComponents()) {
      if (component.getClass().equals(EntityInfoComponent.class)) continue;

      EntityData.Component componentData = serializeComponent(component);
      if (componentData != null) {
        entity.addComponent(componentData);
      }
    }
    return entity.build();
  }
  public void update(float delta) {
    for (EntityRef entity : entityManager.iteratorEntities(MiniaturizerComponent.class)) {
      MiniaturizerComponent min = entity.getComponent(MiniaturizerComponent.class);

      if (min.chunkMesh == null && min.miniatureChunk != null) {
        min.chunkMesh =
            worldRenderer.getChunkTesselator().generateMinaturizedMesh(min.miniatureChunk);
        min.chunkMesh.generateVBOs();
        min.chunkMesh._vertexElements = null;
      }

      // min.orientation += delta * 15f;
    }
  }
Beispiel #16
0
 /**
  * The active minion, to be commanded by the minion command item etc uses a slightly different
  * texture to indicate selection
  *
  * @param minion : the new active minion entity
  */
 public static void setActiveMinion(EntityRef minion) {
   SkeletalMeshComponent skelcomp;
   if (activeminion != null) {
     skelcomp = activeminion.getComponent(SkeletalMeshComponent.class);
     if (skelcomp != null) {
       skelcomp.material = Assets.getMaterial("OreoMinions:OreonSkin");
     }
   }
   skelcomp = minion.getComponent(SkeletalMeshComponent.class);
   if (skelcomp != null) {
     skelcomp.material = Assets.getMaterial("OreoMinions:OreonSkinSelected");
   }
   activeminion = minion;
 }
Beispiel #17
0
 private static Vector3f getEntityPosition(EntityRef entity) {
   LocationComponent loc = entity.getComponent(LocationComponent.class);
   if (loc != null) {
     return loc.getWorldPosition();
   }
   return null;
 }
Beispiel #18
0
 private static Vector3f getEntityVelocity(EntityRef entity) {
   CharacterMovementComponent charMove = entity.getComponent(CharacterMovementComponent.class);
   if (charMove != null) {
     return charMove.getVelocity();
   }
   return new Vector3f();
 }
  void update(
      EntityRef localPlayer,
      float delta,
      EntityRef target,
      Vector3i targetBlockPos,
      Vector3f hitPosition,
      Vector3f hitNormal) {
    boolean posInput = positiveInput.getState() == ButtonState.DOWN;
    boolean negInput = negativeInput.getState() == ButtonState.DOWN;

    float targetValue = 0;
    if (!CoreRegistry.get(GUIManager.class).isConsumingInput()) {
      if (posInput) {
        targetValue += 1.0f;
      }
      if (negInput) {
        targetValue -= 1.0f;
      }
    }

    // TODO: Interpolate, based on some settings (immediate, linear, lerp?)

    float newValue = targetValue;

    if (sendEventMode.shouldSendEvent(value, newValue)) {
      event.prepare(id, newValue, delta);
      event.setTarget(target, targetBlockPos, hitPosition, hitNormal);
      localPlayer.send(event);
      sendEventToSubscribers(delta, target);
    }
    value = newValue;
  }
 @ReceiveEvent(components = {BlockRegionComponent.class})
 public void onNewBlockRegion(AddComponentEvent event, EntityRef entity) {
   BlockRegionComponent regionComp = entity.getComponent(BlockRegionComponent.class);
   blockRegions.put(entity, regionComp.region);
   for (Vector3i pos : regionComp.region) {
     blockRegionLookup.put(pos, entity);
   }
 }
Beispiel #21
0
 @Override
 public void shutdown() {
   if (activeminion != null) {
     SkeletalMeshComponent skelcomp = activeminion.getComponent(SkeletalMeshComponent.class);
     if (skelcomp != null) {
       skelcomp.material = Assets.getMaterial("OreoMinions:OreonSkin");
     }
   }
 }
 @Override
 public EntityRef getOrCreateBlockEntityAt(Vector3i blockPosition) {
   EntityRef blockEntity = getBlockEntityAt(blockPosition);
   if (!blockEntity.exists()) {
     Block block = getBlock(blockPosition.x, blockPosition.y, blockPosition.z);
     blockEntity = entityManager.create(block.getEntityPrefab());
     if (block.isEntityTemporary()) {
       tempBlocks.add(blockEntity);
     }
     blockEntity.addComponent(new LocationComponent(blockPosition.toVector3f()));
     blockEntity.addComponent(new BlockComponent(blockPosition, block.isEntityTemporary()));
     // TODO: Get regen and wait from block config?
     if (block.isDestructible()) {
       blockEntity.addComponent(new HealthComponent(block.getHardness(), 2.0f, 1.0f));
     }
   }
   return blockEntity;
 }
 @Override
 public EntityData.Entity serializeEntity(EntityRef entityRef) {
   EntityInfoComponent entityInfo = entityRef.getComponent(EntityInfoComponent.class);
   if (entityInfo != null) {
     if (entityInfo.parentPrefab != null && prefabManager.exists(entityInfo.parentPrefab)) {
       return serializeEntityDelta(entityRef, prefabManager.getPrefab(entityInfo.parentPrefab));
     }
   }
   return serializeEntityFull(entityRef);
 }
Beispiel #24
0
 private static Vector3f getEntityDirection(EntityRef entity) {
   LocationComponent loc = entity.getComponent(LocationComponent.class);
   if (loc != null) {
     Quat4f rot = loc.getWorldRotation();
     Vector3f dir = new Vector3f(0, 0, -1);
     QuaternionUtil.quatRotate(rot, dir, dir);
     return dir;
   }
   return new Vector3f();
 }
 @ReceiveEvent(components = {BlockRegionComponent.class})
 public void onBlockRegionChanged(ChangedComponentEvent event, EntityRef entity) {
   Region3i oldRegion = blockRegions.get(entity);
   for (Vector3i pos : oldRegion) {
     blockRegionLookup.remove(pos);
   }
   BlockRegionComponent regionComp = entity.getComponent(BlockRegionComponent.class);
   blockRegions.put(entity, regionComp.region);
   for (Vector3i pos : regionComp.region) {
     blockRegionLookup.put(pos, entity);
   }
 }
Beispiel #26
0
 /** order the minions by id and return the previous one, or the first if none were active! */
 public static void getPreviousMinion(boolean deleteactive) {
   EntityManager entman = CoreRegistry.get(EntityManager.class);
   List<Integer> sortedlist = new ArrayList<Integer>();
   for (EntityRef minion : entman.iteratorEntities(MinionComponent.class)) {
     if (!minion.getComponent(MinionComponent.class).dying) {
       sortedlist.add(minion.getId());
     }
   }
   if (sortedlist.size() == 0) {
     return;
   } else if (deleteactive && sortedlist.size() == 1) {
     activeminion = null;
     return;
   }
   Collections.sort(sortedlist);
   int index = 0;
   if (activeminion != null) {
     index = sortedlist.indexOf(activeminion.getId());
   }
   if (index == 0) {
     index = sortedlist.size() - 1;
   } else {
     index--;
   }
   index = sortedlist.get(index);
   for (EntityRef minion : entman.iteratorEntities(MinionComponent.class)) {
     if (minion.getId() == index) {
       setActiveMinion(minion);
     }
   }
 }
  @Override
  public EntityRef deserializeEntity(EntityData.Entity entityData) {
    EntityRef entity = entityManager.createEntityRefWithId(entityData.getId());
    if (entityData.hasParentPrefab()
        && !entityData.getParentPrefab().isEmpty()
        && prefabManager.exists(entityData.getParentPrefab())) {
      Prefab prefab = prefabManager.getPrefab(entityData.getParentPrefab());
      for (Component component : prefab.listComponents()) {
        String componentName = ComponentUtil.getComponentClassName(component.getClass());
        if (!containsIgnoreCase(componentName, entityData.getRemovedComponentList())) {
          entity.addComponent(componentLibrary.copy(component));
        }
      }
      entity.addComponent(new EntityInfoComponent(entityData.getParentPrefab()));
    }
    for (EntityData.Component componentData : entityData.getComponentList()) {
      Class<? extends Component> componentClass = getComponentClass(componentData);
      if (componentClass == null) continue;

      if (!entity.hasComponent(componentClass)) {
        entity.addComponent(deserializeComponent(componentData));
      } else {
        deserializeComponentOnto(entity.getComponent(componentClass), componentData);
      }
    }
    return entity;
  }
    public float distanceToEntity(EntityRef target) {
      Transform t = new Transform();
      getMotionState().getWorldTransform(t);
      Matrix4f tMatrix = new Matrix4f();
      t.getMatrix(tMatrix);

      Vector3f blockPlayer = new Vector3f();
      tMatrix.get(blockPlayer);

      LocationComponent loc = target.getComponent(LocationComponent.class);
      if (loc != null) blockPlayer.sub(loc.getWorldPosition());
      else blockPlayer.sub(new Vector3f());

      return blockPlayer.length();
    }
  public void update(float delta) {
    /* GUI */
    updateUserInterface();

    for (UpdateSubscriberSystem updater : _componentSystemManager.iterateUpdateSubscribers()) {
      PerformanceMonitor.startActivity(updater.getClass().getSimpleName());
      updater.update(delta);
    }

    if (_worldRenderer != null && shouldUpdateWorld()) _worldRenderer.update(delta);

    if (!screenHasFocus()) _localPlayerSys.updateInput();

    if (screenHasFocus() || !shouldUpdateWorld()) {
      if (Mouse.isGrabbed()) {
        Mouse.setGrabbed(false);
        Mouse.setCursorPosition(Display.getWidth() / 2, Display.getHeight() / 2);
      }
    } else {
      if (!Mouse.isGrabbed()) Mouse.setGrabbed(true);
    }

    // TODO: This seems a little off - plus is more of a UI than single player game state concern.
    // Move somewhere
    // more appropriate?
    boolean dead = true;
    for (EntityRef entity : _entityManager.iteratorEntities(LocalPlayerComponent.class)) {
      dead = entity.getComponent(LocalPlayerComponent.class).isDead;
    }
    if (dead) {
      _statusScreen.setVisible(true);
      _statusScreen.updateStatus("Sorry! Seems like you have died. :-(");
    } else {
      _statusScreen.setVisible(false);
    }
  }
  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);
      }
    }
  }