From 58a011ab07753716877e60d7df832f337e23c0f2 Mon Sep 17 00:00:00 2001 From: Collin Smith Date: Tue, 12 Nov 2019 01:31:50 -0800 Subject: [PATCH] Refactored alpha and transform updates to components and systems Removed generic update flags from CofComponent Created AlphaUpdate and TransformUpdate components to store flags of layers that need updating AnimationLoaderSystem no longer sets alpha and transform values and sets flags instead Created AlphaUpdateSystem and TransformUpdateSystem to update animation layers --- .../engine/component/CofComponent.java | 2 - .../engine/component/cof/AlphaUpdate.java | 14 +++++ .../engine/component/cof/TransformUpdate.java | 14 +++++ .../engine/system/AnimationLoaderSystem.java | 22 +++++--- .../system/AnimationTransformationSystem.java | 52 ------------------- .../engine/system/cof/AlphaUpdateSystem.java | 44 ++++++++++++++++ .../system/cof/TransformUpdateSystem.java | 44 ++++++++++++++++ 7 files changed, 132 insertions(+), 60 deletions(-) create mode 100644 core/src/com/riiablo/engine/component/cof/AlphaUpdate.java create mode 100644 core/src/com/riiablo/engine/component/cof/TransformUpdate.java delete mode 100644 core/src/com/riiablo/engine/system/AnimationTransformationSystem.java create mode 100644 core/src/com/riiablo/engine/system/cof/AlphaUpdateSystem.java create mode 100644 core/src/com/riiablo/engine/system/cof/TransformUpdateSystem.java diff --git a/core/src/com/riiablo/engine/component/CofComponent.java b/core/src/com/riiablo/engine/component/CofComponent.java index 9a0097a5..dcce7fbc 100644 --- a/core/src/com/riiablo/engine/component/CofComponent.java +++ b/core/src/com/riiablo/engine/component/CofComponent.java @@ -65,7 +65,6 @@ public class CofComponent implements Component, Pool.Poolable { public COF cof; public int dirty; // cof layers need to be loaded public int load; // cof layers loading - public int update; // update component public int speed; public final int component[]; @@ -90,7 +89,6 @@ public class CofComponent implements Component, Pool.Poolable { cof = null; dirty = Dirty.NONE; load = Dirty.NONE; - update = Dirty.NONE; speed = SPEED_NULL; System.arraycopy(DEFAULT_COMPONENT, 0, component, 0, COF.Component.NUM_COMPONENTS); System.arraycopy(DEFAULT_TRANSFORM, 0, transform, 0, COF.Component.NUM_COMPONENTS); diff --git a/core/src/com/riiablo/engine/component/cof/AlphaUpdate.java b/core/src/com/riiablo/engine/component/cof/AlphaUpdate.java new file mode 100644 index 00000000..cf01290b --- /dev/null +++ b/core/src/com/riiablo/engine/component/cof/AlphaUpdate.java @@ -0,0 +1,14 @@ +package com.riiablo.engine.component.cof; + +import com.badlogic.ashley.core.Component; +import com.badlogic.gdx.utils.Pool; +import com.riiablo.engine.Dirty; + +public class AlphaUpdate implements Component, Pool.Poolable { + public int flags = Dirty.NONE; + + @Override + public void reset() { + flags = Dirty.NONE; + } +} diff --git a/core/src/com/riiablo/engine/component/cof/TransformUpdate.java b/core/src/com/riiablo/engine/component/cof/TransformUpdate.java new file mode 100644 index 00000000..44421737 --- /dev/null +++ b/core/src/com/riiablo/engine/component/cof/TransformUpdate.java @@ -0,0 +1,14 @@ +package com.riiablo.engine.component.cof; + +import com.badlogic.ashley.core.Component; +import com.badlogic.gdx.utils.Pool; +import com.riiablo.engine.Dirty; + +public class TransformUpdate implements Component, Pool.Poolable { + public int flags = Dirty.NONE; + + @Override + public void reset() { + flags = Dirty.NONE; + } +} diff --git a/core/src/com/riiablo/engine/system/AnimationLoaderSystem.java b/core/src/com/riiablo/engine/system/AnimationLoaderSystem.java index b0bc82a7..f010ee36 100644 --- a/core/src/com/riiablo/engine/system/AnimationLoaderSystem.java +++ b/core/src/com/riiablo/engine/system/AnimationLoaderSystem.java @@ -14,6 +14,8 @@ import com.riiablo.engine.Dirty; import com.riiablo.engine.SystemPriority; import com.riiablo.engine.component.AnimationComponent; import com.riiablo.engine.component.CofComponent; +import com.riiablo.engine.component.cof.AlphaUpdate; +import com.riiablo.engine.component.cof.TransformUpdate; @DependsOn(CofLoaderSystem.class) public class AnimationLoaderSystem extends IteratingSystem { @@ -25,6 +27,9 @@ public class AnimationLoaderSystem extends IteratingSystem { private final ComponentMapper cofComponent = ComponentMapper.getFor(CofComponent.class); private final ComponentMapper animComponent = ComponentMapper.getFor(AnimationComponent.class); + private final ComponentMapper transformUpdate = ComponentMapper.getFor(TransformUpdate.class); + private final ComponentMapper alphaUpdate = ComponentMapper.getFor(AlphaUpdate.class); + public AnimationLoaderSystem() { super(Family.all(CofComponent.class, AnimationComponent.class).get(), SystemPriority.AnimationLoaderSystem); } @@ -63,13 +68,18 @@ public class AnimationLoaderSystem extends IteratingSystem { if (Riiablo.assets.isLoaded(descriptor)) { int flag = (1 << layer.component); cofComponent.load &= ~flag; - cofComponent.update &= ~flag; - Gdx.app.debug(TAG, "finished loading " + descriptor); + if (DEBUG_LOAD) Gdx.app.debug(TAG, "finished loading " + descriptor); DC dc = Riiablo.assets.get(descriptor); - anim.setLayer(layer, dc, false) - .setTransform(cofComponent.transform[layer.component]) - .setAlpha(cofComponent.alpha[layer.component]) - ; + anim.setLayer(layer, dc, false); + + TransformUpdate transformUpdate = this.transformUpdate.get(entity); + if (transformUpdate == null) entity.add(transformUpdate = getEngine().createComponent(TransformUpdate.class)); + transformUpdate.flags |= flag; + + AlphaUpdate alphaUpdate = this.alphaUpdate.get(entity); + if (alphaUpdate == null) entity.add(alphaUpdate = getEngine().createComponent(AlphaUpdate.class)); + alphaUpdate.flags |= flag; + changed = true; } } diff --git a/core/src/com/riiablo/engine/system/AnimationTransformationSystem.java b/core/src/com/riiablo/engine/system/AnimationTransformationSystem.java deleted file mode 100644 index 852f21a6..00000000 --- a/core/src/com/riiablo/engine/system/AnimationTransformationSystem.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.riiablo.engine.system; - -import com.badlogic.ashley.core.ComponentMapper; -import com.badlogic.ashley.core.Entity; -import com.badlogic.ashley.core.Family; -import com.badlogic.ashley.systems.IteratingSystem; -import com.riiablo.codec.Animation; -import com.riiablo.codec.COF; -import com.riiablo.engine.Dirty; -import com.riiablo.engine.SystemPriority; -import com.riiablo.engine.component.AnimationComponent; -import com.riiablo.engine.component.CofComponent; - -public class AnimationTransformationSystem extends IteratingSystem { - private static final String TAG = "AnimationTransformationSystem"; - - private static final boolean DEBUG = true; - private static final boolean DEBUG_STATE = DEBUG && !true; - - private final ComponentMapper cofComponent = ComponentMapper.getFor(CofComponent.class); - private final ComponentMapper animComponent = ComponentMapper.getFor(AnimationComponent.class); - - public AnimationTransformationSystem() { - super(Family.all(CofComponent.class, AnimationComponent.class).get(), SystemPriority.AnimationLoaderSystem); - } - - @Override - protected void processEntity(Entity entity, float delta) { - CofComponent cofComponent = this.cofComponent.get(entity); - int flags = cofComponent.update; - if (flags == Dirty.NONE) return; - - AnimationComponent animComponent = this.animComponent.get(entity); - Animation animation = animComponent.animation; - if (animation == null) return; - - COF cof = cofComponent.cof; - for (int l = 0, numLayers = cof.getNumLayers(); l < numLayers; l++) { - int component = cof.getLayer(l).component; - if (!Dirty.isDirty(flags, component)) continue; - Animation.Layer layer = animation.getLayer(component); - if (layer != null) { - layer - .setTransform(cofComponent.transform[component]) - .setAlpha(cofComponent.alpha[component]) - ; - } - } - - cofComponent.update = Dirty.NONE; - } -} diff --git a/core/src/com/riiablo/engine/system/cof/AlphaUpdateSystem.java b/core/src/com/riiablo/engine/system/cof/AlphaUpdateSystem.java new file mode 100644 index 00000000..1e6fb1dd --- /dev/null +++ b/core/src/com/riiablo/engine/system/cof/AlphaUpdateSystem.java @@ -0,0 +1,44 @@ +package com.riiablo.engine.system.cof; + +import com.badlogic.ashley.core.ComponentMapper; +import com.badlogic.ashley.core.Entity; +import com.badlogic.ashley.core.Family; +import com.badlogic.ashley.systems.IteratingSystem; +import com.riiablo.codec.Animation; +import com.riiablo.codec.COF; +import com.riiablo.engine.Dirty; +import com.riiablo.engine.component.AnimationComponent; +import com.riiablo.engine.component.CofComponent; +import com.riiablo.engine.component.cof.AlphaUpdate; + +public class AlphaUpdateSystem extends IteratingSystem { + private final ComponentMapper updateComponent = ComponentMapper.getFor(AlphaUpdate.class); + private final ComponentMapper cofComponent = ComponentMapper.getFor(CofComponent.class); + private final ComponentMapper animationComponent = ComponentMapper.getFor(AnimationComponent.class); + + public AlphaUpdateSystem() { + super(Family.all(AlphaUpdate.class, CofComponent.class, AnimationComponent.class).get()); + } + + @Override + protected void processEntity(Entity entity, float delta) { + AlphaUpdate updateComponent = this.updateComponent.get(entity); + int flags = updateComponent.flags; + assert flags != Dirty.NONE; + + CofComponent cofComponent = this.cofComponent.get(entity); + COF cof = cofComponent.cof; + float[] alpha = cofComponent.alpha; + + AnimationComponent animationComponent = this.animationComponent.get(entity); + Animation animation = animationComponent.animation; + + for (int l = 0, numLayers = cof.getNumLayers(); l < numLayers; l++) { + int component = cofComponent.cof.getLayer(l).component; + if (!Dirty.isDirty(flags, component)) continue; + animation.getLayer(component).setAlpha(alpha[component]); + } + + entity.remove(AlphaUpdate.class); + } +} diff --git a/core/src/com/riiablo/engine/system/cof/TransformUpdateSystem.java b/core/src/com/riiablo/engine/system/cof/TransformUpdateSystem.java new file mode 100644 index 00000000..734ed1f6 --- /dev/null +++ b/core/src/com/riiablo/engine/system/cof/TransformUpdateSystem.java @@ -0,0 +1,44 @@ +package com.riiablo.engine.system.cof; + +import com.badlogic.ashley.core.ComponentMapper; +import com.badlogic.ashley.core.Entity; +import com.badlogic.ashley.core.Family; +import com.badlogic.ashley.systems.IteratingSystem; +import com.riiablo.codec.Animation; +import com.riiablo.codec.COF; +import com.riiablo.engine.Dirty; +import com.riiablo.engine.component.AnimationComponent; +import com.riiablo.engine.component.CofComponent; +import com.riiablo.engine.component.cof.TransformUpdate; + +public class TransformUpdateSystem extends IteratingSystem { + private final ComponentMapper updateComponent = ComponentMapper.getFor(TransformUpdate.class); + private final ComponentMapper cofComponent = ComponentMapper.getFor(CofComponent.class); + private final ComponentMapper animationComponent = ComponentMapper.getFor(AnimationComponent.class); + + public TransformUpdateSystem() { + super(Family.all(TransformUpdate.class, CofComponent.class, AnimationComponent.class).get()); + } + + @Override + protected void processEntity(Entity entity, float delta) { + TransformUpdate updateComponent = this.updateComponent.get(entity); + int flags = updateComponent.flags; + assert flags != Dirty.NONE; + + CofComponent cofComponent = this.cofComponent.get(entity); + COF cof = cofComponent.cof; + byte[] transform = cofComponent.transform; + + AnimationComponent animationComponent = this.animationComponent.get(entity); + Animation animation = animationComponent.animation; + + for (int l = 0, numLayers = cof.getNumLayers(); l < numLayers; l++) { + int component = cofComponent.cof.getLayer(l).component; + if (!Dirty.isDirty(flags, component)) continue; + animation.getLayer(component).setTransform(transform[component]); + } + + entity.remove(TransformUpdate.class); + } +}