1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-06-13 13:04:03 +00:00

Replace enumColors with string literals

This commit is contained in:
Bjorn Stromberg 2020-08-17 17:44:20 +09:00
parent 1ff76e0b2e
commit 0a5cfbf84c
11 changed files with 247 additions and 194 deletions

View File

@ -1,163 +1,206 @@
/** @enum {string} */ /** @typedef {"r" | "g" | "b" | "y" | "p" | "c" | "w" | "u" | "0"} ColorShortcode **/
export const enumColors = {
red: "red",
green: "green",
blue: "blue",
yellow: "yellow", /** @type {Color[]} **/
purple: "purple", export const colors = ["red", "green", "blue", "yellow", "purple", "cyan", "white", "uncolored", "black"];
cyan: "cyan",
white: "white", /** @type {ColorShortcode[]} **/
uncolored: "uncolored", export const colorShortcodes = ["r", "g", "b", "y", "p", "c", "w", "u", "0"];
};
/** @enum {string} */ /**
export const enumColorToShortcode = { * @param {unknown} value
[enumColors.red]: "r", * @returns {value is Color}
[enumColors.green]: "g", **/
[enumColors.blue]: "b", export function isColor(value) {
return colors.includes(/** @type {Color} **/ (value));
[enumColors.yellow]: "y",
[enumColors.purple]: "p",
[enumColors.cyan]: "c",
[enumColors.white]: "w",
[enumColors.uncolored]: "u",
};
/** @enum {enumColors} */
export const enumShortcodeToColor = {};
for (const key in enumColorToShortcode) {
enumShortcodeToColor[enumColorToShortcode[key]] = key;
} }
/** @enum {string} */ /** @type {Record<Color, ColorShortcode>} **/
export const enumColorsToHexCode = { export const colorShortcodeMap = {
[enumColors.red]: "#ff666a", red: "r",
[enumColors.green]: "#78ff66", green: "g",
[enumColors.blue]: "#66a7ff", blue: "b",
yellow: "y",
// red + green purple: "p",
[enumColors.yellow]: "#fcf52a", cyan: "c",
white: "w",
// red + blue uncolored: "u",
[enumColors.purple]: "#dd66ff", black: "0",
// blue + green
[enumColors.cyan]: "#00fcff",
// blue + green + red
[enumColors.white]: "#ffffff",
[enumColors.uncolored]: "#aaaaaa",
}; };
const c = enumColors; /** @type {Record<ColorShortcode, Color>} **/
/** @enum {Object.<string, string>} */ export const shortcodeColorMap = {
export const enumColorMixingResults = { "r": "red",
"g": "green",
"b": "blue",
"y": "yellow",
"p": "purple",
"c": "cyan",
"w": "white",
"u": "uncolored",
"0": "black",
};
/** @type {Record<Color, HexColor>} **/
export const colorHexColorMap = {
red: "#ff666a",
green: "#78ff66",
blue: "#66a7ff",
yellow: "#fcf52a",
purple: "#dd66ff",
cyan: "#00fcff",
white: "#ffffff",
uncolored: "#aaaaaa",
black: "#31383a",
};
/** @type {Record<Color, Record<Color, Color>>} **/
export const colorMixingMap = {
// 255, 0, 0 // 255, 0, 0
[c.red]: { red: {
[c.green]: c.yellow, red: "red",
[c.blue]: c.purple, green: "yellow",
blue: "purple",
[c.yellow]: c.yellow, yellow: "yellow",
[c.purple]: c.purple, purple: "purple",
[c.cyan]: c.white, cyan: "white",
white: "white",
[c.white]: c.white, uncolored: "red",
black: "red",
}, },
// 0, 255, 0 // 0, 255, 0
[c.green]: { green: {
[c.blue]: c.cyan, red: "yellow",
blue: "cyan",
[c.yellow]: c.yellow, green: "green",
[c.purple]: c.white, yellow: "yellow",
[c.cyan]: c.cyan, purple: "white",
cyan: "cyan",
[c.white]: c.white, white: "white",
uncolored: "green",
black: "green",
}, },
// 0, 255, 0 // 0, 255, 0
[c.blue]: { blue: {
[c.yellow]: c.white, red: "purple",
[c.purple]: c.purple, green: "cyan",
[c.cyan]: c.cyan, blue: "blue",
yellow: "white",
[c.white]: c.white, purple: "purple",
cyan: "cyan",
white: "white",
uncolored: "blue",
black: "blue",
}, },
// 255, 255, 0 // 255, 255, 0
[c.yellow]: { yellow: {
[c.purple]: c.white, red: "yellow",
[c.cyan]: c.white, green: "yellow",
blue: "white",
purple: "white",
cyan: "white",
yellow: "yellow",
white: "white",
uncolored: "yellow",
black: "yellow",
}, },
// 255, 0, 255 // 255, 0, 255
[c.purple]: { purple: {
[c.cyan]: c.white, red: "purple",
green: "white",
blue: "purple",
cyan: "white",
yellow: "white",
purple: "purple",
white: "white",
uncolored: "purple",
black: "purple",
}, },
// 0, 255, 255 // 0, 255, 255
[c.cyan]: {}, cyan: {
red: "white",
green: "cyan",
blue: "cyan",
cyan: "cyan",
yellow: "white",
purple: "white",
white: "white",
uncolored: "cyan",
black: "cyan",
},
//// SPECIAL COLORS //// SPECIAL COLORS
// 255, 255, 255 // 255, 255, 255
[c.white]: { white: {
// auto red: "white",
green: "white",
blue: "white",
cyan: "white",
yellow: "white",
purple: "white",
white: "white",
uncolored: "white",
black: "uncolored",
}, },
// X, X, X // X, X, X
[c.uncolored]: { uncolored: {
// auto red: "red",
green: "green",
blue: "blue",
cyan: "cyan",
yellow: "yellow",
purple: "purple",
white: "white",
uncolored: "uncolored",
black: "black",
},
black: {
red: "red",
green: "green",
blue: "blue",
cyan: "cyan",
yellow: "yellow",
purple: "purple",
white: "uncolored",
uncolored: "black",
black: "black",
}, },
}; };
// Create same color lookups for (const colorA in colorMixingMap) {
for (const color in enumColors) { for (const colorB in colorMixingMap[colorA]) {
enumColorMixingResults[color][color] = color; const resultColor = colorMixingMap[colorA][colorB];
enumColorMixingResults[color][c.white] = c.white; const existingResult = colorMixingMap[colorB][colorA];
// Anything with uncolored is the same color if (existingResult && existingResult !== resultColor) {
enumColorMixingResults[color][c.uncolored] = color; assertAlways(
} false,
"invalid color mixing configuration, " +
// Create reverse lookup and check color mixing lookups colorA +
for (const colorA in enumColorMixingResults) { " + " +
for (const colorB in enumColorMixingResults[colorA]) { colorB +
const resultColor = enumColorMixingResults[colorA][colorB]; " is " +
if (!enumColorMixingResults[colorB]) { resultColor +
enumColorMixingResults[colorB] = { " but " +
[colorA]: resultColor, colorB +
}; " + " +
} else { colorA +
const existingResult = enumColorMixingResults[colorB][colorA]; " is " +
if (existingResult && existingResult !== resultColor) { existingResult
assertAlways( );
false,
"invalid color mixing configuration, " +
colorA +
" + " +
colorB +
" is " +
resultColor +
" but " +
colorB +
" + " +
colorA +
" is " +
existingResult
);
}
enumColorMixingResults[colorB][colorA] = resultColor;
} }
} }
} }
for (const colorA in enumColorMixingResults) { for (const colorA in colorMixingMap) {
for (const colorB in enumColorMixingResults) { for (const colorB in colorMixingMap) {
if (!enumColorMixingResults[colorA][colorB]) { if (!colorMixingMap[colorA][colorB]) {
assertAlways(false, "Color mixing of", colorA, "with", colorB, "is not defined"); assertAlways(false, "Color mixing of", colorA, "with", colorB, "is not defined");
} }
} }

View File

@ -1,7 +1,7 @@
import { globalConfig } from "../core/config"; import { globalConfig } from "../core/config";
import { clamp, findNiceIntegerValue, randomChoice, randomInt } from "../core/utils"; import { clamp, findNiceIntegerValue, randomChoice, randomInt } from "../core/utils";
import { BasicSerializableObject, types } from "../savegame/serialization"; import { BasicSerializableObject, types } from "../savegame/serialization";
import { enumColors } from "./colors"; import { colors } from "./colors";
import { enumItemProcessorTypes } from "./components/item_processor"; import { enumItemProcessorTypes } from "./components/item_processor";
import { GameRoot } from "./root"; import { GameRoot } from "./root";
import { enumSubShape, ShapeDefinition } from "./shape_definition"; import { enumSubShape, ShapeDefinition } from "./shape_definition";
@ -327,7 +327,7 @@ export class HubGoals extends BasicSerializableObject {
/** @type {Array<import("./shape_definition").ShapeLayer>} */ /** @type {Array<import("./shape_definition").ShapeLayer>} */
let layers = []; let layers = [];
const randomColor = () => randomChoice(Object.values(enumColors)); const randomColor = () => randomChoice(colors);
const randomShape = () => randomChoice(Object.values(enumSubShape)); const randomShape = () => randomChoice(Object.values(enumSubShape));
let anyIsMissingTwo = false; let anyIsMissingTwo = false;

View File

@ -1,7 +1,6 @@
import { BaseHUDPart } from "../base_hud_part"; import { BaseHUDPart } from "../base_hud_part";
import { makeDiv } from "../../../core/utils"; import { makeDiv } from "../../../core/utils";
import { TrackedState } from "../../../core/tracked_state"; import { TrackedState } from "../../../core/tracked_state";
import { enumColors } from "../../colors";
import { ColorItem } from "../../items/color_item"; import { ColorItem } from "../../items/color_item";
import { DrawParameters } from "../../../core/draw_parameters"; import { DrawParameters } from "../../../core/draw_parameters";
import { THEME } from "../../theme"; import { THEME } from "../../theme";
@ -19,7 +18,7 @@ export class HUDColorBlindHelper extends BaseHUDPart {
/** /**
* Called when the color below the current tile changed * Called when the color below the current tile changed
* @param {enumColors|null} color * @param {Color|null} color
*/ */
onColorBelowTileChanged(color) { onColorBelowTileChanged(color) {
this.belowTileIndicator.classList.toggle("visible", !!color); this.belowTileIndicator.classList.toggle("visible", !!color);
@ -30,7 +29,7 @@ export class HUDColorBlindHelper extends BaseHUDPart {
/** /**
* Computes the color below the current tile * Computes the color below the current tile
* @returns {enumColors} * @returns {Color}
*/ */
computeColorBelowTile() { computeColorBelowTile() {
const mousePosition = this.root.app.mousePosition; const mousePosition = this.root.app.mousePosition;

View File

@ -3,7 +3,7 @@ import { smoothenDpi } from "../../core/dpi_manager";
import { DrawParameters } from "../../core/draw_parameters"; import { DrawParameters } from "../../core/draw_parameters";
import { types } from "../../savegame/serialization"; import { types } from "../../savegame/serialization";
import { BaseItem } from "../base_item"; import { BaseItem } from "../base_item";
import { enumColors, enumColorsToHexCode } from "../colors"; import { colors, colorHexColorMap } from "../colors";
import { THEME } from "../theme"; import { THEME } from "../theme";
import { drawSpriteClipped } from "../../core/draw_utils"; import { drawSpriteClipped } from "../../core/draw_utils";
@ -13,7 +13,7 @@ export class ColorItem extends BaseItem {
} }
static getSchema() { static getSchema() {
return types.enum(enumColors); return types.enum(colors);
} }
serialize() { serialize() {
@ -37,7 +37,7 @@ export class ColorItem extends BaseItem {
} }
/** /**
* @param {enumColors} color * @param {Color} color
*/ */
constructor(color) { constructor(color) {
super(); super();
@ -45,6 +45,9 @@ export class ColorItem extends BaseItem {
this.bufferGenerator = null; this.bufferGenerator = null;
} }
/**
* @returns {HexColor}
*/
getBackgroundColorAsResource() { getBackgroundColorAsResource() {
return THEME.map.resources[this.color]; return THEME.map.resources[this.color];
} }
@ -95,7 +98,7 @@ export class ColorItem extends BaseItem {
context.translate((w * dpi) / 2, (h * dpi) / 2); context.translate((w * dpi) / 2, (h * dpi) / 2);
context.scale((dpi * w) / 12, (dpi * h) / 12); context.scale((dpi * w) / 12, (dpi * h) / 12);
context.fillStyle = enumColorsToHexCode[this.color]; context.fillStyle = colorHexColorMap[this.color];
context.strokeStyle = THEME.items.outline; context.strokeStyle = THEME.items.outline;
context.lineWidth = 2 * THEME.items.outlineWidth; context.lineWidth = 2 * THEME.items.outlineWidth;
context.beginCircle(2, -1, 3); context.beginCircle(2, -1, 3);
@ -111,12 +114,14 @@ export class ColorItem extends BaseItem {
} }
} }
/** export const COLOR_ITEM_SINGLETONS = {
* Singleton instances red: new ColorItem("red"),
* @type {Object<enumColors, ColorItem>} green: new ColorItem("green"),
*/ blue: new ColorItem("blue"),
export const COLOR_ITEM_SINGLETONS = {}; yellow: new ColorItem("yellow"),
purple: new ColorItem("purple"),
for (const color in enumColors) { cyan: new ColorItem("cyan"),
COLOR_ITEM_SINGLETONS[color] = new ColorItem(color); white: new ColorItem("white"),
} black: new ColorItem("black"),
uncolored: new ColorItem("uncolored"),
};

View File

@ -4,7 +4,6 @@ import { RandomNumberGenerator } from "../core/rng";
import { clamp, fastArrayDeleteValueIfContained, make2DUndefinedArray } from "../core/utils"; import { clamp, fastArrayDeleteValueIfContained, make2DUndefinedArray } from "../core/utils";
import { Vector } from "../core/vector"; import { Vector } from "../core/vector";
import { BaseItem } from "./base_item"; import { BaseItem } from "./base_item";
import { enumColors } from "./colors";
import { Entity } from "./entity"; import { Entity } from "./entity";
import { COLOR_ITEM_SINGLETONS } from "./items/color_item"; import { COLOR_ITEM_SINGLETONS } from "./items/color_item";
import { GameRoot } from "./root"; import { GameRoot } from "./root";
@ -165,9 +164,10 @@ export class MapChunk {
*/ */
internalGenerateColorPatch(rng, colorPatchSize, distanceToOriginInChunks) { internalGenerateColorPatch(rng, colorPatchSize, distanceToOriginInChunks) {
// First, determine available colors // First, determine available colors
let availableColors = [enumColors.red, enumColors.green]; /** @type {Color[]} */
const availableColors = ["red", "green"];
if (distanceToOriginInChunks > 2) { if (distanceToOriginInChunks > 2) {
availableColors.push(enumColors.blue); availableColors.push("blue");
} }
this.internalGeneratePatch(rng, colorPatchSize, COLOR_ITEM_SINGLETONS[rng.choice(availableColors)]); this.internalGeneratePatch(rng, colorPatchSize, COLOR_ITEM_SINGLETONS[rng.choice(availableColors)]);
} }
@ -298,7 +298,7 @@ export class MapChunk {
*/ */
generatePredefined(rng) { generatePredefined(rng) {
if (this.x === 0 && this.y === 0) { if (this.x === 0 && this.y === 0) {
this.internalGeneratePatch(rng, 2, COLOR_ITEM_SINGLETONS[enumColors.red], 7, 7); this.internalGeneratePatch(rng, 2, COLOR_ITEM_SINGLETONS.red, 7, 7);
return true; return true;
} }
if (this.x === -1 && this.y === 0) { if (this.x === -1 && this.y === 0) {
@ -313,7 +313,7 @@ export class MapChunk {
} }
if (this.x === -1 && this.y === -1) { if (this.x === -1 && this.y === -1) {
this.internalGeneratePatch(rng, 2, COLOR_ITEM_SINGLETONS[enumColors.green]); this.internalGeneratePatch(rng, 2, COLOR_ITEM_SINGLETONS.green);
return true; return true;
} }

View File

@ -4,17 +4,12 @@ import { smoothenDpi } from "../core/dpi_manager";
import { DrawParameters } from "../core/draw_parameters"; import { DrawParameters } from "../core/draw_parameters";
import { Vector } from "../core/vector"; import { Vector } from "../core/vector";
import { BasicSerializableObject, types } from "../savegame/serialization"; import { BasicSerializableObject, types } from "../savegame/serialization";
import { enumColors, enumColorsToHexCode, enumColorToShortcode, enumShortcodeToColor } from "./colors"; import { colorHexColorMap, colorShortcodeMap, shortcodeColorMap } from "./colors";
import { THEME } from "./theme"; import { THEME } from "./theme";
/** /**
* @typedef {{ * @typedef {{ subShape: enumSubShape, color: Color }} ShapeLayerItem
* subShape: enumSubShape, *
* color: enumColors,
* }} ShapeLayerItem
*/
/**
* Order is Q1 (tr), Q2(br), Q3(bl), Q4(tl) * Order is Q1 (tr), Q2(br), Q3(bl), Q4(tl)
* @typedef {[ShapeLayerItem?, ShapeLayerItem?, ShapeLayerItem?, ShapeLayerItem?]} ShapeLayer * @typedef {[ShapeLayerItem?, ShapeLayerItem?, ShapeLayerItem?, ShapeLayerItem?]} ShapeLayer
*/ */
@ -51,13 +46,13 @@ for (const key in enumSubShapeToShortcode) {
/** /**
* Converts the given parameters to a valid shape definition * Converts the given parameters to a valid shape definition
* @param {*} layers * @param {*} layers
* @returns {Array<import("./shape_definition").ShapeLayer>} * @returns {Array<ShapeLayer>}
*/ */
export function createSimpleShape(layers) { export function createSimpleShape(layers) {
layers.forEach(layer => { layers.forEach(layer => {
layer.forEach(item => { layer.forEach(item => {
if (item) { if (item) {
item.color = item.color || enumColors.uncolored; item.color = item.color || "uncolored";
} }
}); });
}); });
@ -129,7 +124,7 @@ export class ShapeDefinition extends BasicSerializableObject {
for (let quad = 0; quad < 4; ++quad) { for (let quad = 0; quad < 4; ++quad) {
const shapeText = text[quad * 2 + 0]; const shapeText = text[quad * 2 + 0];
const subShape = enumShortcodeToSubShape[shapeText]; const subShape = enumShortcodeToSubShape[shapeText];
const color = enumShortcodeToColor[text[quad * 2 + 1]]; const color = shortcodeColorMap[text[quad * 2 + 1]];
if (subShape) { if (subShape) {
assert(color, "Invalid shape short key:", key); assert(color, "Invalid shape short key:", key);
quads[quad] = { quads[quad] = {
@ -186,7 +181,7 @@ export class ShapeDefinition extends BasicSerializableObject {
const shapeText = text[quad * 2 + 0]; const shapeText = text[quad * 2 + 0];
const colorText = text[quad * 2 + 1]; const colorText = text[quad * 2 + 1];
const subShape = enumShortcodeToSubShape[shapeText]; const subShape = enumShortcodeToSubShape[shapeText];
const color = enumShortcodeToColor[colorText]; const color = shortcodeColorMap[colorText];
// Valid shape // Valid shape
if (subShape) { if (subShape) {
@ -256,7 +251,7 @@ export class ShapeDefinition extends BasicSerializableObject {
for (let quadrant = 0; quadrant < layer.length; ++quadrant) { for (let quadrant = 0; quadrant < layer.length; ++quadrant) {
const item = layer[quadrant]; const item = layer[quadrant];
if (item) { if (item) {
id += enumSubShapeToShortcode[item.subShape] + enumColorToShortcode[item.color]; id += enumSubShapeToShortcode[item.subShape] + colorShortcodeMap[item.color];
} else { } else {
id += "--"; id += "--";
} }
@ -352,7 +347,7 @@ export class ShapeDefinition extends BasicSerializableObject {
context.translate(centerQuadrantX, centerQuadrantY); context.translate(centerQuadrantX, centerQuadrantY);
context.rotate(rotation); context.rotate(rotation);
context.fillStyle = enumColorsToHexCode[color]; context.fillStyle = colorHexColorMap[color];
context.strokeStyle = THEME.items.outline; context.strokeStyle = THEME.items.outline;
context.lineWidth = THEME.items.outlineWidth; context.lineWidth = THEME.items.outlineWidth;
@ -571,7 +566,7 @@ export class ShapeDefinition extends BasicSerializableObject {
/** /**
* Clones the shape and colors everything in the given color * Clones the shape and colors everything in the given color
* @param {enumColors} color * @param {Color} color
*/ */
cloneAndPaintWith(color) { cloneAndPaintWith(color) {
const newLayers = this.internalCloneLayers(); const newLayers = this.internalCloneLayers();
@ -590,7 +585,7 @@ export class ShapeDefinition extends BasicSerializableObject {
/** /**
* Clones the shape and colors everything in the given colors * Clones the shape and colors everything in the given colors
* @param {[enumColors, enumColors, enumColors, enumColors]} colors * @param {[Color, Color, Color, Color]} colors
*/ */
cloneAndPaintWith4Colors(colors) { cloneAndPaintWith4Colors(colors) {
const newLayers = this.internalCloneLayers(); const newLayers = this.internalCloneLayers();

View File

@ -1,9 +1,8 @@
import { createLogger } from "../core/logging"; import { createLogger } from "../core/logging";
import { BasicSerializableObject } from "../savegame/serialization"; import { BasicSerializableObject } from "../savegame/serialization";
import { enumColors } from "./colors";
import { ShapeItem } from "./items/shape_item"; import { ShapeItem } from "./items/shape_item";
import { GameRoot } from "./root"; import { GameRoot } from "./root";
import { enumSubShape, ShapeDefinition } from "./shape_definition"; import { enumSubShape, ShapeDefinition, ShapeLayer } from "./shape_definition";
const logger = createLogger("shape_definition_manager"); const logger = createLogger("shape_definition_manager");
@ -198,7 +197,7 @@ export class ShapeDefinitionManager extends BasicSerializableObject {
/** /**
* Generates a definition for painting it with the given color * Generates a definition for painting it with the given color
* @param {ShapeDefinition} definition * @param {ShapeDefinition} definition
* @param {enumColors} color * @param {Color} color
* @returns {ShapeDefinition} * @returns {ShapeDefinition}
*/ */
shapeActionPaintWith(definition, color) { shapeActionPaintWith(definition, color) {
@ -215,7 +214,7 @@ export class ShapeDefinitionManager extends BasicSerializableObject {
/** /**
* Generates a definition for painting it with the 4 colors * Generates a definition for painting it with the 4 colors
* @param {ShapeDefinition} definition * @param {ShapeDefinition} definition
* @param {[enumColors, enumColors, enumColors, enumColors]} colors * @param {[Color, Color, Color, Color]} colors
* @returns {ShapeDefinition} * @returns {ShapeDefinition}
*/ */
shapeActionPaintWith4Colors(definition, colors) { shapeActionPaintWith4Colors(definition, colors) {
@ -249,10 +248,8 @@ export class ShapeDefinitionManager extends BasicSerializableObject {
* @param {[enumSubShape, enumSubShape, enumSubShape, enumSubShape]} subShapes * @param {[enumSubShape, enumSubShape, enumSubShape, enumSubShape]} subShapes
* @returns {ShapeDefinition} * @returns {ShapeDefinition}
*/ */
getDefinitionFromSimpleShapes(subShapes, color = enumColors.uncolored) { getDefinitionFromSimpleShapes(subShapes, color = "uncolored") {
const shapeLayer = /** @type {import("./shape_definition").ShapeLayer} */ (subShapes.map( const shapeLayer = /** @type {ShapeLayer} */ (subShapes.map(subShape => ({ subShape, color })));
subShape => ({ subShape, color })
));
return this.registerOrReturnHandle(new ShapeDefinition({ layers: [shapeLayer] })); return this.registerOrReturnHandle(new ShapeDefinition({ layers: [shapeLayer] }));
} }

View File

@ -2,7 +2,7 @@ import trim from "trim";
import { DialogWithForm } from "../../core/modal_dialog_elements"; import { DialogWithForm } from "../../core/modal_dialog_elements";
import { FormElementInput } from "../../core/modal_dialog_forms"; import { FormElementInput } from "../../core/modal_dialog_forms";
import { BaseItem } from "../base_item"; import { BaseItem } from "../base_item";
import { enumColors } from "../colors"; import { isColor } from "../colors";
import { ConstantSignalComponent } from "../components/constant_signal"; import { ConstantSignalComponent } from "../components/constant_signal";
import { Entity } from "../entity"; import { Entity } from "../entity";
import { GameSystemWithFilter } from "../game_system_with_filter"; import { GameSystemWithFilter } from "../game_system_with_filter";
@ -109,7 +109,7 @@ export class ConstantSignalSystem extends GameSystemWithFilter {
code = trim(code); code = trim(code);
const codeLower = code.toLowerCase(); const codeLower = code.toLowerCase();
if (enumColors[codeLower]) { if (isColor(codeLower)) {
return COLOR_ITEM_SINGLETONS[codeLower]; return COLOR_ITEM_SINGLETONS[codeLower];
} }
if (code === "1" || codeLower === "true") { if (code === "1" || codeLower === "true") {

View File

@ -1,7 +1,8 @@
import { globalConfig } from "../../core/config"; import { globalConfig } from "../../core/config";
import { AtlasSprite, DrawParameters } from "../../core/draw_utils";
import { Loader } from "../../core/loader"; import { Loader } from "../../core/loader";
import { BaseItem } from "../base_item"; import { BaseItem } from "../base_item";
import { enumColors } from "../colors"; import { colors } from "../colors";
import { DisplayComponent } from "../components/display"; import { DisplayComponent } from "../components/display";
import { GameSystemWithFilter } from "../game_system_with_filter"; import { GameSystemWithFilter } from "../game_system_with_filter";
import { ColorItem, COLOR_ITEM_SINGLETONS } from "../items/color_item"; import { ColorItem, COLOR_ITEM_SINGLETONS } from "../items/color_item";
@ -12,15 +13,12 @@ export class DisplaySystem extends GameSystemWithFilter {
constructor(root) { constructor(root) {
super(root, [DisplayComponent]); super(root, [DisplayComponent]);
/** @type {Object<string, import("../../core/draw_utils").AtlasSprite>} */ this.displaySprites = colors
this.displaySprites = {}; .filter(color => !["black", "uncolored"].includes(color))
.reduce((sprites, color) => {
for (const colorId in enumColors) { sprites[color] = Loader.getSprite(`sprites/wires/display/${color}.png`);
if (colorId === enumColors.uncolored) { return sprites;
continue; }, /** @type {Record<Exclude<Color, "black" | "uncolored">, AtlasSprite>} */ ({}));
}
this.displaySprites[colorId] = Loader.getSprite("sprites/wires/display/" + colorId + ".png");
}
} }
/** /**
@ -35,14 +33,12 @@ export class DisplaySystem extends GameSystemWithFilter {
switch (value.getItemType()) { switch (value.getItemType()) {
case "boolean": { case "boolean": {
return /** @type {BooleanItem} */ (value).value return /** @type {BooleanItem} */ (value).value ? COLOR_ITEM_SINGLETONS.white : null;
? COLOR_ITEM_SINGLETONS[enumColors.white]
: null;
} }
case "color": { case "color": {
const item = /**@type {ColorItem} */ (value); const item = /**@type {ColorItem} */ (value);
return item.color === enumColors.uncolored ? null : item; return item.color === "uncolored" ? null : item;
} }
case "shape": { case "shape": {
@ -56,7 +52,7 @@ export class DisplaySystem extends GameSystemWithFilter {
/** /**
* Draws a given chunk * Draws a given chunk
* @param {import("../../core/draw_utils").DrawParameters} parameters * @param {DrawParameters} parameters
* @param {MapChunkView} chunk * @param {MapChunkView} chunk
*/ */
drawChunk(parameters, chunk) { drawChunk(parameters, chunk) {

View File

@ -1,6 +1,6 @@
import { globalConfig } from "../../core/config"; import { globalConfig } from "../../core/config";
import { BaseItem } from "../base_item"; import { BaseItem } from "../base_item";
import { enumColorMixingResults } from "../colors"; import { colorMixingMap } from "../colors";
import { enumItemProcessorTypes, ItemProcessorComponent } from "../components/item_processor"; import { enumItemProcessorTypes, ItemProcessorComponent } from "../components/item_processor";
import { Entity } from "../entity"; import { Entity } from "../entity";
import { GameSystemWithFilter } from "../game_system_with_filter"; import { GameSystemWithFilter } from "../game_system_with_filter";
@ -241,12 +241,8 @@ export class ItemProcessorSystem extends GameSystemWithFilter {
const color1 = item1.color; const color1 = item1.color;
const color2 = item2.color; const color2 = item2.color;
// Try finding mixer color, and if we can't mix it we simply return the same color const resultColor = colorMixingMap[color1][color2];
const mixedColor = enumColorMixingResults[color1][color2];
let resultColor = color1;
if (mixedColor) {
resultColor = mixedColor;
}
outItems.push({ outItems.push({
item: COLOR_ITEM_SINGLETONS[resultColor], item: COLOR_ITEM_SINGLETONS[resultColor],
}); });

22
src/js/globals.d.ts vendored
View File

@ -203,3 +203,25 @@ declare module "worker-loader?inline=true&fallback=false!*" {
export default WebpackWorker; export default WebpackWorker;
} }
declare type Color =
| "red"
| "green"
| "blue"
| "yellow"
| "purple"
| "cyan"
| "white"
| "uncolored"
| "black";
declare type HexColor =
| "#ff666a"
| "#78ff66"
| "#66a7ff"
| "#fcf52a"
| "#dd66ff"
| "#00fcff"
| "#ffffff"
| "#aaaaaa"
| "#31383a";