@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.js CHANGED
@@ -107,12 +107,10 @@ __export(index_exports, {
107
107
  imul: () => imul,
108
108
  isNullOrUndefined: () => isNullOrUndefined,
109
109
  isPositiveString: () => isPositiveString,
110
+ k2xy: () => k2xy,
111
+ k2xyz: () => k2xyz,
110
112
  lerp: () => lerp,
111
113
  log: () => log,
112
- logdev: () => logdev,
113
- logerr: () => logerr,
114
- loginf: () => loginf,
115
- logwrn: () => logwrn,
116
114
  manhattanDistance: () => manhattanDistance,
117
115
  max: () => max,
118
116
  min: () => min,
@@ -122,7 +120,9 @@ __export(index_exports, {
122
120
  roundTo: () => roundTo,
123
121
  sin: () => sin,
124
122
  sqrt: () => sqrt,
125
- squaredDistance: () => squaredDistance
123
+ squaredDistance: () => squaredDistance,
124
+ xy2k: () => xy2k,
125
+ xyz2k: () => xyz2k
126
126
  });
127
127
  module.exports = __toCommonJS(index_exports);
128
128
 
@@ -293,6 +293,7 @@ var Clock = class extends Emitter {
293
293
  };
294
294
  requestAnimationFrame(update);
295
295
  }
296
+ return this;
296
297
  }
297
298
  stop() {
298
299
  this._isRunning = false;
@@ -513,12 +514,12 @@ var Vector = class _Vector {
513
514
  // src/dungeon-generator.ts
514
515
  var import_heap_js = require("heap-js");
515
516
 
516
- // src/logger.ts
517
- var logdev = console.debug.bind(console, "%c[dbg]", "font-weight: bold; color: #888;");
518
- var log = logdev;
519
- var loginf = console.log.bind(console, "%c[inf]", "font-weight: bold;");
520
- var logwrn = console.log.bind(console, "%c[wrn]", "font-weight: bold; color: orange;");
521
- var logerr = console.log.bind(console, "%c[err]", "font-weight: bold; color: red;");
517
+ // src/log.ts
518
+ var log = console.debug.bind(
519
+ console,
520
+ "%c[dev]",
521
+ "font-weight: bold; color: #888;"
522
+ );
522
523
 
523
524
  // src/map2d.ts
524
525
  var DEFAULT_VALUE = -1;
@@ -872,353 +873,100 @@ function GenerateWalls(map) {
872
873
  }
873
874
 
874
875
  // src/ecs.ts
875
- var ECS = class _ECS {
876
- _globals = /* @__PURE__ */ new Map();
877
- _entities = /* @__PURE__ */ new Map();
878
- _components = /* @__PURE__ */ new Map();
879
- _defaultComponents = [];
880
- _systems = /* @__PURE__ */ new Map();
881
- _onTickStartCallbacks = [];
882
- _onTickEndCallbacks = [];
883
- get components() {
884
- return [...this._components.keys()];
885
- }
886
- get systems() {
887
- return [...this._systems.keys()];
888
- }
889
- get entities() {
890
- return [...this._entities.values()];
891
- }
892
- uid(length = 8) {
893
- const numBytes = Math.ceil(length / 2);
894
- const buffer = new Uint8Array(numBytes);
895
- window.crypto.getRandomValues(buffer);
896
- let uid = "";
897
- for (let i = 0; i < buffer.length; i++) {
898
- uid += buffer[i].toString(16).padStart(2, "0");
899
- }
900
- return uid.substring(0, length);
901
- }
902
- createComponent(uid, data = null) {
903
- if (!this._components.has(uid)) {
904
- this._components.set(uid, data);
905
- log("created component:", { uid, data });
906
- } else {
907
- logwrn("createComponent: a component already exists with uid:", uid);
908
- }
909
- return this;
910
- }
911
- includeAsDefaultComponents(...components) {
912
- components.forEach((component) => {
913
- if (!this._defaultComponents.includes(component)) {
914
- this._defaultComponents.push(component);
915
- log("includeAsDefaultComponents:", component);
916
- } else {
917
- log("includeAsDefaultComponents: already exists:", component);
876
+ var ECS = class {
877
+ nextEntityId = 1;
878
+ components = /* @__PURE__ */ new Map();
879
+ createEntity() {
880
+ return this.nextEntityId++;
881
+ }
882
+ addComponent(entity, name, data) {
883
+ if (!this.components.has(name)) {
884
+ this.components.set(name, /* @__PURE__ */ new Map());
885
+ }
886
+ this.components.get(name).set(entity, data);
887
+ }
888
+ getComponent(entity, name) {
889
+ return this.components.get(name)?.get(entity);
890
+ }
891
+ removeComponent(entity, name) {
892
+ this.components.get(name)?.delete(entity);
893
+ }
894
+ getEntitiesWithComponents(...componentNames) {
895
+ if (componentNames.length === 0) return [];
896
+ const first = this.components.get(componentNames[0]);
897
+ if (!first) return [];
898
+ const result = [];
899
+ for (const entity of first.keys()) {
900
+ if (componentNames.every((name) => this.components.get(name)?.has(entity))) {
901
+ result.push(entity);
918
902
  }
919
- });
920
- return this;
921
- }
922
- createSystem(uid, ...components) {
923
- const callback = components.pop();
924
- const comps = components.filter((comp) => typeof comp === "string");
925
- if (!this._systems.has(uid)) {
926
- this._systems.set(uid, { components: comps, callback });
927
- log("created system:", { uid, components, callback });
928
- } else {
929
- logwrn("createSystem: a system already exists with uid:", uid);
930
903
  }
931
- return this;
932
- }
933
- createEntityWithUid(uid, ...components) {
934
- if (!this._entities.has(uid)) {
935
- const componentList = [
936
- .../* @__PURE__ */ new Set([
937
- ...this._defaultComponents,
938
- ...components
939
- ])
940
- ];
941
- const comps = /* @__PURE__ */ new Map();
942
- componentList.forEach((component) => {
943
- if (this._components.has(component)) {
944
- const componentData = this._components.get(component);
945
- const data = {};
946
- for (const uid2 in componentData) {
947
- const value = componentData[uid2];
948
- data[uid2] = typeof value === "function" ? value(uid2) : value;
949
- }
950
- comps.set(component, data);
951
- } else {
952
- logwrn("createEntityWithUid: missing component:", component);
953
- }
954
- });
955
- if (comps.size === componentList.length) {
956
- const entity = { uid, components: comps };
957
- this._entities.set(uid, entity);
958
- log("created entity:", entity);
959
- } else {
960
- logwrn("createEntityWithUid: failed to create an entity with missing components");
961
- }
962
- } else {
963
- logwrn("createEntityWithUid: an entity already exists with uid:", uid);
964
- }
965
- return this;
966
- }
967
- createEntity(...components) {
968
- const uid = this.uid();
969
- return this.createEntityWithUid(uid, ...components);
970
- }
971
- createEntities(count, ...components) {
972
- for (; count > 0; count--) {
973
- this.createEntity(...components);
974
- }
975
- return this;
976
- }
977
- getEntity(uid) {
978
- return this._entities.get(uid);
979
- }
980
- getEntitiesWithComponents(...components) {
981
- const filter = components.pop();
982
- const uids = components.filter((component) => typeof component === "string");
983
- const entities = [];
984
- this._entities.forEach((entity) => {
985
- if (uids.every(
986
- (component) => [...entity.components.keys()].includes(component)
987
- )) {
988
- for (const filterComponent in filter) {
989
- for (const filterKey in filter[filterComponent]) {
990
- const entityValue = entity.components.get(filterComponent)[filterKey];
991
- const filterValue = filter[filterComponent][filterKey];
992
- if (entityValue === filterValue) {
993
- entities.push(entity);
994
- }
995
- }
996
- }
997
- }
998
- });
999
- return entities;
1000
- }
1001
- addComponent(uid, component, values = {}) {
1002
- const entity = this._entities.get(uid);
1003
- if (entity) {
1004
- if (this._components.has(component)) {
1005
- const comp = this._components.get(component);
1006
- const componentData = {
1007
- ...comp,
1008
- ...values
1009
- };
1010
- entity.components.set(component, componentData);
1011
- log("addComponent: added component:", component, "to entity:", uid);
1012
- } else {
1013
- logwrn("addComponent: component not found:", component);
1014
- }
1015
- } else {
1016
- logwrn("addComponent: entity not found:", uid);
1017
- }
1018
- return this;
1019
- }
1020
- onAllEntitiesNow(callback) {
1021
- this._entities.forEach((entity) => callback(entity));
1022
- log("onAllEntitiesNow: executed on all entities:", callback);
1023
- return this;
1024
- }
1025
- duplicateEntity(uid, count = 1, deep = false) {
1026
- const entity = this._entities.get(uid);
1027
- if (entity) {
1028
- if (!deep) {
1029
- for (let i = 0; i < count; i++) this.createEntity(...entity.components.keys());
1030
- } else {
1031
- for (let i = 0; i < count; i++) {
1032
- const newEntity = { uid: this.uid(), components: /* @__PURE__ */ new Map() };
1033
- entity.components.forEach((value, key) => {
1034
- newEntity.components.set(key, value);
1035
- });
1036
- this._entities.set(newEntity.uid, newEntity);
1037
- }
1038
- ;
1039
- }
1040
- log("duplicateEntity: duplicated entity:", uid);
1041
- } else {
1042
- logwrn("duplicateEntity: entity not found:", uid);
1043
- }
1044
- return this;
1045
- }
1046
- getGlobal(key) {
1047
- return this._globals.get(key);
1048
- }
1049
- setGlobal(key, value) {
1050
- this._globals.set(key, value);
1051
- return this;
1052
- }
1053
- beforeTick(callback) {
1054
- this._onTickStartCallbacks.push(callback);
1055
- log("beforeTick: added:", callback);
1056
- return this;
1057
- }
1058
- afterTick(callback) {
1059
- this._onTickEndCallbacks.push(callback);
1060
- log("afterTick: added:", callback);
1061
- return this;
1062
- }
1063
- runSystem(uid) {
1064
- const system = this._systems.get(uid);
1065
- if (system) {
1066
- this._entities.forEach((entity) => {
1067
- if (system.components.every(
1068
- (component) => [...entity.components.keys()].includes(component)
1069
- )) {
1070
- system.callback(entity, Object.fromEntries(entity.components));
1071
- }
1072
- });
1073
- } else {
1074
- logwrn("runSystem: system not found:", uid);
1075
- }
1076
- return this;
1077
- }
1078
- update() {
1079
- this._onTickStartCallbacks.forEach((cb) => cb());
1080
- this._systems.forEach((_, uid) => this.runSystem(uid));
1081
- this._onTickEndCallbacks.forEach((cb) => cb());
1082
- }
1083
- static get instance() {
1084
- if (!_ECS._instance) {
1085
- _ECS._instance = new _ECS();
1086
- }
1087
- return _ECS._instance;
1088
- }
1089
- static _instance;
1090
- constructor() {
904
+ return result;
1091
905
  }
1092
906
  };
1093
907
 
1094
908
  // src/input/index.ts
1095
909
  var Input = class extends Emitter {
910
+ _down = /* @__PURE__ */ new Map();
911
+ _pointerPosition = new Vector();
1096
912
  inputThreshold = 200;
1097
- _keyStates = {};
1098
- _mouseButtonStates = {};
1099
- _mousePosition = [0, 0];
1100
- _mousePosition2 = [0, 0];
1101
- _contextMenuTarget;
1102
- get mousePosition() {
1103
- return this._mousePosition;
913
+ get down() {
914
+ return this._down;
1104
915
  }
1105
- get mousePosition2() {
1106
- return this._mousePosition2;
916
+ get pointerPosition() {
917
+ return this._pointerPosition;
1107
918
  }
1108
919
  constructor() {
1109
920
  super();
1110
- window.addEventListener("keydown", this._onKeyDown.bind(this));
1111
- window.addEventListener("keyup", this._onKeyUp.bind(this));
1112
- window.addEventListener("mousedown", this._onMouseButtonDown.bind(this));
1113
- window.addEventListener("mouseup", this._onMouseButtonUp.bind(this));
1114
- window.addEventListener("mousemove", this._onMouseMove.bind(this));
1115
- window.addEventListener("wheel", this._onWheel.bind(this));
1116
- }
1117
- blockContextMenu(target) {
1118
- this.stopBlockingContextMenu();
1119
- this._contextMenuTarget = target;
1120
- target.addEventListener("contextmenu", this._onContextMenu);
1121
- }
1122
- stopBlockingContextMenu() {
1123
- this._contextMenuTarget?.removeEventListener("contextmenu", this._onContextMenu);
1124
- this._contextMenuTarget = void 0;
1125
- }
1126
- _onContextMenu = (ev) => {
1127
- ev.preventDefault();
1128
- this.emit("contextmenu");
1129
- };
1130
- _getCommonEventProps(ev) {
1131
- const props = {
1132
- timeStamp: ev.timeStamp,
1133
- altKey: ev.altKey,
1134
- ctrlKey: ev.ctrlKey,
1135
- metaKey: ev.metaKey,
1136
- shiftKey: ev.shiftKey
1137
- };
1138
- return props;
1139
- }
1140
- _onKeyDown(ev) {
1141
- const props = this._getCommonEventProps(ev);
1142
- const { code, key } = ev;
1143
- if (!ev.repeat) {
1144
- this._keyStates[code] = { state: 1, timeStamp: props.timeStamp };
1145
- this.emit("key_down", { ...props, code, key });
1146
- this.emit(`${code.toLowerCase()}_down`, props);
1147
- }
1148
- }
1149
- _onKeyUp(ev) {
1150
- const props = this._getCommonEventProps(ev);
1151
- const { code, key } = ev;
1152
- const deltaStamp = props.timeStamp - (this._keyStates[code]?.timeStamp ?? 0);
1153
- this._keyStates[code] = { state: 0, timeStamp: props.timeStamp };
1154
- this.emit("key_up", { ...props, code, key });
1155
- this.emit(`${code.toLowerCase()}_up`, props);
1156
- if (deltaStamp < this.inputThreshold) {
1157
- this.emit("key_pressed", { ...props, code, key });
1158
- this.emit(`${code.toLowerCase()}_pressed`, props);
1159
- }
1160
- }
1161
- _onMouseButtonDown(ev) {
1162
- const props = this._getCommonEventProps(ev);
1163
- const { button } = ev;
1164
- if (!this._mouseButtonStates[button]?.state) {
1165
- this._mouseButtonStates[button] = { state: 1, timeStamp: props.timeStamp };
1166
- this.emit("mouse_button_down", { ...props, button });
1167
- this.emit(`mouse_button_${button}_down`, props);
1168
- }
1169
- }
1170
- _onMouseButtonUp(ev) {
1171
- const props = this._getCommonEventProps(ev);
1172
- const { button } = ev;
1173
- const deltaStamp = props.timeStamp - (this._mouseButtonStates[button]?.timeStamp ?? 0);
1174
- this._mouseButtonStates[button] = { state: 0, timeStamp: props.timeStamp };
1175
- this.emit("mouse_button_up", { ...props, button });
1176
- this.emit(`mouse_button_${button}_up`, props);
1177
- if (deltaStamp < this.inputThreshold) {
1178
- this.emit("mouse_button_clicked", { ...props, button });
1179
- this.emit(`mouse_button_${button}_clicked`, props);
1180
- }
1181
- }
1182
- _onMouseMove(ev) {
1183
- const props = this._getCommonEventProps(ev);
1184
- const { buttons, offsetX, offsetY, movementX, movementY } = ev;
1185
- this._mousePosition = [offsetX, offsetY];
1186
- const width = window.innerWidth;
1187
- const height = window.innerHeight;
1188
- this._mousePosition2 = [
1189
- ev.clientX / width * 2 - 1,
1190
- -(ev.clientY / height) * 2 + 1
1191
- ];
1192
- this.emit("mouse_move", {
1193
- ...props,
1194
- buttons,
1195
- x: offsetX,
1196
- y: offsetY,
1197
- deltaX: movementX,
1198
- deltaY: movementY
921
+ window.addEventListener("contextmenu", (ev) => {
922
+ log("contextmenu:", ev);
1199
923
  });
1200
- }
1201
- _onWheel(ev) {
1202
- const props = this._getCommonEventProps(ev);
1203
- const { deltaX, deltaY, deltaZ } = ev;
1204
- this.emit("mouse_wheel", {
1205
- ...props,
1206
- deltaX,
1207
- deltaY,
1208
- deltaZ
924
+ window.addEventListener("keydown", (ev) => {
925
+ const { code, repeat, timeStamp } = ev;
926
+ if (!repeat) {
927
+ this._down.set(code, { state: 1, time: timeStamp });
928
+ this.emit("key_down", code, ev);
929
+ }
930
+ });
931
+ window.addEventListener("keyup", (ev) => {
932
+ const { code, timeStamp } = ev;
933
+ const delta = timeStamp - (this._down.get(code)?.time ?? 0);
934
+ this._down.set(code, { state: 0, time: timeStamp });
935
+ this.emit("key_up", code, ev);
936
+ if (delta < this.inputThreshold) {
937
+ this.emit("key_pressed", code, ev);
938
+ }
939
+ });
940
+ window.addEventListener("pointerdown", (ev) => {
941
+ const { button, timeStamp } = ev;
942
+ const code = `Button${button}`;
943
+ if (!this._down.get(code)) {
944
+ this._down.set(code, { state: 1, time: timeStamp });
945
+ this.emit("pointer_down", code, ev);
946
+ }
947
+ });
948
+ window.addEventListener("pointerup", (ev) => {
949
+ const { button, timeStamp } = ev;
950
+ const code = `Button${button}`;
951
+ const delta = timeStamp - (this._down.get(code)?.time ?? 0);
952
+ this._down.set(code, { state: 0, time: timeStamp });
953
+ if (delta < this.inputThreshold) {
954
+ this.emit("button_clicked", code, ev);
955
+ }
956
+ });
957
+ window.addEventListener("mousemove", (ev) => {
958
+ const { offsetX, offsetY, movementX, movementY } = ev;
959
+ this._pointerPosition.x = offsetX;
960
+ this._pointerPosition.y = offsetY;
961
+ this.emit("pointer_move", movementX, movementY, ev);
962
+ });
963
+ window.addEventListener("wheel", (ev) => {
964
+ const { deltaY } = ev;
965
+ this.emit("wheel", deltaY, ev);
1209
966
  });
1210
967
  }
1211
- isKeyDown(keyCode) {
1212
- return this._keyStates[keyCode]?.state === 1 ? true : false;
1213
- }
1214
- getKeyState(keyCode) {
1215
- return this._keyStates[keyCode]?.state ?? 0;
1216
- }
1217
- isMouseButtonDown(mouseButton) {
1218
- return this._mouseButtonStates[mouseButton]?.state === 1 ? true : false;
1219
- }
1220
- getMouseButtonState(button) {
1221
- return this._mouseButtonStates[button]?.state ?? 0;
968
+ isDown(name) {
969
+ return this._down.get(name) ? true : false;
1222
970
  }
1223
971
  };
1224
972
 
@@ -1306,6 +1054,10 @@ function GetUrlParams() {
1306
1054
  }
1307
1055
  return params;
1308
1056
  }
1057
+ var xy2k = (x, y, delimiter = ",") => `${x}${delimiter}${y}`;
1058
+ var k2xy = (key, delimiter = ",") => key.split(delimiter).map(parseFloat);
1059
+ var xyz2k = (x, y, z, delimiter = ",") => `${x}${delimiter},${y},${delimiter},${z}`;
1060
+ var k2xyz = (key, delimiter = ",") => key.split(delimiter).map(parseFloat);
1309
1061
 
1310
1062
  // src/noise.ts
1311
1063
  var import_simplex_noise = require("simplex-noise");
@@ -1411,7 +1163,7 @@ var Color = class _Color {
1411
1163
  [this._r, this._g, this._b, this._a] = this._parse_hex_color_string(value);
1412
1164
  this._hex = value;
1413
1165
  } else {
1414
- logwrn(`Color: invalid color string: ${value}. Should be a hexadecimal color string (i.e.: '#ffffff' or '#ffffffff'). Keeping the current color.`);
1166
+ log(`Color: invalid color string: ${value}. Should be a hexadecimal color string (i.e.: '#ffffff' or '#ffffffff'). Keeping the current color.`);
1415
1167
  }
1416
1168
  }
1417
1169
  get rgba() {
@@ -1422,7 +1174,7 @@ var Color = class _Color {
1422
1174
  if (r.startsWith("#")) {
1423
1175
  [r, g, b, a] = this._parse_hex_color_string(r);
1424
1176
  } else {
1425
- logwrn(`Color: invalid color string: ${r}. Should be a hexadecimal color string (i.e.: '#ffffff' or '#ffffffff'). Defaulting to opaque white.`);
1177
+ log(`Color: invalid color string: ${r}. Should be a hexadecimal color string (i.e.: '#ffffff' or '#ffffffff'). Defaulting to opaque white.`);
1426
1178
  r = g = b = a = 255;
1427
1179
  }
1428
1180
  } else {
@@ -1523,7 +1275,7 @@ var Color = class _Color {
1523
1275
  const [r, g, b, a] = rgba.map(Number);
1524
1276
  return new _Color(r, g, b, a);
1525
1277
  } else {
1526
- logwrn(`Color: invalid CSS color name: ${name}. Defaulting to transparent.`);
1278
+ log(`Color: invalid CSS color name: ${name}. Defaulting to transparent.`);
1527
1279
  return _Color.TRANSPARENT;
1528
1280
  }
1529
1281
  }
@@ -2159,7 +1911,7 @@ var ThreeJsBoilerPlate = class {
2159
1911
  this.orbitControls?.dispose();
2160
1912
  this.orbitControls = void 0;
2161
1913
  this.input.on("mouse_move", ({ deltaX, deltaY }) => {
2162
- if (this.input.isMouseButtonDown(0)) {
1914
+ if (this.input.isDown("Button0")) {
2163
1915
  this.cameraRig.orbit(deltaX, deltaY);
2164
1916
  }
2165
1917
  }).on("mouse_wheel", ({ deltaY }) => this.cameraRig.dolly(deltaY));
@@ -2206,8 +1958,8 @@ var ThreeJsBoilerPlate = class {
2206
1958
  }
2207
1959
  }
2208
1960
  pick() {
2209
- const pickX = this.input.mousePosition[0] / this.renderer.domElement.width * 2 - 1;
2210
- const pickY = -(this.input.mousePosition[1] / this.renderer.domElement.height) * 2 + 1;
1961
+ const pickX = this.input.pointerPosition.x / this.renderer.domElement.width * 2 - 1;
1962
+ const pickY = -(this.input.pointerPosition.y / this.renderer.domElement.height) * 2 + 1;
2211
1963
  this.raycaster.setFromCamera(new import_three4.Vector2(pickX, pickY), this.cameraRig.camera);
2212
1964
  const intersected = this.raycaster.intersectObjects(this.scene.children);
2213
1965
  return intersected.length ? intersected[0] : null;
@@ -3463,12 +3215,10 @@ var WorkerInterface = class extends Emitter {
3463
3215
  imul,
3464
3216
  isNullOrUndefined,
3465
3217
  isPositiveString,
3218
+ k2xy,
3219
+ k2xyz,
3466
3220
  lerp,
3467
3221
  log,
3468
- logdev,
3469
- logerr,
3470
- loginf,
3471
- logwrn,
3472
3222
  manhattanDistance,
3473
3223
  max,
3474
3224
  min,
@@ -3478,5 +3228,7 @@ var WorkerInterface = class extends Emitter {
3478
3228
  roundTo,
3479
3229
  sin,
3480
3230
  sqrt,
3481
- squaredDistance
3231
+ squaredDistance,
3232
+ xy2k,
3233
+ xyz2k
3482
3234
  });