From 602ad993eae48c41a6cb13256f90d72b1d20dffd Mon Sep 17 00:00:00 2001 From: drawcall Date: Fri, 10 Sep 2021 15:27:31 +0800 Subject: [PATCH] feat: solve the problem of circular dependency. --- src/core/Particle.js | 3 +- src/initialize/InitializeUtil.js | 6 +-- src/utils/PropUtil.js | 63 ++++++++++++++++++++++++++++++++ src/utils/Util.js | 63 -------------------------------- 4 files changed, 68 insertions(+), 67 deletions(-) create mode 100644 src/utils/PropUtil.js diff --git a/src/core/Particle.js b/src/core/Particle.js index f8d1242..ecdb5da 100755 --- a/src/core/Particle.js +++ b/src/core/Particle.js @@ -4,6 +4,7 @@ import Rgb from "../utils/Rgb"; import Puid from "../utils/Puid"; import Util from "../utils/Util"; +import PropUtil from "../utils/PropUtil"; import ease from "../math/ease"; import Vector2D from "../math/Vector2D"; import MathUtil from "../math/MathUtil"; @@ -62,7 +63,7 @@ export default class Particle { this.rgb = new Rgb(); this.reset(); - conf && Util.setProp(this, conf); + conf && PropUtil.setProp(this, conf); } getDirection() { diff --git a/src/initialize/InitializeUtil.js b/src/initialize/InitializeUtil.js index 085dfa5..786df34 100755 --- a/src/initialize/InitializeUtil.js +++ b/src/initialize/InitializeUtil.js @@ -1,4 +1,4 @@ -import Util from "../utils/Util"; +import PropUtil from "../utils/PropUtil"; import Initialize from "./Initialize"; import MathUtil from "../math/MathUtil"; @@ -20,8 +20,8 @@ export default { // init init(emitter, particle, initialize) { - Util.setProp(particle, initialize); - Util.setVectorVal(particle, initialize); + PropUtil.setProp(particle, initialize); + PropUtil.setVectorVal(particle, initialize); }, bindEmitter(emitter, particle) { diff --git a/src/utils/PropUtil.js b/src/utils/PropUtil.js new file mode 100644 index 0000000..8f66078 --- /dev/null +++ b/src/utils/PropUtil.js @@ -0,0 +1,63 @@ +export default { + hasProp(target, key) { + if (!target) return false; + return target[key] !== undefined; + // return obj.hasOwnProperty(key); + }, + + /** + * set the prototype in a given prototypeObject + * + * @memberof Proton#Proton.Util + * @method setProp + * + * @todo add description for param `target` + * @todo translate desription from chinese to english + * + * @param {Object} target + * @param {Object} prototypeObject An object of single prototypes + * + * @return {Object} target + */ + setProp(target, props) { + for (let prop in props) { + if (target.hasOwnProperty(prop)) { + target[prop] = Span.getSpanValue(props[prop]); + } + } + + return target; + }, + + /** + * @memberof Proton#Proton.Util + * @method setVectorVal + * + * @todo add description for param `target` + * @todo add description for param `conf` + * @todo add description for function + * + * @param {Object} target + * @param {Object} conf + */ + setVectorVal(particle, conf = null) { + if (!conf) return; + + if (this.hasProp(conf, "x")) particle.p.x = conf["x"]; + if (this.hasProp(conf, "y")) particle.p.y = conf["y"]; + + if (this.hasProp(conf, "vx")) particle.v.x = conf["vx"]; + if (this.hasProp(conf, "vy")) particle.v.y = conf["vy"]; + + if (this.hasProp(conf, "ax")) particle.a.x = conf["ax"]; + if (this.hasProp(conf, "ay")) particle.a.y = conf["ay"]; + + if (this.hasProp(conf, "p")) particle.p.copy(conf["p"]); + if (this.hasProp(conf, "v")) particle.v.copy(conf["v"]); + if (this.hasProp(conf, "a")) particle.a.copy(conf["a"]); + + if (this.hasProp(conf, "position")) particle.p.copy(conf["position"]); + if (this.hasProp(conf, "velocity")) particle.v.copy(conf["velocity"]); + if (this.hasProp(conf, "accelerate")) particle.a.copy(conf["accelerate"]); + } +}; diff --git a/src/utils/Util.js b/src/utils/Util.js index 1334b74..0c60fa8 100755 --- a/src/utils/Util.js +++ b/src/utils/Util.js @@ -1,4 +1,3 @@ -import Span from "../math/Span"; import ImgUtil from "./ImgUtil"; export default { @@ -86,68 +85,6 @@ export default { } }, - /** - * @memberof Proton#Proton.Util - * @method setVectorVal - * - * @todo add description for param `target` - * @todo add description for param `conf` - * @todo add description for function - * - * @param {Object} target - * @param {Object} conf - */ - setVectorVal(particle, conf = null) { - if (!conf) return; - - if (this.hasProp(conf, "x")) particle.p.x = conf["x"]; - if (this.hasProp(conf, "y")) particle.p.y = conf["y"]; - - if (this.hasProp(conf, "vx")) particle.v.x = conf["vx"]; - if (this.hasProp(conf, "vy")) particle.v.y = conf["vy"]; - - if (this.hasProp(conf, "ax")) particle.a.x = conf["ax"]; - if (this.hasProp(conf, "ay")) particle.a.y = conf["ay"]; - - if (this.hasProp(conf, "p")) particle.p.copy(conf["p"]); - if (this.hasProp(conf, "v")) particle.v.copy(conf["v"]); - if (this.hasProp(conf, "a")) particle.a.copy(conf["a"]); - - if (this.hasProp(conf, "position")) particle.p.copy(conf["position"]); - if (this.hasProp(conf, "velocity")) particle.v.copy(conf["velocity"]); - if (this.hasProp(conf, "accelerate")) particle.a.copy(conf["accelerate"]); - }, - - hasProp(target, key) { - if (!target) return false; - return target[key] !== undefined; - // return obj.hasOwnProperty(key); - }, - - /** - * set the prototype in a given prototypeObject - * - * @memberof Proton#Proton.Util - * @method setProp - * - * @todo add description for param `target` - * @todo translate desription from chinese to english - * - * @param {Object} target - * @param {Object} prototypeObject An object of single prototypes - * - * @return {Object} target - */ - setProp(target, props) { - for (let prop in props) { - if (target.hasOwnProperty(prop)) { - target[prop] = Span.getSpanValue(props[prop]); - } - } - - return target; - }, - /** * This will get the image data. It could be necessary to create a Proton.Zone. *