Skip to content

Commit 70db3b3

Browse files
committed
服务端框架移动新地址
1 parent f16911f commit 70db3b3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+363
-24941
lines changed

client-test/dist/gs-client.js

+18-52
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,16 @@ var InputSystem = /** @class */ (function (_super) {
117117
if (inputEvent.data.isKeyDown) {
118118
switch (inputEvent.data.key.toLowerCase()) {
119119
case 'w':
120-
velocity.y = -10;
120+
velocity.y = -100;
121121
break;
122122
case 's':
123-
velocity.y = 10;
123+
velocity.y = 100;
124124
break;
125125
case 'a':
126-
velocity.x = -10;
126+
velocity.x = -100;
127127
break;
128128
case 'd':
129-
velocity.x = 10;
129+
velocity.x = 100;
130130
break;
131131
}
132132
}
@@ -319,48 +319,9 @@ function deleteAllPlayer() {
319319
core.systemManager.invalidateEntityCacheForSystem(drawSystem);
320320
}
321321
var syncStrategy = new gs.SnapshotInterpolationStrategy();
322-
syncStrategy.onInterpolation = function (prevSnapshot, nextSnapshot, progress) {
323-
var e_5, _a;
324-
var _b, _c;
325-
// 用户实现自定义实体插值逻辑
326-
// 例如:根据实体的类型更新位置、旋转、缩放等属性
327-
console.log("onInterpolation ".concat(progress));
328-
var _loop_1 = function (serializedEntity) {
329-
// 根据实体的唯一标识符查找对应的游戏实体
330-
var entityId = serializedEntity.id;
331-
var entity = core.entityManager.getEntity(entityId);
332-
if (entity) {
333-
// 使用插值进度来解析实体的位置
334-
var positionComponent = entity.getComponent(PositionComponent);
335-
if (positionComponent) {
336-
// 使用插值进度来解析实体的位置
337-
var prevPosition = (_b = prevSnapshot.snapshot.entities.find(function (e) { return e.id === entityId; })) === null || _b === void 0 ? void 0 : _b.components["PositionComponent"];
338-
var nextPosition = (_c = nextSnapshot.snapshot.entities.find(function (e) { return e.id === entityId; })) === null || _c === void 0 ? void 0 : _c.components["PositionComponent"];
339-
if (prevPosition && nextPosition) {
340-
// 使用线性插值来更新位置
341-
var interpolatedX = prevPosition.x + (nextPosition.x - prevPosition.x) * progress;
342-
var interpolatedY = prevPosition.y + (nextPosition.y - prevPosition.y) * progress;
343-
// 更新实体的位置属性
344-
positionComponent.x = interpolatedX;
345-
positionComponent.y = interpolatedY;
346-
}
347-
}
348-
}
349-
};
350-
try {
351-
for (var _d = __values(nextSnapshot.snapshot.entities), _e = _d.next(); !_e.done; _e = _d.next()) {
352-
var serializedEntity = _e.value;
353-
_loop_1(serializedEntity);
354-
}
355-
}
356-
catch (e_5_1) { e_5 = { error: e_5_1 }; }
357-
finally {
358-
try {
359-
if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
360-
}
361-
finally { if (e_5) throw e_5.error; }
362-
}
363-
};
322+
syncStrategy.setInterpolationCallback(function (prevSnapshot, nextSnapshot, progress) {
323+
console.log("".concat(prevSnapshot, " ").concat(nextSnapshot, " ").concat(progress));
324+
});
364325
var strategyManager = new gs.SyncStrategyManager(syncStrategy); // 发送状态
365326
document.addEventListener("DOMContentLoaded", function () {
366327
// 获取加入房间按钮
@@ -369,6 +330,7 @@ document.addEventListener("DOMContentLoaded", function () {
369330
var leaveRoomButton = document.getElementById('leave-room-btn');
370331
var startFrameBtn = document.getElementById('start-frame');
371332
var endFrameBtn = document.getElementById('end-frame');
333+
var clearLogBtn = document.getElementById('clear-log');
372334
joinRoomButton.onclick = function (ev) {
373335
console.log("发送加入房间指令");
374336
// 获取输入框元素
@@ -385,6 +347,9 @@ document.addEventListener("DOMContentLoaded", function () {
385347
createPlayer(playerId);
386348
});
387349
};
350+
clearLogBtn.onclick = function (ev) {
351+
document.getElementById('loggerArea').textContent = '';
352+
};
388353
leaveRoomButton.onclick = function (ev) {
389354
console.log("发送退出房间指令");
390355
networkAdapter.RoomAPI.leaveRoom(roomId);
@@ -409,7 +374,7 @@ document.addEventListener("DOMContentLoaded", function () {
409374
}
410375
});
411376
networkAdapter.RoomAPI.setPlayerJoinedCallback(function (joinPlayerId, room) {
412-
var e_6, _a;
377+
var e_5, _a;
413378
if (joinPlayerId == playerId) {
414379
roomId = room.id;
415380
// 自己加入房间
@@ -420,12 +385,12 @@ document.addEventListener("DOMContentLoaded", function () {
420385
createPlayer(player.id);
421386
}
422387
}
423-
catch (e_6_1) { e_6 = { error: e_6_1 }; }
388+
catch (e_5_1) { e_5 = { error: e_5_1 }; }
424389
finally {
425390
try {
426391
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
427392
}
428-
finally { if (e_6) throw e_6.error; }
393+
finally { if (e_5) throw e_5.error; }
429394
}
430395
}
431396
else {
@@ -437,6 +402,7 @@ document.addEventListener("DOMContentLoaded", function () {
437402
if (payload.actions) {
438403
console.log(payload.frame, payload.actions);
439404
}
405+
document.getElementById('frame').textContent = "\u5E27: ".concat(payload.frame);
440406
var snapshot = core.entityManager.createIncrementalStateSnapshot(lastSnapshotVersion);
441407
// 如果有增量数据
442408
if (snapshot.entities.length > 0) {
@@ -448,9 +414,9 @@ document.addEventListener("DOMContentLoaded", function () {
448414
});
449415
networkAdapter.RoomAPI.setSnapshotCallback(function (snapshot) {
450416
console.log("receive snapshot: ".concat(snapshot));
451-
// strategyManager.receiveState(snapshot);
452417
lastSnapshotVersion = snapshot.lastSnapVersion;
453-
core.entityManager.applyIncrementalSnapshot(snapshot.snapshot);
418+
strategyManager.receiveState(snapshot);
419+
// core.entityManager.applyIncrementalSnapshot(snapshot.snapshot);
454420
});
455421
var inputAdapter = new MyInputAdapter(core.entityManager.getInputManager(), networkAdapter);
456422
document.addEventListener("keydown", function (event) {
@@ -470,7 +436,7 @@ function update(timestamp) {
470436
// 请求下一帧
471437
requestAnimationFrame(update);
472438
// 处理状态更新
473-
strategyManager.handleStateUpdate(0.33);
439+
strategyManager.handleStateUpdate(deltaTime);
474440
}
475441
// 开始更新循环
476442
requestAnimationFrame(update);

client-test/index.html

+2
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ <h2>房间信息</h2>
2323
<button id="join-room-btn">加入房间</button>
2424
<button id="leave-room-btn">退出房间</button>
2525

26+
<p id="frame">帧: </p>
2627
<button id="start-frame">房间开始帧同步</button>
2728
<button id="end-frame">房间结束帧同步</button>
2829
</div>
2930

3031
<div id="logger">
3132
<h2>日志信息</h2>
3233
<label for="loggerArea"></label><textarea id="loggerArea"></textarea>
34+
<button id="clear-log">清理日志</button>
3335
</div>
3436

3537
<canvas id="game-canvas" width="400" height="400"></canvas>

client-test/lib/gs.d.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -898,14 +898,24 @@ declare module gs {
898898
handleStateUpdate(deltaTime: number): void;
899899
}
900900
}
901+
declare module gs {
902+
interface InterpolationStrategy {
903+
interpolate(prevSnapshot: any, nextSnapshot: any, progress: number): any;
904+
}
905+
}
906+
declare module gs {
907+
class LinearInterpolationStrategy implements InterpolationStrategy {
908+
interpolate(prevSnapshot: any, nextSnapshot: any, progress: number): any;
909+
}
910+
}
901911
declare module gs {
902912
/**
903913
* 快照插值策略
904914
*/
905915
class SnapshotInterpolationStrategy implements ISyncStrategy {
906916
private snapshotQueue;
907-
private interpolationTime;
908-
onInterpolation: (prevSnapshot: any, nextSnapshot: any, progress: number) => void;
917+
private onInterpolation;
918+
setInterpolationCallback(callback: (prevSnapshot: any, nextSnapshot: any) => void): void;
909919
/**
910920
* 发送游戏状态
911921
* @param state
@@ -917,7 +927,11 @@ declare module gs {
917927
*/
918928
receiveState(state: any): void;
919929
handleStateUpdate(state: any): void;
920-
private interpolateAndUpdateGameState;
930+
}
931+
}
932+
declare module gs {
933+
class SplineInterpolationStrategy implements InterpolationStrategy {
934+
interpolate(prevSnapshot: any, nextSnapshot: any, progress: number): any;
921935
}
922936
}
923937
declare module gs {

client-test/lib/gs.js

+80-16
Original file line numberDiff line numberDiff line change
@@ -2325,15 +2325,50 @@ var gs;
23252325
gs.RoomApi = RoomApi;
23262326
})(gs || (gs = {}));
23272327
var gs;
2328+
(function (gs) {
2329+
var LinearInterpolationStrategy = /** @class */ (function () {
2330+
function LinearInterpolationStrategy() {
2331+
}
2332+
LinearInterpolationStrategy.prototype.interpolate = function (prevSnapshot, nextSnapshot, progress) {
2333+
if (prevSnapshot && nextSnapshot) {
2334+
var interpolatedState = {};
2335+
// 遍历快照中的所有属性
2336+
for (var key in prevSnapshot) {
2337+
if (typeof prevSnapshot[key] === 'object' && typeof nextSnapshot[key] === 'object') {
2338+
// 递归处理嵌套属性
2339+
interpolatedState[key] = this.interpolate(prevSnapshot[key], nextSnapshot[key], progress);
2340+
}
2341+
else if (typeof prevSnapshot[key] === 'number' && typeof nextSnapshot[key] === 'number') {
2342+
interpolatedState[key] = prevSnapshot[key] + (nextSnapshot[key] - prevSnapshot[key]) * progress;
2343+
}
2344+
else {
2345+
// 非数值属性,直接复制
2346+
interpolatedState[key] = nextSnapshot[key];
2347+
}
2348+
}
2349+
return interpolatedState;
2350+
}
2351+
else {
2352+
// 如果无法插值,返回上一个快照
2353+
return prevSnapshot;
2354+
}
2355+
};
2356+
return LinearInterpolationStrategy;
2357+
}());
2358+
gs.LinearInterpolationStrategy = LinearInterpolationStrategy;
2359+
})(gs || (gs = {}));
2360+
var gs;
23282361
(function (gs) {
23292362
/**
23302363
* 快照插值策略
23312364
*/
23322365
var SnapshotInterpolationStrategy = /** @class */ (function () {
23332366
function SnapshotInterpolationStrategy() {
23342367
this.snapshotQueue = [];
2335-
this.interpolationTime = 0;
23362368
}
2369+
SnapshotInterpolationStrategy.prototype.setInterpolationCallback = function (callback) {
2370+
this.onInterpolation = callback;
2371+
};
23372372
/**
23382373
* 发送游戏状态
23392374
* @param state
@@ -2358,29 +2393,58 @@ var gs;
23582393
this.snapshotQueue.shift();
23592394
return;
23602395
}
2361-
var timeManager = gs.TimeManager.getInstance();
2362-
var deltaTime = timeManager.deltaTime * timeManager.timeScale;
2363-
var timeBetweenSnapshots = (nextSnapshot.timestamp - prevSnapshot.timestamp) / 1000; // 将毫秒转换为秒
2364-
this.interpolationTime += deltaTime;
2365-
var interpolationProgress = this.interpolationTime / timeBetweenSnapshots;
2366-
// 确保插值进度在 0 到 1 之间
2367-
interpolationProgress = Math.min(1, interpolationProgress);
2368-
this.interpolateAndUpdateGameState(prevSnapshot, nextSnapshot, interpolationProgress);
2369-
if (this.interpolationTime >= timeBetweenSnapshots) {
2370-
this.snapshotQueue.shift();
2371-
this.interpolationTime = 0; // 重置插值时间
2372-
}
2373-
};
2374-
SnapshotInterpolationStrategy.prototype.interpolateAndUpdateGameState = function (prevSnapshot, nextSnapshot, progress) {
2396+
// 调用用户自定义的插值回调方法
23752397
if (this.onInterpolation) {
2376-
this.onInterpolation(prevSnapshot, nextSnapshot, progress);
2398+
this.onInterpolation(prevSnapshot, nextSnapshot);
23772399
}
2400+
this.snapshotQueue.shift();
23782401
};
23792402
return SnapshotInterpolationStrategy;
23802403
}());
23812404
gs.SnapshotInterpolationStrategy = SnapshotInterpolationStrategy;
23822405
})(gs || (gs = {}));
23832406
var gs;
2407+
(function (gs) {
2408+
var SplineInterpolationStrategy = /** @class */ (function () {
2409+
function SplineInterpolationStrategy() {
2410+
}
2411+
SplineInterpolationStrategy.prototype.interpolate = function (prevSnapshot, nextSnapshot, progress) {
2412+
if (prevSnapshot && nextSnapshot) {
2413+
var interpolatedState = {};
2414+
// 遍历快照中的所有属性
2415+
for (var key in prevSnapshot) {
2416+
if (typeof prevSnapshot[key] === 'object' && typeof nextSnapshot[key] === 'object') {
2417+
// 递归处理嵌套属性
2418+
interpolatedState[key] = this.interpolate(prevSnapshot[key], nextSnapshot[key], progress);
2419+
}
2420+
else if (typeof prevSnapshot[key] === 'number' && typeof nextSnapshot[key] === 'number') {
2421+
// 使用三次样条插值计算插值后的值
2422+
var p0 = prevSnapshot[key];
2423+
var p1 = nextSnapshot[key];
2424+
var t = progress;
2425+
var a = p1 - p0;
2426+
var b = p0;
2427+
var c = (-3 * p0 + 3 * p1 - 2 * a) / t;
2428+
var d = (2 * p0 - 2 * p1 + a) / (t * t);
2429+
interpolatedState[key] = a * t * t * t + b * t * t + c * t + d;
2430+
}
2431+
else {
2432+
// 非数值属性,直接复制
2433+
interpolatedState[key] = nextSnapshot[key];
2434+
}
2435+
}
2436+
return interpolatedState;
2437+
}
2438+
else {
2439+
// 如果无法插值,返回上一个快照
2440+
return prevSnapshot;
2441+
}
2442+
};
2443+
return SplineInterpolationStrategy;
2444+
}());
2445+
gs.SplineInterpolationStrategy = SplineInterpolationStrategy;
2446+
})(gs || (gs = {}));
2447+
var gs;
23842448
(function (gs) {
23852449
/**
23862450
* 状态压缩策略

client-test/lib/gs.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client-test/src/InputSystem.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class InputSystem extends gs.System {
2+
23
entityFilter(entity: gs.Entity): boolean {
34
return entity.hasTag("player");
45
}
@@ -28,16 +29,16 @@ class InputSystem extends gs.System {
2829
if (inputEvent.data.isKeyDown) {
2930
switch (inputEvent.data.key.toLowerCase()) {
3031
case 'w':
31-
velocity.y = -10;
32+
velocity.y = -100;
3233
break;
3334
case 's':
34-
velocity.y = 10;
35+
velocity.y = 100;
3536
break;
3637
case 'a':
37-
velocity.x = -10;
38+
velocity.x = -100;
3839
break;
3940
case 'd':
40-
velocity.x = 10;
41+
velocity.x = 100;
4142
break;
4243
}
4344
}

0 commit comments

Comments
 (0)