Code snippets

Elliot Ford edited this page May 23, 2015 · 6 revisions

Creating and using a simple Entity type

public interface Friend extends Entity {

  @GetAttribute
  String getName();
  
  @SetAttribute
  void setName(String name);
}

Friend f = jalse.newEntity(Friend.class);
f.setName("Ellzord");

System.out.println(f.getName());

Filtering entities by type (using inheritance)

public interface Animal extends Entity{}
public interface FlyingAnimal extends Animal{}

...

jalse.streamEntitiesOfType(Animal.class).foreach(/* Feed */);

Creating an EntityListener

jalse.addEntityListener(new EntityListener() {

  public void entityKilled(EntityEvent event) {
    if (event.getEntity().isMarkedAsType(Evil.class)) {
      /* Spawn more */
    }
  }
});

Creating an AttributeListener

entity.addAttributeListener("danger", Attributes.BOOLEAN_TYPE, new AttributeListener<Boolean>(){

  public void attributeAdded(AttributeEvent<Boolean> event) {
    if (event.getValue()) {
      /* Run like hell */
    }
  }
});

Creating and scheduling an Action

StrandedSurvivors living = jalse.newEntity(StrandedSurvivors.class);
for (int i = 0; i < 10; i++) {
  living.newSurvivor();
}

living.scheduleForActor(new Action<StrandedSurvivors>() {

  public void perform(ActionContext<StrandedSurvivors> context) {
    context.getActor().streamSurvivors().foreach(Survivor::lookForFood);
  }
}, 0, 1, TimeUnit.SECONDS);

Adding and removing attributes

entity.setAttribute("falling", Attributes.BOOLEAN_TYPE, true);

...

entity.removeAttribute("falling", Attributes.BOOLEAN_TYPE);
entity.setAttribute("death", Attributes.newTypeOf(Date.class), new Date());
Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.