From 3cd8fb4b9edcda8393c96e710524ece6fa51bd62 Mon Sep 17 00:00:00 2001 From: wyb10a10 Date: Fri, 18 Aug 2023 21:29:59 +0800 Subject: [PATCH] add more property replicated --- assets/Script/example/sync/SyncCube.ts | 24 ++++++++++++++++++- assets/Script/sync/ReplicateMark.ts | 8 +++---- .../sync/components/ServerReplicator.ts | 2 +- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/assets/Script/example/sync/SyncCube.ts b/assets/Script/example/sync/SyncCube.ts index cc538cd..5a09d88 100644 --- a/assets/Script/example/sync/SyncCube.ts +++ b/assets/Script/example/sync/SyncCube.ts @@ -1,6 +1,7 @@ import { Component, _decorator } from "cc"; import { replicated } from "../../sync/SyncUtil"; import { CCString } from "cc"; +import { CCInteger } from "cc"; const { ccclass, property } = _decorator; @@ -11,10 +12,31 @@ export default class SyncCube extends Component { @property({type : CCString}) public cubeName: string = "cube"; + @replicated() + @property({type : CCInteger}) + public cubeX = 0; + + @property({type : CCInteger}) + public dontSync = 0; + + @replicated() + @property({type : Array}) + public cubePos: number[] = [0, 0, 0]; + onLoad() { // 随机生成一个cubeName,后面接的随机数为0-1000的整数 this.cubeName = "cube" + Math.floor(Math.random() * 1000); - console.log("cubeName: " + this.cubeName); + + // 随机生成一个cubeX,后面接的随机数为0-1000的整数 + this.cubeX = Math.floor(Math.random() * 1000); + + // 随机生成一个cubePos,后面接的随机数为0-1000的整数 + this.cubePos = [Math.floor(Math.random() * 1000), Math.floor(Math.random() * 1000), Math.floor(Math.random() * 1000)]; + + // 随机生成一个dontSync,后面接的随机数为0-1000的整数 + this.dontSync = Math.floor(Math.random() * 1000); + + console.log("onLoad", this.node.uuid, this.cubeName, this.cubeX, this.cubePos, this.dontSync); } // update (dt) {} } diff --git a/assets/Script/sync/ReplicateMark.ts b/assets/Script/sync/ReplicateMark.ts index a33a2e9..2be7197 100644 --- a/assets/Script/sync/ReplicateMark.ts +++ b/assets/Script/sync/ReplicateMark.ts @@ -66,10 +66,10 @@ export interface ReplicateMarkInfo { * @param target 要修饰的类对象 * @returns ReplicateMark */ -export function getReplicateMark(target: any, autoCreator: boolean = true, option?: ObjectReplicatedOption): ReplicateMark | undefined { +export function getReplicateMark(target: any, autoCreator: boolean = true, option?: ObjectReplicatedOption, autoScan?: boolean): ReplicateMark | undefined { let ret: ReplicateMark | undefined = target[REPLICATE_MARK_INDEX]; if (!ret && autoCreator) { - ret = new ReplicateMark(target, option); + ret = new ReplicateMark(target, option, autoScan); Object.defineProperty(target, REPLICATE_MARK_INDEX, { value: ret, enumerable: false, @@ -91,13 +91,13 @@ export default class ReplicateMark { private defaultMark = false; private cls: any; - public constructor(cls: any, objMark?: ObjectReplicatedOption) { + public constructor(cls: any, objMark?: ObjectReplicatedOption, autoScan?: boolean) { this.cls = cls; this.objMark = objMark; // 如果明确指定了syncProperty // 或cls存在成员变量,才执行initMark // 其他情况表示,cls还未被初始化 - if ((objMark && objMark.SyncProperty) || Object.keys(cls).length > 0) { + if ((objMark && objMark.SyncProperty) || (autoScan && Object.keys(cls).length > 0)) { this.initMark(); } } diff --git a/assets/Script/sync/components/ServerReplicator.ts b/assets/Script/sync/components/ServerReplicator.ts index 7dd0944..d909f5f 100644 --- a/assets/Script/sync/components/ServerReplicator.ts +++ b/assets/Script/sync/components/ServerReplicator.ts @@ -8,7 +8,7 @@ export class ServerReplicator extends Component { public prefabs: Prefab[] = []; private instanceCounter: number = 0; - private serverVersion: number = -1; + private serverVersion: number = 0; public createRandomPrefab() { const randomIndex = Math.floor(Math.random() * this.prefabs.length);