@dcl/ecs 7.21.1-22918726402.commit-ee210ee → 7.21.1-23252789519.commit-9f806e1
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/components/index.d.ts +0 -5
- package/dist/components/index.js +2 -5
- package/dist/components/manual/Transform.d.ts +13 -9
- package/dist/components/manual/Transform.js +11 -3
- package/dist/components/types.d.ts +0 -1
- package/dist/engine/component.d.ts +1 -52
- package/dist/engine/grow-only-value-set-component-definition.js +2 -45
- package/dist/engine/lww-element-set-component-definition.d.ts +1 -3
- package/dist/engine/lww-element-set-component-definition.js +12 -64
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/runtime/helpers/index.d.ts +1 -0
- package/dist/runtime/helpers/index.js +1 -0
- package/dist/runtime/helpers/tree.d.ts +6 -0
- package/dist/runtime/helpers/tree.js +2 -2
- package/dist/runtime/helpers/vectors.d.ts +1 -0
- package/dist/runtime/helpers/vectors.js +36 -0
- package/dist/runtime/initialization/index.d.ts +7 -0
- package/dist/runtime/initialization/index.js +6 -0
- package/dist/serialization/crdt/index.d.ts +0 -1
- package/dist/serialization/crdt/index.js +0 -1
- package/dist/serialization/crdt/network/utils.d.ts +9 -0
- package/dist/serialization/crdt/network/utils.js +60 -0
- package/dist/serialization/crdt/types.d.ts +3 -25
- package/dist/serialization/crdt/types.js +1 -3
- package/dist/systems/crdt/index.d.ts +1 -0
- package/dist/systems/crdt/index.js +146 -55
- package/dist/systems/physics-force.d.ts +1 -0
- package/dist/systems/physics-force.js +140 -0
- package/dist/systems/physics-impulse.d.ts +12 -0
- package/dist/systems/physics-impulse.js +85 -0
- package/dist/systems/physics.d.ts +77 -0
- package/dist/systems/physics.js +18 -0
- package/dist-cjs/components/index.d.ts +0 -5
- package/dist-cjs/components/index.js +3 -7
- package/dist-cjs/components/manual/Transform.d.ts +13 -9
- package/dist-cjs/components/manual/Transform.js +34 -3
- package/dist-cjs/components/types.d.ts +0 -1
- package/dist-cjs/engine/component.d.ts +1 -52
- package/dist-cjs/engine/grow-only-value-set-component-definition.js +1 -44
- package/dist-cjs/engine/lww-element-set-component-definition.d.ts +1 -3
- package/dist-cjs/engine/lww-element-set-component-definition.js +13 -67
- package/dist-cjs/index.d.ts +2 -2
- package/dist-cjs/index.js +2 -2
- package/dist-cjs/runtime/helpers/index.d.ts +1 -0
- package/dist-cjs/runtime/helpers/index.js +1 -0
- package/dist-cjs/runtime/helpers/tree.d.ts +6 -0
- package/dist-cjs/runtime/helpers/tree.js +3 -2
- package/dist-cjs/runtime/helpers/vectors.d.ts +1 -0
- package/dist-cjs/runtime/helpers/vectors.js +39 -0
- package/dist-cjs/runtime/initialization/index.d.ts +7 -0
- package/dist-cjs/runtime/initialization/index.js +7 -1
- package/dist-cjs/serialization/crdt/index.d.ts +0 -1
- package/dist-cjs/serialization/crdt/index.js +0 -1
- package/dist-cjs/serialization/crdt/network/utils.d.ts +9 -0
- package/dist-cjs/serialization/crdt/network/utils.js +67 -0
- package/dist-cjs/serialization/crdt/types.d.ts +3 -25
- package/dist-cjs/serialization/crdt/types.js +1 -3
- package/dist-cjs/systems/crdt/index.d.ts +1 -0
- package/dist-cjs/systems/crdt/index.js +169 -55
- package/dist-cjs/systems/physics-force.d.ts +1 -0
- package/dist-cjs/systems/physics-force.js +167 -0
- package/dist-cjs/systems/physics-impulse.d.ts +12 -0
- package/dist-cjs/systems/physics-impulse.js +112 -0
- package/dist-cjs/systems/physics.d.ts +77 -0
- package/dist-cjs/systems/physics.js +23 -0
- package/package.json +3 -2
- package/dist/components/manual/CreatedBy.d.ts +0 -9
- package/dist/components/manual/CreatedBy.js +0 -8
- package/dist/serialization/crdt/authoritativePutComponent.d.ts +0 -15
- package/dist/serialization/crdt/authoritativePutComponent.js +0 -47
- package/dist-cjs/components/manual/CreatedBy.d.ts +0 -9
- package/dist-cjs/components/manual/CreatedBy.js +0 -10
- package/dist-cjs/serialization/crdt/authoritativePutComponent.d.ts +0 -15
- package/dist-cjs/serialization/crdt/authoritativePutComponent.js +0 -50
|
@@ -11,8 +11,7 @@ export declare enum CrdtMessageType {
|
|
|
11
11
|
PUT_COMPONENT_NETWORK = 5,
|
|
12
12
|
DELETE_COMPONENT_NETWORK = 6,
|
|
13
13
|
DELETE_ENTITY_NETWORK = 7,
|
|
14
|
-
|
|
15
|
-
MAX_MESSAGE_TYPE = 9
|
|
14
|
+
MAX_MESSAGE_TYPE = 8
|
|
16
15
|
}
|
|
17
16
|
/**
|
|
18
17
|
* Min length = 8 bytes
|
|
@@ -49,23 +48,6 @@ export type PutNetworkComponentMessageBody = Omit<PutComponentMessageBody, 'type
|
|
|
49
48
|
type: CrdtMessageType.PUT_COMPONENT_NETWORK;
|
|
50
49
|
networkId: number;
|
|
51
50
|
};
|
|
52
|
-
/**
|
|
53
|
-
* Server authoritative message - identical to PutComponentMessageBody but with forced processing
|
|
54
|
-
* Min. length = header (8 bytes) + 16 bytes = 24 bytes
|
|
55
|
-
*
|
|
56
|
-
* @param entity - Uint32 number of the entity
|
|
57
|
-
* @param componentId - Uint32 number of id
|
|
58
|
-
* @param timestamp - Uint32 Lamport timestamp (server's authoritative timestamp)
|
|
59
|
-
* @param data - Uint8[] data of component => length(4 bytes) + block of bytes[0..length-1]
|
|
60
|
-
* @public
|
|
61
|
-
*/
|
|
62
|
-
export type AuthoritativePutComponentMessageBody = {
|
|
63
|
-
type: CrdtMessageType.AUTHORITATIVE_PUT_COMPONENT;
|
|
64
|
-
entityId: Entity;
|
|
65
|
-
componentId: number;
|
|
66
|
-
timestamp: number;
|
|
67
|
-
data: Uint8Array;
|
|
68
|
-
};
|
|
69
51
|
/**
|
|
70
52
|
* Min. length = header (8 bytes) + 16 bytes = 24 bytes
|
|
71
53
|
*
|
|
@@ -132,10 +114,6 @@ export type AppendValueMessage = CrdtMessageHeader & AppendValueMessageBody;
|
|
|
132
114
|
* @public
|
|
133
115
|
*/
|
|
134
116
|
export type PutComponentMessage = CrdtMessageHeader & PutComponentMessageBody;
|
|
135
|
-
/**
|
|
136
|
-
* @public
|
|
137
|
-
*/
|
|
138
|
-
export type AuthoritativePutComponentMessage = CrdtMessageHeader & AuthoritativePutComponentMessageBody;
|
|
139
117
|
/**
|
|
140
118
|
* @public
|
|
141
119
|
*/
|
|
@@ -159,7 +137,7 @@ export type DeleteEntityNetworkMessage = CrdtMessageHeader & DeleteEntityNetwork
|
|
|
159
137
|
/**
|
|
160
138
|
* @public
|
|
161
139
|
*/
|
|
162
|
-
export type CrdtMessage = PutComponentMessage |
|
|
140
|
+
export type CrdtMessage = PutComponentMessage | DeleteComponentMessage | AppendValueMessage | DeleteEntityMessage | PutNetworkComponentMessage | DeleteComponentNetworkMessage | DeleteEntityNetworkMessage;
|
|
163
141
|
/**
|
|
164
142
|
* @public
|
|
165
143
|
*/
|
|
@@ -167,7 +145,7 @@ export type CrdtNetworkMessageBody = PutNetworkComponentMessageBody | DeleteComp
|
|
|
167
145
|
/**
|
|
168
146
|
* @public
|
|
169
147
|
*/
|
|
170
|
-
export type CrdtMessageBody = PutComponentMessageBody |
|
|
148
|
+
export type CrdtMessageBody = PutComponentMessageBody | DeleteComponentMessageBody | DeleteEntityMessageBody | AppendValueMessageBody | CrdtNetworkMessageBody;
|
|
171
149
|
export declare enum ProcessMessageResultType {
|
|
172
150
|
/**
|
|
173
151
|
* Typical message and new state set.
|
|
@@ -13,9 +13,7 @@ export var CrdtMessageType;
|
|
|
13
13
|
CrdtMessageType[CrdtMessageType["PUT_COMPONENT_NETWORK"] = 5] = "PUT_COMPONENT_NETWORK";
|
|
14
14
|
CrdtMessageType[CrdtMessageType["DELETE_COMPONENT_NETWORK"] = 6] = "DELETE_COMPONENT_NETWORK";
|
|
15
15
|
CrdtMessageType[CrdtMessageType["DELETE_ENTITY_NETWORK"] = 7] = "DELETE_ENTITY_NETWORK";
|
|
16
|
-
|
|
17
|
-
CrdtMessageType[CrdtMessageType["AUTHORITATIVE_PUT_COMPONENT"] = 8] = "AUTHORITATIVE_PUT_COMPONENT";
|
|
18
|
-
CrdtMessageType[CrdtMessageType["MAX_MESSAGE_TYPE"] = 9] = "MAX_MESSAGE_TYPE";
|
|
16
|
+
CrdtMessageType[CrdtMessageType["MAX_MESSAGE_TYPE"] = 8] = "MAX_MESSAGE_TYPE";
|
|
19
17
|
})(CrdtMessageType || (CrdtMessageType = {}));
|
|
20
18
|
/**
|
|
21
19
|
* @public
|
|
@@ -1,17 +1,24 @@
|
|
|
1
1
|
import { EntityState } from '../../engine/entity';
|
|
2
2
|
import { ReadWriteByteBuffer } from '../../serialization/ByteBuffer';
|
|
3
|
-
import { AppendValueOperation, CrdtMessageProtocol } from '../../serialization/crdt';
|
|
3
|
+
import { AppendValueOperation, CrdtMessageProtocol, DeleteComponentNetwork, DeleteEntityNetwork } from '../../serialization/crdt';
|
|
4
4
|
import { DeleteComponent } from '../../serialization/crdt/deleteComponent';
|
|
5
5
|
import { DeleteEntity } from '../../serialization/crdt/deleteEntity';
|
|
6
6
|
import { PutComponentOperation } from '../../serialization/crdt/putComponent';
|
|
7
|
-
import { AuthoritativePutComponentOperation } from '../../serialization/crdt/authoritativePutComponent';
|
|
8
7
|
import { CrdtMessageType } from '../../serialization/crdt/types';
|
|
8
|
+
import { PutNetworkComponentOperation } from '../../serialization/crdt/network/putComponentNetwork';
|
|
9
|
+
import { NetworkEntity as defineNetworkEntity, NetworkParent as defineNetworkParent, Transform as defineTransform } from '../../components';
|
|
10
|
+
import * as networkUtils from '../../serialization/crdt/network/utils';
|
|
11
|
+
// NetworkMessages can only have a MAX_SIZE of 12kb. So we need to send it in chunks.
|
|
12
|
+
export const LIVEKIT_MAX_SIZE = 12;
|
|
9
13
|
/**
|
|
10
14
|
* @internal
|
|
11
15
|
*/
|
|
12
16
|
export function crdtSceneSystem(engine, onProcessEntityComponentChange) {
|
|
13
17
|
const transports = [];
|
|
14
|
-
//
|
|
18
|
+
// Components that we used on this system
|
|
19
|
+
const NetworkEntity = defineNetworkEntity(engine);
|
|
20
|
+
const NetworkParent = defineNetworkParent(engine);
|
|
21
|
+
const Transform = defineTransform(engine);
|
|
15
22
|
// Messages that we received at transport.onMessage waiting to be processed
|
|
16
23
|
const receivedMessages = [];
|
|
17
24
|
// Messages already processed by the engine but that we need to broadcast to other transports.
|
|
@@ -36,20 +43,27 @@ export function crdtSceneSystem(engine, onProcessEntityComponentChange) {
|
|
|
36
43
|
if (header.type === CrdtMessageType.DELETE_COMPONENT) {
|
|
37
44
|
message = DeleteComponent.read(buffer);
|
|
38
45
|
}
|
|
46
|
+
else if (header.type === CrdtMessageType.DELETE_COMPONENT_NETWORK) {
|
|
47
|
+
message = DeleteComponentNetwork.read(buffer);
|
|
48
|
+
}
|
|
39
49
|
else if (header.type === CrdtMessageType.PUT_COMPONENT) {
|
|
40
50
|
message = PutComponentOperation.read(buffer);
|
|
41
51
|
}
|
|
42
|
-
else if (header.type === CrdtMessageType.
|
|
43
|
-
message =
|
|
52
|
+
else if (header.type === CrdtMessageType.PUT_COMPONENT_NETWORK) {
|
|
53
|
+
message = PutNetworkComponentOperation.read(buffer);
|
|
44
54
|
}
|
|
45
55
|
else if (header.type === CrdtMessageType.DELETE_ENTITY) {
|
|
46
56
|
message = DeleteEntity.read(buffer);
|
|
47
57
|
}
|
|
58
|
+
else if (header.type === CrdtMessageType.DELETE_ENTITY_NETWORK) {
|
|
59
|
+
message = DeleteEntityNetwork.read(buffer);
|
|
60
|
+
}
|
|
48
61
|
else if (header.type === CrdtMessageType.APPEND_VALUE) {
|
|
49
62
|
message = AppendValueOperation.read(buffer);
|
|
63
|
+
// Unknown message, we skip it
|
|
50
64
|
}
|
|
51
65
|
else {
|
|
52
|
-
//
|
|
66
|
+
// consume the message
|
|
53
67
|
buffer.incrementReadOffset(header.length);
|
|
54
68
|
}
|
|
55
69
|
if (message) {
|
|
@@ -70,6 +84,22 @@ export function crdtSceneSystem(engine, onProcessEntityComponentChange) {
|
|
|
70
84
|
const messagesToProcess = value.splice(0, value.length);
|
|
71
85
|
return messagesToProcess;
|
|
72
86
|
}
|
|
87
|
+
/**
|
|
88
|
+
* Find the local entityId associated to the network component message.
|
|
89
|
+
* It's a mapping Network -> to Local
|
|
90
|
+
* If it's not a network message, return the entityId received by the message
|
|
91
|
+
*/
|
|
92
|
+
function findNetworkId(msg) {
|
|
93
|
+
const hasNetworkId = 'networkId' in msg;
|
|
94
|
+
if (hasNetworkId) {
|
|
95
|
+
for (const [entityId, network] of engine.getEntitiesWith(NetworkEntity)) {
|
|
96
|
+
if (network.networkId === msg.networkId && network.entityId === msg.entityId) {
|
|
97
|
+
return { entityId, network };
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return { entityId: msg.entityId };
|
|
102
|
+
}
|
|
73
103
|
/**
|
|
74
104
|
* This fn will be called on every tick.
|
|
75
105
|
* Process all the messages queue received by the transport
|
|
@@ -78,52 +108,46 @@ export function crdtSceneSystem(engine, onProcessEntityComponentChange) {
|
|
|
78
108
|
const messagesToProcess = getMessages(receivedMessages);
|
|
79
109
|
const entitiesShouldBeCleaned = [];
|
|
80
110
|
for (const msg of messagesToProcess) {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
111
|
+
let { entityId, network } = findNetworkId(msg);
|
|
112
|
+
// We receive a new Entity. Create the localEntity and map it to the NetworkEntity component
|
|
113
|
+
if (networkUtils.isNetworkMessage(msg) && !network) {
|
|
114
|
+
entityId = engine.addEntity();
|
|
115
|
+
network = { entityId: msg.entityId, networkId: msg.networkId };
|
|
116
|
+
NetworkEntity.createOrReplace(entityId, network);
|
|
117
|
+
}
|
|
118
|
+
if (msg.type === CrdtMessageType.DELETE_ENTITY || msg.type === CrdtMessageType.DELETE_ENTITY_NETWORK) {
|
|
119
|
+
entitiesShouldBeCleaned.push(entityId);
|
|
84
120
|
broadcastMessages.push(msg);
|
|
85
121
|
}
|
|
86
122
|
else {
|
|
87
|
-
const entityState = engine.entityContainer.getEntityState(
|
|
88
|
-
// Skip updates from removed
|
|
123
|
+
const entityState = engine.entityContainer.getEntityState(entityId);
|
|
124
|
+
// Skip updates from removed entityes
|
|
89
125
|
if (entityState === EntityState.Removed)
|
|
90
126
|
continue;
|
|
91
|
-
// Entities with unknown
|
|
127
|
+
// Entities with unknown entities should update its entity state
|
|
92
128
|
if (entityState === EntityState.Unknown) {
|
|
93
|
-
engine.entityContainer.updateUsedEntity(
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
if (
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
? component.__forceUpdateFromCrdt(msg)
|
|
104
|
-
: component.updateFromCrdt(msg);
|
|
105
|
-
}
|
|
106
|
-
catch (e) {
|
|
107
|
-
console.error('[receiveMessages] ERROR processing message', msg, e);
|
|
108
|
-
return null;
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
const result = tryUpdate();
|
|
112
|
-
if (!result)
|
|
113
|
-
continue;
|
|
114
|
-
const [conflictMessage, value] = result;
|
|
115
|
-
if (!conflictMessage) {
|
|
116
|
-
// Add message to broadcast queue when no conflict
|
|
117
|
-
broadcastMessages.push(msg);
|
|
118
|
-
onProcessEntityComponentChange && onProcessEntityComponentChange(msg.entityId, msg.type, component, value);
|
|
119
|
-
}
|
|
129
|
+
engine.entityContainer.updateUsedEntity(entityId);
|
|
130
|
+
}
|
|
131
|
+
const component = engine.getComponentOrNull(msg.componentId);
|
|
132
|
+
/* istanbul ignore else */
|
|
133
|
+
if (component) {
|
|
134
|
+
if (msg.type === CrdtMessageType.PUT_COMPONENT &&
|
|
135
|
+
component.componentId === Transform.componentId &&
|
|
136
|
+
NetworkEntity.has(entityId) &&
|
|
137
|
+
NetworkParent.has(entityId)) {
|
|
138
|
+
msg.data = networkUtils.fixTransformParent(msg);
|
|
120
139
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
140
|
+
const [conflictMessage, value] = component.updateFromCrdt({ ...msg, entityId });
|
|
141
|
+
if (!conflictMessage) {
|
|
142
|
+
// Add message to transport queue to be processed by others transports
|
|
124
143
|
broadcastMessages.push(msg);
|
|
144
|
+
onProcessEntityComponentChange && onProcessEntityComponentChange(entityId, msg.type, component, value);
|
|
125
145
|
}
|
|
126
146
|
}
|
|
147
|
+
else {
|
|
148
|
+
// TODO: test this line, it is fundammental to make the editor work
|
|
149
|
+
broadcastMessages.push(msg);
|
|
150
|
+
}
|
|
127
151
|
}
|
|
128
152
|
}
|
|
129
153
|
// the last stage of the syncrhonization is to delete the entities
|
|
@@ -137,17 +161,16 @@ export function crdtSceneSystem(engine, onProcessEntityComponentChange) {
|
|
|
137
161
|
}
|
|
138
162
|
}
|
|
139
163
|
/**
|
|
140
|
-
*
|
|
164
|
+
* Iterates the dirty map and generates crdt messages to be send
|
|
141
165
|
*/
|
|
142
166
|
async function sendMessages(entitiesDeletedThisTick) {
|
|
143
|
-
//
|
|
167
|
+
// CRDT Messages will be the merge between the recieved transport messages and the new crdt messages
|
|
144
168
|
const crdtMessages = getMessages(broadcastMessages);
|
|
145
169
|
const buffer = new ReadWriteByteBuffer();
|
|
146
|
-
// Generate CRDT messages from component updates
|
|
147
170
|
for (const component of engine.componentsIter()) {
|
|
148
171
|
for (const message of component.getCrdtUpdates()) {
|
|
149
172
|
const offset = buffer.currentWriteOffset();
|
|
150
|
-
//
|
|
173
|
+
// Avoid creating messages if there is no transport that will handle it
|
|
151
174
|
if (transports.some((t) => t.filter(message))) {
|
|
152
175
|
if (message.type === CrdtMessageType.PUT_COMPONENT) {
|
|
153
176
|
PutComponentOperation.write(message.entityId, message.timestamp, message.componentId, message.data, buffer);
|
|
@@ -171,7 +194,7 @@ export function crdtSceneSystem(engine, onProcessEntityComponentChange) {
|
|
|
171
194
|
}
|
|
172
195
|
}
|
|
173
196
|
}
|
|
174
|
-
//
|
|
197
|
+
// After all updates, I execute the DeletedEntity messages
|
|
175
198
|
for (const entityId of entitiesDeletedThisTick) {
|
|
176
199
|
const offset = buffer.currentWriteOffset();
|
|
177
200
|
DeleteEntity.write(entityId, buffer);
|
|
@@ -182,19 +205,87 @@ export function crdtSceneSystem(engine, onProcessEntityComponentChange) {
|
|
|
182
205
|
});
|
|
183
206
|
onProcessEntityComponentChange && onProcessEntityComponentChange(entityId, CrdtMessageType.DELETE_ENTITY);
|
|
184
207
|
}
|
|
185
|
-
//
|
|
186
|
-
|
|
187
|
-
|
|
208
|
+
// Send CRDT messages to transports
|
|
209
|
+
const transportBuffer = new ReadWriteByteBuffer();
|
|
210
|
+
for (const index in transports) {
|
|
211
|
+
const __NetworkMessagesBuffer = [];
|
|
212
|
+
const transportIndex = Number(index);
|
|
213
|
+
const transport = transports[transportIndex];
|
|
214
|
+
const isRendererTransport = transport.type === 'renderer';
|
|
215
|
+
const isNetworkTransport = transport.type === 'network';
|
|
216
|
+
// Reset Buffer for each Transport
|
|
217
|
+
transportBuffer.resetBuffer();
|
|
218
|
+
const buffer = new ReadWriteByteBuffer();
|
|
219
|
+
// Then we send all the new crdtMessages that the transport needs to process
|
|
188
220
|
for (const message of crdtMessages) {
|
|
221
|
+
// Check if adding this message would exceed the size limit
|
|
222
|
+
const currentBufferSize = transportBuffer.toBinary().byteLength;
|
|
223
|
+
const messageSize = message.messageBuffer.byteLength;
|
|
224
|
+
if (isNetworkTransport && (currentBufferSize + messageSize) / 1024 > LIVEKIT_MAX_SIZE) {
|
|
225
|
+
// If the current buffer has content, save it as a chunk
|
|
226
|
+
if (currentBufferSize > 0) {
|
|
227
|
+
__NetworkMessagesBuffer.push(transportBuffer.toCopiedBinary());
|
|
228
|
+
transportBuffer.resetBuffer();
|
|
229
|
+
}
|
|
230
|
+
// If the message itself is larger than the limit, we need to handle it specially
|
|
231
|
+
// For now, we'll skip it to prevent infinite loops
|
|
232
|
+
if (messageSize / 1024 > LIVEKIT_MAX_SIZE) {
|
|
233
|
+
console.error(`Message too large (${messageSize} bytes), skipping message for entity ${message.entityId}`);
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
189
237
|
// Avoid echo messages
|
|
190
|
-
if (message.transportId ===
|
|
238
|
+
if (message.transportId === transportIndex)
|
|
239
|
+
continue;
|
|
240
|
+
// Redundant message for the transport
|
|
241
|
+
if (!transport.filter(message))
|
|
242
|
+
continue;
|
|
243
|
+
const { entityId } = findNetworkId(message);
|
|
244
|
+
const transformNeedsFix = 'componentId' in message &&
|
|
245
|
+
message.componentId === Transform.componentId &&
|
|
246
|
+
Transform.has(entityId) &&
|
|
247
|
+
NetworkParent.has(entityId) &&
|
|
248
|
+
NetworkEntity.has(entityId);
|
|
249
|
+
// If there was a LOCAL change in the transform. Add the parent to that transform
|
|
250
|
+
if (isRendererTransport && message.type === CrdtMessageType.PUT_COMPONENT && transformNeedsFix) {
|
|
251
|
+
const parent = findNetworkId(NetworkParent.get(entityId));
|
|
252
|
+
const transformData = networkUtils.fixTransformParent(message, Transform.get(entityId), parent.entityId);
|
|
253
|
+
const offset = buffer.currentWriteOffset();
|
|
254
|
+
PutComponentOperation.write(entityId, message.timestamp, message.componentId, transformData, buffer);
|
|
255
|
+
transportBuffer.writeBuffer(buffer.buffer().subarray(offset, buffer.currentWriteOffset()), false);
|
|
191
256
|
continue;
|
|
192
|
-
// Check if transport wants this message
|
|
193
|
-
if (transport.filter(message)) {
|
|
194
|
-
transportBuffer.writeBuffer(message.messageBuffer, false);
|
|
195
257
|
}
|
|
258
|
+
if (isRendererTransport && networkUtils.isNetworkMessage(message)) {
|
|
259
|
+
// If it's the renderer transport and its a NetworkMessage, we need to fix the entityId field and convert it to a known Message.
|
|
260
|
+
// PUT_NETWORK_COMPONENT -> PUT_COMPONENT
|
|
261
|
+
let transformData = 'data' in message ? message.data : new Uint8Array();
|
|
262
|
+
if (transformNeedsFix) {
|
|
263
|
+
const parent = findNetworkId(NetworkParent.get(entityId));
|
|
264
|
+
transformData = networkUtils.fixTransformParent(message, Transform.get(entityId), parent.entityId);
|
|
265
|
+
}
|
|
266
|
+
networkUtils.networkMessageToLocal({ ...message, data: transformData }, entityId, buffer, transportBuffer);
|
|
267
|
+
// Iterate the next message
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
// If its a network transport and its a PUT_COMPONENT that has a NetworkEntity component, we need to send this message
|
|
271
|
+
// through comms with the EntityID and NetworkID from ther NetworkEntity so everyone can recieve this message and map to their custom entityID.
|
|
272
|
+
if (isNetworkTransport && !networkUtils.isNetworkMessage(message)) {
|
|
273
|
+
const networkData = NetworkEntity.getOrNull(message.entityId);
|
|
274
|
+
// If it has networkData convert the message to PUT_NETWORK_COMPONENT.
|
|
275
|
+
if (networkData) {
|
|
276
|
+
networkUtils.localMessageToNetwork(message, networkData, buffer, transportBuffer);
|
|
277
|
+
// Iterate the next message
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
// Common message
|
|
282
|
+
transportBuffer.writeBuffer(message.messageBuffer, false);
|
|
283
|
+
}
|
|
284
|
+
if (isNetworkTransport && transportBuffer.currentWriteOffset()) {
|
|
285
|
+
__NetworkMessagesBuffer.push(transportBuffer.toBinary());
|
|
196
286
|
}
|
|
197
|
-
|
|
287
|
+
const message = isNetworkTransport ? __NetworkMessagesBuffer : transportBuffer.toBinary();
|
|
288
|
+
await transport.send(message);
|
|
198
289
|
}
|
|
199
290
|
}
|
|
200
291
|
/**
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import * as components from '../components';
|
|
2
|
+
import { Vector3, createTimers } from '../runtime/helpers';
|
|
3
|
+
import { KnockbackFalloff } from './physics-impulse';
|
|
4
|
+
/** @internal */
|
|
5
|
+
export function createPhysicsForceHelper(engine) {
|
|
6
|
+
const PhysicsCombinedForce = components.PhysicsCombinedForce(engine);
|
|
7
|
+
const Transform = components.Transform(engine);
|
|
8
|
+
const timers = createTimers(engine);
|
|
9
|
+
const durationTimers = new Map();
|
|
10
|
+
// Key = source entity producing the force (not the target); all forces target PlayerEntity.
|
|
11
|
+
const forceSources = new Map();
|
|
12
|
+
const repulsionSources = new Map();
|
|
13
|
+
let lastWrittenForceVector = null;
|
|
14
|
+
function recalcForce() {
|
|
15
|
+
if (forceSources.size === 0) {
|
|
16
|
+
if (PhysicsCombinedForce.has(engine.PlayerEntity)) {
|
|
17
|
+
PhysicsCombinedForce.deleteFrom(engine.PlayerEntity);
|
|
18
|
+
}
|
|
19
|
+
lastWrittenForceVector = null;
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
const current = PhysicsCombinedForce.getOrNull(engine.PlayerEntity);
|
|
23
|
+
if (current && lastWrittenForceVector && current.vector) {
|
|
24
|
+
if (!Vector3.equals(current.vector, lastWrittenForceVector)) {
|
|
25
|
+
console.error('PBPhysicsCombinedForce was modified externally.', 'Expected:', lastWrittenForceVector, 'Found:', current.vector, '-- overwriting with local state.');
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
let sum = { x: 0, y: 0, z: 0 };
|
|
29
|
+
for (const v of forceSources.values()) {
|
|
30
|
+
sum = Vector3.add(sum, v);
|
|
31
|
+
}
|
|
32
|
+
PhysicsCombinedForce.createOrReplace(engine.PlayerEntity, { vector: sum });
|
|
33
|
+
lastWrittenForceVector = sum;
|
|
34
|
+
}
|
|
35
|
+
function applyForceToPlayer(source, vector, magnitude) {
|
|
36
|
+
let finalVector;
|
|
37
|
+
if (Vector3.equalsToFloats(vector, 0, 0, 0))
|
|
38
|
+
return;
|
|
39
|
+
if (typeof magnitude === 'number') {
|
|
40
|
+
finalVector = Vector3.scale(Vector3.normalize(vector), magnitude);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
finalVector = vector;
|
|
44
|
+
}
|
|
45
|
+
forceSources.set(source, finalVector);
|
|
46
|
+
recalcForce();
|
|
47
|
+
}
|
|
48
|
+
function removeForceFromPlayer(source) {
|
|
49
|
+
repulsionSources.delete(source);
|
|
50
|
+
const timerId = durationTimers.get(source);
|
|
51
|
+
if (timerId !== undefined) {
|
|
52
|
+
timers.clearTimeout(timerId);
|
|
53
|
+
durationTimers.delete(source);
|
|
54
|
+
}
|
|
55
|
+
if (!forceSources.has(source))
|
|
56
|
+
return;
|
|
57
|
+
forceSources.delete(source);
|
|
58
|
+
recalcForce();
|
|
59
|
+
}
|
|
60
|
+
function scheduleForceDuration(source, seconds) {
|
|
61
|
+
const existing = durationTimers.get(source);
|
|
62
|
+
if (existing !== undefined) {
|
|
63
|
+
timers.clearTimeout(existing);
|
|
64
|
+
}
|
|
65
|
+
const timerId = timers.setTimeout(() => {
|
|
66
|
+
durationTimers.delete(source);
|
|
67
|
+
removeForceFromPlayer(source);
|
|
68
|
+
}, seconds * 1000);
|
|
69
|
+
durationTimers.set(source, timerId);
|
|
70
|
+
}
|
|
71
|
+
function applyForceToPlayerForDuration(source, duration, vector, magnitude) {
|
|
72
|
+
applyForceToPlayer(source, vector, magnitude);
|
|
73
|
+
scheduleForceDuration(source, duration);
|
|
74
|
+
}
|
|
75
|
+
function computeRepulsionVector(fromPosition, magnitude, radius, falloff) {
|
|
76
|
+
const diff = Vector3.subtract(Transform.get(engine.PlayerEntity).position, fromPosition);
|
|
77
|
+
if (Vector3.equalsToFloats(diff, 0, 0, 0))
|
|
78
|
+
return { x: 0, y: magnitude, z: 0 };
|
|
79
|
+
// Fast path: default params — no need to compute distance
|
|
80
|
+
if (radius === Infinity && falloff === KnockbackFalloff.CONSTANT) {
|
|
81
|
+
return Vector3.scale(Vector3.normalize(diff), magnitude);
|
|
82
|
+
}
|
|
83
|
+
const distance = Vector3.length(diff);
|
|
84
|
+
if (distance > radius)
|
|
85
|
+
return null;
|
|
86
|
+
let effectiveMagnitude;
|
|
87
|
+
switch (falloff) {
|
|
88
|
+
case KnockbackFalloff.LINEAR:
|
|
89
|
+
effectiveMagnitude = magnitude * (1 - distance / radius);
|
|
90
|
+
break;
|
|
91
|
+
case KnockbackFalloff.INVERSE_SQUARE:
|
|
92
|
+
effectiveMagnitude = magnitude / (distance * distance + 1);
|
|
93
|
+
break;
|
|
94
|
+
case KnockbackFalloff.CONSTANT:
|
|
95
|
+
default:
|
|
96
|
+
effectiveMagnitude = magnitude;
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
if (effectiveMagnitude === 0)
|
|
100
|
+
return null;
|
|
101
|
+
// normalize(diff) * effectiveMagnitude in one step
|
|
102
|
+
return Vector3.scale(diff, effectiveMagnitude / distance);
|
|
103
|
+
}
|
|
104
|
+
function applyRepulsionForceToPlayer(source, fromPosition, magnitude, radius = Infinity, falloff = KnockbackFalloff.CONSTANT) {
|
|
105
|
+
repulsionSources.set(source, { fromPosition, magnitude, radius, falloff });
|
|
106
|
+
const vector = computeRepulsionVector(fromPosition, magnitude, radius, falloff);
|
|
107
|
+
if (vector) {
|
|
108
|
+
forceSources.set(source, vector);
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
forceSources.delete(source);
|
|
112
|
+
}
|
|
113
|
+
recalcForce();
|
|
114
|
+
}
|
|
115
|
+
// Background system: recalculate repulsion vectors and clean up stale forces every tick.
|
|
116
|
+
// Stale forces can appear when CRDT sync from another client writes PhysicsCombinedForce
|
|
117
|
+
// externally (entity-1 ambiguity: each client interprets entity 1 as its own player).
|
|
118
|
+
engine.addSystem(() => {
|
|
119
|
+
// Repulsion forces need per-tick direction recalculation as the player moves
|
|
120
|
+
if (repulsionSources.size > 0) {
|
|
121
|
+
for (const [source, { fromPosition, magnitude, radius, falloff }] of repulsionSources) {
|
|
122
|
+
const vector = computeRepulsionVector(fromPosition, magnitude, radius, falloff);
|
|
123
|
+
if (vector) {
|
|
124
|
+
forceSources.set(source, vector);
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
forceSources.delete(source);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
recalcForce();
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
// No local sources — clean up any externally-created component (e.g. from CRDT sync)
|
|
134
|
+
if (forceSources.size === 0 && PhysicsCombinedForce.has(engine.PlayerEntity)) {
|
|
135
|
+
PhysicsCombinedForce.deleteFrom(engine.PlayerEntity);
|
|
136
|
+
lastWrittenForceVector = null;
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
return { applyForceToPlayer, removeForceFromPlayer, applyForceToPlayerForDuration, applyRepulsionForceToPlayer };
|
|
140
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @public
|
|
3
|
+
* Falloff mode for knockback force over distance.
|
|
4
|
+
*/
|
|
5
|
+
export declare enum KnockbackFalloff {
|
|
6
|
+
/** Same force at any distance within radius */
|
|
7
|
+
CONSTANT = 0,
|
|
8
|
+
/** Smooth linear decrease to 0 at radius edge: F = magnitude * (1 - distance / radius) */
|
|
9
|
+
LINEAR = 1,
|
|
10
|
+
/** Sharp drop-off, physically realistic: F = magnitude / (distance^2 + 1) */
|
|
11
|
+
INVERSE_SQUARE = 2
|
|
12
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import * as components from '../components';
|
|
2
|
+
import { Vector3 } from '../runtime/helpers';
|
|
3
|
+
/**
|
|
4
|
+
* @public
|
|
5
|
+
* Falloff mode for knockback force over distance.
|
|
6
|
+
*/
|
|
7
|
+
export var KnockbackFalloff;
|
|
8
|
+
(function (KnockbackFalloff) {
|
|
9
|
+
/** Same force at any distance within radius */
|
|
10
|
+
KnockbackFalloff[KnockbackFalloff["CONSTANT"] = 0] = "CONSTANT";
|
|
11
|
+
/** Smooth linear decrease to 0 at radius edge: F = magnitude * (1 - distance / radius) */
|
|
12
|
+
KnockbackFalloff[KnockbackFalloff["LINEAR"] = 1] = "LINEAR";
|
|
13
|
+
/** Sharp drop-off, physically realistic: F = magnitude / (distance^2 + 1) */
|
|
14
|
+
KnockbackFalloff[KnockbackFalloff["INVERSE_SQUARE"] = 2] = "INVERSE_SQUARE";
|
|
15
|
+
})(KnockbackFalloff || (KnockbackFalloff = {}));
|
|
16
|
+
/** @internal */
|
|
17
|
+
export function createPhysicsImpulseHelper(engine) {
|
|
18
|
+
const PhysicsCombinedImpulse = components.PhysicsCombinedImpulse(engine);
|
|
19
|
+
const Transform = components.Transform(engine);
|
|
20
|
+
const EngineInfo = components.EngineInfo(engine);
|
|
21
|
+
let impulseEventId = 0;
|
|
22
|
+
let lastWrittenEventId = 0;
|
|
23
|
+
let lastWrittenTick = -1;
|
|
24
|
+
function applyImpulseToPlayer(vector, magnitude) {
|
|
25
|
+
let finalVector;
|
|
26
|
+
if (typeof magnitude === 'number') {
|
|
27
|
+
if (Vector3.equalsToFloats(vector, 0, 0, 0))
|
|
28
|
+
return;
|
|
29
|
+
finalVector = Vector3.scale(Vector3.normalize(vector), magnitude);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
if (Vector3.equalsToFloats(vector, 0, 0, 0))
|
|
33
|
+
return;
|
|
34
|
+
finalVector = vector;
|
|
35
|
+
}
|
|
36
|
+
const currentTick = EngineInfo.getOrNull(engine.RootEntity)?.tickNumber ?? 0;
|
|
37
|
+
const existing = PhysicsCombinedImpulse.getOrNull(engine.PlayerEntity);
|
|
38
|
+
if (existing && existing.eventId !== lastWrittenEventId && lastWrittenEventId !== 0) {
|
|
39
|
+
throw new Error('PBPhysicsCombinedImpulse was modified outside Physics helper. ' +
|
|
40
|
+
'Do not mix direct component access with Physics.applyImpulseToPlayer().');
|
|
41
|
+
}
|
|
42
|
+
if (lastWrittenTick === currentTick && existing) {
|
|
43
|
+
finalVector = Vector3.add(existing.vector ?? { x: 0, y: 0, z: 0 }, finalVector);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
lastWrittenEventId = ++impulseEventId;
|
|
47
|
+
}
|
|
48
|
+
lastWrittenTick = currentTick;
|
|
49
|
+
PhysicsCombinedImpulse.createOrReplace(engine.PlayerEntity, {
|
|
50
|
+
vector: finalVector,
|
|
51
|
+
eventId: lastWrittenEventId
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
function applyKnockbackToPlayer(fromPosition, magnitude, radius = Infinity, falloff = KnockbackFalloff.CONSTANT) {
|
|
55
|
+
const diff = Vector3.subtract(Transform.get(engine.PlayerEntity).position, fromPosition);
|
|
56
|
+
if (Vector3.equalsToFloats(diff, 0, 0, 0)) {
|
|
57
|
+
applyImpulseToPlayer({ x: 0, y: magnitude, z: 0 });
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
// Fast path: default params — no need to compute distance
|
|
61
|
+
if (radius === Infinity && falloff === KnockbackFalloff.CONSTANT) {
|
|
62
|
+
applyImpulseToPlayer(Vector3.scale(Vector3.normalize(diff), magnitude));
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const distance = Vector3.length(diff);
|
|
66
|
+
if (distance > radius)
|
|
67
|
+
return;
|
|
68
|
+
let effectiveMagnitude;
|
|
69
|
+
switch (falloff) {
|
|
70
|
+
case KnockbackFalloff.LINEAR:
|
|
71
|
+
effectiveMagnitude = magnitude * (1 - distance / radius);
|
|
72
|
+
break;
|
|
73
|
+
case KnockbackFalloff.INVERSE_SQUARE:
|
|
74
|
+
effectiveMagnitude = magnitude / (distance * distance + 1);
|
|
75
|
+
break;
|
|
76
|
+
case KnockbackFalloff.CONSTANT:
|
|
77
|
+
default:
|
|
78
|
+
effectiveMagnitude = magnitude;
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
// normalize(diff) * effectiveMagnitude in one step
|
|
82
|
+
applyImpulseToPlayer(Vector3.scale(diff, effectiveMagnitude / distance));
|
|
83
|
+
}
|
|
84
|
+
return { applyImpulseToPlayer, applyKnockbackToPlayer };
|
|
85
|
+
}
|