Skip to content

Commit

Permalink
Move more View logic into ViewBase.
Browse files Browse the repository at this point in the history
  • Loading branch information
player-03 committed Oct 7, 2024
1 parent 838bb8f commit 2edf56a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 52 deletions.
8 changes: 7 additions & 1 deletion src/echoes/ComponentStorage.hx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ComponentStorage<T> {
return _relatedViews;
}

@:allow(echoes.ViewBase)
@:allow(echoes.DynamicComponentStorage)
private final _relatedViews:Array<ViewBase> = [];

/**
Expand Down Expand Up @@ -192,6 +192,12 @@ abstract DynamicComponentStorage(ComponentStorage<Dynamic>) {
@:from private static inline function fromComponentStorage<T>(componentStorage:ComponentStorage<T>):DynamicComponentStorage {
return cast componentStorage;
}

@:allow(echoes.ViewBase)
private var _relatedViews(get, never):Array<ViewBase>;
private inline function get__relatedViews():Array<ViewBase> {
return this._relatedViews;
}
}

/**
Expand Down
38 changes: 30 additions & 8 deletions src/echoes/View.hx
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,25 @@ import echoes.utils.ReadOnlyData;
class View<Rest> extends ViewBase { }

class ViewBase {
private var activations:Int = 0;
public var active(get, never):Bool;
private inline function get_active():Bool return activations > 0;

/**
* All `ComponentStorage` instances related to this view.
*/
public final componentStorage:ReadOnlyArray<DynamicComponentStorage>;

@:allow(echoes.Echoes) private final _entities:Array<Entity> = [];
/**
* All entities in this view.
*/
public var entities(get, never):ReadOnlyArray<Entity>;
private inline function get_entities():ReadOnlyArray<Entity> return _entities;

private var activations:Int = 0;
public var active(get, never):Bool;
private inline function get_active():Bool return activations > 0;
public inline function new(componentStorage:Array<DynamicComponentStorage>) {
this.componentStorage = componentStorage;
}

public function activate():Void {
activations++;
Expand All @@ -28,6 +37,9 @@ class ViewBase {
for(e in Echoes.activeEntities) {
add(e);
}
for(storage in componentStorage) {
storage._relatedViews.push(this);
}
}
}

Expand Down Expand Up @@ -59,9 +71,15 @@ class ViewBase {
/**
* Returns whether the entity has all of the view's required components.
*/
private function isMatched(entity:Entity):Bool {
//Overridden by `ViewBuilder`.
return false;
private inline function isMatched(entity:Entity):Bool {
var result:Bool = true;
for(storage in componentStorage) {
if(!storage.exists(entity)) {
result = false;
break;
}
}
return result;
}

@:allow(echoes.Entity) @:allow(echoes.ComponentStorage)
Expand All @@ -85,9 +103,13 @@ class ViewBase {
activations = 0;
Echoes._activeViews.remove(this);
_entities.resize(0);

for(storage in componentStorage) {
storage._relatedViews.remove(this);
}
}

public function toString():String {
return "ViewBase";
public inline function toString():String {
return "View<" + [for(storage in componentStorage) storage.name].join(", ") + ">";
}
}
45 changes: 2 additions & 43 deletions src/echoes/macro/ViewBuilder.hx
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,8 @@ class ViewBuilder {
public final onAdded = new echoes.utils.Signal<$callbackType>();
public final onRemoved = new echoes.utils.Signal<$callbackType>();

private function new() { }

public override function activate():Void {
super.activate();

if(activations == 1) $b{
[for(component in components) {
//Add this to the component's `_relatedViews`, so that
//given the component, you'll be able to find this view.
final storage:Expr = component.getComponentStorage();
macro $storage._relatedViews.push(this);
}]
}
private function new() {
super([$a{ { [for(component in components) macro ${ component.getComponentStorage() }]; } }]);
}

private override function dispatchAddedCallback(entity:echoes.Entity):Void {
Expand Down Expand Up @@ -193,14 +182,6 @@ class ViewBuilder {
super.reset();
onAdded.clear();
onRemoved.clear();

//Remove this from all `_relatedViews` arrays.
$b{
[for(component in components) {
final storage:Expr = component.getComponentStorage();
macro ${ storage }._relatedViews.remove(this);
}]
}
}

public inline function iter(callback:$callbackType):Void {
Expand All @@ -211,28 +192,6 @@ class ViewBuilder {
forEachEntityInView(macro callback, args, macro 0);
} }
}

private override function isMatched(entity:echoes.Entity):Bool {
return ${ {
//The expression consists of several `exists()` checks. For
//instance, in a `View<Hue, Saturation>`, the two checks
//would be `HueContainer.instance.exists(entity)` and
//`SaturationContainer.instance.exists(entity)`.
final checks:Array<Expr> = [for(component in components)
macro ${ component.getComponentStorage() }.exists(entity)];
//The checks are joined by `&&` operators.
checks.fold((a, b) -> macro $a && $b, checks.shift());
} };
}

public override function toString():String {
//Insert the value of a string formed by joining the component
//names. For instance, in a `View<Hue, Saturation>`, the string
//would be `"Hue, Saturation"`.
return $v{
components.map(new Printer().printComplexType).join(", ")
};
}
}

Context.defineType(def);
Expand Down

0 comments on commit 2edf56a

Please sign in to comment.