Search in sources :

Example 1 with EntityBase

use of io.xol.chunkstories.api.entity.EntityBase in project chunkstories by Hugobros3.

the class WorldImplementation method addEntity.

@Override
public void addEntity(final Entity entity) {
    // Assign an UUID to entities lacking one
    if (this instanceof WorldMaster && entity.getUUID() == -1) {
        long nextUUID = nextEntityId();
        entity.setUUID(nextUUID);
    }
    Entity check = this.getEntityByUUID(entity.getUUID());
    if (check != null) {
        logger().error("Added an entity twice " + check + " conflits with " + entity + " UUID: " + entity.getUUID());
        // logger().save();
        Thread.dumpStack();
        // System.exit(-1);
        return;
    }
    // Add it to the world
    ((EntityBase) entity).markHasSpawned();
    assert entity.getWorld() == this;
    Chunk chunk = this.getChunkWorldCoordinates(entity.getLocation());
    if (chunk != null) {
        ((EntityBase) entity).positionComponent.trySnappingToChunk();
    }
    this.entities.insertEntity(entity);
}
Also used : EntityBase(io.xol.chunkstories.api.entity.EntityBase) Entity(io.xol.chunkstories.api.entity.Entity) Chunk(io.xol.chunkstories.api.world.chunk.Chunk) CubicChunk(io.xol.chunkstories.world.chunk.CubicChunk) WorldMaster(io.xol.chunkstories.api.world.WorldMaster)

Example 2 with EntityBase

use of io.xol.chunkstories.api.entity.EntityBase in project chunkstories by Hugobros3.

the class ServerPlayer method unsubscribeAll.

@Override
public void unsubscribeAll() {
    Iterator<Entity> iterator = getSubscribedToList();
    while (iterator.hasNext()) {
        Entity entity = iterator.next();
        // If one of the entities is controllable ...
        if (entity instanceof EntityControllable) {
            EntityControllable controllableEntity = (EntityControllable) entity;
            Controller entityController = controllableEntity.getControllerComponent().getController();
            // If said entity is controlled by this subscriber/player
            if (entityController == this) {
                // Set the controller to null
                controllableEntity.getControllerComponent().setController(null);
            }
        }
        ((EntityBase) entity).unsubscribe(this);
        iterator.remove();
    }
}
Also used : EntityBase(io.xol.chunkstories.api.entity.EntityBase) Entity(io.xol.chunkstories.api.entity.Entity) Controller(io.xol.chunkstories.api.entity.Controller) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable)

Example 3 with EntityBase

use of io.xol.chunkstories.api.entity.EntityBase in project chunkstories by Hugobros3.

the class WorldImplementation method tick.

@Override
public void tick() {
    // Iterates over every entity
    try {
        entitiesLock.writeLock().lock();
        Iterator<Entity> iter = this.getAllLoadedEntities();
        Entity entity;
        while (iter.hasNext()) {
            entity = iter.next();
            // Check entity's region is loaded
            if (entity.getChunk() != null)
                entity.tick();
            else
                // Tries to snap the entity to the region if it ends up being loaded
                ((EntityBase) entity).positionComponent.trySnappingToChunk();
        }
    } finally {
        entitiesLock.writeLock().unlock();
    }
    // Increase the ticks counter
    worldTicksCounter++;
    // Time cycle
    if (this instanceof WorldMaster && internalData.getBoolean("doTimeCycle", true))
        if (worldTicksCounter % 60 == 0)
            worldTime++;
}
Also used : EntityBase(io.xol.chunkstories.api.entity.EntityBase) Entity(io.xol.chunkstories.api.entity.Entity) WorldMaster(io.xol.chunkstories.api.world.WorldMaster)

Example 4 with EntityBase

use of io.xol.chunkstories.api.entity.EntityBase in project chunkstories by Hugobros3.

the class WorldImplementation method removeEntity.

@Override
public boolean removeEntity(Entity entity) {
    try {
        entitiesLock.writeLock().lock();
        if (entity != null) {
            EntityBase ent = (EntityBase) entity;
            // Only once
            if (ent.getComponentExistence().exists()) {
                // Destroys it
                ent.getComponentExistence().destroyEntity();
                // Removes it's reference within the region
                if (ent.positionComponent.getChunkWithin() != null)
                    ent.positionComponent.getChunkWithin().removeEntity(entity);
                // Actually removes it from the world list
                removeEntityFromList(entity);
                return true;
            }
        }
        return false;
    } finally {
        entitiesLock.writeLock().unlock();
    }
}
Also used : EntityBase(io.xol.chunkstories.api.entity.EntityBase)

Aggregations

EntityBase (io.xol.chunkstories.api.entity.EntityBase)4 Entity (io.xol.chunkstories.api.entity.Entity)3 WorldMaster (io.xol.chunkstories.api.world.WorldMaster)2 Controller (io.xol.chunkstories.api.entity.Controller)1 EntityControllable (io.xol.chunkstories.api.entity.interfaces.EntityControllable)1 Chunk (io.xol.chunkstories.api.world.chunk.Chunk)1 CubicChunk (io.xol.chunkstories.world.chunk.CubicChunk)1