Skip to content

Commit

Permalink
⚡ ct.timer and (Issue 151) ct.flow, ct.u.wait, ct.tween now operate b…
Browse files Browse the repository at this point in the history
…ased on ct.delta, with optional setTimeout mode (#179 by @naturecodevoid)

Co-authored-by: Cosmo Myzrail Gorynych <[email protected]>
  • Loading branch information
amsam0 and CosmoMyzrailGorynych authored May 4, 2020
1 parent ef8aaa3 commit b5b560f
Show file tree
Hide file tree
Showing 20 changed files with 626 additions and 216 deletions.
180 changes: 91 additions & 89 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,89 +1,91 @@
# Mac

.DS_Store

# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
node_modules
bower_components

temp/
desktop\.ini
*.png~
.directory

# Generated files
src/typedefs/ct.js/types.d.ts
app/examples
app/data/bundle.js
app/data/bundle.css
app/data/node_requires/**/*
app/data/fonts/style/.css
app/data/theme*.css
app/data/typedefs/global.d.ts
app/data/typedefs/pixi.js.d.ts
/app/data/docs
app/index.html
app/preview.html
app/empty.html
app/debuggerToolbar.html
app/qrCodePanel.html
app/exportDesktop
app/temp
/temp
app/data/fonts/style/.css
app/data/patronsCache.csv
builds
build
cache
tempChangelog.md

# tests
error_screenshots/
reports/

# ct specific
/export
app/zipexport
app/*.ict.zip
app/Screenshot of *.png
app/export.zip
app/export
*.ict.recovery

# editor-specific
/.vscode

# docs
docs/db.json
docs/node_modules
docs/public
app/data/icons.svg
export.zip
app/pleaseCtJSLoadWithoutGPUAccelerationMmkay
app/debugger.html
# Mac

.DS_Store

# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
node_modules
bower_components

temp/
desktop\.ini
*.png~
.directory

# Generated files
src/typedefs/ct.js/types.d.ts
app/examples
app/data/bundle.js
app/data/bundle.css
app/data/node_requires/**/*
app/data/fonts/style/.css
app/data/theme*.css
app/data/typedefs/global.d.ts
app/data/typedefs/pixi.js.d.ts
/app/data/docs
app/index.html
app/preview.html
app/empty.html
app/debuggerToolbar.html
app/qrCodePanel.html
app/exportDesktop
app/temp
/temp
app/data/fonts/style/.css
app/data/patronsCache.csv
builds
build
cache
tempChangelog.md

# tests
error_screenshots/
reports/

# ct specific
/export
app/zipexport
app/*.ict.zip
app/Screenshot of *.png
app/export.zip
app/export
*.ict.recovery

# editor-specific
/.vscode

# docs
docs/db.json
docs/node_modules
docs/public
app/data/icons.svg
export.zip
app/pleaseCtJSLoadWithoutGPUAccelerationMmkay
app/debugger.html

**/**/*.orig
3 changes: 3 additions & 0 deletions app/data/ct.libs/flow/DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,12 @@ Similar to `ct.flow.delay`, this method will return a new function that will lim
the execution of the `func` to max once in `ms` period. It will call the function first
and then block the execution for `ms` time, though.

It takes into account `ct.delta` or `ct.deltaUi`.

**Returns**: `function` - a new triggerable function

| Param | Type | Description |
| --- | --- | --- |
| func | `function` | The function to limit |
| ms | `Number` | The period to wait, in milliseconds |
| [useUiDelta=false] | `Boolean` | If true, use `ct.deltaUi` instead of `ct.delta` |
4 changes: 1 addition & 3 deletions app/data/ct.libs/flow/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
A collection of high-level utilities for flow control, that are especially useful while working with asynchronous events.

Please note that this function **ignores the scaling of `ct.delta`**, meaning that triggers will work even while the game is paused.
A collection of high-level utilities for flow control, that are especially useful while working with asynchronous events.
11 changes: 8 additions & 3 deletions app/data/ct.libs/flow/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,21 @@
*
* @param {Function} func The function to limit
* @param {Number} ms The period to wait, in milliseconds
* @param {boolean} [useUiDelta=false] If true, use ct.deltaUi instead of ct.delta
* @returns {Function} a new triggerable function
*/
timer(func, ms) {
timer(func, ms, useUiDelta = false) {
// The same may be done with ct.flow.gate + ct.flow.
var timer;
var delay = function () {
if (!timer) {
timer = setTimeout(() => {
timer = true;
ct.u.wait(ms, useUiDelta).then(() => {
timer = false;
}, ms);
});
/*timer = setTimeout(() => {
timer = false;
}, ms);*/
func();
}
};
Expand Down
1 change: 1 addition & 0 deletions app/data/ct.libs/tween/DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Creates a new tween effect and adds it to the game loop.
* `options.fields` A map with pairs `fieldName: newValue`. Values must be of numerical type.
* `options.curve` An interpolating function. You can write your own, or use default ones written below. The default one is `ct.tween.ease`.
* `options.duration` The duration of easing, in milliseconds.
* `options.useUiDelta` If true, use `ct.deltaUi` instead of `ct.delta`. The default is `false`.

Returns a Promise which is resolved if the effect was fully played, or rejected if it was interrupted manually by code, room switching or Copy kill.

Expand Down
52 changes: 27 additions & 25 deletions app/data/ct.libs/tween/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
/* global ct */
/* global CtTimer */

ct.tween = {
/**
* Creates a new tween effect and adds it to the game loop
*
*
* @param {Object} options An object with options:
* @param {Object|Copy} options.obj An object to animate. All objects are supported.
* @param {Object} options.fields A map with pairs `fieldName: newValue`. Values must be of numerical type.
* @param {Function} options.curve An interpolating function. You can write your own,
* or use default ones written below. The default one is `ct.tween.ease`.
* @param {Function} options.curve An interpolating function. You can write your own,
* or use default ones (see methods in `ct.tween`). The default one is `ct.tween.ease`.
* @param {Number} options.duration The duration of easing, in milliseconds.
*
* @returns {Promise} A promise which is resolved if the effect was fully played,
* @param {Number} options.useUiDelta If true, use ct.deltaUi instead of ct.delta. The default is `false`.
*
* @returns {Promise} A promise which is resolved if the effect was fully played,
* or rejected if it was interrupted manually by code, room switching or instance kill.
* You can call a `stop()` method on this promise to interrupt it manually.
*/
Expand All @@ -21,7 +22,8 @@ ct.tween = {
fields: options.fields || {},
curve: options.curve || ct.tween.ease,
duration: options.duration || 1000,
registered: (Number(new Date()))
useUiDelta: options.useUiDelta || false,
timer: new CtTimer('ct.tween', this.duration, this.useUiDelta)
};
var promise = new Promise((resolve, reject) => {
tween.resolve = resolve;
Expand All @@ -44,8 +46,8 @@ ct.tween = {
/**
* Linear interpolation.
* Here and below, these parameters are used:
*
* @param {Number} s Starting value
*
* @param {Number} s Starting value
* @param {Number} d The change of value to transition to, the Delta
* @param {Number} a The current timing state, 0-1
* @returns {Number} Interpolated value
Expand All @@ -56,61 +58,61 @@ ct.tween = {
ease(s, d, a) {
a *= 2;
if (a < 1) {
return d/2*a*a + s;
return d / 2 * a * a + s;
}
a--;
return -d/2 * (a*(a-2) - 1) + s;
return -d / 2 * (a * (a - 2) - 1) + s;
},
easeInQuad(s, d, a) {
return d*a*a + s;
return d * a * a + s;
},
easeOutQuad(s, d, a) {
return -d * a*(a-2) + s;
return -d * a * (a - 2) + s;
},
easeInCubic(s, d, a) {
return d*a*a*a + s;
return d * a * a * a + s;
},
easeOutCubic(s, d, a) {
a--;
return d*(a*a*a + 1) + s;
return d * (a * a * a + 1) + s;
},
easeInOutCubic(s, d, a) {
a *= 2;
if (a < 1) {
return d/2*a*a*a + s;
return d / 2 * a * a * a + s;
}
a -= 2;
return d/2*(a*a*a + 2) + s;
return d / 2 * (a * a * a + 2) + s;
},
easeInOutQuart(s, d, a) {
a *= 2;
if (a < 1) {
return d/2*a*a*a*a + s;
return d / 2 * a * a * a * a + s;
}
a -= 2;
return -d/2 * (a*a*a*a - 2) + s;
return -d / 2 * (a * a * a * a - 2) + s;
},
easeInQuart(s, d, a) {
return d*a*a*a*a + s;
return d * a * a * a * a + s;
},
easeOutQuart(s, d, a) {
a--;
return -d * (a*a*a*a - 1) + s;
return -d * (a * a * a * a - 1) + s;
},
easeInCirc(s, d, a) {
return -d * (Math.sqrt(1 - a*a) - 1) + s;
return -d * (Math.sqrt(1 - a * a) - 1) + s;
},
easeOutCirc(s, d, a) {
a--;
return d * Math.sqrt(1 - a*a) + s;
return d * Math.sqrt(1 - a * a) + s;
},
easeInOutCirc(s, d, a) {
a *= 2;
if (a < 1) {
return -d/2 * (Math.sqrt(1 - a*a) - 1) + s;
return -d / 2 * (Math.sqrt(1 - a * a) - 1) + s;
}
a -= 2;
return d/2 * (Math.sqrt(1 - a*a) + 1) + s;
return d / 2 * (Math.sqrt(1 - a * a) + 1) + s;
},
tweens: [],
wait: ct.u.wait
Expand Down
5 changes: 1 addition & 4 deletions app/data/ct.libs/tween/injects/beforeroomstep.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
/* global ct */

var i = 0;
var newTime = Number(new Date());
while (i < ct.tween.tweens.length) {
var tween = ct.tween.tweens[i];
if (tween.obj.kill) {
Expand All @@ -12,7 +9,7 @@ while (i < ct.tween.tweens.length) {
ct.tween.tweens.splice(i, 1);
continue;
}
var a = (newTime - tween.registered) / tween.duration;
var a = tween.timer.time / tween.duration;
if (a > 1) {
a = 1;
}
Expand Down
Loading

0 comments on commit b5b560f

Please sign in to comment.