1import { Sandbox, SandboxOptions, SandboxPlayer } from "ZEPETO.Multiplay";
2import { DataStorage } from "ZEPETO.Multiplay.DataStorage";
3import { Player, Transform, Vector3 } from "ZEPETO.Multiplay.Schema";
4
5export default class extends Sandbox {
6
7 storageMap: Map<string, DataStorage> = new Map<string, DataStorage>();
8
9
10 private _botMap: Map<string, Player> = new Map<string, Player>();
11 private playTime: number = 0;
12
13 constructor() {
14 super();
15 }
16
17 onCreate(options: SandboxOptions) {
18
19
20
21
22 this.onMessage("onChangedTransform", (client, message) => {
23
24 this.state.players.get(client.sessionId);
25
26 const player = this.state.players.get(client.sessionId);
27
28 const transform = new Transform();
29 transform.position = new Vector3();
30 transform.position.x = message.position.x;
31 transform.position.y = message.position.y;
32 transform.position.z = message.position.z;
33
34 transform.rotation = new Vector3();
35 transform.rotation.x = message.rotation.x;
36 transform.rotation.y = message.rotation.y;
37 transform.rotation.z = message.rotation.z;
38
39 if (player) {
40 player.transform = transform;
41 }
42 });
43
44 this.onMessage("onChangedState", (client, message) => {
45 const player = this.state.players.get(client.sessionId);
46 if (player) {
47 player.state = message.state;
48 player.subState = message.subState;
49 }
50 });
51
52
53 this.onMessage("CreateBot", (client, message) => {
54 this.CreateBot(message);
55 });
56
57
58 this.onMessage("MoveBot", (client, message) => {
59 this.MoveBot(client, message);
60 });
61 }
62
63
64 CreateBot(userId: string) {
65
66
67 const sessionId = "Bot_" + userId;
68
69
70 if (this.state.players.has(sessionId)) {
71 return;
72 }
73
74
75 const player: Player = new Player();
76 player.sessionId = sessionId;
77 if (userId) {
78 player.zepetoUserId = userId;
79 }
80 player.isBot = true;
81
82
83 this.state.players.set(player.sessionId, player);
84 this._botMap.set(sessionId, player);
85
86
87 this.CheckPlayerNumber();
88 }
89
90
91 CheckPlayerNumber() {
92
93
94 console.log(`玩家数量, ${this.state.players.size}`);
95
96
97 if (this.state.players.size >= 4) {
98 this.StartWorld();
99 }
100 }
101
102
103 StartWorld() {
104 this.playTime += 1;
105
106
107 console.log("开始世界!");
108 this.broadcast("StartWorld", this.playTime);
109 }
110
111
112 MoveBot(client: SandboxPlayer, message: string) {
113
114 const position = JSON.parse(message);
115
116
117 const newMessage = {
118 user: client.sessionId,
119 positionX: position.x,
120 positionY: position.y,
121 positionZ: position.z
122 }
123
124
125 this.broadcast("MoveBotToPosition", JSON.stringify(newMessage));
126 }
127
128 async onJoin(client: SandboxPlayer) {
129
130
131 console.log(`[OnJoin] 会话 ID : ${client.sessionId}, 哈希码 : ${client.hashCode}, 用户 ID : ${client.userId}`)
132
133 const player = new Player();
134 player.sessionId = client.sessionId;
135
136 if (client.hashCode) {
137 player.zepetoHash = client.hashCode;
138 }
139 if (client.userId) {
140 player.zepetoUserId = client.userId;
141 }
142
143
144 const storage: DataStorage = client.loadDataStorage();
145
146 this.storageMap.set(client.sessionId, storage);
147
148 let visit_cnt = await storage.get("VisitCount") as number;
149 if (visit_cnt == null) visit_cnt = 0;
150
151 console.log(`[OnJoin] ${client.sessionId} 的访问计数 : ${visit_cnt}`)
152
153
154 await storage.set("VisitCount", ++visit_cnt);
155
156
157
158 this.state.players.set(client.sessionId, player);
159
160
161 this.CheckPlayerNumber();
162 }
163
164 onTick(deltaTime: number): void {
165
166 }
167
168 async onLeave(client: SandboxPlayer, consented?: boolean) {
169
170
171
172 this.state.players.delete(client.sessionId);
173 }
174}