Last active
December 29, 2015 20:29
-
-
Save Buildstarted/7724218 to your computer and use it in GitHub Desktop.
Trigger object to define areas that cause events.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export enum TriggerType { | |
TriggerOnceAndRemove, | |
TriggerOnEnter, | |
TriggerOnLeave, | |
TriggerOnLeaveAndRemove, | |
TriggerOnEnterAndLeave | |
} | |
export class Trigger<T> extends eg.Collision.Collidable { | |
public Name: string; | |
public OnTriggered: eg.EventHandler1<T>; | |
public OnLeave: eg.EventHandler; | |
public Bounds: eg.Bounds.Bounds2d; | |
private _graphic: eg.Graphics.Rectangle; | |
private _triggerType: TriggerType; | |
private _lastCollisionObject: any; | |
private _disposing: boolean = false; | |
public get Graphic(): eg.Graphics.Rectangle { | |
//lazily load the graphic | |
if (!this._graphic) { | |
var bounds = <eg.Bounds.BoundingRectangle>this.Bounds; | |
this._graphic = new eg.Graphics.Rectangle(bounds.Position.X, bounds.Position.Y, bounds.Size.Width, bounds.Size.Height, eg.Graphics.Color.FromARGB(.25, 255, 0, 0)); | |
this._graphic.AddChild(new eg.Graphics.Text2d(0, 0, this.Name)); | |
} | |
return this._graphic; | |
} | |
constructor(name: string, triggerType: TriggerType, x: number, y: number, width: number, height: number) { | |
super(new eg.Bounds.BoundingRectangle(new eg.Vector2d(x, y), new eg.Size2d(width, height))); | |
this.Name = name; | |
this._triggerType = triggerType; | |
this.OnTriggered = new eg.EventHandler1<T>(); | |
this.OnLeave = new eg.EventHandler(); | |
this.OnCollision.Bind((data: eg.Collision.CollisionData) => this.OnCollided(data)); | |
this.OnDisposed.Bind(() => this.Disposing()); | |
} | |
private Disposing(): void { | |
if (!this._disposing) { | |
if (this._graphic) { | |
this._graphic.Dispose(); | |
} | |
this.OnTriggered.Dispose(); | |
this.OnLeave.Dispose(); | |
this._disposing = true; | |
} | |
} | |
private OnCollided(data: eg.Collision.CollisionData): void { | |
if (!this._lastCollisionObject) { | |
switch (this._triggerType) { | |
case TriggerType.TriggerOnEnter: | |
case TriggerType.TriggerOnceAndRemove: | |
case TriggerType.TriggerOnEnterAndLeave: | |
this.OnTriggered.Trigger(<T>(<any>data.With)); | |
break; | |
} | |
this._lastCollisionObject = data.With; | |
if (this._triggerType === TriggerType.TriggerOnceAndRemove) { | |
this.Dispose(); | |
} | |
} | |
} | |
public Update(gameTime: eg.GameTime): void { | |
if (this._lastCollisionObject && !this.IsCollidingWith(this._lastCollisionObject)) { | |
switch (this._triggerType) { | |
case TriggerType.TriggerOnLeave: | |
case TriggerType.TriggerOnLeaveAndRemove: | |
case TriggerType.TriggerOnEnterAndLeave: | |
this.OnTriggered.Trigger(<T>(<any>this._triggerType)); | |
} | |
this.OnLeave.Trigger(); | |
this._lastCollisionObject = null; | |
if (this._triggerType === TriggerType.TriggerOnLeaveAndRemove) { | |
this.Dispose(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment