@n3rdw1z4rd/core 0.15.0 → 0.16.1

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/dist/index.mjs CHANGED
@@ -165,6 +165,7 @@ var Clock = class extends Emitter {
165
165
  };
166
166
  requestAnimationFrame(update);
167
167
  }
168
+ return this;
168
169
  }
169
170
  stop() {
170
171
  this._isRunning = false;
@@ -385,12 +386,12 @@ var Vector = class _Vector {
385
386
  // src/dungeon-generator.ts
386
387
  import { Heap } from "heap-js";
387
388
 
388
- // src/logger.ts
389
- var logdev = console.debug.bind(console, "%c[dbg]", "font-weight: bold; color: #888;");
390
- var log = logdev;
391
- var loginf = console.log.bind(console, "%c[inf]", "font-weight: bold;");
392
- var logwrn = console.log.bind(console, "%c[wrn]", "font-weight: bold; color: orange;");
393
- var logerr = console.log.bind(console, "%c[err]", "font-weight: bold; color: red;");
389
+ // src/log.ts
390
+ var log = console.debug.bind(
391
+ console,
392
+ "%c[dev]",
393
+ "font-weight: bold; color: #888;"
394
+ );
394
395
 
395
396
  // src/map2d.ts
396
397
  var DEFAULT_VALUE = -1;
@@ -481,6 +482,28 @@ var RandomNumberGenerator = class {
481
482
  const theta = this.nextf * 2 * PI;
482
483
  return [cos(theta), sin(theta)];
483
484
  }
485
+ pointInUnitCircle(radius = 1, floored = false) {
486
+ const theta = this.nextf * 2 * PI;
487
+ const r = sqrt(this.nextf) * radius;
488
+ const x = r * cos(theta);
489
+ const y = r * sin(theta);
490
+ return !floored ? [x, y] : [floor(x), floor(y)];
491
+ }
492
+ pointInUnitSphere(radius = 1, floored = false) {
493
+ var u = this.nextf;
494
+ var v = this.nextf;
495
+ var theta = u * 2 * PI;
496
+ var phi = Math.acos(2 * v - 1);
497
+ var r = radius * Math.cbrt(rng.nextf);
498
+ var sinTheta = sin(theta);
499
+ var cosTheta = cos(theta);
500
+ var sinPhi = sin(phi);
501
+ var cosPhi = cos(phi);
502
+ var x = r * sinPhi * cosTheta;
503
+ var y = r * sinPhi * sinTheta;
504
+ var z = r * cosPhi;
505
+ return !floored ? [x, y, z] : [floor(x), floor(y), floor(z)];
506
+ }
484
507
  parkMillerNormal() {
485
508
  const mean = 1 / 2;
486
509
  const stddev = 1 / 6;
@@ -744,353 +767,100 @@ function GenerateWalls(map) {
744
767
  }
745
768
 
746
769
  // src/ecs.ts
747
- var ECS = class _ECS {
748
- _globals = /* @__PURE__ */ new Map();
749
- _entities = /* @__PURE__ */ new Map();
750
- _components = /* @__PURE__ */ new Map();
751
- _defaultComponents = [];
752
- _systems = /* @__PURE__ */ new Map();
753
- _onTickStartCallbacks = [];
754
- _onTickEndCallbacks = [];
755
- get components() {
756
- return [...this._components.keys()];
757
- }
758
- get systems() {
759
- return [...this._systems.keys()];
760
- }
761
- get entities() {
762
- return [...this._entities.values()];
763
- }
764
- uid(length = 8) {
765
- const numBytes = Math.ceil(length / 2);
766
- const buffer = new Uint8Array(numBytes);
767
- window.crypto.getRandomValues(buffer);
768
- let uid = "";
769
- for (let i = 0; i < buffer.length; i++) {
770
- uid += buffer[i].toString(16).padStart(2, "0");
771
- }
772
- return uid.substring(0, length);
773
- }
774
- createComponent(uid, data = null) {
775
- if (!this._components.has(uid)) {
776
- this._components.set(uid, data);
777
- log("created component:", { uid, data });
778
- } else {
779
- logwrn("createComponent: a component already exists with uid:", uid);
780
- }
781
- return this;
782
- }
783
- includeAsDefaultComponents(...components) {
784
- components.forEach((component) => {
785
- if (!this._defaultComponents.includes(component)) {
786
- this._defaultComponents.push(component);
787
- log("includeAsDefaultComponents:", component);
788
- } else {
789
- log("includeAsDefaultComponents: already exists:", component);
790
- }
791
- });
792
- return this;
793
- }
794
- createSystem(uid, ...components) {
795
- const callback = components.pop();
796
- const comps = components.filter((comp) => typeof comp === "string");
797
- if (!this._systems.has(uid)) {
798
- this._systems.set(uid, { components: comps, callback });
799
- log("created system:", { uid, components, callback });
800
- } else {
801
- logwrn("createSystem: a system already exists with uid:", uid);
802
- }
803
- return this;
804
- }
805
- createEntityWithUid(uid, ...components) {
806
- if (!this._entities.has(uid)) {
807
- const componentList = [
808
- .../* @__PURE__ */ new Set([
809
- ...this._defaultComponents,
810
- ...components
811
- ])
812
- ];
813
- const comps = /* @__PURE__ */ new Map();
814
- componentList.forEach((component) => {
815
- if (this._components.has(component)) {
816
- const componentData = this._components.get(component);
817
- const data = {};
818
- for (const uid2 in componentData) {
819
- const value = componentData[uid2];
820
- data[uid2] = typeof value === "function" ? value(uid2) : value;
821
- }
822
- comps.set(component, data);
823
- } else {
824
- logwrn("createEntityWithUid: missing component:", component);
825
- }
826
- });
827
- if (comps.size === componentList.length) {
828
- const entity = { uid, components: comps };
829
- this._entities.set(uid, entity);
830
- log("created entity:", entity);
831
- } else {
832
- logwrn("createEntityWithUid: failed to create an entity with missing components");
770
+ var ECS = class {
771
+ nextEntityId = 1;
772
+ components = /* @__PURE__ */ new Map();
773
+ createEntity() {
774
+ return this.nextEntityId++;
775
+ }
776
+ addComponent(entity, name, data) {
777
+ if (!this.components.has(name)) {
778
+ this.components.set(name, /* @__PURE__ */ new Map());
779
+ }
780
+ this.components.get(name).set(entity, data);
781
+ }
782
+ getComponent(entity, name) {
783
+ return this.components.get(name)?.get(entity);
784
+ }
785
+ removeComponent(entity, name) {
786
+ this.components.get(name)?.delete(entity);
787
+ }
788
+ getEntitiesWithComponents(...componentNames) {
789
+ if (componentNames.length === 0) return [];
790
+ const first = this.components.get(componentNames[0]);
791
+ if (!first) return [];
792
+ const result = [];
793
+ for (const entity of first.keys()) {
794
+ if (componentNames.every((name) => this.components.get(name)?.has(entity))) {
795
+ result.push(entity);
833
796
  }
834
- } else {
835
- logwrn("createEntityWithUid: an entity already exists with uid:", uid);
836
- }
837
- return this;
838
- }
839
- createEntity(...components) {
840
- const uid = this.uid();
841
- return this.createEntityWithUid(uid, ...components);
842
- }
843
- createEntities(count, ...components) {
844
- for (; count > 0; count--) {
845
- this.createEntity(...components);
846
797
  }
847
- return this;
848
- }
849
- getEntity(uid) {
850
- return this._entities.get(uid);
851
- }
852
- getEntitiesWithComponents(...components) {
853
- const filter = components.pop();
854
- const uids = components.filter((component) => typeof component === "string");
855
- const entities = [];
856
- this._entities.forEach((entity) => {
857
- if (uids.every(
858
- (component) => [...entity.components.keys()].includes(component)
859
- )) {
860
- for (const filterComponent in filter) {
861
- for (const filterKey in filter[filterComponent]) {
862
- const entityValue = entity.components.get(filterComponent)[filterKey];
863
- const filterValue = filter[filterComponent][filterKey];
864
- if (entityValue === filterValue) {
865
- entities.push(entity);
866
- }
867
- }
868
- }
869
- }
870
- });
871
- return entities;
872
- }
873
- addComponent(uid, component, values = {}) {
874
- const entity = this._entities.get(uid);
875
- if (entity) {
876
- if (this._components.has(component)) {
877
- const comp = this._components.get(component);
878
- const componentData = {
879
- ...comp,
880
- ...values
881
- };
882
- entity.components.set(component, componentData);
883
- log("addComponent: added component:", component, "to entity:", uid);
884
- } else {
885
- logwrn("addComponent: component not found:", component);
886
- }
887
- } else {
888
- logwrn("addComponent: entity not found:", uid);
889
- }
890
- return this;
891
- }
892
- onAllEntitiesNow(callback) {
893
- this._entities.forEach((entity) => callback(entity));
894
- log("onAllEntitiesNow: executed on all entities:", callback);
895
- return this;
896
- }
897
- duplicateEntity(uid, count = 1, deep = false) {
898
- const entity = this._entities.get(uid);
899
- if (entity) {
900
- if (!deep) {
901
- for (let i = 0; i < count; i++) this.createEntity(...entity.components.keys());
902
- } else {
903
- for (let i = 0; i < count; i++) {
904
- const newEntity = { uid: this.uid(), components: /* @__PURE__ */ new Map() };
905
- entity.components.forEach((value, key) => {
906
- newEntity.components.set(key, value);
907
- });
908
- this._entities.set(newEntity.uid, newEntity);
909
- }
910
- ;
911
- }
912
- log("duplicateEntity: duplicated entity:", uid);
913
- } else {
914
- logwrn("duplicateEntity: entity not found:", uid);
915
- }
916
- return this;
917
- }
918
- getGlobal(key) {
919
- return this._globals.get(key);
920
- }
921
- setGlobal(key, value) {
922
- this._globals.set(key, value);
923
- return this;
924
- }
925
- beforeTick(callback) {
926
- this._onTickStartCallbacks.push(callback);
927
- log("beforeTick: added:", callback);
928
- return this;
929
- }
930
- afterTick(callback) {
931
- this._onTickEndCallbacks.push(callback);
932
- log("afterTick: added:", callback);
933
- return this;
934
- }
935
- runSystem(uid) {
936
- const system = this._systems.get(uid);
937
- if (system) {
938
- this._entities.forEach((entity) => {
939
- if (system.components.every(
940
- (component) => [...entity.components.keys()].includes(component)
941
- )) {
942
- system.callback(entity, Object.fromEntries(entity.components));
943
- }
944
- });
945
- } else {
946
- logwrn("runSystem: system not found:", uid);
947
- }
948
- return this;
949
- }
950
- update() {
951
- this._onTickStartCallbacks.forEach((cb) => cb());
952
- this._systems.forEach((_, uid) => this.runSystem(uid));
953
- this._onTickEndCallbacks.forEach((cb) => cb());
954
- }
955
- static get instance() {
956
- if (!_ECS._instance) {
957
- _ECS._instance = new _ECS();
958
- }
959
- return _ECS._instance;
960
- }
961
- static _instance;
962
- constructor() {
798
+ return result;
963
799
  }
964
800
  };
965
801
 
966
802
  // src/input/index.ts
967
803
  var Input = class extends Emitter {
804
+ _down = /* @__PURE__ */ new Map();
805
+ _pointerPosition = new Vector();
968
806
  inputThreshold = 200;
969
- _keyStates = {};
970
- _mouseButtonStates = {};
971
- _mousePosition = [0, 0];
972
- _mousePosition2 = [0, 0];
973
- _contextMenuTarget;
974
- get mousePosition() {
975
- return this._mousePosition;
807
+ get down() {
808
+ return this._down;
976
809
  }
977
- get mousePosition2() {
978
- return this._mousePosition2;
810
+ get pointerPosition() {
811
+ return this._pointerPosition;
979
812
  }
980
813
  constructor() {
981
814
  super();
982
- window.addEventListener("keydown", this._onKeyDown.bind(this));
983
- window.addEventListener("keyup", this._onKeyUp.bind(this));
984
- window.addEventListener("mousedown", this._onMouseButtonDown.bind(this));
985
- window.addEventListener("mouseup", this._onMouseButtonUp.bind(this));
986
- window.addEventListener("mousemove", this._onMouseMove.bind(this));
987
- window.addEventListener("wheel", this._onWheel.bind(this));
988
- }
989
- blockContextMenu(target) {
990
- this.stopBlockingContextMenu();
991
- this._contextMenuTarget = target;
992
- target.addEventListener("contextmenu", this._onContextMenu);
993
- }
994
- stopBlockingContextMenu() {
995
- this._contextMenuTarget?.removeEventListener("contextmenu", this._onContextMenu);
996
- this._contextMenuTarget = void 0;
997
- }
998
- _onContextMenu = (ev) => {
999
- ev.preventDefault();
1000
- this.emit("contextmenu");
1001
- };
1002
- _getCommonEventProps(ev) {
1003
- const props = {
1004
- timeStamp: ev.timeStamp,
1005
- altKey: ev.altKey,
1006
- ctrlKey: ev.ctrlKey,
1007
- metaKey: ev.metaKey,
1008
- shiftKey: ev.shiftKey
1009
- };
1010
- return props;
1011
- }
1012
- _onKeyDown(ev) {
1013
- const props = this._getCommonEventProps(ev);
1014
- const { code, key } = ev;
1015
- if (!ev.repeat) {
1016
- this._keyStates[code] = { state: 1, timeStamp: props.timeStamp };
1017
- this.emit("key_down", { ...props, code, key });
1018
- this.emit(`${code.toLowerCase()}_down`, props);
1019
- }
1020
- }
1021
- _onKeyUp(ev) {
1022
- const props = this._getCommonEventProps(ev);
1023
- const { code, key } = ev;
1024
- const deltaStamp = props.timeStamp - (this._keyStates[code]?.timeStamp ?? 0);
1025
- this._keyStates[code] = { state: 0, timeStamp: props.timeStamp };
1026
- this.emit("key_up", { ...props, code, key });
1027
- this.emit(`${code.toLowerCase()}_up`, props);
1028
- if (deltaStamp < this.inputThreshold) {
1029
- this.emit("key_pressed", { ...props, code, key });
1030
- this.emit(`${code.toLowerCase()}_pressed`, props);
1031
- }
1032
- }
1033
- _onMouseButtonDown(ev) {
1034
- const props = this._getCommonEventProps(ev);
1035
- const { button } = ev;
1036
- if (!this._mouseButtonStates[button]?.state) {
1037
- this._mouseButtonStates[button] = { state: 1, timeStamp: props.timeStamp };
1038
- this.emit("mouse_button_down", { ...props, button });
1039
- this.emit(`mouse_button_${button}_down`, props);
1040
- }
1041
- }
1042
- _onMouseButtonUp(ev) {
1043
- const props = this._getCommonEventProps(ev);
1044
- const { button } = ev;
1045
- const deltaStamp = props.timeStamp - (this._mouseButtonStates[button]?.timeStamp ?? 0);
1046
- this._mouseButtonStates[button] = { state: 0, timeStamp: props.timeStamp };
1047
- this.emit("mouse_button_up", { ...props, button });
1048
- this.emit(`mouse_button_${button}_up`, props);
1049
- if (deltaStamp < this.inputThreshold) {
1050
- this.emit("mouse_button_clicked", { ...props, button });
1051
- this.emit(`mouse_button_${button}_clicked`, props);
1052
- }
1053
- }
1054
- _onMouseMove(ev) {
1055
- const props = this._getCommonEventProps(ev);
1056
- const { buttons, offsetX, offsetY, movementX, movementY } = ev;
1057
- this._mousePosition = [offsetX, offsetY];
1058
- const width = window.innerWidth;
1059
- const height = window.innerHeight;
1060
- this._mousePosition2 = [
1061
- ev.clientX / width * 2 - 1,
1062
- -(ev.clientY / height) * 2 + 1
1063
- ];
1064
- this.emit("mouse_move", {
1065
- ...props,
1066
- buttons,
1067
- x: offsetX,
1068
- y: offsetY,
1069
- deltaX: movementX,
1070
- deltaY: movementY
815
+ window.addEventListener("contextmenu", (ev) => {
816
+ log("contextmenu:", ev);
1071
817
  });
1072
- }
1073
- _onWheel(ev) {
1074
- const props = this._getCommonEventProps(ev);
1075
- const { deltaX, deltaY, deltaZ } = ev;
1076
- this.emit("mouse_wheel", {
1077
- ...props,
1078
- deltaX,
1079
- deltaY,
1080
- deltaZ
818
+ window.addEventListener("keydown", (ev) => {
819
+ const { code, repeat, timeStamp } = ev;
820
+ if (!repeat) {
821
+ this._down.set(code, { state: 1, time: timeStamp });
822
+ this.emit("key_down", code, ev);
823
+ }
824
+ });
825
+ window.addEventListener("keyup", (ev) => {
826
+ const { code, timeStamp } = ev;
827
+ const delta = timeStamp - (this._down.get(code)?.time ?? 0);
828
+ this._down.set(code, { state: 0, time: timeStamp });
829
+ this.emit("key_up", code, ev);
830
+ if (delta < this.inputThreshold) {
831
+ this.emit("key_pressed", code, ev);
832
+ }
833
+ });
834
+ window.addEventListener("pointerdown", (ev) => {
835
+ const { button, timeStamp } = ev;
836
+ const code = `Button${button}`;
837
+ if (!this._down.get(code)) {
838
+ this._down.set(code, { state: 1, time: timeStamp });
839
+ this.emit("pointer_down", code, ev);
840
+ }
841
+ });
842
+ window.addEventListener("pointerup", (ev) => {
843
+ const { button, timeStamp } = ev;
844
+ const code = `Button${button}`;
845
+ const delta = timeStamp - (this._down.get(code)?.time ?? 0);
846
+ this._down.set(code, { state: 0, time: timeStamp });
847
+ if (delta < this.inputThreshold) {
848
+ this.emit("button_clicked", code, ev);
849
+ }
850
+ });
851
+ window.addEventListener("mousemove", (ev) => {
852
+ const { offsetX, offsetY, movementX, movementY } = ev;
853
+ this._pointerPosition.x = offsetX;
854
+ this._pointerPosition.y = offsetY;
855
+ this.emit("pointer_move", movementX, movementY, ev);
856
+ });
857
+ window.addEventListener("wheel", (ev) => {
858
+ const { deltaY } = ev;
859
+ this.emit("wheel", deltaY, ev);
1081
860
  });
1082
861
  }
1083
- isKeyDown(keyCode) {
1084
- return this._keyStates[keyCode]?.state === 1 ? true : false;
1085
- }
1086
- getKeyState(keyCode) {
1087
- return this._keyStates[keyCode]?.state ?? 0;
1088
- }
1089
- isMouseButtonDown(mouseButton) {
1090
- return this._mouseButtonStates[mouseButton]?.state === 1 ? true : false;
1091
- }
1092
- getMouseButtonState(button) {
1093
- return this._mouseButtonStates[button]?.state ?? 0;
862
+ isDown(name) {
863
+ return this._down.get(name) ? true : false;
1094
864
  }
1095
865
  };
1096
866
 
@@ -1178,6 +948,10 @@ function GetUrlParams() {
1178
948
  }
1179
949
  return params;
1180
950
  }
951
+ var xy2k = (x, y, delimiter = ",") => `${x}${delimiter}${y}`;
952
+ var k2xy = (key, delimiter = ",") => key.split(delimiter).map(parseFloat);
953
+ var xyz2k = (x, y, z, delimiter = ",") => `${x}${delimiter},${y},${delimiter},${z}`;
954
+ var k2xyz = (key, delimiter = ",") => key.split(delimiter).map(parseFloat);
1181
955
 
1182
956
  // src/noise.ts
1183
957
  import { createNoise2D, createNoise3D, createNoise4D } from "simplex-noise";
@@ -1283,7 +1057,7 @@ var Color = class _Color {
1283
1057
  [this._r, this._g, this._b, this._a] = this._parse_hex_color_string(value);
1284
1058
  this._hex = value;
1285
1059
  } else {
1286
- logwrn(`Color: invalid color string: ${value}. Should be a hexadecimal color string (i.e.: '#ffffff' or '#ffffffff'). Keeping the current color.`);
1060
+ log(`Color: invalid color string: ${value}. Should be a hexadecimal color string (i.e.: '#ffffff' or '#ffffffff'). Keeping the current color.`);
1287
1061
  }
1288
1062
  }
1289
1063
  get rgba() {
@@ -1294,7 +1068,7 @@ var Color = class _Color {
1294
1068
  if (r.startsWith("#")) {
1295
1069
  [r, g, b, a] = this._parse_hex_color_string(r);
1296
1070
  } else {
1297
- logwrn(`Color: invalid color string: ${r}. Should be a hexadecimal color string (i.e.: '#ffffff' or '#ffffffff'). Defaulting to opaque white.`);
1071
+ log(`Color: invalid color string: ${r}. Should be a hexadecimal color string (i.e.: '#ffffff' or '#ffffffff'). Defaulting to opaque white.`);
1298
1072
  r = g = b = a = 255;
1299
1073
  }
1300
1074
  } else {
@@ -1395,7 +1169,7 @@ var Color = class _Color {
1395
1169
  const [r, g, b, a] = rgba.map(Number);
1396
1170
  return new _Color(r, g, b, a);
1397
1171
  } else {
1398
- logwrn(`Color: invalid CSS color name: ${name}. Defaulting to transparent.`);
1172
+ log(`Color: invalid CSS color name: ${name}. Defaulting to transparent.`);
1399
1173
  return _Color.TRANSPARENT;
1400
1174
  }
1401
1175
  }
@@ -2031,7 +1805,7 @@ var ThreeJsBoilerPlate = class {
2031
1805
  this.orbitControls?.dispose();
2032
1806
  this.orbitControls = void 0;
2033
1807
  this.input.on("mouse_move", ({ deltaX, deltaY }) => {
2034
- if (this.input.isMouseButtonDown(0)) {
1808
+ if (this.input.isDown("Button0")) {
2035
1809
  this.cameraRig.orbit(deltaX, deltaY);
2036
1810
  }
2037
1811
  }).on("mouse_wheel", ({ deltaY }) => this.cameraRig.dolly(deltaY));
@@ -2078,8 +1852,8 @@ var ThreeJsBoilerPlate = class {
2078
1852
  }
2079
1853
  }
2080
1854
  pick() {
2081
- const pickX = this.input.mousePosition[0] / this.renderer.domElement.width * 2 - 1;
2082
- const pickY = -(this.input.mousePosition[1] / this.renderer.domElement.height) * 2 + 1;
1855
+ const pickX = this.input.pointerPosition.x / this.renderer.domElement.width * 2 - 1;
1856
+ const pickY = -(this.input.pointerPosition.y / this.renderer.domElement.height) * 2 + 1;
2083
1857
  this.raycaster.setFromCamera(new Vector2(pickX, pickY), this.cameraRig.camera);
2084
1858
  const intersected = this.raycaster.intersectObjects(this.scene.children);
2085
1859
  return intersected.length ? intersected[0] : null;
@@ -3334,12 +3108,10 @@ export {
3334
3108
  imul,
3335
3109
  isNullOrUndefined,
3336
3110
  isPositiveString,
3111
+ k2xy,
3112
+ k2xyz,
3337
3113
  lerp,
3338
3114
  log,
3339
- logdev,
3340
- logerr,
3341
- loginf,
3342
- logwrn,
3343
3115
  manhattanDistance,
3344
3116
  max,
3345
3117
  min,
@@ -3349,5 +3121,7 @@ export {
3349
3121
  roundTo,
3350
3122
  sin,
3351
3123
  sqrt,
3352
- squaredDistance
3124
+ squaredDistance,
3125
+ xy2k,
3126
+ xyz2k
3353
3127
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@n3rdw1z4rd/core",
3
- "version": "0.15.0",
3
+ "version": "0.16.1",
4
4
  "description": "core library",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -11,7 +11,8 @@
11
11
  "scripts": {
12
12
  "build-old": "tsup && shx cp -r ./src/css ./dist/",
13
13
  "build": "tsup",
14
- "dev": "tsup --watch"
14
+ "dev": "tsup --watch",
15
+ "test": "npx ts-node src/test"
15
16
  },
16
17
  "repository": {
17
18
  "type": "git",