Skip to content

Commit

Permalink
Merge branch 'master' into feat-add-3d-map-editor-to-threejs
Browse files Browse the repository at this point in the history
  • Loading branch information
moe-moe-pupil committed Jun 20, 2024
2 parents 719178d + 3e10900 commit 5ca52b8
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 64 deletions.
1 change: 1 addition & 0 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ const Client = TaroEventingClass.extend({
});

//go fetch
taro.addComponent(GameTextComponent);
taro.addComponent(GameComponent);
taro.addComponent(ProfilerComponent);
taro.addComponent(MenuUiComponent);
Expand Down
74 changes: 14 additions & 60 deletions src/gameClasses/ClientNetworkEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,56 +209,11 @@ var ClientNetworkEvents = {
},

_onUpdateUiTextForTime: function (data) {
$(`.ui-text-${data.target}`).show();
$(`.ui-text-${data.target}`).html(taro.clientSanitizer(data.value));

if (!this.textTimerData) {
this.textTimerData = {};
}

// stop the timeout and remove old textTimerData
if (this.textTimerData[data.target]?.textTimer) {
clearTimeout(this.textTimerData[data.target].textTimer);
delete this.textTimerData[data.target];
}

if (data.time && data.time > 0) {
this.textTimerData[data.target] = {
target: data.target,
textTimer: setTimeout(() => {
$(`.ui-text-${this.textTimerData[data.target].target}`).hide();
delete this.textTimerData[data.target];
}, data.time),
};
}
taro.gameText.updateUiTextForTime(data);
},

_onUpdateUiText: function (data) {
const runAction = functionalTryCatch(() => {
if (!data.target) {
return;
}
const key = `ui-text-${data.target}-id`;

if (!taro.uiTextElementsObj[key]) {
taro.uiTextElementsObj[key] = document.getElementById(key);
}
if (!taro.uiTextElementsObj[key]) {
return;
}

if (data.action == 'show') {
// $(`.ui-text-${data.target}`).show();
taro.uiTextElementsObj[key].style.display = 'block';
} else if (data.action == 'hide') {
taro.uiTextElementsObj[key].style.display = 'none';
} else {
taro.uiTextElementsObj[key].innerHTML = data.value;
}
});
if (runAction[0] !== null) {
// console.error(runAction[0]);
}
taro.gameText.updateUiText(data);
},

_onUpdateUIRealtimeCSS: function (data) {
Expand Down Expand Up @@ -388,17 +343,19 @@ var ClientNetworkEvents = {
},

// when client receives a ping response back from the server
_onPing: function(data) {
_onPing: function (data) {
const self = this;
const now = taro._currentTime;
const latency = now - data.sentAt;

// console.log("onPing", taro._currentTime, data.sentAt, latency);

// start reconciliation based on discrepancy between
// where my unit when ping was sent and where unit is when ping is received
if (taro.client.selectedUnit?.posHistory &&
taro.client.myUnitStreamedPosition && !taro.client.selectedUnit.isTeleporting
if (
taro.client.selectedUnit?.posHistory &&
taro.client.myUnitStreamedPosition &&
!taro.client.selectedUnit.isTeleporting
) {
let history = taro.client.selectedUnit.posHistory;
while (history.length > 0) {
Expand All @@ -408,20 +365,19 @@ var ClientNetworkEvents = {
// console.log("found it!")
taro.client.myUnitPositionWhenPingSent = {
x: historyFrame[1][0],
y: historyFrame[1][1]
y: historyFrame[1][1],
};

taro.client.selectedUnit.reconRemaining = {
x: taro.client.myUnitStreamedPosition.x - taro.client.myUnitPositionWhenPingSent.x,
y: taro.client.myUnitStreamedPosition.y - taro.client.myUnitPositionWhenPingSent.y
}
y: taro.client.myUnitStreamedPosition.y - taro.client.myUnitPositionWhenPingSent.y,
};

// console.log(latency, taro.client.myUnitPositionWhenPingSent.y, taro.client.myUnitStreamedPosition.y, taro.client.selectedUnit.reconRemaining.y);

taro.client.selectedUnit.posHistory = []
taro.client.selectedUnit.posHistory = [];

if (taro.env === 'local' || taro.debugCSP) {

taro.client.selectedUnit.emit('transform-debug', {
debug: 'red-square',
x: taro.client.myUnitPositionWhenPingSent?.x,
Expand All @@ -440,18 +396,16 @@ var ClientNetworkEvents = {

taro.client.sendNextPingAt = taro.now + 100;
break;

}
}
}

taro.pingElement = taro.pingElement || document.getElementById('updateping');
taro.pingElement.innerHTML = Math.floor(latency);
taro.pingLatency = taro.pingLatency || [];
taro.pingLatency.push(Math.floor(latency));
},


_onPlayAd: function (data) {
const runAction = functionalTryCatch(() => {
taro.ad.play(data);
Expand Down
65 changes: 61 additions & 4 deletions src/gameClasses/components/ui/GameTextComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,24 @@ var GameTextComponent = TaroEntity.extend({
},

updateText: function (data, clientId) {
this.latestText[data.target] = data.value;

if (taro.isServer) {
this.latestText[data.target] = data.value;
data.value = taro.sanitizer(data.value);
taro.network.send('updateUiText', data, clientId); // update text for all clients
} else {
data.value = taro.clientSanitizer(data.value);
this.updateUiText(data);
}
},

updateTextForTime: function (data, clientId) {
this.latestText[data.target] = data.value;

if (taro.isServer) {
this.latestText[data.target] = data.value;
data.value = taro.sanitizer(data.value);
taro.network.send('updateUiTextForTime', data, clientId); // update text for all clients
} else {
data.value = taro.clientSanitizer(data.value);
this.updateUiTextForTime(data);
}
},

Expand All @@ -48,6 +52,59 @@ var GameTextComponent = TaroEntity.extend({
}
},

updateUiText: function (data) {
const runAction = functionalTryCatch(() => {
if (!data.target) {
return;
}
const key = `ui-text-${data.target}-id`;

if (!taro.uiTextElementsObj[key]) {
taro.uiTextElementsObj[key] = document.getElementById(key);
}
if (!taro.uiTextElementsObj[key]) {
return;
}

if (data.action == 'show') {
// $(`.ui-text-${data.target}`).show();
taro.uiTextElementsObj[key].style.display = 'block';
} else if (data.action == 'hide') {
taro.uiTextElementsObj[key].style.display = 'none';
} else {
taro.uiTextElementsObj[key].innerHTML = data.value;
}
});
if (runAction[0] !== null) {
// console.error(runAction[0]);
}
},

updateUiTextForTime: function (data) {
$(`.ui-text-${data.target}`).show();
$(`.ui-text-${data.target}`).html(taro.clientSanitizer(data.value));

if (!this.textTimerData) {
this.textTimerData = {};
}

// stop the timeout and remove old textTimerData
if (this.textTimerData[data.target]?.textTimer) {
clearTimeout(this.textTimerData[data.target].textTimer);
delete this.textTimerData[data.target];
}

if (data.time && data.time > 0) {
this.textTimerData[data.target] = {
target: data.target,
textTimer: setTimeout(() => {
$(`.ui-text-${this.textTimerData[data.target].target}`).hide();
delete this.textTimerData[data.target];
}, data.time),
};
}
},

// showKillStreakMessage: function(playerName, killStreakCount) {

// var message = "";
Expand Down

0 comments on commit 5ca52b8

Please sign in to comment.