Events in the Sketch

This sketch makes heavy use of CustomEvents. See below for details on those events

The primary purpose of these events is to be able to centralize actions taken on the entire world. Rather than hook deep into methods like mutate() in the Gene class, these events allow the establishing of global event handlers that can respond to these changes to affect the world, manage lifespans, and update statistics

In addition all of these events, by default, bubble. Each class in the sketch inherits a parentTarget property that, when set, allows events to bubble. This property must be set manually. For Vehicles, this is set to the world, for Genes, it is set to DNA and DNA to Vehicle. The world does not get a parentTarget.

In order for events to work properly, creation of objects should be delegated entirely to the appropriate methods in the world object. Raw constructors should be avoided.

Objects must extend the CustomEventTarget class in order to have these custom events dispatched on them.

Back to program

Event Name Event Target Description
die Vehicle Dispatched on a vehicle when the vehicle dies. If a vehicle dies due to two different causes, the event will fire twice. Once for each cause. This is possible if say, a vehicle has its health reduced to zero and passes the maximum lifespan in the same frame.

Properties:
  • detail — object
    • self — the vehicle that died
    • cause — reason for death
eat Vehicle Dispatched on a vehicle when it consumes an Environmental like food or poison or is affected by a PassiveEnvironmental

Properties:
  • detail — object
    • augment — the amount of change in health the object provides
    • position — the position of the object that was eaten
eaten Environmental Dispatched on an Environmental when the it effects the Vehicle consuming it. Never fired on PassiveEnvironmentals which cannot be consumed. This event fires in affect

Properties:
  • detail — object
    • agent — the object which consumed the given Environmental
    • position — the position of the object that was eaten
invalidate Environmental Dispatched on an Environmental when the it is invalidated by a Vehicle consuming it. Never fired on PassiveEnvironmentals which cannot be consumed. This event fires in invalidate

Properties:
  • detail — object
    • type — the type of the Environmental (an actual class not string)
    • position — the position of the object that was eaten
malice Vehicle Dispatched on a vehicle when the vehicle commiting an act of malice performs the malice. This is an asymmetric event. Only the attacking vehicle has the event dispatched on it

Properties:
  • detail — object
    • self — the vehicle object doing the malice
    • target — the vehicle object being affected by the malice
mutate Gene Dispatched on a gene when the gene mutates

Properties:
  • detail — object
    • name — the name (string) of this Gene
    • old — the old value of the gene
    • current — the current value after mutation
    • parent? — the DNA object which owns this gene
reproduce Vehicle Dispatched on a both vehicles when the vehicles reproduce. This is a symmetric event meaning it fires twice (once on each vehicle) when two vehicles reproduce. It does not fire on the child; instead, the spawn event is dispatched on the child as it would be for any newly created vehicle. This event fires once per reproduction even if the vehicles produce multiple children. The spawn event is, however, dispatched on each child.

Properties:
  • detail — object
    • self — the vehicle object that is reproducing. The second event will fire with self and partner reversed since this is a symmetric event
    • partner — the vehicle reproducing with the current vehicle
    • children — array
    • an array containing all the children produced in this reproduction event
    • childCount - the number of offspring (litter size) produced by the reproduction.
spawn World Dispatched on the World object when it creates the given entity (Vehicle or Environmental). If two vehicles reproduce this event is fired for every spawned in new child, however, the reproduce event only fires once per reproduction regardless of litter size.

Properties:
  • detail — object
    • type — the type (class not string) of the object spawning
    • object — the new, spawned object itself
    • position — the location of the spawned object