1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-12-12 01:31:58 +00:00

Merge branch 'master' into patch-2

This commit is contained in:
EmeraldBlock 2020-09-28 11:11:29 -05:00 committed by GitHub
commit 602f61acde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 197 additions and 200 deletions

View File

@ -266,9 +266,10 @@ export function findNiceIntegerValue(num) {
* Formats a big number * Formats a big number
* @param {number} num * @param {number} num
* @param {string=} separator The decimal separator for numbers like 50.1 (separator='.') * @param {string=} separator The decimal separator for numbers like 50.1 (separator='.')
* @param {number=} number of significant figures to include (for values >= 1000)
* @returns {string} * @returns {string}
*/ */
export function formatBigNumber(num, separator = T.global.decimalSeparator) { export function formatBigNumber(num, separator = T.global.decimalSeparator, precision = 3) {
const sign = num < 0 ? "-" : ""; const sign = num < 0 ? "-" : "";
num = Math.abs(num); num = Math.abs(num);
@ -286,23 +287,19 @@ export function formatBigNumber(num, separator = T.global.decimalSeparator) {
if (num < 1000) { if (num < 1000) {
return sign + "" + num; return sign + "" + num;
} else {
let leadingDigits = num;
let suffix = "";
for (let suffixIndex = 0; suffixIndex < bigNumberSuffixTranslationKeys.length; ++suffixIndex) {
leadingDigits = leadingDigits / 1000;
suffix = T.global.suffix[bigNumberSuffixTranslationKeys[suffixIndex]];
if (leadingDigits < 1000) {
break;
}
}
const leadingDigitsRounded = round1Digit(leadingDigits);
const leadingDigitsNoTrailingDecimal = leadingDigitsRounded
.toString()
.replace(".0", "")
.replace(".", separator);
return sign + leadingDigitsNoTrailingDecimal + suffix;
} }
let leadingDigits = num;
let suffixIndex = 0;
for (; suffixIndex < bigNumberSuffixTranslationKeys.length; ++suffixIndex) {
leadingDigits = leadingDigits / 1000;
if (leadingDigits < 1000) {
break;
}
}
const suffix = T.global.suffix[bigNumberSuffixTranslationKeys[suffixIndex]];
const leadingDigitsRounded = Number(leadingDigits.toPrecision(precision));
const leadingDigitsNoTrailingDecimal = leadingDigitsRounded.toString().replace(".", separator);
return sign + leadingDigitsNoTrailingDecimal + suffix;
} }
/** /**

View File

@ -1,183 +1,183 @@
import { globalConfig } from "../../core/config"; import { globalConfig } from "../../core/config";
import { smoothenDpi } from "../../core/dpi_manager"; import { smoothenDpi } from "../../core/dpi_manager";
import { DrawParameters } from "../../core/draw_parameters"; import { DrawParameters } from "../../core/draw_parameters";
import { drawSpriteClipped } from "../../core/draw_utils"; import { drawSpriteClipped } from "../../core/draw_utils";
import { Loader } from "../../core/loader"; import { Loader } from "../../core/loader";
import { Rectangle } from "../../core/rectangle"; import { Rectangle } from "../../core/rectangle";
import { ORIGINAL_SPRITE_SCALE } from "../../core/sprites"; import { ORIGINAL_SPRITE_SCALE } from "../../core/sprites";
import { formatBigNumber } from "../../core/utils"; import { formatBigNumber } from "../../core/utils";
import { T } from "../../translations"; import { T } from "../../translations";
import { HubComponent } from "../components/hub"; import { HubComponent } from "../components/hub";
import { Entity } from "../entity"; import { Entity } from "../entity";
import { GameSystemWithFilter } from "../game_system_with_filter"; import { GameSystemWithFilter } from "../game_system_with_filter";
const HUB_SIZE_TILES = 4; const HUB_SIZE_TILES = 4;
const HUB_SIZE_PIXELS = HUB_SIZE_TILES * globalConfig.tileSize; const HUB_SIZE_PIXELS = HUB_SIZE_TILES * globalConfig.tileSize;
export class HubSystem extends GameSystemWithFilter { export class HubSystem extends GameSystemWithFilter {
constructor(root) { constructor(root) {
super(root, [HubComponent]); super(root, [HubComponent]);
this.hubSprite = Loader.getSprite("sprites/buildings/hub.png"); this.hubSprite = Loader.getSprite("sprites/buildings/hub.png");
} }
/** /**
* @param {DrawParameters} parameters * @param {DrawParameters} parameters
*/ */
draw(parameters) { draw(parameters) {
for (let i = 0; i < this.allEntities.length; ++i) { for (let i = 0; i < this.allEntities.length; ++i) {
this.drawEntity(parameters, this.allEntities[i]); this.drawEntity(parameters, this.allEntities[i]);
} }
} }
update() { update() {
for (let i = 0; i < this.allEntities.length; ++i) { for (let i = 0; i < this.allEntities.length; ++i) {
// Set hub goal // Set hub goal
const entity = this.allEntities[i]; const entity = this.allEntities[i];
const pinsComp = entity.components.WiredPins; const pinsComp = entity.components.WiredPins;
pinsComp.slots[0].value = this.root.shapeDefinitionMgr.getShapeItemFromDefinition( pinsComp.slots[0].value = this.root.shapeDefinitionMgr.getShapeItemFromDefinition(
this.root.hubGoals.currentGoal.definition this.root.hubGoals.currentGoal.definition
); );
} }
} }
/** /**
* *
* @param {HTMLCanvasElement} canvas * @param {HTMLCanvasElement} canvas
* @param {CanvasRenderingContext2D} context * @param {CanvasRenderingContext2D} context
* @param {number} w * @param {number} w
* @param {number} h * @param {number} h
* @param {number} dpi * @param {number} dpi
*/ */
redrawHubBaseTexture(canvas, context, w, h, dpi) { redrawHubBaseTexture(canvas, context, w, h, dpi) {
// This method is quite ugly, please ignore it! // This method is quite ugly, please ignore it!
context.scale(dpi, dpi); context.scale(dpi, dpi);
const parameters = new DrawParameters({ const parameters = new DrawParameters({
context, context,
visibleRect: new Rectangle(0, 0, w, h), visibleRect: new Rectangle(0, 0, w, h),
desiredAtlasScale: ORIGINAL_SPRITE_SCALE, desiredAtlasScale: ORIGINAL_SPRITE_SCALE,
zoomLevel: dpi * 0.75, zoomLevel: dpi * 0.75,
root: this.root, root: this.root,
}); });
context.clearRect(0, 0, w, h); context.clearRect(0, 0, w, h);
this.hubSprite.draw(context, 0, 0, w, h); this.hubSprite.draw(context, 0, 0, w, h);
const definition = this.root.hubGoals.currentGoal.definition; const definition = this.root.hubGoals.currentGoal.definition;
definition.drawCentered(45, 58, parameters, 36); definition.drawCentered(45, 58, parameters, 36);
const goals = this.root.hubGoals.currentGoal; const goals = this.root.hubGoals.currentGoal;
const textOffsetX = 70; const textOffsetX = 70;
const textOffsetY = 61; const textOffsetY = 61;
if (goals.throughputOnly) { if (goals.throughputOnly) {
// Throughput // Throughput
const deliveredText = T.ingame.statistics.shapesDisplayUnits.second.replace( const deliveredText = T.ingame.statistics.shapesDisplayUnits.second.replace(
"<shapes>", "<shapes>",
formatBigNumber(goals.required) formatBigNumber(goals.required)
); );
context.font = "bold 12px GameFont"; context.font = "bold 12px GameFont";
context.fillStyle = "#64666e"; context.fillStyle = "#64666e";
context.textAlign = "left"; context.textAlign = "left";
context.fillText(deliveredText, textOffsetX, textOffsetY); context.fillText(deliveredText, textOffsetX, textOffsetY);
} else { } else {
// Deliver count // Deliver count
const delivered = this.root.hubGoals.getCurrentGoalDelivered(); const delivered = this.root.hubGoals.getCurrentGoalDelivered();
const deliveredText = "" + formatBigNumber(delivered); const deliveredText = "" + formatBigNumber(delivered);
if (delivered > 999) { if (delivered > 999) {
context.font = "bold 16px GameFont"; context.font = "bold 16px GameFont";
} else { } else {
context.font = "bold 25px GameFont"; context.font = "bold 25px GameFont";
} }
context.fillStyle = "#64666e"; context.fillStyle = "#64666e";
context.textAlign = "left"; context.textAlign = "left";
context.fillText(deliveredText, textOffsetX, textOffsetY); context.fillText(deliveredText, textOffsetX, textOffsetY);
// Required // Required
context.font = "13px GameFont"; context.font = "13px GameFont";
context.fillStyle = "#a4a6b0"; context.fillStyle = "#a4a6b0";
context.fillText("/ " + formatBigNumber(goals.required), textOffsetX, textOffsetY + 13); context.fillText("/ " + formatBigNumber(goals.required), textOffsetX, textOffsetY + 13);
} }
// Reward // Reward
const rewardText = T.storyRewards[goals.reward].title.toUpperCase(); const rewardText = T.storyRewards[goals.reward].title.toUpperCase();
if (rewardText.length > 12) { if (rewardText.length > 12) {
context.font = "bold 8px GameFont"; context.font = "bold 8px GameFont";
} else { } else {
context.font = "bold 10px GameFont"; context.font = "bold 10px GameFont";
} }
context.fillStyle = "#fd0752"; context.fillStyle = "#fd0752";
context.textAlign = "center"; context.textAlign = "center";
context.fillText(rewardText, HUB_SIZE_PIXELS / 2, 105); context.fillText(rewardText, HUB_SIZE_PIXELS / 2, 105);
// Level "8" // Level "8"
context.font = "bold 10px GameFont"; context.font = "bold 10px GameFont";
context.fillStyle = "#fff"; context.fillStyle = "#fff";
context.fillText("" + this.root.hubGoals.level, 27, 32); context.fillText("" + this.root.hubGoals.level, 27, 32);
// "LVL" // "LVL"
context.textAlign = "center"; context.textAlign = "center";
context.fillStyle = "#fff"; context.fillStyle = "#fff";
context.font = "bold 6px GameFont"; context.font = "bold 6px GameFont";
context.fillText(T.buildings.hub.levelShortcut, 27, 22); context.fillText(T.buildings.hub.levelShortcut, 27, 22);
// "Deliver" // "Deliver"
context.fillStyle = "#64666e"; context.fillStyle = "#64666e";
context.font = "bold 10px GameFont"; context.font = "bold 10px GameFont";
context.fillText(T.buildings.hub.deliver.toUpperCase(), HUB_SIZE_PIXELS / 2, 30); context.fillText(T.buildings.hub.deliver.toUpperCase(), HUB_SIZE_PIXELS / 2, 30);
// "To unlock" // "To unlock"
const unlockText = T.buildings.hub.toUnlock.toUpperCase(); const unlockText = T.buildings.hub.toUnlock.toUpperCase();
if (unlockText.length > 15) { if (unlockText.length > 15) {
context.font = "bold 8px GameFont"; context.font = "bold 8px GameFont";
} else { } else {
context.font = "bold 10px GameFont"; context.font = "bold 10px GameFont";
} }
context.fillText(T.buildings.hub.toUnlock.toUpperCase(), HUB_SIZE_PIXELS / 2, 92); context.fillText(T.buildings.hub.toUnlock.toUpperCase(), HUB_SIZE_PIXELS / 2, 92);
context.textAlign = "left"; context.textAlign = "left";
} }
/** /**
* @param {DrawParameters} parameters * @param {DrawParameters} parameters
* @param {Entity} entity * @param {Entity} entity
*/ */
drawEntity(parameters, entity) { drawEntity(parameters, entity) {
const staticComp = entity.components.StaticMapEntity; const staticComp = entity.components.StaticMapEntity;
if (!staticComp.shouldBeDrawn(parameters)) { if (!staticComp.shouldBeDrawn(parameters)) {
return; return;
} }
// Deliver count // Deliver count
const delivered = this.root.hubGoals.getCurrentGoalDelivered(); const delivered = this.root.hubGoals.getCurrentGoalDelivered();
const deliveredText = "" + formatBigNumber(delivered); const deliveredText = "" + formatBigNumber(delivered);
const dpi = smoothenDpi(globalConfig.shapesSharpness * parameters.zoomLevel); const dpi = smoothenDpi(globalConfig.shapesSharpness * parameters.zoomLevel);
const canvas = parameters.root.buffers.getForKey({ const canvas = parameters.root.buffers.getForKey({
key: "hub", key: "hub",
subKey: dpi + "/" + this.root.hubGoals.level + "/" + deliveredText, subKey: dpi + "/" + this.root.hubGoals.level + "/" + deliveredText,
w: globalConfig.tileSize * 4, w: globalConfig.tileSize * 4,
h: globalConfig.tileSize * 4, h: globalConfig.tileSize * 4,
dpi, dpi,
redrawMethod: this.redrawHubBaseTexture.bind(this), redrawMethod: this.redrawHubBaseTexture.bind(this),
}); });
const extrude = 8; const extrude = 8;
drawSpriteClipped({ drawSpriteClipped({
parameters, parameters,
sprite: canvas, sprite: canvas,
x: staticComp.origin.x * globalConfig.tileSize - extrude, x: staticComp.origin.x * globalConfig.tileSize - extrude,
y: staticComp.origin.y * globalConfig.tileSize - extrude, y: staticComp.origin.y * globalConfig.tileSize - extrude,
w: HUB_SIZE_PIXELS + 2 * extrude, w: HUB_SIZE_PIXELS + 2 * extrude,
h: HUB_SIZE_PIXELS + 2 * extrude, h: HUB_SIZE_PIXELS + 2 * extrude,
originalW: HUB_SIZE_PIXELS * dpi, originalW: HUB_SIZE_PIXELS * dpi,
originalH: HUB_SIZE_PIXELS * dpi, originalH: HUB_SIZE_PIXELS * dpi,
}); });
} }
} }