refactor: optimize FloatingText

This commit is contained in:
Tsubasa6848
2024-05-28 08:39:32 +08:00
Unverified
parent 8f9447bcdb
commit 32e70d8318
8 changed files with 102 additions and 110 deletions
+27 -40
View File
@@ -120,7 +120,8 @@ const GMLIB_API = {
getDestroyBlockSpeed: ll.import("GMLIB_API", "getDestroyBlockSpeed")
}
const FloatingTextList = [];
const mStaticFloatingTextMap = new Map();
const mDynamicFloatingTextMap = new Map();
class StaticFloatingText {
constructor(pos, text, papi = true) {
@@ -128,7 +129,7 @@ class StaticFloatingText {
this.mText = text;
this.mPlaceholderAPI = papi;
this.mRuntimeId = GMLIB_API.createFloatingText(this.mPosition, this.mText, this.mPlaceholderAPI);
FloatingTextList.push(this);
mStaticFloatingTextMap.set(this.mRuntimeId, this);
}
sendToClient(player) {
@@ -182,32 +183,21 @@ class StaticFloatingText {
}
destroy() {
let size_t = FloatingTextList.indexOf(this);
FloatingTextList.splice(size_t, 1);
mStaticFloatingTextMap.delete(this.mRuntimeId);
return GMLIB_API.deleteFloatingText(this.mRuntimeId);
}
isDynamic() {
return false;
}
static getFloatingText(runtimeId) {
for (const ft of FloatingTextList) {
if (ft.getRuntimeId() == runtimeId) {
if (!ft.isDynamic()) {
return ft;
}
}
if (mStaticFloatingTextMap.has(runtimeId)) {
return mStaticFloatingTextMap.get(runtimeId);
}
return null;
}
static getAllFloatingTexts() {
let result = [];
FloatingTextList.forEach((ft) => {
if (!ft.isDynamic()) {
result.push(ft);
}
mStaticFloatingTextMap.forEach((ft) => {
result.push(ft);
});
return result;
}
@@ -215,9 +205,13 @@ class StaticFloatingText {
class DynamicFloatingText extends StaticFloatingText {
constructor(pos, text, updateRate = 1, papi = true) {
super(pos, text, papi);
this.mPosition = pos;
this.mText = text;
this.mPlaceholderAPI = papi;
this.mRuntimeId = GMLIB_API.createFloatingText(this.mPosition, this.mText, this.mPlaceholderAPI);
this.mUpdateRate = updateRate;
this.mTaskId = null;
mDynamicFloatingTextMap.set(this.mRuntimeId, this);
this.startUpdate();
}
@@ -242,7 +236,7 @@ class DynamicFloatingText extends StaticFloatingText {
}
stopUpdate() {
if (this.mTaskId != null) {
if (this.mTaskId) {
clearInterval(this.mTaskId);
this.mTaskId = null;
return true;
@@ -251,32 +245,21 @@ class DynamicFloatingText extends StaticFloatingText {
}
destroy() {
let size_t = FloatingTextList.indexOf(this);
FloatingTextList.splice(size_t, 1);
mDynamicFloatingTextMap.delete(this.mRuntimeId);
return GMLIB_API.deleteFloatingText(this.mRuntimeId);
}
isDynamic() {
return true;
}
static getFloatingText(runtimeId) {
for (const ft of FloatingTextList) {
if (ft.getRuntimeId() == runtimeId) {
if (ft.isDynamic()) {
return ft;
}
}
if (mDynamicFloatingTextMap.has(runtimeId)) {
return mDynamicFloatingTextMap.get(runtimeId);
}
return null;
}
static getAllFloatingTexts() {
let result = [];
FloatingTextList.forEach((ft) => {
if (ft.isDynamic()) {
result.push(ft);
}
mDynamicFloatingTextMap.forEach((ft) => {
result.push(ft);
});
return result;
}
@@ -838,14 +821,18 @@ class I18nAPI {
}
static get(key, params = [], langCode = undefined) {
let data = [];
params.forEach((param) => {
data.push(param);
});
if (langCode) {
return GMLIB_API.resourcePackTranslate(key, params, langCode);
return GMLIB_API.resourcePackTranslate(key, data, langCode);
}
return GMLIB_API.resourcePackDefaultTranslate(key, params);
return GMLIB_API.resourcePackDefaultTranslate(key, data);
}
static translate(key, params = []) {
return I18nAPI.get(key, params);
static translate(key, params = [], langCode = undefined) {
return I18nAPI.get(key, params, langCode);
}
static getSupportedLanguages() {