Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tweening framework. #352

Merged
merged 15 commits into from
Nov 8, 2020
Merged

Add Tweening framework. #352

merged 15 commits into from
Nov 8, 2020

Conversation

nightm4re94
Copy link
Member

@nightm4re94 nightm4re94 commented Nov 7, 2020

This PR adds a package for interpolating values over time, offering predefined Easing functions defined by Robert Penner as well as the possibility to define custom mathematical tweening equations. If an object implements the Tweenable interface, it can override the getTweenValues and setTweenValues methods to define which attributes will be modified.
Current examples for Tweenables are Entities(position, dimensions, angle, hitpoints, collision dimensions, velocity) and GuiComponents (position, dimensions, text angle), as well as SoundPlaybacks (volume).
Each game has one instance of a TweenEngine which operates as a manager for all registered Tween instances and applies their TweenEquation with each update. It can be retrieved using Game.tweens().
Tweens can be stopped, resumed, and reset, as the examples below show:

GuiComponent comp = new GuiComponent();

// Start a Tween or restart it, if there is already a Tween. In this case, the width and height of the GuiComponent will be decreased by 10 over the course of 500 milliseconds, using an elastic easing function.
Game.tweens().begin(comp, TweenType.SIZE_BOTH, 500).targetRelative(-10, -10).ease(TweenFunction.ELASTIC_OUT);

// Stop a Tween
Game.tweens().stop(comp, TweenType.SIZE_BOTH);

// Resume a Tween
Game.tweens().resume(comp, TweenType.SIZE_BOTH);

// Reset the component's width and height to how they were before tweening
Game.tweens().reset(comp, TweenType.SIZE_BOTH);

Apart from the Tweening framework, this PR implements the following changes:

  • Add a listener to receive events when a key is released
  • Add setters for row height and column with in ImageComponentLists
  • Remove distinction between Particle color alpha and opacity. (Fix flickering particle opacity when fading.)

Adapt TweenEquations from the TweenEngine, drop the external dependency.
Refactor TweenFunctions.
Allow setting relative tween targets.
Remove distinction between Particle color alpha and opacity.
The TweenEngine is accessible via Game.tweens()
Switch to ConcurrentHashMap instead of CopyOnWriteArrayList for managing Tweens in the TweenEngine.
…lly.

Fix sloppy implementation of Image scaling in ImageComponent introduced in 4ff1076.
…nd creature velocity.

Document Tweening package.
Copy link
Contributor

@steffen-wilke steffen-wilke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Document changes on Particle and IKeyboard.
  • Adjust licensing and remove "unneccessary" easing functions.

TODO:

  • Loopable Tweens
  • Missing events of tweens
  • Possibility to determine whether a TweenType is supported by a Tweenable

Log warnings instead of crashing when the TweenType of a Tween is not implemented for a Tweenable.
Remove external licenses in the TweenFunction enum as the functions have been reimplemented.
Remove some tweening functions that are basically indistinguishable in many use cases.
@nightm4re94
Copy link
Member Author

Last change adressed these remarks:

  • Possibility to determine whether a TweenType is supported by a Tweenable
  • Document changes on Particle and IKeyboard.
  • Adjust licensing and remove "unneccessary" easing functions.

@nightm4re94 nightm4re94 merged commit 5ea4f83 into master Nov 8, 2020
@nightm4re94 nightm4re94 deleted the tweening branch November 8, 2020 19:46
@TheRamenChef
Copy link
Collaborator

TheRamenChef commented Nov 8, 2020

It seems I came just a few hours too late to offer my feedback on this PR.

First of all, the complete list of VolumeControl instances should not be exposed publicly, nor should it be accessed anywhere other than the updateVolume method, because a method iterating over it does not know what any of them are supposed to represent. And the list is grossly misused by the various fade methods, which currently fade out every volume control attached to the playback instance. They should either create an independent volume control for it or use the master volume control (as the fade method seems to be mainly concerning the master volume).

On the subject of the fade method also, the original fadeMusic method was supposed to make the music fade out, and would stop the playback as soon as the volume reached zero. It no longer does this in the current version.

Second, it seems that TweenType and Tweenable could be replaced with interfaces to get and set tweened values (or just use DoubleSupplier and DoubleConsumer), possibly with an additional class to manage several at once.

Third, the TweenFunction enum appears to exist only as a wrapper for a TweenEquation that can't be instantiated, and removing it in favor of the underlying TweenFunctions would make the API simpler and more flexible.

It would also be useful to have an event listener for when a tween finishes.

I'm sure I could find more ways to improve this API, but I haven't looked at it all that closely yet. Expect a PR shortly containing my proposed changes.

@nightm4re94
Copy link
Member Author

nightm4re94 commented Nov 9, 2020

You're very welcome to enhance this, it's been a bit of a rushed PR as we wanted to have a common ground for our project this month without overcomplicating the branching of the engine.
Some further changes have already been proposed in #353, please track your proposals there.

@nightm4re94 nightm4re94 mentioned this pull request Feb 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants