View source

class flaxen.ComponentSet

Available on all platforms

A ComponentSet is a list of steps that can be added (installed) into an entity (or ideally more). Most steps are for adding instances of components. This is its primary function, to quickly add components to an entity. Steps can also be used to remove components from entities, or execute arbitrary functions that may or may not return the instance to be added.

This set adds some base components to two different entities, and ensures neither entity contains an Animation:

	flaxen.newComponentSet("base")
		.add(Origin.center)
		.add(new Offset(10, 10))
		.addClass(Position, [0, 0])
		.remove(Animation);
	flaxen.addSet(entity1, "base");
	flaxen.addSet(entity2, "base");

Instance Fields

function new():Void

function add(component:Dynamic):ComponentSet

Adds a component instance to the component set. Note that all entities using this set will be sharing the same single instance of this component. See addClass. TODO Verify this is not a Class or Function being passed in.

function addClass(clazz:Class<Dynamic>, ?args:Array<Dynamic>):ComponentSet

Adds a class and parameters to the set. The class will be instantiated with the supplied paramters every time the set is installed in an entity. This prevents sharing of instances that happens when you call set.add(new MyComponent(A, B, C)). For example: set.addClass(MyComponent, [A, B, C]);

function addEntity(entity:Entity):ComponentSet

Adds all the components from one entity into the set. Note that these components are added immediately, so if you add or remove from the supplied entity at a later point, these changes will not be reflected in the component set.

function addFunction(func:Entity ->Dynamic):ComponentSet

Adds a function. This function must accept installing entity as a parameter. The function will be executed when the set is installed into an entity. If the function returns a component instance, it will be added to the entity. If it returns null, it will be ignored. TODO Allow functions with no parameters as well

function addSet(set:ComponentSet):ComponentSet

Adds a ComponentSet to this set. When the set is installed in an entity, the supplied set will also be installed to the entity. Use this to implement a component set hiearchy of parent/child sets. For instance, a gun is a weapon. You could create a WeaponSet and a GunSet and install them both to your new gun entity, or instead add the WeaponSet to the GunSet using this method. Then when install GunSet, the entity will also get WeaponSet installed.

function install(entity:Entity):Void

Installs the components into the entity

function remove(clazz:Class<Dynamic>):ComponentSet

\* Does not add an instance. Instead, when installed, removes the supplied component from the entity, if it has one.

function removeAll(?except:Array<Class<Dynamic>>):ComponentSet

Does not add an instance. Instead, when installed, removes all components from an entity. Completely blanks out an entity. Do this before any add calls. If an array of component classes is supplied, these components will be spared removal

function toString():String

TODO This reports "[func]" if it encounters a remove or removeAll. This could be fixed by adding all steps as independent class objects.