Created
February 12, 2016 21:02
-
-
Save fponticelli/8ad0c8d178b4e157c696 to your computer and use it in GitHub Desktop.
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
private typedef _SActionFunc = EntityScript -> _SA -> Void; | |
private class _SActions { | |
// Every function in this class must be of type _SActionFunc. | |
// Who knows what will happen if this is not true. | |
public static function Idle(entity:EntityScript, action:_SA):Void { | |
} | |
public static function AnotherAction(entity:EntityScript, action:_SA):Void { | |
} | |
public static function Function(entity:EntityScript, action:_SA):Void { | |
} | |
} | |
private class _SATable { | |
private static var s_functionMap:Map<String, _SActionFunc>; | |
private static var s_initialized:Bool = false; | |
public static function Initialize() { | |
if (s_initialized == true) { | |
return; | |
} | |
s_initialized = true; | |
s_functionMap = new Map<String, _SActionFunc>(); | |
var fields = Type.getClassFields(_SActions); | |
for (f in fields) { | |
var func = Reflect.field(_SActions, f); | |
// I have no idea how to type-check functions, or if it's even | |
// possible to do so, so we just assume everything in _SActions | |
// is of the correct type. | |
if (Reflect.isFunction(func)) { | |
s_functionMap.set(f, func); | |
} | |
} | |
} | |
public inline static function Get(name:String) { | |
if (s_initialized == false) { | |
Initialize(); | |
} | |
return s_functionMap.get(name); | |
} | |
} | |
private class _SA { | |
public var Action(default, null):_SActionFunc; | |
public var DrawFrames(default, null):Array<String>; | |
public var DurationPerFrame(default, null):Int; | |
public var P1(default, null):String; | |
public var P2(default, null):String; | |
public var P3(default, null):String; | |
public function new(action:String, drawFrames:Array<String>, ?durationPerFrame:Int = 1, | |
?p1:String = null, ?p2:String = null, ?p3:String = null) | |
{ | |
this.Action = _SATable.Get(action); | |
if (this.Action == null) { | |
this.Action = _SActions.Idle; | |
} | |
this.DrawFrames = drawFrames; | |
this.DurationPerFrame = durationPerFrame; | |
this.P1 = p1; | |
this.P2 = p2; | |
this.P3 = p3; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment