feat: TimerActionComponent to schedule actions per entity.#1469
feat: TimerActionComponent to schedule actions per entity.#1469michqql wants to merge 2 commits intoAlmasB:devfrom
Conversation
With TimerActionComponentTest for tests and sample application (PongVsComputerApp) to see usage.
| * | ||
| * @author Michael Pearson (<a href="https://github.com/michqql/">https://github.com/michqql/</a>) | ||
| */ | ||
| public class TimerActionComponent extends Component { |
There was a problem hiding this comment.
Should be fine to make it final by default. It can be opened in the future if required
|
|
||
| @Override | ||
| public void onUpdate(double tpf) { | ||
| super.onUpdate(tpf); |
There was a problem hiding this comment.
super.onUpdate(tpf); calls Component onUpdate(), which I think may be empty. If it is, then we can safely remove this line.
| import javafx.util.Duration; | ||
|
|
||
| /** | ||
| * Component to schedule the execution of actions. |
There was a problem hiding this comment.
Worth noting that the timer's clock will be tied to the entity's clock. So 1 second in this TimerActionComponent will depend on how long 1 second is for the entity, e.g. TimeComponent may alter that behaviour (which is the correct desirable behaviour). Not quite sure exactly the wording for the doc, but something along these lines should do the trick.
| private static class Counter { | ||
| private int value; | ||
|
|
||
| int get () { return value ; } |
There was a problem hiding this comment.
This formatting is largely inconsistent with the rest of the project's formatting. This will need to be updated to follow the standard Java guidelines. Though I think the fix for the above comment will remove the need for this altogether.
| } | ||
|
|
||
| private final Counter executionCounter = new Counter(); | ||
| private final Runnable action = executionCounter::increment; |
There was a problem hiding this comment.
Same comment about formatting Line 34 and 35 as above. There is no need to align elements.
| public class TimerActionComponentTest { | ||
|
|
||
| /* Counter class to allow for atomic operations from Runnable action */ | ||
| private static class Counter { |
There was a problem hiding this comment.
It's fine to use such a helper class, but best to avoid using the same object from multiple tests in this way. For example, if the test environment runs the tests in parallel (I don't think it does, however we should try best practice where it's easy to do so), there may be an issue with multiple threads writing and reading the same data.
This would mean simply using an int count = 0. If this doesn't work because of lambda functions, you can use var count = new SimpleIntegerProperty(), in each test (like you have done in public void testRunAtIntervalConditionalCancelledEarly()). This should give you more or less the same functionality, but in a relatively safer way.
Amended comments in TimerActionComponent and changed the TimerActionComponentTest.
|
Commit today:
|
Addresses issue #1439.
Summary
Componentthat wraps aTimerobject and updates timer actions dependent on the entities time per frame value. This component can be used to schedule actions with either a delay or interval, with overloaded functions matching the underlyingTimerclass.A
PongVsComputerAppsample is included to see example usage of theTimerActionComponent.Features
Includes the same overloaded methods as
Timer:runAtInterval(Runnable action, Duration interval)runAtInterval(Runnable action, Duration interval, int limit)runAtIntervalWhile(Runnable action, Duration interval, ReadOnlyBooleanProperty whileCondition)runOnceAfter(Runnable action, Duration delay)clear()Testing
TimerActionComponentTestincludes 12 tests to verify the actions are executed. However these tests try not to duplicateTimerActionTest.Example Usage
(From the sample)