Skip to content

Commit

Permalink
add more property replicated
Browse files Browse the repository at this point in the history
  • Loading branch information
wyb10a10 committed Aug 18, 2023
1 parent 02bf836 commit 3cd8fb4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
24 changes: 23 additions & 1 deletion assets/Script/example/sync/SyncCube.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<number>})
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) {}
}
8 changes: 4 additions & 4 deletions assets/Script/sync/ReplicateMark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();
}
}
Expand Down
2 changes: 1 addition & 1 deletion assets/Script/sync/components/ServerReplicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 3cd8fb4

Please sign in to comment.