Skip to content

Instantly share code, notes, and snippets.

@joshimoo
Last active August 29, 2015 14:16
Show Gist options
  • Save joshimoo/2f3b6fb6a559941494f7 to your computer and use it in GitHub Desktop.
Save joshimoo/2f3b6fb6a559941494f7 to your computer and use it in GitHub Desktop.
public class ExampleFactory extends EntityFactory{
public static Entity createEntity(Vector2f pos) {
Entity banana = createEntity(EntityType.PROJECTILE, pos);
/**
* Here are some ways to use the CollidedWithEvent.
* CollidedWithEvent is a custom Event that I wrote.
* @see de.tu_darmstadt.gdi1.gorillas.entities.events.CollidedWithEvent
*/
CollisionEvent collision = new CollisionEvent();
/** Specific Action for specific Collision Type */
ANDEvent gorillaCollision = new ANDEvent(collision, new CollidedWithEvent(collision, EntityType.PLAYER.name()));
gorillaCollision.addAction(new DestroyEntityAction());
banana.addComponent(gorillaCollision);
/** Same Action, for a set of targets */
CollidedWithEvent[] targets = new CollidedWithEvent[] {
new CollidedWithEvent(collision, EntityType.CLOUD.name()),
new CollidedWithEvent(collision, EntityType.SUN.name()),
};
// Because of the boolean exit early logic, valid targets is only evaluated if there was a collision first
// if you use this setup, collsion detection is also only evaluated once per frame, awesome :)
OREvent validTargets = new OREvent(targets);
ANDEvent groupCollision = new ANDEvent(collision, validTargets);
groupCollision.addAction((gc, sb, delta, event) -> {
System.out.println("Single Use Action, implemented as Lambda Block");
System.out.println("There was a collision && it was a valid target");
});
banana.addComponent(groupCollision);
/** Different Actions, for a set of targets */
CollidedWithEvent target0 = new CollidedWithEvent(collision, EntityType.CLOUD.name());
target0.addAction((gc, sb, delta, event) -> System.out.println("Do Target0 Action"));
CollidedWithEvent target1 = new CollidedWithEvent(collision, "DynamicRuntimeEntity");
target1.addAction((gc, sb, delta, event) -> System.out.println("Do Target1 Action"));
OREvent validTargetsIndividualActions = new OREvent(target0, target1);
validTargetsIndividualActions.addAction((gc, sb, delta, event) -> {
System.out.println("We can have inidivual actions per target");
System.out.println("but also general actions that executes as long as one target was valid");
System.out.println("It all depends on how you combine the boolean logic");
System.out.println("And where in the chain you add the action too");
});
banana.addComponent(new ANDEvent(collision, validTargetsIndividualActions));
/** Either Collision or other action */
// Collisions
ANDEvent validCollision = new ANDEvent(collision, new CollidedWithEvent(collision, EntityType.PROJECTILE.name()));
validCollision.addAction((gc, sb, delta, event) -> System.out.println("while we are colliding, change image to XXX"));
// No Collision
LoopEvent invalidTarget = new LoopEvent();
invalidTarget.addAction((gc, sb, delta, event) -> System.out.println("We are no longer colliding, change image back to YYY"));
// The OREvent makes sure that there is only one possible action executed
// Since OR only evaluates it's events till one of them is true
banana.addComponent(new OREvent(validCollision, invalidTarget));
/**
* A completely different approach is to create a new Collision Event
* That gets passed a collisionMatrix in it's constructor
* But generally this composition approach is more modular
*/
return banana;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment