@cyclonium/core 0.0.103 → 0.0.105

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,3 @@
1
1
  export { cycloBuiltinClass } from '../decorator/legacy/legacy-decorator.ts';
2
+ export { TimeAccumulator } from '../framework/time-accumulator.js';
2
3
  //# sourceMappingURL=internal.d.ts.map
@@ -1,2 +1,3 @@
1
1
  export { cycloBuiltinClass } from "../decorator/legacy/legacy-decorator.js";
2
+ export { TimeAccumulator } from '../framework/time-accumulator.js';
2
3
  //# sourceMappingURL=internal.js.map
@@ -12,6 +12,7 @@ import { addFrameTask, TaskPriority } from "./tasking.js";
12
12
  import { CoroutineRunner, stopCoroutine as stopCoroutineRecord } from "./coroutine.js";
13
13
  import { retainIf } from '@cyclonium/algorithm/retain-if';
14
14
  import { EDITOR_NOT_IN_PREVIEW } from 'cc/env';
15
+ import { TimeAccumulator } from './time-accumulator.js';
15
16
  class ComponentScheduler {
16
17
  enableComponent(component) {
17
18
  const index = this._fixedUpdateRegistry.findIndex(({ component: c }) => c === component);
@@ -259,27 +260,22 @@ class FixedUpdateTask {
259
260
  update(deltaTime) {
260
261
  const scene = director.getScene();
261
262
  if (!scene) {
262
- this._accumulatedTime = 0;
263
+ this._timeAccumulator.reset();
263
264
  return;
264
265
  }
265
- const fixedTimeStep = this._fixedTimeStep;
266
+ const timeAccumulator = this._timeAccumulator;
267
+ const fixedTimeStep = timeAccumulator.timeStep;
266
268
  const maxSteps = this.maxSteps;
267
- const accumulatedTime = this._accumulatedTime + deltaTime;
268
- const expectedSteps = Math.floor(accumulatedTime / fixedTimeStep);
269
- const overloading = expectedSteps > maxSteps;
269
+ const actualSteps = timeAccumulator.advance(deltaTime, maxSteps);
270
+ const overloading = deltaTime > maxSteps * fixedTimeStep;
270
271
  const previousOverloading = this._overloading;
271
272
  this._overloading = overloading;
272
- let actualSteps = 0;
273
273
  if (overloading) {
274
- actualSteps = maxSteps;
275
- this._accumulatedTime = 0;
276
274
  if (overloading !== previousOverloading) {
277
- this._emitOverloadingBegin(expectedSteps);
275
+ this._emitOverloadingBegin();
278
276
  }
279
277
  }
280
278
  else {
281
- actualSteps = expectedSteps;
282
- this._accumulatedTime = accumulatedTime - actualSteps * fixedTimeStep;
283
279
  if (overloading !== previousOverloading) {
284
280
  this._emitOverloadingEnd();
285
281
  }
@@ -288,12 +284,10 @@ class FixedUpdateTask {
288
284
  globalComponentScheduler.invokeFixedUpdate(fixedTimeStep);
289
285
  }
290
286
  }
291
- _fixedTimeStep = 1 / 60;
292
- _accumulatedTime = 0;
287
+ _timeAccumulator = new TimeAccumulator(1 / 60);
293
288
  _overloading = false;
294
- _emitOverloadingBegin(expectedSteps) {
289
+ _emitOverloadingBegin() {
295
290
  // todo
296
- void expectedSteps;
297
291
  }
298
292
  _emitOverloadingEnd() {
299
293
  // todo
@@ -0,0 +1,10 @@
1
+ export declare class TimeAccumulator {
2
+ constructor(timeStep: number);
3
+ get timeStep(): number;
4
+ /** Adds at most `maxSteps` worth of elapsed time and consumes complete steps. */
5
+ advance(deltaTime: number, maxSteps: number): number;
6
+ reset(): void;
7
+ private readonly _timeStep;
8
+ private _accumulatedTime;
9
+ }
10
+ //# sourceMappingURL=time-accumulator.d.ts.map
@@ -0,0 +1,29 @@
1
+ export class TimeAccumulator {
2
+ constructor(timeStep) {
3
+ this._timeStep = timeStep;
4
+ }
5
+ get timeStep() {
6
+ return this._timeStep;
7
+ }
8
+ /** Adds at most `maxSteps` worth of elapsed time and consumes complete steps. */
9
+ advance(deltaTime, maxSteps) {
10
+ const timeStep = this._timeStep;
11
+ const maximumDeltaTime = maxSteps * timeStep;
12
+ const acceptedDeltaTime = Math.min(deltaTime, maximumDeltaTime);
13
+ const accumulatedTime = this._accumulatedTime + acceptedDeltaTime;
14
+ const steps = Math.min(countCompleteSteps(accumulatedTime, timeStep), maxSteps);
15
+ this._accumulatedTime = Math.max(accumulatedTime - steps * timeStep, 0);
16
+ return steps;
17
+ }
18
+ reset() {
19
+ this._accumulatedTime = 0;
20
+ }
21
+ _timeStep;
22
+ _accumulatedTime = 0;
23
+ }
24
+ function countCompleteSteps(accumulatedTime, timeStep) {
25
+ const stepRatio = accumulatedTime / timeStep;
26
+ const tolerance = Number.EPSILON * Math.max(Math.abs(stepRatio), 1) * 4;
27
+ return Math.floor(stepRatio + tolerance);
28
+ }
29
+ //# sourceMappingURL=time-accumulator.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cyclonium/core",
3
- "version": "0.0.103",
3
+ "version": "0.0.105",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/shrinktofit/cyclonium",
@@ -29,7 +29,7 @@
29
29
  "dependencies": {
30
30
  "@cyclonium/abort-controller": "0.0.102",
31
31
  "@cyclonium/algorithm": "1.0.0",
32
- "@cyclonium/math": "0.0.103"
32
+ "@cyclonium/math": "0.0.104"
33
33
  },
34
34
  "devDependencies": {
35
35
  "@types/node": "^25.9.2",