actionscript 3 - Storing parameter templates to create new entities from? -
my entity system game uses templates lay out how entities created. like:
entitytemplatemanager.register("zombie", new physicscomponent(10), // speed new spritecomponent("zombie"), // graphic new healthcomponent(100) // health ); i part of system because makes sure don't miss parameters or screw type.
then can create new entity this:
entity : entity = entitytemplatemanager.create("zombie");
pretty straight forward.
what now, create entity, have entitytemplatemanager create 1 entity use default, call clone() on entity , goes through , clone()s of components , returns result.
actual code:
public function clone() : entity { var components : vector.<component> = new vector.<component>(); var length : int = _components.length; (var : int = 0; < length; ++i) components.push(_components[i].clone()); // copy local components new entity return new entity(_name, _type, components); } the problem is, every time create (design) new component, have write (design) clone() method inside of , keep track of parameters called on constructor create brand new, default stated component.
so end junk this:
public class componentx { protected var _defaultname : string; protected var _defaultstartinganimation : string; protected var _defaultbroadcastanimationended : boolean; protected var _defaultoffsetx : number; protected var _defaultoffsety : number; // other stuff hidden public function componentx(name : string, startinganimation : string, broadcastanimationended : boolean = false, offsetx : number = 0, offsety : number = 0) : void { super(); _defaultname = name; _defaultstartinganimation = startinganimation; _defaultbroadcastanimationended = broadcastanimationended; _defaultoffsetx = offsetx; _defaultoffsety = offsety; } public function clone() : component { return new componentx(_defaultname, _defaultstartinganimation, _defaultbroadcastanimationended, _defaultoffsetx, _defaultoffsety); } // other methods } i don't -- it's wasteful , error prone.
how best store parameters of entitytemplatemanager.register() function (including components' constructors parameters) , use new storage create entities instead?
i've tried generic clone() methods bytearray or describetype(), don't work protected / private variables.
ideas?
few months ago wrote own entity-component manager too. i've done (in case):
entitytemplatemanager.register("zombie", [ {type: physicscomponent, attr: {speed: 10}}, {type: spritecomponent, attr: {graphic: "zombie"}}, {type: healthcomponent, attr: {health: 100}} ]); registering new template works below:
private static var definitions:dictionary = new dictionary(); public static function register(templatename:string, componentdefinitions:array):void { definitions[templatename] = componentdefinitions; } the create function retrieves specified component template , create components this:
var components:vector.<component> = new vector.<component>(); // create components each (var definition:object in componentdefinitions) { var componentclass:class = definition.type; var component:component = new componentclass(); // set default parameters (var prop:string in definition.attr) { component[prop] = definition.attr[prop]; } components.push(component); } // create entity , attach freshly created components. // ...
Comments
Post a Comment