@dcl/ecs 7.1.8-4756315995.commit-6d7fbd0 → 7.1.8-4757726595.commit-28cb54f

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.d.ts CHANGED
@@ -4,6 +4,7 @@ export * from './runtime/initialization';
4
4
  export * from './runtime/types';
5
5
  export { cyclicParentingChecker } from './systems/cyclicParentingChecker';
6
6
  export * from './systems/events';
7
+ export * from './systems/raycast';
7
8
  export * from './systems/async-task';
8
9
  export * from './engine/entity';
9
10
  export * from './components/types';
package/dist/index.js CHANGED
@@ -5,6 +5,7 @@ export * from './runtime/initialization';
5
5
  export * from './runtime/types';
6
6
  export { cyclicParentingChecker } from './systems/cyclicParentingChecker';
7
7
  export * from './systems/events';
8
+ export * from './systems/raycast';
8
9
  export * from './systems/async-task';
9
10
  export * from './engine/entity';
10
11
  export * from './components/types';
@@ -6,6 +6,7 @@ import { IEngine } from '../../engine';
6
6
  import { Task } from '../../systems/async-task';
7
7
  import { PointerEventsSystem } from '../../systems/events';
8
8
  import { IInputSystem } from './../../engine/input';
9
+ import { RaycastSystem } from '../../systems/raycast';
9
10
  /**
10
11
  * @public
11
12
  * The engine is the part of the scene that sits in the middle and manages all of the other parts.
@@ -31,10 +32,16 @@ export declare const inputSystem: IInputSystem;
31
32
  export { IInputSystem };
32
33
  /**
33
34
  * @public
34
- * Register callback functions to a particular entity.
35
+ * Register callback functions to a particular entity on input events.
35
36
  */
36
37
  export declare const pointerEventsSystem: PointerEventsSystem;
37
38
  export { PointerEventsSystem };
39
+ /**
40
+ * @public
41
+ * Register callback functions to a particular entity on raycast results.
42
+ */
43
+ export declare const raycastSystem: RaycastSystem;
44
+ export { RaycastSystem };
38
45
  /**
39
46
  * @public
40
47
  * Runs an async function
@@ -4,8 +4,9 @@
4
4
  */
5
5
  import { Engine } from '../../engine';
6
6
  import { createTaskSystem } from '../../systems/async-task';
7
- import { createPointerEventSystem } from '../../systems/events';
7
+ import { createPointerEventsSystem } from '../../systems/events';
8
8
  import { createInputSystem } from './../../engine/input';
9
+ import { createRaycastSystem } from '../../systems/raycast';
9
10
  /**
10
11
  * @public
11
12
  * The engine is the part of the scene that sits in the middle and manages all of the other parts.
@@ -30,9 +31,14 @@ export const engine = /* @__PURE__ */ Engine();
30
31
  export const inputSystem = /* @__PURE__ */ createInputSystem(engine);
31
32
  /**
32
33
  * @public
33
- * Register callback functions to a particular entity.
34
+ * Register callback functions to a particular entity on input events.
34
35
  */
35
- export const pointerEventsSystem = /* @__PURE__ */ createPointerEventSystem(engine, inputSystem);
36
+ export const pointerEventsSystem = /* @__PURE__ */ createPointerEventsSystem(engine, inputSystem);
37
+ /**
38
+ * @public
39
+ * Register callback functions to a particular entity on raycast results.
40
+ */
41
+ export const raycastSystem = /* @__PURE__ */ createRaycastSystem(engine);
36
42
  /**
37
43
  * @public
38
44
  * Runs an async function
@@ -4,7 +4,7 @@ import { checkNotThenable } from '../runtime/invariant';
4
4
  /**
5
5
  * @internal
6
6
  */
7
- export function createPointerEventSystem(engine, inputSystem) {
7
+ export function createPointerEventsSystem(engine, inputSystem) {
8
8
  const PointerEvents = components.PointerEvents(engine);
9
9
  let EventType;
10
10
  (function (EventType) {
@@ -0,0 +1,80 @@
1
+ import { RaycastQueryType, PBRaycastResult } from '../components';
2
+ import { DeepReadonlyObject, Entity } from '../engine';
3
+ import { Vector3 } from '../components/generated/pb/decentraland/common/vectors.gen';
4
+ /**
5
+ * @public
6
+ */
7
+ export type RaycastSystemCallback = (event: DeepReadonlyObject<PBRaycastResult>) => void;
8
+ /**
9
+ * @public
10
+ */
11
+ export type RaycastSystemOptions = {
12
+ originOffset?: Vector3 | undefined;
13
+ maxDistance: number;
14
+ queryType: RaycastQueryType;
15
+ continuous?: boolean | undefined;
16
+ collisionMask?: number | undefined;
17
+ };
18
+ export type LocalDirectionRaycastSystemOptions = {
19
+ direction?: Vector3;
20
+ };
21
+ export type LocalDirectionRaycastOptions = RaycastSystemOptions & LocalDirectionRaycastSystemOptions;
22
+ export type GlobalDirectionRaycastSystemOptions = {
23
+ direction?: Vector3;
24
+ };
25
+ export type GlobalDirectionRaycastOptions = RaycastSystemOptions & GlobalDirectionRaycastSystemOptions;
26
+ export type GlobalTargetRaycastSystemOptions = {
27
+ target?: Vector3;
28
+ };
29
+ export type GlobalTargetRaycastOptions = RaycastSystemOptions & GlobalTargetRaycastSystemOptions;
30
+ export type TargetEntityRaycastSystemOptions = {
31
+ targetEntity?: number;
32
+ };
33
+ export type TargetEntityRaycastOptions = RaycastSystemOptions & TargetEntityRaycastSystemOptions;
34
+ /**
35
+ * @public
36
+ */
37
+ export interface RaycastSystem {
38
+ /**
39
+ * @public
40
+ * Remove the callback for raycast event
41
+ * @param entity - Entity where the callback was attached
42
+ */
43
+ removeRaycasterEntity(entity: Entity): void;
44
+ /**
45
+ * @public
46
+ * Execute callback when the entity receives a RaycastResult component update.
47
+ * Uses a Vector3 entity-local direction value to calculate the ray direction
48
+ * @param entity - Entity to attach the callback
49
+ * @param callback - Function to execute when the entity's RaycastResult component is updated
50
+ * @param options - Raycast configuration options
51
+ */
52
+ registerLocalDirectionRaycast(entity: Entity, callback: RaycastSystemCallback, options?: Partial<LocalDirectionRaycastOptions>): void;
53
+ /**
54
+ * @public
55
+ * Execute callback when the entity receives a RaycastResult component update.
56
+ * Uses a Vector3 global direction value to calculate the ray direction
57
+ * @param entity - Entity to attach the callback
58
+ * @param callback - Function to execute when the entity's RaycastResult component is updated
59
+ * @param options - Raycast configuration options
60
+ */
61
+ registerGlobalDirectionRaycast(entity: Entity, callback: RaycastSystemCallback, options?: Partial<GlobalDirectionRaycastOptions>): void;
62
+ /**
63
+ * @public
64
+ * Execute callback when the entity receives a RaycastResult component update.
65
+ * Uses a Vector3 global target position to calculate the ray direction
66
+ * @param entity - Entity to attach the callback
67
+ * @param callback - Function to execute when the entity's RaycastResult component is updated
68
+ * @param options - Raycast configuration options
69
+ */
70
+ registerGlobalTargetRaycast(entity: Entity, callback: RaycastSystemCallback, options?: Partial<GlobalTargetRaycastOptions>): void;
71
+ /**
72
+ * @public
73
+ * Execute callback when the entity receives a RaycastResult component update.
74
+ * Uses an target Entity value to calculate the ray direction
75
+ * @param entity - Entity to attach the callback
76
+ * @param callback - Function to execute when the entity's RaycastResult component is updated
77
+ * @param options - Raycast configuration options
78
+ */
79
+ registerTargetEntityRaycast(entity: Entity, callback: RaycastSystemCallback, options?: Partial<TargetEntityRaycastOptions>): void;
80
+ }
@@ -0,0 +1,97 @@
1
+ import * as components from '../components';
2
+ import { EntityState } from '../engine/entity';
3
+ /**
4
+ * @internal
5
+ */
6
+ export function createRaycastSystem(engine) {
7
+ const raycastComponent = components.Raycast(engine);
8
+ const raycastResultComponent = components.RaycastResult(engine);
9
+ const entitiesCallbackResultMap = new Map();
10
+ const defaultOptions = {
11
+ maxDistance: 16,
12
+ queryType: 0 /* RaycastQueryType.RQT_HIT_FIRST */,
13
+ continuous: false,
14
+ originOffset: { x: 0, y: 0, z: 0 },
15
+ collisionMask: 2 /* ColliderLayer.CL_PHYSICS */
16
+ };
17
+ const getLocalDirectionRaycastDefaultOptions = (options = {}) => ({
18
+ ...defaultOptions,
19
+ ...options,
20
+ directionRawValue: {
21
+ $case: 'localDirection',
22
+ localDirection: options.direction || { x: 0, y: 0, z: 1 }
23
+ }
24
+ });
25
+ const getGlobalDirectionRaycastDefaultOptions = (options = {}) => ({
26
+ ...defaultOptions,
27
+ ...options,
28
+ directionRawValue: {
29
+ $case: 'globalDirection',
30
+ globalDirection: options.direction || { x: 0, y: 0, z: 1 }
31
+ }
32
+ });
33
+ const getGlobalTargetRaycastDefaultOptions = (options = {}) => ({
34
+ ...defaultOptions,
35
+ ...options,
36
+ directionRawValue: {
37
+ $case: 'globalTarget',
38
+ globalTarget: options.target || { x: 0, y: 0, z: 0 }
39
+ }
40
+ });
41
+ const getTargetEntityRaycastDefaultOptions = (options = {}) => ({
42
+ ...defaultOptions,
43
+ ...options,
44
+ directionRawValue: {
45
+ $case: 'targetEntity',
46
+ targetEntity: options.targetEntity || 0
47
+ }
48
+ });
49
+ function registerRaycast(entity, callback, options) {
50
+ const raycast = raycastComponent.getOrCreateMutable(entity);
51
+ raycast.maxDistance = options.maxDistance;
52
+ raycast.originOffset = options.originOffset;
53
+ raycast.collisionMask = options.collisionMask;
54
+ raycast.direction = options.directionRawValue;
55
+ raycast.continuous = options.continuous;
56
+ raycast.queryType = options.queryType;
57
+ entitiesCallbackResultMap.set(entity, { callback: callback });
58
+ }
59
+ function removeRaycast(entity) {
60
+ raycastComponent.deleteFrom(entity);
61
+ raycastResultComponent.deleteFrom(entity);
62
+ entitiesCallbackResultMap.delete(entity);
63
+ }
64
+ // @internal
65
+ engine.addSystem(function EventSystem() {
66
+ for (const [entity, data] of entitiesCallbackResultMap) {
67
+ const raycast = raycastComponent.getOrNull(entity);
68
+ if (engine.getEntityState(entity) === EntityState.Removed || !raycast) {
69
+ entitiesCallbackResultMap.delete(entity);
70
+ continue;
71
+ }
72
+ const currentResult = raycastResultComponent.getOrNull(entity);
73
+ if (!currentResult)
74
+ continue;
75
+ data.callback(currentResult);
76
+ if (!raycast.continuous)
77
+ removeRaycast(entity);
78
+ }
79
+ });
80
+ return {
81
+ removeRaycasterEntity(entity) {
82
+ removeRaycast(entity);
83
+ },
84
+ registerLocalDirectionRaycast(entity, callback, opts) {
85
+ registerRaycast(entity, callback, getLocalDirectionRaycastDefaultOptions(opts));
86
+ },
87
+ registerGlobalDirectionRaycast(entity, callback, opts) {
88
+ registerRaycast(entity, callback, getGlobalDirectionRaycastDefaultOptions(opts));
89
+ },
90
+ registerGlobalTargetRaycast(entity, callback, opts) {
91
+ registerRaycast(entity, callback, getGlobalTargetRaycastDefaultOptions(opts));
92
+ },
93
+ registerTargetEntityRaycast(entity, callback, opts) {
94
+ registerRaycast(entity, callback, getTargetEntityRaycastDefaultOptions(opts));
95
+ }
96
+ };
97
+ }
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@dcl/ecs",
3
3
  "description": "Decentraland ECS",
4
- "version": "7.1.8-4756315995.commit-6d7fbd0",
4
+ "version": "7.1.8-4757726595.commit-28cb54f",
5
5
  "author": "DCL",
6
6
  "bugs": "https://github.com/decentraland/ecs/issues",
7
7
  "dependencies": {
8
- "@dcl/js-runtime": "7.1.8-4756315995.commit-6d7fbd0"
8
+ "@dcl/js-runtime": "7.1.8-4757726595.commit-28cb54f"
9
9
  },
10
10
  "devDependencies": {
11
11
  "ts-proto": "^1.122.0"
@@ -34,5 +34,5 @@
34
34
  },
35
35
  "types": "./dist/index.d.ts",
36
36
  "typings": "./dist/index.d.ts",
37
- "commit": "6d7fbd0161e9d081ffb08992f9b6695f5ee026ab"
37
+ "commit": "28cb54f0e017f80c02592ebe17b35faaef720882"
38
38
  }