Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Sectors #2975

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
83cf26a
Implement sector-level entity stores
Vizaxo Jun 9, 2017
c4e6658
Rename EnittyStore to EntityCache (EntityStore already existed)
Vizaxo Jun 10, 2017
4bf43c8
Refactor EntityManager into EntityManager and EntityCache
Vizaxo Jun 13, 2017
ea66429
Implement clearing of EntityCache
Vizaxo Jun 13, 2017
25e5cd1
Add fixes and notes regarding EntityManager methods on sectors
Vizaxo Jun 13, 2017
aae66e2
Reorder methods in PojoEntityManager to be in their original order
Vizaxo Jun 14, 2017
b1ae5d1
Make createEntityRef check all caches
Vizaxo Jun 14, 2017
15ca85c
Allow any EntityRef to have invalidate() called on it
Vizaxo Jun 15, 2017
4bf88fa
Make getEntityStore return an unmodifiable map
Vizaxo Jun 16, 2017
47511ee
Make the EntityManager store entity components in the correct cache
Vizaxo Jun 16, 2017
0a537c6
Make small code style improvements
Vizaxo Jun 16, 2017
ae7a6d1
Add checks to handle null prefabs
Vizaxo Jun 16, 2017
1ee645b
Add checks to treat null component lists as empty lists
Vizaxo Jun 17, 2017
28eb9dd
Add the getAllEntities method to the EntityCache
Vizaxo Jun 18, 2017
5261096
Implement getEntitiesWith method for EntityCache
Vizaxo Jun 19, 2017
93542c9
Simplify creation of EntityBuilders with prefabs
Vizaxo Jun 20, 2017
fe0aa62
Add getExistingEntity method
Vizaxo Jun 20, 2017
5263091
Add hasComponent method to EntityCache
Vizaxo Jun 20, 2017
3cb07c2
Add the SectorManager and update PojoEntityManager to use it
Vizaxo Jun 20, 2017
bd8225e
Refactor entity creation methods to use EntityBuilder
Vizaxo Jun 20, 2017
19b710b
Refactor createEntityWithoutLifecycleEvents to use EntityBuilder
Vizaxo Jun 20, 2017
6d92ec0
Refactor EntityManager to extend EntityCache & add EngineEntityCache
Vizaxo Jun 24, 2017
d73c2f5
Refactor PojoSectorManager to implement EngineEntityCache
Vizaxo Jun 24, 2017
74411b3
Update getAllEntities method to iterate over all caches
Vizaxo Jun 26, 2017
dc5ea5f
Update getEntitiesWith method to iterate over all caches
Vizaxo Jun 27, 2017
487ae2d
Allow serialization of sector-scope entities
Vizaxo Jun 28, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
package org.terasology.entitySystem.entity;

import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.entitySystem.Component;
import org.terasology.entitySystem.MutableComponentContainer;
import org.terasology.entitySystem.entity.internal.EngineEntityManager;
import org.terasology.entitySystem.entity.internal.EntityInfoComponent;
import org.terasology.entitySystem.prefab.Prefab;

import java.util.Map;

Expand All @@ -30,11 +33,59 @@
*/
public class EntityBuilder implements MutableComponentContainer {


private static final Logger logger = LoggerFactory.getLogger(EntityBuilder.class);

private Map<Class<? extends Component>, Component> components = Maps.newHashMap();
private EngineEntityManager manager;
private EntityCache cache;
private EngineEntityManager entityManager;

private boolean sendLifecycleEvents = true;

public EntityBuilder(EngineEntityManager entityManager) {
this.entityManager = entityManager;
this.cache = entityManager.getGlobalCache();
}

public EntityBuilder(EngineEntityManager manager) {
this.manager = manager;
public EntityBuilder(EngineEntityManager entityManager, EntityCache cache) {
this.entityManager = entityManager;
this.cache = cache;
}

/**
* Adds all of the components from a prefab to this builder
*
* @param prefabName the name of the prefab to add
* @return whether the prefab was successfully added
*/
public boolean addPrefab(String prefabName) {
if (prefabName != null && !prefabName.isEmpty()) {
Prefab prefab = entityManager.getPrefabManager().getPrefab(prefabName);
if (prefab == null) {
logger.warn("Unable to instantiate unknown prefab: \"{}\"", prefabName);
return false;
}
addPrefab(prefab);
return true;
} else {
return false;
}
}
/**
* Adds all of the components from a prefab to this builder
*
* @param prefab the prefab to add
* @return whether the prefab was successfully added
*/
public void addPrefab(Prefab prefab) {
if (prefab != null) {
for (Component component : prefab.iterateComponents()) {
addComponent(entityManager.getComponentLibrary().copy(component));
}
addComponent(new EntityInfoComponent(prefab, prefab.isPersisted(), prefab.isAlwaysRelevant()));
} else {
addComponent(new EntityInfoComponent());
}
}

/**
Expand All @@ -43,11 +94,11 @@ public EntityBuilder(EngineEntityManager manager) {
* @return The built entity.
*/
public EntityRef build() {
return manager.create(components.values());
return cache.create(components.values(), sendLifecycleEvents);
}

public EntityRef buildWithoutLifecycleEvents() {
return manager.createEntityWithoutLifecycleEvents(components.values());
return cache.create(components.values(), false);
}

@Override
Expand Down Expand Up @@ -113,4 +164,12 @@ private EntityInfoComponent getEntityInfo() {
return entityInfo;
}

public boolean willSendLifecycleEvents() {
return sendLifecycleEvents;
}

public void setSendLifecycleEvents(boolean sendLifecycleEvents) {
this.sendLifecycleEvents = sendLifecycleEvents;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/*
* Copyright 2017 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.terasology.entitySystem.entity;

import org.terasology.entitySystem.Component;
import org.terasology.entitySystem.prefab.Prefab;
import org.terasology.math.geom.Quat4f;
import org.terasology.math.geom.Vector3f;

/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do a final iteration over the Javadoc in the end. Most of it is pretty self-documenting. However, try to avoid "empty" Javadoc where just the parameter names and such are auto-generated. A general summary of what the EntityCache actually is might be helpful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I will. I didn't want to go documenting lots of stuff now when it's still rapidly changing.

*/
public interface EntityCache {

/**
* Removes all entities from the cache.
*/
void clear();

/**
* Creates an EntityBuilder.
*
* @return A new entity builder
*/
EntityBuilder newBuilder();

/**
* Creates an EntityBuilder, from a prefab
*
* @return A new entity builder
*/
EntityBuilder newBuilder(String prefabName);

/**
* Creates an EntityBuilder, from a prefab
*
* @return A new entity builder
*/
EntityBuilder newBuilder(Prefab prefab);

/**
* @return A references to a new, unused entity
*/
EntityRef create();

/**
* @return A references to a new, unused entity with the desired components
*/
EntityRef create(Component... components);

/**
* @return A references to a new, unused entity with the desired components
*/
EntityRef create(Iterable<Component> components);


/**
* Creates a new entity from the given components.
*
* @param components the components to create this entity from
* @param sendLifecycleEvents will only send lifecycle events if this is true
* @return
*/
EntityRef create(Iterable<Component> components, boolean sendLifecycleEvents);

/**
* @param prefabName The name of the prefab to create.
* @return A new entity, based on the the prefab of the given name. If the prefab doesn't exist, just a new entity.
*/
EntityRef create(String prefabName);

/**
* @param prefab
* @return A new entity, based on the given prefab
*/
EntityRef create(Prefab prefab);

// TODO: Review. Probably better to move these into a static helper

/**
* @param prefab
* @param position
* @return A new entity, based on the given prefab, at the desired position
*/
EntityRef create(String prefab, Vector3f position);

/**
* @param prefab
* @param position
* @return A new entity, based on the given prefab, at the desired position
*/
EntityRef create(Prefab prefab, Vector3f position);

/**
* @param prefab
* @param position
* @param rotation
* @return
*/
EntityRef create(Prefab prefab, Vector3f position, Quat4f rotation);

/**
* Creates an entity but doesn't send any lifecycle events.
* <br><br>
* This is used by the block entity system to give an illusion of permanence to temporary block entities.
*
* @param components
* @return The newly created entity ref.
*/
EntityRef createEntityWithoutLifecycleEvents(Iterable<Component> components);

/**
* Creates an entity but doesn't send any lifecycle events.
* <br><br>
* This is used by the block entity system to give an illusion of permanence to temporary block entities.
*
* @param prefab
* @return The newly created entity ref.
*/
EntityRef createEntityWithoutLifecycleEvents(String prefab);

EntityRef createEntityWithoutLifecycleEvents(Prefab prefab);

/**
* Allows the creation of an entity with a given id - this is used
* when loading persisted entities
*
* @param id
* @param components
* @return The entityRef for the newly created entity
*/
EntityRef createEntityWithId(long id, Iterable<Component> components);

/**
* Creates an entity ref with the given id. This is used when loading components with references.
*
* @param id
* @return The entityRef for the given id
*/
EntityRef createEntityRefWithId(long id);

Iterable<EntityRef> getAllEntities();

/**
* @param componentClasses
* @return An iterable over all entities with the provided component types.
*/
Iterable<EntityRef> getEntitiesWith(Class<? extends Component>... componentClasses);

/**
* @param componentClasses
* @return A count of entities with the provided component types
*/
int getCountOfEntitiesWith(Class<? extends Component>... componentClasses);

/**
* @return A count of currently active entities
*/
int getActiveEntityCount();

/**
* Gets an entity, if it already exists.
*
* @param id the id of the desired entity
* @return the {@link EntityRef}, if it exists; {@link EntityRef#NULL} otherwise
*/
EntityRef getExistingEntity(long id);

}
Loading