Git Product home page Git Product logo

artemis-odb-eeel's Introduction

Build Status

artemis-odb-eeel

Easy Entity Event Listening

EEEL is a drop in plugin for Artemis-odb

EEEL provided an easy interface to subscribe to Entity events, using either Annotations, or registration.

EEEL also provides an event bus to dispatch entity-bound events, and listen to events based on the related entitie's aspect.

Susbscribing to Aspects

The first step to using EEEL is to add the plugin to your world:

WorldConfiguration config = new WorldConfigurationBuilder()
  .with(new EEELPlugin())
  ...
  .build();
world = new World(config);

Any system can then subscribe to entities.

Using Annotations

Any system can subscribe to Aspects by using the @Inserted and @Removed annotations, along side the @All, @One and @Exclude annotations.

class MySystem extends BaseSystem {
  
  @Inserted
  @All(Position.class)
  public void handlePositionEntityInsert(int entityId) {
    //do Something
  }
  
  @Inserted
  @All({life.class, damage.class})
  @Exclude(invurnerable.class)
  public void handleDamage(int entityId) {
    //do Something
  }
  
  @Removed
  @All(Position.class)
  public void handleRemoved(int entityId) {
    //do Something
  }
  
  @Inserted
  @Removed
  @All(TestComponent.class)
  public void addedOrRemoved(int entityId) {
    //do something
  }
 
  
  ...
}

As you can see, EEEL removes the need for event listeners, and thus makes complex systems much easier to manage.

Annotations in other objects

You can register any other objects to listen to events using the EEELSystem.register function:

world.getSystem(EEELSystem.class).register(MyListener);

Using method registration

Its possible you may not want to use Annotations due to the cost associated with invoking methods. This is why EEEL also provides a way to directly register methods (or lambdas) to be called. This is also useful if you need to register eventListeners on the fly.

This is does easily with the EEELSystem.inserted and EEELSystem.removed methods.

class MySystem extends BaseSystem {
    
    EEELSystem eeel;
    
    protected void initialize() {
        eeel.inserted(Aspect.all(health.class, attack.class), this::handleAttack);
        eeel.removed(Aspect.all(health.class), this::handleDead);
    }
    
    public handleAttack(int entity) {
        //do something
    }
    
    public handleDead(int entity) {
        //do something
    }
    
    ...
}

This example shows the use of Method references, but lambdas can be used in the same way:

eeel.inserted(Aspect.all(Position.class), entity -> {
   //do something
});

Custom Events

EEEL custom events is an experimental, proof of concept feature. EEEL also provides an Event Bus for custom events. Events can be dispatched for individual entities, and systems can subscribe to events according to their matching apsects.

Dispatching events

EEElEventSystem eventSystem;

 public void CalculateDamage(int entity) {
     int damage = 5;
     eventSystem.dispatchEvent(new DamageEvent(damage), entity);
 }

subscribing to events

EEELEventSystem eventSystem;

public void initialize() {
    eventSystem.registerEvent(this::onDamage, DamageEvent.class, Aspect.all(Health.class).exclude(Armor.class));
}

public void onDamage(DamageEvent event, int entity) {
    //do something
}

Getting started

Maven

<dependency>
	<groupId>net.fbridault.eeel</groupId>
	<artifactId>artemis-odb-eeel</artifactId>
	<version>1.2</version>
</dependency>

Gradle

dependencies { compile "net.fbridault.eeel:artemis-odb-eeel:1.2.1" }

artemis-odb-eeel's People

Contributors

dependabot[bot] avatar gmwolf avatar schosin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

mmmika schosin

artemis-odb-eeel's Issues

NPE in EEELEventSystem.registerEvent when invoked in BaseSystem#initialize

Adding event listeners in com.artemis.BaseSystem#initialize results in a NPE when EEELEventSystem#registerEvent is invoked before EEELEventSystem#initialize.

I added EEELPlugin using WorldConfigurationBuilder#with(ArtemisPlugin...). Since setup will only be called after all other systems are added, I am running into an NPE when trying to register event listeners before the listenerMap is initialized.

My suggestion would be to change the listenerMap in EEELEventSystem to a final field and initialize it inline. Otherwise the EEELPlugin would need to add the system with a higher priority, but even that may fail.

Advanced examples

I'm interested to see what EEEL would look like in a bigger project. Do you have anything you could share?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.