mirror of
https://github.com/tobspr/shapez.io.git
synced 2026-03-02 03:39:21 +00:00
Refactor static map entity component to store building metaclass
This commit is contained in:
@@ -18,6 +18,7 @@ import { DynamicDomAttach } from "../dynamic_dom_attach";
|
||||
import { HUDBuildingPlacerLogic } from "./building_placer_logic";
|
||||
import { makeOffscreenBuffer } from "../../../core/buffer_utils";
|
||||
import { enumLayer } from "../../root";
|
||||
import { getCodeFromBuildingData } from "../../building_codes";
|
||||
|
||||
export class HUDBuildingPlacer extends HUDBuildingPlacerLogic {
|
||||
/**
|
||||
@@ -84,9 +85,8 @@ export class HUDBuildingPlacer extends HUDBuildingPlacerLogic {
|
||||
label: "lock-direction-indicator",
|
||||
});
|
||||
|
||||
// Loader.getSprite("sprites/misc/lock_direction_indicator.png").draw(context, 0, 0, 48, 48);
|
||||
context.fillStyle = THEME.map.directionLock[enumLayer.wires].color;
|
||||
context.strokeStyle = THEME.map.directionLock[enumLayer.wires].color;
|
||||
context.fillStyle = THEME.map.directionLock[layer].color;
|
||||
context.strokeStyle = THEME.map.directionLock[layer].color;
|
||||
context.lineWidth = 2;
|
||||
|
||||
const padding = 5;
|
||||
@@ -313,6 +313,11 @@ export class HUDBuildingPlacer extends HUDBuildingPlacerLogic {
|
||||
staticComp.rotation = rotation;
|
||||
staticComp.tileSize = metaBuilding.getDimensions(this.currentVariant.get());
|
||||
metaBuilding.updateVariants(this.fakeEntity, rotationVariant, this.currentVariant.get());
|
||||
staticComp.code = getCodeFromBuildingData(
|
||||
this.currentMetaBuilding.get(),
|
||||
this.currentVariant.get(),
|
||||
rotationVariant
|
||||
);
|
||||
|
||||
const canBuild = this.root.logic.checkCanPlaceEntity(this.fakeEntity);
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ import { SOUNDS } from "../../../platform/sound";
|
||||
import { MetaMinerBuilding, enumMinerVariants } from "../../buildings/miner";
|
||||
import { enumHubGoalRewards } from "../../tutorial_goals";
|
||||
import { enumLayer } from "../../root";
|
||||
import { getBuildingDataFromCode, getCodeFromBuildingData } from "../../building_codes";
|
||||
|
||||
/**
|
||||
* Contains all logic for the building placer - this doesn't include the rendering
|
||||
@@ -338,109 +339,26 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
|
||||
}
|
||||
|
||||
// Try to extract the building
|
||||
const extracted = this.hack_reconstructMetaBuildingAndVariantFromBuilding(contents);
|
||||
const buildingCode = contents.components.StaticMapEntity.code;
|
||||
const extracted = getBuildingDataFromCode(buildingCode);
|
||||
|
||||
// If the building we are picking is the same as the one we have, clear the cursor.
|
||||
if (
|
||||
!extracted ||
|
||||
(extracted.metaBuilding === this.currentMetaBuilding.get() &&
|
||||
extracted.variant === this.currentVariant.get())
|
||||
extracted.metaInstance.getId() === this.currentMetaBuilding.get().getId() &&
|
||||
extracted.variant === this.currentVariant.get()
|
||||
) {
|
||||
this.currentMetaBuilding.set(null);
|
||||
return;
|
||||
}
|
||||
|
||||
this.currentMetaBuilding.set(extracted.metaBuilding);
|
||||
this.currentMetaBuilding.set(extracted.metaInstance);
|
||||
this.currentVariant.set(extracted.variant);
|
||||
this.currentBaseRotation = contents.components.StaticMapEntity.rotation;
|
||||
}
|
||||
|
||||
/**
|
||||
* HACK!
|
||||
*
|
||||
* This attempts to reconstruct the meta building and its variant from a given entity
|
||||
* @param {Entity} entity
|
||||
* @returns {{ metaBuilding: MetaBuilding, variant: string }}
|
||||
* Switches the side for the direction lock manually
|
||||
*/
|
||||
hack_reconstructMetaBuildingAndVariantFromBuilding(entity) {
|
||||
if (entity.components.Hub) {
|
||||
// Hub is not copyable
|
||||
return null;
|
||||
}
|
||||
|
||||
const matches = [];
|
||||
const metaBuildings = gMetaBuildingRegistry.entries;
|
||||
for (let i = 0; i < metaBuildings.length; ++i) {
|
||||
const metaBuilding = metaBuildings[i];
|
||||
const availableVariants = metaBuilding.getAvailableVariants(this.root);
|
||||
checkVariant: for (let k = 0; k < availableVariants.length; ++k) {
|
||||
const variant = availableVariants[k];
|
||||
let unplaced = metaBuilding.createEntity({
|
||||
root: this.root,
|
||||
variant,
|
||||
origin: new Vector(0, 0),
|
||||
rotation: 0,
|
||||
originalRotation: 0,
|
||||
rotationVariant: 0,
|
||||
});
|
||||
|
||||
// Compare if both entities share the same components
|
||||
for (let component in entity.components) {
|
||||
if ((entity.components[component] == null) !== (unplaced.components[component] == null)) {
|
||||
continue checkVariant;
|
||||
}
|
||||
}
|
||||
|
||||
// Check for same item processor
|
||||
if (
|
||||
entity.components.ItemProcessor &&
|
||||
entity.components.ItemProcessor.type != unplaced.components.ItemProcessor.type
|
||||
) {
|
||||
continue checkVariant;
|
||||
}
|
||||
|
||||
// Check for underground belt
|
||||
if (
|
||||
entity.components.UndergroundBelt &&
|
||||
entity.components.UndergroundBelt.tier != unplaced.components.UndergroundBelt.tier
|
||||
) {
|
||||
continue checkVariant;
|
||||
}
|
||||
|
||||
// Check for same sprite key - except for underground belts
|
||||
// since the sprite may vary here
|
||||
if (
|
||||
!entity.components.UndergroundBelt &&
|
||||
entity.components.StaticMapEntity.spriteKey !=
|
||||
unplaced.components.StaticMapEntity.spriteKey
|
||||
) {
|
||||
continue checkVariant;
|
||||
}
|
||||
|
||||
if (metaBuilding.id === "wire" && entity.layer !== enumLayer.wires) {
|
||||
continue checkVariant;
|
||||
}
|
||||
|
||||
if (metaBuilding.id === "belt" && entity.layer !== enumLayer.regular) {
|
||||
continue checkVariant;
|
||||
}
|
||||
matches.push({ metaBuilding, variant });
|
||||
}
|
||||
}
|
||||
|
||||
if (matches.length == 1) {
|
||||
const staticEntity = entity.components.StaticMapEntity;
|
||||
const key = staticEntity.spriteKey || staticEntity.blueprintSpriteKey;
|
||||
assert(
|
||||
key &&
|
||||
key.includes(matches[0].metaBuilding.id) &&
|
||||
(matches[0].variant === defaultBuildingVariant || key.includes(matches[0].variant))
|
||||
);
|
||||
return matches[0];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
switchDirectionLockSide() {
|
||||
this.currentDirectionLockSide = 1 - this.currentDirectionLockSide;
|
||||
}
|
||||
@@ -673,7 +591,7 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
|
||||
origin: new Vector(0, 0),
|
||||
rotation: 0,
|
||||
tileSize: metaBuilding.getDimensions(this.currentVariant.get()).copy(),
|
||||
blueprintSpriteKey: "",
|
||||
code: getCodeFromBuildingData(metaBuilding, variant, 0),
|
||||
})
|
||||
);
|
||||
metaBuilding.updateVariants(this.fakeEntity, 0, this.currentVariant.get());
|
||||
|
||||
@@ -88,8 +88,8 @@ export class HUDDebugInfo extends BaseHUDPart {
|
||||
const mouseTile = this.root.camera.screenToWorld(mousePos).toTileSpace();
|
||||
const cameraTile = this.root.camera.center.toTileSpace();
|
||||
|
||||
this.trackedMousePosition.set(`Pos: <code>${mouseTile.x}</code> / <code>${mouseTile.y}</code>`);
|
||||
this.trackedCameraPosition.set(`Center: <code>${cameraTile.x}</code> / <code>${cameraTile.y}</code>`);
|
||||
this.trackedMousePosition.set(`Mouse: <code>${mouseTile.x}</code> / <code>${mouseTile.y}</code>`);
|
||||
this.trackedCameraPosition.set(`Camera: <code>${cameraTile.x}</code> / <code>${cameraTile.y}</code>`);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user