Skip to content

Commit

Permalink
link node replicator to node sync component
Browse files Browse the repository at this point in the history
  • Loading branch information
wyb10a10 committed Aug 6, 2023
1 parent 8431d2b commit 1460367
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion assets/Script/example/sync/SyncUI.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { _decorator, Component, Button } from 'cc';
import { ClientReplicator } from '../../sync/components/ClientReplicator';
import { ServerReplicator } from '../../sync/components/ServerReplicator';
import ClientReplicator from '../../sync/components/ClientReplicator';
const { ccclass, property } = _decorator;

@ccclass('SyncUI')
Expand Down
7 changes: 6 additions & 1 deletion assets/Script/sync/NodeReplicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { IReplicator } from './SyncUtil';
import { createReplicator } from './ReplicatorFactory';
import { getReplicateMark } from './ReplicateMark';

class NodeReplicator implements IReplicator {
/**
* NodeReplicator用于管理当前节点及其子节点的所有标记为同步的组件
* 维护这些组件的Replicator,以及生成Diff
* NodeReplicator当前的版本不支持节点和组件的动态添加和删除
*/
export default class NodeReplicator implements IReplicator {
private target: Node;
private replicators: Map<string, IReplicator> = new Map();
private version: number = 0;
Expand Down
5 changes: 4 additions & 1 deletion assets/Script/sync/ReplicatorFactory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Vec3 } from "cc";
import { Vec3, Node } from "cc";
import { ArrayLinkReplicator, ArrayReplicator, SimpleArrayReplicator } from "./ArrayReplicator";
import { CCVec3Replicator } from "./CocosReplicator";
import { ReplicateScanner } from "./DiffScaner";
Expand All @@ -7,6 +7,7 @@ import ReplicateMark, { ReplicateType } from "./ReplicateMark";
import { IReplicator, isSimpleType } from "./SyncUtil";
import { SimpleSetReplicator } from "./SetReplicator";
import { HashReplicator, SimpleHashReplicator } from "./HashReplicator";
import NodeReplicator from "./NodeReplicator";

export function createReplicator(target: any, mark?: ReplicateMark): IReplicator | null {
// 根据target的类型和mark参数决定创建哪种类型的Replicator
Expand All @@ -33,6 +34,8 @@ export function createReplicator(target: any, mark?: ReplicateMark): IReplicator
return null;
} else if (target instanceof Vec3) {
return new CCVec3Replicator(target);
} else if (target instanceof Node) {
return new NodeReplicator(target);
} else if (target instanceof Set) {
return new SimpleSetReplicator(target, mark);
} else if (target instanceof Map) {
Expand Down
29 changes: 29 additions & 0 deletions assets/Script/sync/components/NodeSync.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { _decorator, Component, Node } from 'cc';
import { ServerReplicator } from './ServerReplicator';
import NodeReplicator from '../NodeReplicator';
const { ccclass, property } = _decorator;

@ccclass('NodeSync')
Expand All @@ -10,6 +11,9 @@ export class NodeSync extends Component {
@property
public prefabPath: string = '';

@property
public replicator: NodeReplicator | null = null;

public childrenNodeSync: NodeSync[] = [];

public setInstanceId(id: number) {
Expand All @@ -26,6 +30,31 @@ export class NodeSync extends Component {

onEnable() {
this.registerToParent();
if (!this.replicator) {
this.replicator = new NodeReplicator(this.node);
}
}

/**
* 生成一个Diff对象
*/
genDiff(fromVersion: number, toVersion: number): any {
return this.replicator?.genDiff(fromVersion, toVersion);
}

/**
* 应用一个Diff对象
* @param diff Diff对象
*/
applyDiff(diff: any): void {
this.replicator?.applyDiff(diff);
}

/**
* 获取当前版本
*/
getVersion(): number {
return this.replicator?.getVersion() || 0;
}

private registerToParent() {
Expand Down
2 changes: 1 addition & 1 deletion profiles/v2/packages/scene.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@
}
},
"camera-uuids": [
"f46876e4-e81b-4931-b493-6d367be385e7"
"4753fa27-55ac-44df-94a5-a91c50b1cd4d"
],
"float-window": {
"position": {
Expand Down

0 comments on commit 1460367

Please sign in to comment.