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.
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:
|
eat
|
Vehicle
|
Dispatched on a vehicle when it consumes an Environmental like food or poison or is affected by a
PassiveEnvironmental
Properties:
|
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:
|
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:
|
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:
|
mutate
|
Gene
|
Dispatched on a gene when the gene mutates
Properties:
|
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:
|
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:
|