1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-06-13 13:04:03 +00:00
tobspr_shapez.io/src/js/game/systems/display.js

100 lines
3.4 KiB
JavaScript
Raw Normal View History

2020-08-14 12:55:37 +00:00
import { globalConfig } from "../../core/config";
import { Loader } from "../../core/loader";
2020-08-15 16:39:08 +00:00
import { BaseItem } from "../base_item";
2020-08-14 12:55:37 +00:00
import { enumColors } from "../colors";
import { DisplayComponent } from "../components/display";
import { GameSystemWithFilter } from "../game_system_with_filter";
import { ColorItem, COLOR_ITEM_SINGLETONS } from "../items/color_item";
import { MapChunkView } from "../map_chunk_view";
import { BooleanItem } from "../items/boolean_item";
export class DisplaySystem extends GameSystemWithFilter {
constructor(root) {
super(root, [DisplayComponent]);
/** @type {Object<string, import("../../core/draw_utils").AtlasSprite>} */
this.displaySprites = {};
for (const colorId in enumColors) {
if (colorId === enumColors.uncolored) {
continue;
}
this.displaySprites[colorId] = Loader.getSprite("sprites/wires/display/" + colorId + ".png");
}
}
/**
* Returns the color / value a display should show
* @param {BaseItem} value
* @returns {BaseItem}
*/
getDisplayItem(value) {
if (!value) {
return null;
}
switch (value.getItemType()) {
2020-08-15 16:39:08 +00:00
case "boolean": {
2020-08-14 12:55:37 +00:00
return /** @type {BooleanItem} */ (value).value
? COLOR_ITEM_SINGLETONS[enumColors.white]
: null;
}
2020-08-15 16:39:08 +00:00
case "color": {
2020-08-14 12:55:37 +00:00
const item = /**@type {ColorItem} */ (value);
return item.color === enumColors.uncolored ? null : item;
}
2020-08-15 16:39:08 +00:00
case "shape": {
2020-08-14 12:55:37 +00:00
return value;
}
default:
assertAlways(false, "Unknown item type: " + value.getItemType());
}
}
/**
* Draws a given chunk
* @param {import("../../core/draw_utils").DrawParameters} parameters
* @param {MapChunkView} chunk
*/
drawChunk(parameters, chunk) {
2020-08-15 16:39:08 +00:00
const contents = chunk.containedEntitiesByLayer.regular;
2020-08-14 12:55:37 +00:00
for (let i = 0; i < contents.length; ++i) {
const entity = contents[i];
if (entity && entity.components.Display) {
const pinsComp = entity.components.WiredPins;
const network = pinsComp.slots[0].linkedNetwork;
2020-08-14 12:55:37 +00:00
if (!network || !network.currentValue) {
continue;
}
2020-08-14 12:55:37 +00:00
const value = this.getDisplayItem(network.currentValue);
if (!value) {
continue;
}
const origin = entity.components.StaticMapEntity.origin;
2020-08-15 16:39:08 +00:00
if (value.getItemType() === "color") {
2020-08-14 12:55:37 +00:00
this.displaySprites[/** @type {ColorItem} */ (value).color].drawCachedCentered(
parameters,
(origin.x + 0.5) * globalConfig.tileSize,
(origin.y + 0.5) * globalConfig.tileSize,
globalConfig.tileSize
2020-08-14 12:55:37 +00:00
);
2020-08-15 16:39:08 +00:00
} else if (value.getItemType() === "shape") {
value.drawItemCenteredClipped(
2020-08-14 12:55:37 +00:00
(origin.x + 0.5) * globalConfig.tileSize,
(origin.y + 0.5) * globalConfig.tileSize,
parameters,
30
);
}
}
}
}
}