From ee0770ec0bb16c1d98c0185ab72a62513a4e125a Mon Sep 17 00:00:00 2001 From: Exund Date: Thu, 10 Sep 2020 00:01:23 +0200 Subject: [PATCH] Better buildings support --- src/js/GeoZ/main.js | 10 +++--- src/js/GeoZ/mod_building.js | 71 +++++++++++++++++++++++-------------- src/js/GeoZ/mod_utils.js | 14 ++++++++ src/js/main.js | 31 ---------------- 4 files changed, 63 insertions(+), 63 deletions(-) diff --git a/src/js/GeoZ/main.js b/src/js/GeoZ/main.js index 9a39daff..4f9804c1 100644 --- a/src/js/GeoZ/main.js +++ b/src/js/GeoZ/main.js @@ -3,6 +3,7 @@ import { ModComponent } from "./mod_component"; import { ModItem } from "./mod_item"; import { ModProcessor } from "./mod_processor"; import { ModSystem, ModSystemWithFilter } from "./mod_system"; +import { keyCodeOf } from "./mod_utils"; import { gComponentRegistry, gItemRegistry, gMetaBuildingRegistry } from "../core/global_registries"; import { GameSystemManager } from "../game/game_system_manager"; import { GameCore } from "../game/core"; @@ -11,6 +12,7 @@ import { registerBuildingVariant } from "../game/building_codes"; import { supportedBuildings } from "../game/hud/parts/buildings_toolbar"; import { KEYMAPPINGS, key } from "../game/key_action_mapper"; import { T } from "../translations"; +import { globalConfig } from "../core/config"; export { MetaModBuilding } from "./mod_building"; export { ModComponent } from "./mod_component"; @@ -211,7 +213,7 @@ export async function initMods() { supportedBuildings.push(building); - KEYMAPPINGS.buildings[base_id] = { keyCode: key(building.getKeybinding()) }; + KEYMAPPINGS.buildings[base_id] = { keyCode: keyCodeOf(building.getKeybinding()), id: base_id }; const translations = building.getTranslations(); @@ -227,11 +229,7 @@ export async function initMods() { logger.log(mod_infos); } - for (const categoryId in KEYMAPPINGS) { - for (const mappingId in KEYMAPPINGS[categoryId]) { - KEYMAPPINGS[categoryId][mappingId].id = mappingId; - } - } + initShapes(); logger.log(`${Mods.length} mods loaded`); } diff --git a/src/js/GeoZ/mod_building.js b/src/js/GeoZ/mod_building.js index 71ad203e..ef255847 100644 --- a/src/js/GeoZ/mod_building.js +++ b/src/js/GeoZ/mod_building.js @@ -6,12 +6,26 @@ import { Loader } from "../core/loader"; /** * @typedef {{ - * url: string - * width: number + * url: string, + * width: number, * height: number * }} ExternalSpriteMeta */ +/** + * @typedef {{ + * normal: ExternalSpriteMeta + * blueprint: ExternalSpriteMeta + * }} SpriteTypesMetas + */ + +/** + * @typedef {{ + * default: Array + * [variant: string]: Array + * }} BuildingSpriteMetas + */ + /** * @typedef {{ * name: string, @@ -21,7 +35,7 @@ import { Loader } from "../core/loader"; /** * @typedef {{ - * variants: {[x: string]: BuildingVariantTranslation, default: BuildingVariantTranslation}, + * variants: {[variant: string]: BuildingVariantTranslation, default: BuildingVariantTranslation}, * keybinding: string * }} BuildingTranlsations */ @@ -46,7 +60,7 @@ export class MetaModBuilding extends MetaBuilding { /** * Returns the building keybinding - * @returns {String} + * @returns {String | number} */ static getKeybinding() { abstract; @@ -54,12 +68,12 @@ export class MetaModBuilding extends MetaBuilding { } /** - * Returns the building translations + * Returns the building translations * @returns {BuildingTranlsations} */ static getTranslations() { abstract; - return {variants: { default: { name: "", description: ""} }, keybinding: ""}; + return { variants: { default: { name: "", description: "" } }, keybinding: "" }; } /** @@ -76,23 +90,30 @@ export class MetaModBuilding extends MetaBuilding { * Returns the sprite for a given variant * @param {number} rotationVariant * @param {string} variant + * @param {keyof BuildingSpriteMetas} type * @returns {AtlasSprite} */ - getSprite(rotationVariant, variant) { + getSprite(rotationVariant, variant, type = "normal") { const sprite_id = - this.id + (variant === defaultBuildingVariant ? "" : "-" + variant) + "-" + rotationVariant; + this.id + + (variant === defaultBuildingVariant ? "" : "-" + variant) + + "-" + + rotationVariant + + "-" + + type; if (this.cachedSprites[sprite_id]) { return this.cachedSprites[sprite_id]; } const sprite = new AtlasSprite(sprite_id); + this.cachedSprites[sprite_id] = sprite; - const meta = this.getSpriteMeta(rotationVariant, variant); - const scales = atlasFiles.map(af => af.meta.scale); - for (const res of scales) { - sprite.linksByResolution[res] = Loader.spriteNotFoundSprite.linksByResolution[res]; - } + const meta = this.getSpriteMetas()[variant][rotationVariant][type]; + const scales = atlasFiles.map(af => af.meta.scale); + for (const res of scales) { + sprite.linksByResolution[res] = Loader.spriteNotFoundSprite.linksByResolution[res]; + } getFileAsDataURI(meta.url).then(data => { const img = document.createElement("img"); @@ -115,24 +136,22 @@ export class MetaModBuilding extends MetaBuilding { }); return sprite; - } - - getBlueprintSprite(rotationVariant = 0, variant = defaultBuildingVariant) { - return this.getSprite(rotationVariant, variant); - } + } - getPreviewSprite(rotationVariant = 0, variant = defaultBuildingVariant) { - return this.getSprite(rotationVariant, variant); - } + getBlueprintSprite(rotationVariant = 0, variant = defaultBuildingVariant) { + return this.getSprite(rotationVariant, variant, "blueprint"); + } + + getPreviewSprite(rotationVariant = 0, variant = defaultBuildingVariant) { + return this.getSprite(rotationVariant, variant); + } /** * Returns the sprite metadata for a given variant - * @param {number} rotationVariant - * @param {string} variant - * @returns {ExternalSpriteMeta} + * @returns {BuildingSpriteMetas} */ - getSpriteMeta(rotationVariant, variant) { + getSpriteMetas() { abstract; - return { url: "", width: 0, height: 0 }; + return null; } } diff --git a/src/js/GeoZ/mod_utils.js b/src/js/GeoZ/mod_utils.js index 1fe30d89..ecc30741 100644 --- a/src/js/GeoZ/mod_utils.js +++ b/src/js/GeoZ/mod_utils.js @@ -13,4 +13,18 @@ export function getFileAsDataURI(url) { reader.readAsDataURL(blob) ; }); }); +} + +/** + * + * @param {number | string} key + */ +export function keyCodeOf(key) { + if (typeof key === "number") { + return key; + } + if (key.match(/F\d+/)) { + return 111 + +key.slice(1); + } + return key.toUpperCase().charCodeAt(0); } \ No newline at end of file diff --git a/src/js/main.js b/src/js/main.js index 48f3ff26..48393e2b 100644 --- a/src/js/main.js +++ b/src/js/main.js @@ -19,37 +19,6 @@ if (window.coreThreadLoadedCb) { window.coreThreadLoadedCb(); } -// Logrocket -// if (!G_IS_DEV && !G_IS_STANDALONE) { -// const monthlyUsers = 300; // thousand -// const logrocketLimit = 10; // thousand -// const percentageOfUsers = logrocketLimit / monthlyUsers; - -// if (Math.random() <= percentageOfUsers) { -// logger.log("Analyzing this session with logrocket"); -// const logrocket = require("logrocket"); -// logrocket.init("p1x9zh/shapezio"); - -// try { -// logrocket.getSessionURL(function (sessionURL) { -// logger.log("Connected lockrocket to GA"); -// // @ts-ignore -// try { -// window.ga("send", { -// hitType: "event", -// eventCategory: "LogRocket", -// eventAction: sessionURL, -// }); -// } catch (ex) { -// logger.warn("Logrocket connection to analytics failed:", ex); -// } -// }); -// } catch (ex) { -// logger.warn("Logrocket connection to analytics failed:", ex); -// } -// } -// } - console.log( `%cshapez.io ️%c\n© 2020 Tobias Springer IT Solutions\nCommit %c${G_BUILD_COMMIT_HASH}%c on %c${new Date( G_BUILD_TIME