-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
188 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Entity } from '../../engine' | ||
import { IEngine, LastWriteWinElementSetComponentDefinition } from '../../engine/types' | ||
import { Schemas } from '../../schemas' | ||
|
||
export interface INetowrkParentType { | ||
networkId: number | ||
entityId: Entity | ||
} | ||
|
||
export type INetowrkParent = LastWriteWinElementSetComponentDefinition<INetowrkParentType> | ||
|
||
function defineNetworkParentComponent(engine: Pick<IEngine, 'defineComponent'>) { | ||
const EntityNetwork = engine.defineComponent('core-schema::Network-Parent', { | ||
networkId: Schemas.Int64, | ||
entityId: Schemas.Entity | ||
}) | ||
return EntityNetwork | ||
} | ||
|
||
export default defineNetworkParentComponent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Entity, NetworkEntity, NetworkParent, Transform } from '@dcl/ecs' | ||
|
||
export function parentEntity(entity: Entity, parent: Entity) { | ||
const network = NetworkEntity.getOrNull(parent) | ||
if (!network) { | ||
throw new Error('Please call syncEntity on the parent before parentEntity fn') | ||
} | ||
|
||
// Create network parent component | ||
NetworkParent.createOrReplace(entity, network) | ||
|
||
// If we dont have a transform for this entity, create an empty one to send it to the renderer | ||
if (!Transform.getOrNull(entity)) { | ||
Transform.create(entity) | ||
} | ||
} | ||
/** | ||
* enum SyncEnum { | ||
* PARENT_DOOR, | ||
* CHILD_DOOR | ||
* } | ||
* // Create parent | ||
* const parent = engine.addEntity() // 512 | ||
* Transform.create(parent, { position: 4, 1, 4 }) | ||
* syncEntity(parent, [], SyncEnum.PARENT_DOOR) // { networkId: 0, entityId: 1 } | ||
* // Create Child | ||
* const child = engine.addEntity() // 513 | ||
* Transform.create(child) | ||
* parentEntity(child, parent) // NetworkParent => { networkId: 0, entityId: 1 } | ||
* syncEntity(child, [Transform.componentId], SyncEnum.CHILD_DOOR) // { networkId: 0, entityId: 2 } | ||
* | ||
* // Now we should see the child on the position 4,1,4 on every client. | ||
* // But WHAAAAAAAAAAT if we create a new entity on user click, and change the parenting to that entity with the position of the user. | ||
* // TODO: this case. | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,21 @@ | ||
import { Entity, NetworkEntity, SyncComponents } from '@dcl/ecs' | ||
import { myProfile } from './utils' | ||
|
||
export function syncEntity(entity: Entity, componentIds: number[], id?: number) { | ||
export function syncEntity(entityId: Entity, componentIds: number[], entityEnumId?: number) { | ||
// Profile not initialized | ||
if (!myProfile?.networkId) { | ||
throw new Error('USER_ID NOT INITIALIZED') | ||
throw new Error('Profile not initialized. Called syncEntity inside the main() function.') | ||
} | ||
// It it has an custom Id, then the networkId and the entityId should be the same for everyone | ||
// If not, use the profile as the networkId, and the real entityId to then map the entity | ||
|
||
// If there is an entityEnumId, it means is the same entity for all the clients created on the main funciton. | ||
// So the networkId should be the same in all the clients to avoid re-creating this entity. | ||
// For this case we use networkId = 0. | ||
// If is not defined, then is a entity created in runtime (what we called dynamic/runtime entities). | ||
// We use the networkId generated by the user address to identify this entity through the network | ||
const networkEntity = | ||
id !== undefined ? { entityId: id as Entity, networkId: 0 } : { entityId: entity, networkId: myProfile.networkId } | ||
NetworkEntity.createOrReplace(entity, networkEntity) | ||
SyncComponents.createOrReplace(entity, { componentIds }) | ||
entityEnumId !== undefined | ||
? { entityId: entityEnumId as Entity, networkId: 0 } | ||
: { entityId, networkId: myProfile.networkId } | ||
NetworkEntity.createOrReplace(entityId, networkEntity) | ||
SyncComponents.createOrReplace(entityId, { componentIds }) | ||
} |