Observers, Global Observers, and Observer Objects
v1.0
Last updated
Was this helpful?
v1.0
Last updated
Was this helpful?
Observers are passive watchers of lifecycle events, and can act depending on what they see. are Monobehaviours
which can watch a Projectile.
The Observer
class derives from ProjectileBehavior
, as do all of the other classes, giving it access to lifecycle events on Projectiles. While behaviors aim to act upon the Projectile itself, an Observer is intended to act upon something external to the Projectile.
The Demo Actor Observer in the demo scene is used to recognize when a projetile fired by the "Player" Actor
collides with a "Target" Actor
in order to compute and transfer damage to the target.
The Turn Off On Collision Observer is used to turn off a GameObject
whenever a specific Projectile collides with something.
There are two ways to attach an Observer ScriptableObject
to a Projectile
.
The has an Observers List<Observer>
. Each Projectile that is created by this Spawner will be given an instance of each Observer in the list. This is how the Demo Actor Observer is added to all Projectiles the player spawns.
A Global Observer is an object that intends to be interested in Projectiles spawned by more than just one ProjectileSpawner
. In the demo scenes, the Camera
has a GlobalObserver
class on it, which shakes the camera anytime a projectile collides.
Global Observers subscribe to the , which repeats events from the Projectiles registered to it.
The Projectile Spawner also has one Observer assigned to the Observers
list.
Whenever CollisionEnter
or TriggerEnter
is called by a Projectile, the HitObject
method on this observer is called. If the HitObject
has a ProjectileDemoActor
component, then the Actor
who spawned the projectile will run the RegisterHit
method, which computes damage and passes it to the hit Actor
.
In the demo scene, the Camera
has a CameraShakeOnProjectileCollision
component on it. This class inherits from GlobalObserver
. This means it automatically subscribes to the FactoryManager
lifecycle events.
The class overrides the CollisionEnter
method, which is called whenever ANY projectile that is registered with the FactoryManager
collides.
Whenever this occurs, the method will shake the camera, with the shake impact determined by the distance the collision is from the camera itself.
Observer Objects are MonoBehaviours
which connect to an external Projectile, and have access to all the lifecycle events on that Projectile. Each one will derive from ObserverObject
.
I prefer to include "ObserverObject" in the class name. It's easier to find, and more clear what kind of class it is.
Spawner Observer Objects are MonoBehaviours
which connect to a ProjectileSpawner
, and have access to all the lifecycle events on that spawner. Each one will derive from SpawnerObserverObject
.
The SpawnOnSpawnPointsSpawnerObserverObject will spawn objects onto one or all of the Spawn Points at the events you select. In the example above, the objects will be spawned when the projecitles start shooting, and will be destroyed when they stop shooting. We use it in one of the Integraitons to show a "charging" particle, along with a Projectile that has a 0.5 second delay.
This observer has no exposed parameters. As an ProjectileObserver
, it automatically subscribes to all lifecycle events on each Projectile spawned -- this class inherits from ProjectileBehavior
, as do all of the .
The exists in all scenes, as it is a Static Scriptable Object
. This manager maintains a List
of all ProjectileSpawner
and Projectile
objects in the scene, along with , which are ProjectileObserver
objects.
In the example above, the will turn the list of objects on and off at the defined events.