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

refactor: use EntityBuilder for creating test character #97

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 24 additions & 16 deletions src/test/java/org/terasology/module/behaviors/MovementTests.java
Expand Up @@ -16,10 +16,10 @@
import org.junit.jupiter.params.provider.MethodSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.engine.entitySystem.entity.EntityBuilder;
import org.terasology.engine.entitySystem.entity.EntityManager;
import org.terasology.engine.entitySystem.entity.EntityRef;
import org.terasology.engine.logic.characters.CharacterMovementComponent;
import org.terasology.engine.logic.characters.CharacterTeleportEvent;
import org.terasology.engine.logic.location.LocationComponent;
import org.terasology.engine.physics.engine.PhysicsEngine;
import org.terasology.engine.registry.In;
Expand Down Expand Up @@ -325,30 +325,38 @@ void cleanUp() {
}

private EntityRef createMovingCharacter(float height, float radius, Vector3i start, Vector3i stop, String... movementTypes) {
EntityRef entity = entityManager.create("Behaviors:testcharacter");
entity.send(new CharacterTeleportEvent(new Vector3f(start)));

MinionMoveComponent minionMoveComponent = new MinionMoveComponent();
minionMoveComponent.setPathGoal(stop);
minionMoveComponent.movementTypes.addAll(Sets.newHashSet(movementTypes));
entity.addOrSaveComponent(minionMoveComponent);
EntityBuilder builder = entityManager.newBuilder("Behaviors:testCharacter");
builder.setSendLifecycleEvents(true);
builder.upsertComponent(MinionMoveComponent.class, maybeComponent -> {
MinionMoveComponent moveComponent = maybeComponent.orElse(new MinionMoveComponent());
moveComponent.setPathGoal(stop);
moveComponent.movementTypes.clear();
moveComponent.movementTypes.addAll(Sets.newHashSet(movementTypes));
return moveComponent;
});
builder.updateComponent(CharacterMovementComponent.class, characterMovement -> {
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder whether we should use upsertComponent all the time, or at least log something if the component is not present? 🤔 Should updateComponent itself log something (DEBUG level?) if the component is not present?

The main drawback of using updateComponent is that it will silently do nothing if the component is not present. So, upsertComponent is the safe bet to ensure the right thing will happen, while updateComponent is a bit nicer to read if you're pretty certain that the component will be present...

Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure whether I would encourage using updateComponent only because it's more readable but in some cases doesn't do what I want it to do based on an assumption I had that might have changed by now...
I guess upsertComponent might indeed be better here.

characterMovement.height = height;
characterMovement.radius = radius;
return characterMovement;
});
builder.updateComponent(LocationComponent.class, location -> {
location.setWorldPosition(new Vector3f(start));
return location;
});

CharacterMovementComponent charMovementComponent = entity.getComponent(CharacterMovementComponent.class);
charMovementComponent.height = height;
charMovementComponent.radius = radius;
entity.saveComponent(charMovementComponent);
EntityRef character = builder.build();
physicsEngine.recomputeCharacterCollider(character);

physicsEngine.removeCharacterCollider(entity);
physicsEngine.getCharacterCollider(entity);
return entity;
return character;
}

/**
* Detect path for entity at map {@code path}
*
* @param path map with path
* @param airHeight air height for world
* @param start (?) ref parameter - set start point
* @param stop (!) ref parameter - set end point
* @param stop (!) ref parameter - set end point
*/
private void detectPath(String[] path, int airHeight, Vector3i start, Vector3i stop) {
for (int z = 0; z < path.length; z++) {
Expand Down