From 9f6f41b241f918990d15767e3d9855b7f8b6da7a Mon Sep 17 00:00:00 2001 From: Emanuel Rabina Date: Sun, 19 May 2024 11:07:42 +1200 Subject: [PATCH] Cleanup and fix unit/sprite centering --- .../redhorizon/classic/units/Unit.groovy | 7 ++-- .../engine/scenegraph/GraphicsElement.groovy | 7 ++++ .../engine/scenegraph/SceneEvents.groovy | 7 ---- .../engine/scenegraph/nodes/Animation.groovy | 28 +++++++-------- .../engine/scenegraph/nodes/Video.groovy | 4 +-- .../engine/scenegraph/scripting/Script.groovy | 7 ++++ .../redhorizon/explorer/Explorer.groovy | 8 +++-- .../scripts/UnitShowcaseScript.groovy | 4 --- .../redhorizon/explorer/UnitTests.groovy | 34 ++++++++++++------- 9 files changed, 62 insertions(+), 44 deletions(-) diff --git a/redhorizon-classic/source/nz/net/ultraq/redhorizon/classic/units/Unit.groovy b/redhorizon-classic/source/nz/net/ultraq/redhorizon/classic/units/Unit.groovy index 0042ed2e..b14e4fe6 100644 --- a/redhorizon-classic/source/nz/net/ultraq/redhorizon/classic/units/Unit.groovy +++ b/redhorizon-classic/source/nz/net/ultraq/redhorizon/classic/units/Unit.groovy @@ -50,9 +50,9 @@ class Unit extends Node implements FactionColours, Rotatable, Temporal { final ImagesFile imagesFile final Palette palette final UnitData unitData + final PalettedSprite body + final PalettedSprite turret - private PalettedSprite body - private PalettedSprite turret private int stateIndex = 0 private long animationStartTime private SpriteSheet spriteSheet @@ -75,6 +75,9 @@ class Unit extends Node implements FactionColours, Rotatable, Temporal { }) addChild(turret) } + else { + turret = null + } } /** diff --git a/redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/scenegraph/GraphicsElement.groovy b/redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/scenegraph/GraphicsElement.groovy index 156a579e..741cc2e8 100644 --- a/redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/scenegraph/GraphicsElement.groovy +++ b/redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/scenegraph/GraphicsElement.groovy @@ -29,4 +29,11 @@ interface GraphicsElement { * Render the element for display. */ void render(GraphicsRenderer renderer) + + /** + * Called on every frame before the node is rendered, allowing it to perform + * any processing as a response to changes in the scene. + */ + default void update() { + } } diff --git a/redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/scenegraph/SceneEvents.groovy b/redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/scenegraph/SceneEvents.groovy index bce08135..bbc3b01a 100644 --- a/redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/scenegraph/SceneEvents.groovy +++ b/redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/scenegraph/SceneEvents.groovy @@ -41,11 +41,4 @@ interface SceneEvents { return CompletableFuture.completedFuture(null) } - - /** - * Called on every frame before the node is rendered, allowing it to perform - * any processing as a response to changes in the scene. - */ - default void update() { - } } diff --git a/redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/scenegraph/nodes/Animation.groovy b/redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/scenegraph/nodes/Animation.groovy index bbdc3ee7..a65fc5c2 100644 --- a/redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/scenegraph/nodes/Animation.groovy +++ b/redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/scenegraph/nodes/Animation.groovy @@ -51,6 +51,8 @@ class Animation extends Node implements GraphicsElement, Playable, Te private final AnimationSource animationSource + private final int numFrames + private final float frameRate private long startTimeMs private int currentFrame = -1 private Mesh mesh @@ -62,19 +64,23 @@ class Animation extends Node implements GraphicsElement, Playable, Te */ Animation(AnimationFile animationFile) { - this(animationFile.width, animationFile.height, animationFile.forVgaMonitors, - new StreamingAnimationSource(((Streaming)animationFile).streamingDecoder, animationFile.frameRate, animationFile.numFrames, true)) + this(animationFile.width, animationFile.height, animationFile.numFrames, animationFile.frameRate, + animationFile.forVgaMonitors, new StreamingAnimationSource(((Streaming)animationFile).streamingDecoder, true)) } /** * Constructor, create an animation using any implementation of the * {@link AnimationSource} interface. */ - Animation(int width, int height, boolean forVgaMonitors = false, AnimationSource animationSource) { + Animation(int width, int height, int numFrames, float frameRate, boolean forVgaMonitors = false, + AnimationSource animationSource) { + + bounds.set(0, 0, width, forVgaMonitors ? height * 1.2f as float : height) + this.numFrames = numFrames + this.frameRate = frameRate this.animationSource = animationSource animationSource.relay(Event, this) - bounds.set(0, 0, width, forVgaMonitors ? height * 1.2f as float : height) } @Override @@ -135,8 +141,8 @@ class Animation extends Node implements GraphicsElement, Playable, Te @Override void update() { - var nextFrame = Math.floor((currentTimeMs - startTimeMs) / 1000 * animationSource.frameRate) as int - if (nextFrame < animationSource.numFrames) { + var nextFrame = Math.floor((currentTimeMs - startTimeMs) / 1000 * frameRate) as int + if (nextFrame < numFrames) { currentFrame = nextFrame } else { @@ -147,11 +153,7 @@ class Animation extends Node implements GraphicsElement, Playable, Te /** * Interface for any source from which frames of animation can be obtained. */ - interface AnimationSource extends EventTarget, SceneEvents { - - float getFrameRate() - - int getNumFrames() + static interface AnimationSource extends EventTarget, SceneEvents { /** * Called during {@code render}, return the texture to be used for rendering @@ -167,8 +169,6 @@ class Animation extends Node implements GraphicsElement, Playable, Te static class StreamingAnimationSource implements AnimationSource { final StreamingDecoder streamingDecoder - final float frameRate - final int numFrames final boolean autoStream private List frames = [] @@ -197,7 +197,7 @@ class Animation extends Node implements GraphicsElement, Playable, Te .requestCreateOrGet(new TextureRequest(event.width, event.height, event.format, event.frameFlippedVertical)) .get() buffersAdded++ - if (buffersAdded == Math.ceil(frameRate) as int) { + if (buffersAdded == 10) { trigger(new PlaybackReadyEvent()) } } diff --git a/redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/scenegraph/nodes/Video.groovy b/redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/scenegraph/nodes/Video.groovy index ff7ce5db..7fe10289 100644 --- a/redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/scenegraph/nodes/Video.groovy +++ b/redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/scenegraph/nodes/Video.groovy @@ -51,8 +51,8 @@ class Video extends Node