Created
December 20, 2014 11:55
-
-
Save triplefox/d7e558a449caaa96e13c to your computer and use it in GitHub Desktop.
Example event system
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
| class Eventsys | |
| { | |
| public var entities = new Map<Int, IEntity>(); | |
| public function new(){} | |
| /* raw spawning tools. instantiate and assign parameters to entities with your own configuration mechanism. */ | |
| public function spawn(e : IEntity, id : Int) { if (entities.exists(id)) { throw "overlapping entity id"; } e.id = id; entities.set(id, e); } | |
| public function despawn(e : IEntity) { entities.remove(e.id); } | |
| public function update(events : Array<Event>) | |
| { | |
| while (events.length > 0) | |
| { | |
| var e = events.shift(); | |
| for (r in entities.get(e.target[e.target.length-1]).listen(e)) events.push(r); | |
| } | |
| } | |
| public function heartbeat() | |
| { | |
| update([for (e in entities) new Event([e.id], Heartbeat)]); | |
| } | |
| } | |
| interface IEntity | |
| { | |
| public var id : Int; | |
| public function listen(event : Event) : Array<Event>; /* program all your types of listeners into this */ | |
| } | |
| class Event | |
| { | |
| public var target : Array<Int>; /* the history of all targets, the most recent one is the last entry */ | |
| public var data : EventData; | |
| public function new(target : Array<Int>, data : EventData) { this.target = target; this.data = data; } | |
| public function retarget(next_target : Int) { this.target.push(next_target); return this; } | |
| public function transform(next_target : Int, next_data : EventData) { this.target.push(next_target); this.data = next_data; return this; } | |
| public function clone() { return new Event(this.target.copy(), this.data); } | |
| } | |
| enum EventData | |
| { | |
| Heartbeat; | |
| TraceEvent(string : String); | |
| } | |
| class ExampleEventSys | |
| { | |
| public static var sys = new Eventsys(); | |
| public static function main() { | |
| spawnTracer(0, 1); | |
| spawnTracer(1, 0); | |
| sys.heartbeat(); | |
| } | |
| public static function spawnTracer(from : Int, to : Int) | |
| { | |
| var e = new ExampleEventEntity(to); | |
| sys.spawn(e, from); | |
| return e; | |
| } | |
| } | |
| class ExampleEventEntity implements IEntity | |
| { | |
| public var id : Int; | |
| public var trace_to : Int; | |
| public function new(trace_to) { this.trace_to = trace_to; } | |
| public function listen(event : Event) : Array<Event> | |
| { | |
| switch(event.data) | |
| { | |
| case Heartbeat: | |
| return [event.transform(trace_to, TraceEvent('sent from $id'))]; | |
| case TraceEvent(s): | |
| trace('recieved in $id, ' + s); return []; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment