@tsparticles/plugin-infection 3.0.0-alpha.1 → 3.0.0-beta.0

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.
package/cjs/index.js CHANGED
@@ -13,15 +13,6 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
13
13
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
17
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
18
- return new (P || (P = Promise))(function (resolve, reject) {
19
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
22
- step((generator = generator.apply(thisArg, _arguments || [])).next());
23
- });
24
- };
25
16
  Object.defineProperty(exports, "__esModule", { value: true });
26
17
  exports.loadInfectionPlugin = void 0;
27
18
  const Infection_1 = require("./Options/Classes/Infection");
@@ -35,26 +26,23 @@ class InfectionPlugin {
35
26
  return new InfectionInstance_1.InfectionInstance(container);
36
27
  }
37
28
  loadOptions(options, source) {
38
- if (!this.needsPlugin(source)) {
29
+ if (!this.needsPlugin(options) && !this.needsPlugin(source)) {
39
30
  return;
40
31
  }
41
32
  let infectionOptions = options.infection;
42
- if ((infectionOptions === null || infectionOptions === void 0 ? void 0 : infectionOptions.load) === undefined) {
33
+ if (infectionOptions?.load === undefined) {
43
34
  options.infection = infectionOptions = new Infection_1.Infection();
44
35
  }
45
- infectionOptions.load(source === null || source === void 0 ? void 0 : source.infection);
36
+ infectionOptions.load(source?.infection);
46
37
  }
47
38
  needsPlugin(options) {
48
- var _a, _b;
49
- return (_b = (_a = options === null || options === void 0 ? void 0 : options.infection) === null || _a === void 0 ? void 0 : _a.enable) !== null && _b !== void 0 ? _b : false;
39
+ return options?.infection?.enable ?? false;
50
40
  }
51
41
  }
52
- function loadInfectionPlugin(engine) {
53
- return __awaiter(this, void 0, void 0, function* () {
54
- const plugin = new InfectionPlugin();
55
- yield engine.addPlugin(plugin);
56
- yield engine.addInteractor("particlesInfection", (container) => new ParticlesInfecter_1.ParticlesInfecter(container));
57
- });
42
+ async function loadInfectionPlugin(engine, refresh = true) {
43
+ const plugin = new InfectionPlugin();
44
+ await engine.addPlugin(plugin, refresh);
45
+ await engine.addInteractor("particlesInfection", (container) => new ParticlesInfecter_1.ParticlesInfecter(container), refresh);
58
46
  }
59
47
  exports.loadInfectionPlugin = loadInfectionPlugin;
60
48
  __exportStar(require("./Options/Interfaces/IInfection"), exports);
package/esm/Infecter.js CHANGED
@@ -1,97 +1,95 @@
1
1
  export class Infecter {
2
2
  constructor(container) {
3
- this.container = container;
3
+ this._nextInfectionStage = (particle) => {
4
+ const infectionOptions = this._container.actualOptions.infection, { infection } = particle;
5
+ if (!infectionOptions || !infection) {
6
+ return;
7
+ }
8
+ const stagesCount = infectionOptions.stages.length;
9
+ if (stagesCount <= 0 || infection.stage === undefined) {
10
+ return;
11
+ }
12
+ infection.time = 0;
13
+ if (stagesCount <= ++infection.stage) {
14
+ if (infectionOptions.cure) {
15
+ delete infection.stage;
16
+ delete infection.time;
17
+ return;
18
+ }
19
+ else {
20
+ infection.stage = 0;
21
+ infection.time = 0;
22
+ }
23
+ }
24
+ };
25
+ this._container = container;
4
26
  }
5
27
  startInfection(particle, stage) {
6
- const options = this.container.actualOptions;
7
- if (!options.infection || !particle.infection) {
28
+ const infectionOptions = this._container.actualOptions.infection, { infection } = particle;
29
+ if (!infectionOptions || !infection) {
8
30
  return;
9
31
  }
10
- const stages = options.infection.stages, stagesCount = stages.length;
32
+ const stages = infectionOptions.stages, stagesCount = stages.length;
11
33
  if (stage > stagesCount || stage < 0) {
12
34
  return;
13
35
  }
14
- particle.infection.delay = 0;
15
- particle.infection.delayStage = stage;
36
+ infection.delay = 0;
37
+ infection.delayStage = stage;
16
38
  }
17
39
  updateInfection(particle, delta) {
18
- const infection = this.container.actualOptions.infection;
19
- if (!infection || !particle.infection) {
40
+ const infectionOptions = this._container.actualOptions.infection, { infection } = particle;
41
+ if (!infectionOptions || !infection) {
20
42
  return;
21
43
  }
22
- const stages = infection.stages, stagesCount = stages.length;
23
- if (particle.infection.delay !== undefined && particle.infection.delayStage !== undefined) {
24
- const stage = particle.infection.delayStage;
44
+ const stages = infectionOptions.stages, stagesCount = stages.length;
45
+ if (infection.delay !== undefined && infection.delayStage !== undefined) {
46
+ const stage = infection.delayStage;
25
47
  if (stage > stagesCount || stage < 0) {
26
48
  return;
27
49
  }
28
- if (particle.infection.delay >= infection.delay * 1000) {
29
- particle.infection.stage = stage;
30
- particle.infection.time = 0;
31
- delete particle.infection.delay;
32
- delete particle.infection.delayStage;
50
+ if (infection.delay >= infectionOptions.delay * 1000) {
51
+ infection.stage = stage;
52
+ infection.time = 0;
53
+ delete infection.delay;
54
+ delete infection.delayStage;
33
55
  }
34
56
  else {
35
- particle.infection.delay += delta;
57
+ infection.delay += delta;
36
58
  }
37
59
  }
38
60
  else {
39
- delete particle.infection.delay;
40
- delete particle.infection.delayStage;
61
+ delete infection.delay;
62
+ delete infection.delayStage;
41
63
  }
42
- if (particle.infection.stage !== undefined && particle.infection.time !== undefined) {
43
- const infectionStage = stages[particle.infection.stage];
64
+ if (infection.stage !== undefined && infection.time !== undefined) {
65
+ const infectionStage = stages[infection.stage];
44
66
  if (infectionStage.duration !== undefined && infectionStage.duration >= 0) {
45
- if (particle.infection.time > infectionStage.duration * 1000) {
46
- this.nextInfectionStage(particle);
67
+ if (infection.time > infectionStage.duration * 1000) {
68
+ this._nextInfectionStage(particle);
47
69
  }
48
70
  else {
49
- particle.infection.time += delta;
71
+ infection.time += delta;
50
72
  }
51
73
  }
52
74
  else {
53
- particle.infection.time += delta;
75
+ infection.time += delta;
54
76
  }
55
77
  }
56
78
  else {
57
- delete particle.infection.stage;
58
- delete particle.infection.time;
79
+ delete infection.stage;
80
+ delete infection.time;
59
81
  }
60
82
  }
61
83
  updateInfectionStage(particle, stage) {
62
- const options = this.container.actualOptions;
63
- if (!options.infection || !particle.infection) {
84
+ const options = this._container.actualOptions, { infection } = particle;
85
+ if (!options.infection || !infection) {
64
86
  return;
65
87
  }
66
88
  const stagesCount = options.infection.stages.length;
67
- if (stage > stagesCount ||
68
- stage < 0 ||
69
- (particle.infection.stage !== undefined && particle.infection.stage > stage)) {
89
+ if (stage > stagesCount || stage < 0 || (infection.stage !== undefined && infection.stage > stage)) {
70
90
  return;
71
91
  }
72
- particle.infection.stage = stage;
73
- particle.infection.time = 0;
74
- }
75
- nextInfectionStage(particle) {
76
- const options = this.container.actualOptions;
77
- if (!options.infection || !particle.infection) {
78
- return;
79
- }
80
- const stagesCount = options.infection.stages.length;
81
- if (stagesCount <= 0 || particle.infection.stage === undefined) {
82
- return;
83
- }
84
- particle.infection.time = 0;
85
- if (stagesCount <= ++particle.infection.stage) {
86
- if (options.infection.cure) {
87
- delete particle.infection.stage;
88
- delete particle.infection.time;
89
- return;
90
- }
91
- else {
92
- particle.infection.stage = 0;
93
- particle.infection.time = 0;
94
- }
95
- }
92
+ infection.stage = stage;
93
+ infection.time = 0;
96
94
  }
97
95
  }
@@ -1,12 +1,12 @@
1
- import { Infecter } from "./Infecter";
2
1
  import { itemFromArray } from "@tsparticles/engine";
2
+ import { Infecter } from "./Infecter";
3
3
  export class InfectionInstance {
4
4
  constructor(container) {
5
- this.container = container;
6
- this.container.infecter = new Infecter(this.container);
5
+ this._container = container;
6
+ this._container.infecter = new Infecter(this._container);
7
7
  }
8
8
  particleFillColor(particle) {
9
- const options = this.container.actualOptions;
9
+ const options = this._container.actualOptions;
10
10
  if (!particle.infection || !options.infection) {
11
11
  return;
12
12
  }
@@ -17,13 +17,12 @@ export class InfectionInstance {
17
17
  return this.particleFillColor(particle);
18
18
  }
19
19
  particlesSetup() {
20
- var _a;
21
- const options = this.container.actualOptions;
20
+ const options = this._container.actualOptions;
22
21
  if (!options.infection) {
23
22
  return;
24
23
  }
25
24
  for (let i = 0; i < options.infection.infections; i++) {
26
- const notInfected = this.container.particles.array.filter((p) => {
25
+ const notInfected = this._container.particles.filter((p) => {
27
26
  const infP = p;
28
27
  if (!infP.infection) {
29
28
  infP.infection = {};
@@ -31,7 +30,7 @@ export class InfectionInstance {
31
30
  return infP.infection.stage === undefined;
32
31
  });
33
32
  const infected = itemFromArray(notInfected);
34
- (_a = this.container.infecter) === null || _a === void 0 ? void 0 : _a.startInfection(infected, 0);
33
+ this._container.infecter?.startInfection(infected, 0);
35
34
  }
36
35
  }
37
36
  }
@@ -8,46 +8,43 @@ export class ParticlesInfecter extends ParticlesInteractorBase {
8
8
  init() {
9
9
  }
10
10
  async interact(p1, delta) {
11
- var _a, _b, _c, _d, _e;
12
11
  const infecter = this.container.infecter;
13
12
  if (!infecter) {
14
13
  return;
15
14
  }
16
15
  infecter.updateInfection(p1, delta.value);
17
- if (((_a = p1.infection) === null || _a === void 0 ? void 0 : _a.stage) === undefined) {
16
+ if (p1.infection?.stage === undefined) {
18
17
  return;
19
18
  }
20
19
  const container = this.container, options = container.actualOptions, infectionOptions = options.infection;
21
- if (!(infectionOptions === null || infectionOptions === void 0 ? void 0 : infectionOptions.enable) || infectionOptions.stages.length < 1) {
20
+ if (!infectionOptions?.enable || infectionOptions.stages.length < 1) {
22
21
  return;
23
22
  }
24
- const infectionStage1 = infectionOptions.stages[p1.infection.stage], pxRatio = container.retina.pixelRatio, radius = p1.getRadius() * 2 + infectionStage1.radius * pxRatio, pos = p1.getPosition(), infectedStage1 = (_b = infectionStage1.infectedStage) !== null && _b !== void 0 ? _b : p1.infection.stage, query = container.particles.quadTree.queryCircle(pos, radius), infections = infectionStage1.rate, neighbors = query.length;
23
+ const infectionStage1 = infectionOptions.stages[p1.infection.stage], pxRatio = container.retina.pixelRatio, radius = p1.getRadius() * 2 + infectionStage1.radius * pxRatio, pos = p1.getPosition(), infectedStage1 = infectionStage1.infectedStage ?? p1.infection.stage, query = container.particles.quadTree.queryCircle(pos, radius), infections = infectionStage1.rate, neighbors = query.length;
25
24
  for (const p2 of query) {
26
25
  const infP2 = p2;
27
26
  if (infP2 === p1 ||
28
27
  infP2.destroyed ||
29
28
  infP2.spawning ||
30
- !(((_c = infP2.infection) === null || _c === void 0 ? void 0 : _c.stage) === undefined || infP2.infection.stage !== p1.infection.stage)) {
29
+ !(infP2.infection?.stage === undefined || infP2.infection.stage !== p1.infection.stage) ||
30
+ getRandom() >= infections / neighbors) {
31
31
  continue;
32
32
  }
33
- if (getRandom() < infections / neighbors) {
34
- if (((_d = infP2.infection) === null || _d === void 0 ? void 0 : _d.stage) === undefined) {
35
- infecter.startInfection(infP2, infectedStage1);
36
- }
37
- else if (infP2.infection.stage < p1.infection.stage) {
38
- infecter.updateInfectionStage(infP2, infectedStage1);
39
- }
40
- else if (infP2.infection.stage > p1.infection.stage) {
41
- const infectionStage2 = infectionOptions.stages[infP2.infection.stage];
42
- const infectedStage2 = (_e = infectionStage2 === null || infectionStage2 === void 0 ? void 0 : infectionStage2.infectedStage) !== null && _e !== void 0 ? _e : infP2.infection.stage;
43
- infecter.updateInfectionStage(p1, infectedStage2);
44
- }
33
+ if (infP2.infection?.stage === undefined) {
34
+ infecter.startInfection(infP2, infectedStage1);
35
+ }
36
+ else if (infP2.infection.stage < p1.infection.stage) {
37
+ infecter.updateInfectionStage(infP2, infectedStage1);
38
+ }
39
+ else if (infP2.infection.stage > p1.infection.stage) {
40
+ const infectionStage2 = infectionOptions.stages[infP2.infection.stage];
41
+ const infectedStage2 = infectionStage2?.infectedStage ?? infP2.infection.stage;
42
+ infecter.updateInfectionStage(p1, infectedStage2);
45
43
  }
46
44
  }
47
45
  }
48
46
  isEnabled() {
49
- var _a, _b, _c;
50
- return (_c = (_b = (_a = this.container.actualOptions) === null || _a === void 0 ? void 0 : _a.infection) === null || _b === void 0 ? void 0 : _b.enable) !== null && _c !== void 0 ? _c : false;
47
+ return this.container.actualOptions?.infection?.enable ?? false;
51
48
  }
52
49
  reset() {
53
50
  }
package/esm/index.js CHANGED
@@ -9,24 +9,23 @@ class InfectionPlugin {
9
9
  return new InfectionInstance(container);
10
10
  }
11
11
  loadOptions(options, source) {
12
- if (!this.needsPlugin(source)) {
12
+ if (!this.needsPlugin(options) && !this.needsPlugin(source)) {
13
13
  return;
14
14
  }
15
15
  let infectionOptions = options.infection;
16
- if ((infectionOptions === null || infectionOptions === void 0 ? void 0 : infectionOptions.load) === undefined) {
16
+ if (infectionOptions?.load === undefined) {
17
17
  options.infection = infectionOptions = new Infection();
18
18
  }
19
- infectionOptions.load(source === null || source === void 0 ? void 0 : source.infection);
19
+ infectionOptions.load(source?.infection);
20
20
  }
21
21
  needsPlugin(options) {
22
- var _a, _b;
23
- return (_b = (_a = options === null || options === void 0 ? void 0 : options.infection) === null || _a === void 0 ? void 0 : _a.enable) !== null && _b !== void 0 ? _b : false;
22
+ return options?.infection?.enable ?? false;
24
23
  }
25
24
  }
26
- export async function loadInfectionPlugin(engine) {
25
+ export async function loadInfectionPlugin(engine, refresh = true) {
27
26
  const plugin = new InfectionPlugin();
28
- await engine.addPlugin(plugin);
29
- await engine.addInteractor("particlesInfection", (container) => new ParticlesInfecter(container));
27
+ await engine.addPlugin(plugin, refresh);
28
+ await engine.addInteractor("particlesInfection", (container) => new ParticlesInfecter(container), refresh);
30
29
  }
31
30
  export * from "./Options/Interfaces/IInfection";
32
31
  export * from "./Options/Interfaces/IInfectionStage";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsparticles/plugin-infection",
3
- "version": "3.0.0-alpha.1",
3
+ "version": "3.0.0-beta.0",
4
4
  "description": "tsParticles infection plugin",
5
5
  "homepage": "https://particles.js.org",
6
6
  "repository": {
@@ -72,6 +72,10 @@
72
72
  "type": "github",
73
73
  "url": "https://github.com/sponsors/matteobruni"
74
74
  },
75
+ {
76
+ "type": "github",
77
+ "url": "https://github.com/sponsors/tsparticles"
78
+ },
75
79
  {
76
80
  "type": "buymeacoffee",
77
81
  "url": "https://www.buymeacoffee.com/matteobruni"
@@ -82,10 +86,11 @@
82
86
  "unpkg": "tsparticles.plugin.infection.min.js",
83
87
  "module": "esm/index.js",
84
88
  "types": "types/index.d.ts",
89
+ "sideEffects": false,
90
+ "dependencies": {
91
+ "@tsparticles/engine": "^3.0.0-beta.0"
92
+ },
85
93
  "publishConfig": {
86
94
  "access": "public"
87
- },
88
- "dependencies": {
89
- "@tsparticles/engine": "^3.0.0-alpha.1"
90
95
  }
91
- }
96
+ }