@n3rdw1z4rd/core 0.15.0 → 0.16.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/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;
@@ -744,353 +745,100 @@ function GenerateWalls(map) {
744
745
  }
745
746
 
746
747
  // 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);
748
+ var ECS = class {
749
+ nextEntityId = 1;
750
+ components = /* @__PURE__ */ new Map();
751
+ createEntity() {
752
+ return this.nextEntityId++;
753
+ }
754
+ addComponent(entity, name, data) {
755
+ if (!this.components.has(name)) {
756
+ this.components.set(name, /* @__PURE__ */ new Map());
757
+ }
758
+ this.components.get(name).set(entity, data);
759
+ }
760
+ getComponent(entity, name) {
761
+ return this.components.get(name)?.get(entity);
762
+ }
763
+ removeComponent(entity, name) {
764
+ this.components.get(name)?.delete(entity);
765
+ }
766
+ getEntitiesWithComponents(...componentNames) {
767
+ if (componentNames.length === 0) return [];
768
+ const first = this.components.get(componentNames[0]);
769
+ if (!first) return [];
770
+ const result = [];
771
+ for (const entity of first.keys()) {
772
+ if (componentNames.every((name) => this.components.get(name)?.has(entity))) {
773
+ result.push(entity);
790
774
  }
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
775
  }
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");
833
- }
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
- }
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() {
776
+ return result;
963
777
  }
964
778
  };
965
779
 
966
780
  // src/input/index.ts
967
781
  var Input = class extends Emitter {
782
+ _down = /* @__PURE__ */ new Map();
783
+ _pointerPosition = new Vector();
968
784
  inputThreshold = 200;
969
- _keyStates = {};
970
- _mouseButtonStates = {};
971
- _mousePosition = [0, 0];
972
- _mousePosition2 = [0, 0];
973
- _contextMenuTarget;
974
- get mousePosition() {
975
- return this._mousePosition;
785
+ get down() {
786
+ return this._down;
976
787
  }
977
- get mousePosition2() {
978
- return this._mousePosition2;
788
+ get pointerPosition() {
789
+ return this._pointerPosition;
979
790
  }
980
791
  constructor() {
981
792
  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
793
+ window.addEventListener("contextmenu", (ev) => {
794
+ log("contextmenu:", ev);
1071
795
  });
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
796
+ window.addEventListener("keydown", (ev) => {
797
+ const { code, repeat, timeStamp } = ev;
798
+ if (!repeat) {
799
+ this._down.set(code, { state: 1, time: timeStamp });
800
+ this.emit("key_down", code, ev);
801
+ }
802
+ });
803
+ window.addEventListener("keyup", (ev) => {
804
+ const { code, timeStamp } = ev;
805
+ const delta = timeStamp - (this._down.get(code)?.time ?? 0);
806
+ this._down.set(code, { state: 0, time: timeStamp });
807
+ this.emit("key_up", code, ev);
808
+ if (delta < this.inputThreshold) {
809
+ this.emit("key_pressed", code, ev);
810
+ }
811
+ });
812
+ window.addEventListener("pointerdown", (ev) => {
813
+ const { button, timeStamp } = ev;
814
+ const code = `Button${button}`;
815
+ if (!this._down.get(code)) {
816
+ this._down.set(code, { state: 1, time: timeStamp });
817
+ this.emit("pointer_down", code, ev);
818
+ }
819
+ });
820
+ window.addEventListener("pointerup", (ev) => {
821
+ const { button, timeStamp } = ev;
822
+ const code = `Button${button}`;
823
+ const delta = timeStamp - (this._down.get(code)?.time ?? 0);
824
+ this._down.set(code, { state: 0, time: timeStamp });
825
+ if (delta < this.inputThreshold) {
826
+ this.emit("button_clicked", code, ev);
827
+ }
828
+ });
829
+ window.addEventListener("mousemove", (ev) => {
830
+ const { offsetX, offsetY, movementX, movementY } = ev;
831
+ this._pointerPosition.x = offsetX;
832
+ this._pointerPosition.y = offsetY;
833
+ this.emit("pointer_move", movementX, movementY, ev);
834
+ });
835
+ window.addEventListener("wheel", (ev) => {
836
+ const { deltaY } = ev;
837
+ this.emit("wheel", deltaY, ev);
1081
838
  });
1082
839
  }
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;
840
+ isDown(name) {
841
+ return this._down.get(name) ? true : false;
1094
842
  }
1095
843
  };
1096
844
 
@@ -1178,6 +926,10 @@ function GetUrlParams() {
1178
926
  }
1179
927
  return params;
1180
928
  }
929
+ var xy2k = (x, y, delimiter = ",") => `${x}${delimiter}${y}`;
930
+ var k2xy = (key, delimiter = ",") => key.split(delimiter).map(parseFloat);
931
+ var xyz2k = (x, y, z, delimiter = ",") => `${x}${delimiter},${y},${delimiter},${z}`;
932
+ var k2xyz = (key, delimiter = ",") => key.split(delimiter).map(parseFloat);
1181
933
 
1182
934
  // src/noise.ts
1183
935
  import { createNoise2D, createNoise3D, createNoise4D } from "simplex-noise";
@@ -1283,7 +1035,7 @@ var Color = class _Color {
1283
1035
  [this._r, this._g, this._b, this._a] = this._parse_hex_color_string(value);
1284
1036
  this._hex = value;
1285
1037
  } else {
1286
- logwrn(`Color: invalid color string: ${value}. Should be a hexadecimal color string (i.e.: '#ffffff' or '#ffffffff'). Keeping the current color.`);
1038
+ log(`Color: invalid color string: ${value}. Should be a hexadecimal color string (i.e.: '#ffffff' or '#ffffffff'). Keeping the current color.`);
1287
1039
  }
1288
1040
  }
1289
1041
  get rgba() {
@@ -1294,7 +1046,7 @@ var Color = class _Color {
1294
1046
  if (r.startsWith("#")) {
1295
1047
  [r, g, b, a] = this._parse_hex_color_string(r);
1296
1048
  } else {
1297
- logwrn(`Color: invalid color string: ${r}. Should be a hexadecimal color string (i.e.: '#ffffff' or '#ffffffff'). Defaulting to opaque white.`);
1049
+ log(`Color: invalid color string: ${r}. Should be a hexadecimal color string (i.e.: '#ffffff' or '#ffffffff'). Defaulting to opaque white.`);
1298
1050
  r = g = b = a = 255;
1299
1051
  }
1300
1052
  } else {
@@ -1395,7 +1147,7 @@ var Color = class _Color {
1395
1147
  const [r, g, b, a] = rgba.map(Number);
1396
1148
  return new _Color(r, g, b, a);
1397
1149
  } else {
1398
- logwrn(`Color: invalid CSS color name: ${name}. Defaulting to transparent.`);
1150
+ log(`Color: invalid CSS color name: ${name}. Defaulting to transparent.`);
1399
1151
  return _Color.TRANSPARENT;
1400
1152
  }
1401
1153
  }
@@ -2031,7 +1783,7 @@ var ThreeJsBoilerPlate = class {
2031
1783
  this.orbitControls?.dispose();
2032
1784
  this.orbitControls = void 0;
2033
1785
  this.input.on("mouse_move", ({ deltaX, deltaY }) => {
2034
- if (this.input.isMouseButtonDown(0)) {
1786
+ if (this.input.isDown("Button0")) {
2035
1787
  this.cameraRig.orbit(deltaX, deltaY);
2036
1788
  }
2037
1789
  }).on("mouse_wheel", ({ deltaY }) => this.cameraRig.dolly(deltaY));
@@ -2078,8 +1830,8 @@ var ThreeJsBoilerPlate = class {
2078
1830
  }
2079
1831
  }
2080
1832
  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;
1833
+ const pickX = this.input.pointerPosition.x / this.renderer.domElement.width * 2 - 1;
1834
+ const pickY = -(this.input.pointerPosition.y / this.renderer.domElement.height) * 2 + 1;
2083
1835
  this.raycaster.setFromCamera(new Vector2(pickX, pickY), this.cameraRig.camera);
2084
1836
  const intersected = this.raycaster.intersectObjects(this.scene.children);
2085
1837
  return intersected.length ? intersected[0] : null;
@@ -3334,12 +3086,10 @@ export {
3334
3086
  imul,
3335
3087
  isNullOrUndefined,
3336
3088
  isPositiveString,
3089
+ k2xy,
3090
+ k2xyz,
3337
3091
  lerp,
3338
3092
  log,
3339
- logdev,
3340
- logerr,
3341
- loginf,
3342
- logwrn,
3343
3093
  manhattanDistance,
3344
3094
  max,
3345
3095
  min,
@@ -3349,5 +3099,7 @@ export {
3349
3099
  roundTo,
3350
3100
  sin,
3351
3101
  sqrt,
3352
- squaredDistance
3102
+ squaredDistance,
3103
+ xy2k,
3104
+ xyz2k
3353
3105
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@n3rdw1z4rd/core",
3
- "version": "0.15.0",
3
+ "version": "0.16.0",
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",