mirror of
https://github.com/tobspr/shapez.io.git
synced 2025-06-13 13:04:03 +00:00
Changes to building variants
This commit is contained in:
parent
0377c6d58f
commit
9114c9e4c3
@ -1,5 +1,5 @@
|
|||||||
/* typehints:start */
|
/* typehints:start */
|
||||||
import { MetaBuilding } from "./meta_building";
|
import { MetaBuilding, MetaBuildingVariant } from "./meta_building";
|
||||||
import { AtlasSprite } from "../core/sprites";
|
import { AtlasSprite } from "../core/sprites";
|
||||||
import { Vector } from "../core/vector";
|
import { Vector } from "../core/vector";
|
||||||
/* typehints:end */
|
/* typehints:end */
|
||||||
@ -8,7 +8,7 @@ import { Vector } from "../core/vector";
|
|||||||
* @typedef {{
|
* @typedef {{
|
||||||
* metaClass: typeof MetaBuilding,
|
* metaClass: typeof MetaBuilding,
|
||||||
* metaInstance?: MetaBuilding,
|
* metaInstance?: MetaBuilding,
|
||||||
* variant?: string,
|
* variant: typeof MetaBuildingVariant,
|
||||||
* rotationVariant?: number,
|
* rotationVariant?: number,
|
||||||
* tileSize?: Vector,
|
* tileSize?: Vector,
|
||||||
* sprite?: AtlasSprite,
|
* sprite?: AtlasSprite,
|
||||||
@ -27,17 +27,12 @@ export const gBuildingVariants = {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers a new variant
|
* Registers a new variant
|
||||||
* @param {number} id
|
* @param {number | string} id
|
||||||
* @param {typeof MetaBuilding} meta
|
* @param {typeof MetaBuilding} meta
|
||||||
* @param {string} variant
|
* @param {typeof MetaBuildingVariant} variant
|
||||||
* @param {number} rotationVariant
|
* @param {number} rotationVariant
|
||||||
*/
|
*/
|
||||||
export function registerBuildingVariant(
|
export function registerBuildingVariant(id, meta, variant, rotationVariant = 0) {
|
||||||
id,
|
|
||||||
meta,
|
|
||||||
variant = "default" /* FIXME: Circular dependency, actually its defaultBuildingVariant */,
|
|
||||||
rotationVariant = 0
|
|
||||||
) {
|
|
||||||
assert(!gBuildingVariants[id], "Duplicate id: " + id);
|
assert(!gBuildingVariants[id], "Duplicate id: " + id);
|
||||||
gBuildingVariants[id] = {
|
gBuildingVariants[id] = {
|
||||||
metaClass: meta,
|
metaClass: meta,
|
||||||
@ -50,7 +45,7 @@ export function registerBuildingVariant(
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param {number} code
|
* @param {number | string} code
|
||||||
* @returns {BuildingVariantIdentifier}
|
* @returns {BuildingVariantIdentifier}
|
||||||
*/
|
*/
|
||||||
export function getBuildingDataFromCode(code) {
|
export function getBuildingDataFromCode(code) {
|
||||||
@ -61,7 +56,7 @@ export function getBuildingDataFromCode(code) {
|
|||||||
/**
|
/**
|
||||||
* Finds the code for a given variant
|
* Finds the code for a given variant
|
||||||
* @param {MetaBuilding} metaBuilding
|
* @param {MetaBuilding} metaBuilding
|
||||||
* @param {string} variant
|
* @param {typeof MetaBuildingVariant} variant
|
||||||
* @param {number} rotationVariant
|
* @param {number} rotationVariant
|
||||||
*/
|
*/
|
||||||
export function getCodeFromBuildingData(metaBuilding, variant, rotationVariant) {
|
export function getCodeFromBuildingData(metaBuilding, variant, rotationVariant) {
|
||||||
@ -77,7 +72,12 @@ export function getCodeFromBuildingData(metaBuilding, variant, rotationVariant)
|
|||||||
}
|
}
|
||||||
assertAlways(
|
assertAlways(
|
||||||
false,
|
false,
|
||||||
"Building not found by data: " + metaBuilding.getId() + " / " + variant + " / " + rotationVariant
|
"Building not found by data: " +
|
||||||
|
metaBuilding.getId() +
|
||||||
|
" / " +
|
||||||
|
variant.getId() +
|
||||||
|
" / " +
|
||||||
|
rotationVariant
|
||||||
);
|
);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ export class StaticMapEntityComponent extends Component {
|
|||||||
originalRotation: types.float,
|
originalRotation: types.float,
|
||||||
|
|
||||||
// See building_codes.js
|
// See building_codes.js
|
||||||
code: types.uint,
|
code: types.string,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,14 +79,14 @@ export class StaticMapEntityComponent extends Component {
|
|||||||
* @param {Vector=} param0.tileSize Size of the entity in tiles
|
* @param {Vector=} param0.tileSize Size of the entity in tiles
|
||||||
* @param {number=} param0.rotation Rotation in degrees. Must be multiple of 90
|
* @param {number=} param0.rotation Rotation in degrees. Must be multiple of 90
|
||||||
* @param {number=} param0.originalRotation Original Rotation in degrees. Must be multiple of 90
|
* @param {number=} param0.originalRotation Original Rotation in degrees. Must be multiple of 90
|
||||||
* @param {number=} param0.code Building code
|
* @param {(number | string)=} param0.code Building code
|
||||||
*/
|
*/
|
||||||
constructor({
|
constructor({
|
||||||
origin = new Vector(),
|
origin = new Vector(),
|
||||||
tileSize = new Vector(1, 1),
|
tileSize = new Vector(1, 1),
|
||||||
rotation = 0,
|
rotation = 0,
|
||||||
originalRotation = 0,
|
originalRotation = 0,
|
||||||
code = 0,
|
code = "0",
|
||||||
}) {
|
}) {
|
||||||
super();
|
super();
|
||||||
assert(
|
assert(
|
||||||
|
@ -155,13 +155,14 @@ export class GameCore {
|
|||||||
this.root.map.seed = randomInt(0, 100000);
|
this.root.map.seed = randomInt(0, 100000);
|
||||||
|
|
||||||
// Place the hub
|
// Place the hub
|
||||||
const hub = gMetaBuildingRegistry.findByClass(MetaHubBuilding).createEntity({
|
const metaBuilding = gMetaBuildingRegistry.findByClass(MetaHubBuilding);
|
||||||
|
const hub = metaBuilding.createEntity({
|
||||||
root: this.root,
|
root: this.root,
|
||||||
origin: new Vector(-2, -2),
|
origin: new Vector(-2, -2),
|
||||||
rotation: 0,
|
rotation: 0,
|
||||||
originalRotation: 0,
|
originalRotation: 0,
|
||||||
rotationVariant: 0,
|
rotationVariant: 0,
|
||||||
variant: defaultBuildingVariant,
|
variant: metaBuilding.getDefaultVariant(this.root),
|
||||||
});
|
});
|
||||||
this.root.map.placeStaticEntity(hub);
|
this.root.map.placeStaticEntity(hub);
|
||||||
this.root.entityMgr.registerEntity(hub);
|
this.root.entityMgr.registerEntity(hub);
|
||||||
|
@ -116,8 +116,9 @@ export class HUDBuildingPlacer extends HUDBuildingPlacerLogic {
|
|||||||
|
|
||||||
const variant = this.currentVariant.get();
|
const variant = this.currentVariant.get();
|
||||||
|
|
||||||
this.buildingInfoElements.label.innerHTML = T.buildings[metaBuilding.id][variant].name;
|
this.buildingInfoElements.label.innerHTML = T.buildings[metaBuilding.id][variant.getId()].name;
|
||||||
this.buildingInfoElements.descText.innerHTML = T.buildings[metaBuilding.id][variant].description;
|
this.buildingInfoElements.descText.innerHTML =
|
||||||
|
T.buildings[metaBuilding.id][variant.getId()].description;
|
||||||
|
|
||||||
const binding = this.root.keyMapper.getBinding(KEYMAPPINGS.buildings[metaBuilding.getId()]);
|
const binding = this.root.keyMapper.getBinding(KEYMAPPINGS.buildings[metaBuilding.getId()]);
|
||||||
this.buildingInfoElements.hotkey.innerHTML = T.ingame.buildingPlacement.hotkeyLabel.replace(
|
this.buildingInfoElements.hotkey.innerHTML = T.ingame.buildingPlacement.hotkeyLabel.replace(
|
||||||
@ -129,12 +130,12 @@ export class HUDBuildingPlacer extends HUDBuildingPlacerLogic {
|
|||||||
"data-icon",
|
"data-icon",
|
||||||
"building_tutorials/" +
|
"building_tutorials/" +
|
||||||
metaBuilding.getId() +
|
metaBuilding.getId() +
|
||||||
(variant === defaultBuildingVariant ? "" : "-" + variant) +
|
(variant === metaBuilding.getDefaultVariant(this.root) ? "" : "-" + variant.getId()) +
|
||||||
".png"
|
".png"
|
||||||
);
|
);
|
||||||
|
|
||||||
removeAllChildren(this.buildingInfoElements.additionalInfo);
|
removeAllChildren(this.buildingInfoElements.additionalInfo);
|
||||||
const additionalInfo = metaBuilding.getAdditionalStatistics(this.root, this.currentVariant.get());
|
const additionalInfo = this.currentVariant.get().getAdditionalStatistics(this.root);
|
||||||
for (let i = 0; i < additionalInfo.length; ++i) {
|
for (let i = 0; i < additionalInfo.length; ++i) {
|
||||||
const [label, contents] = additionalInfo[i];
|
const [label, contents] = additionalInfo[i];
|
||||||
this.buildingInfoElements.additionalInfo.innerHTML += `
|
this.buildingInfoElements.additionalInfo.innerHTML += `
|
||||||
@ -201,12 +202,12 @@ export class HUDBuildingPlacer extends HUDBuildingPlacerLogic {
|
|||||||
|
|
||||||
const element = makeDiv(container, null, ["variant"]);
|
const element = makeDiv(container, null, ["variant"]);
|
||||||
element.classList.toggle("active", variant === this.currentVariant.get());
|
element.classList.toggle("active", variant === this.currentVariant.get());
|
||||||
makeDiv(element, null, ["label"], variant);
|
makeDiv(element, null, ["label"], variant.getId());
|
||||||
|
|
||||||
const iconSize = 64;
|
const iconSize = 64;
|
||||||
|
|
||||||
const dimensions = metaBuilding.getDimensions(variant);
|
const dimensions = variant.getDimensions();
|
||||||
const sprite = metaBuilding.getPreviewSprite(0, variant);
|
const sprite = variant.getPreviewSprite(0, metaBuilding);
|
||||||
const spriteWrapper = makeDiv(element, null, ["iconWrap"]);
|
const spriteWrapper = makeDiv(element, null, ["iconWrap"]);
|
||||||
spriteWrapper.setAttribute("data-tile-w", dimensions.x);
|
spriteWrapper.setAttribute("data-tile-w", dimensions.x);
|
||||||
spriteWrapper.setAttribute("data-tile-h", dimensions.y);
|
spriteWrapper.setAttribute("data-tile-h", dimensions.y);
|
||||||
@ -294,11 +295,10 @@ export class HUDBuildingPlacer extends HUDBuildingPlacerLogic {
|
|||||||
rotation,
|
rotation,
|
||||||
rotationVariant,
|
rotationVariant,
|
||||||
connectedEntities,
|
connectedEntities,
|
||||||
} = metaBuilding.computeOptimalDirectionAndRotationVariantAtTile({
|
} = this.currentVariant.get().computeOptimalDirectionAndRotationVariantAtTile({
|
||||||
root: this.root,
|
root: this.root,
|
||||||
tile: mouseTile,
|
tile: mouseTile,
|
||||||
rotation: this.currentBaseRotation,
|
rotation: this.currentBaseRotation,
|
||||||
variant: this.currentVariant.get(),
|
|
||||||
layer: metaBuilding.getLayer(),
|
layer: metaBuilding.getLayer(),
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -338,7 +338,8 @@ export class HUDBuildingPlacer extends HUDBuildingPlacerLogic {
|
|||||||
const staticComp = this.fakeEntity.components.StaticMapEntity;
|
const staticComp = this.fakeEntity.components.StaticMapEntity;
|
||||||
staticComp.origin = mouseTile;
|
staticComp.origin = mouseTile;
|
||||||
staticComp.rotation = rotation;
|
staticComp.rotation = rotation;
|
||||||
metaBuilding.updateVariants(this.fakeEntity, rotationVariant, this.currentVariant.get());
|
//metaBuilding.updateVariants(this.fakeEntity, rotationVariant, this.currentVariant.get());
|
||||||
|
this.currentVariant.get().updateEntityComponents(this.fakeEntity, rotationVariant, this.root);
|
||||||
staticComp.code = getCodeFromBuildingData(
|
staticComp.code = getCodeFromBuildingData(
|
||||||
this.currentMetaBuilding.get(),
|
this.currentMetaBuilding.get(),
|
||||||
this.currentVariant.get(),
|
this.currentVariant.get(),
|
||||||
@ -373,7 +374,7 @@ export class HUDBuildingPlacer extends HUDBuildingPlacerLogic {
|
|||||||
parameters.context.globalAlpha = 1;
|
parameters.context.globalAlpha = 1;
|
||||||
|
|
||||||
// HACK to draw the entity sprite
|
// HACK to draw the entity sprite
|
||||||
const previewSprite = metaBuilding.getBlueprintSprite(rotationVariant, this.currentVariant.get());
|
const previewSprite = this.currentVariant.get().getBlueprintSprite(rotationVariant, metaBuilding);
|
||||||
staticComp.origin = worldPos.divideScalar(globalConfig.tileSize).subScalars(0.5, 0.5);
|
staticComp.origin = worldPos.divideScalar(globalConfig.tileSize).subScalars(0.5, 0.5);
|
||||||
staticComp.drawSpriteOnBoundsClipped(parameters, previewSprite);
|
staticComp.drawSpriteOnBoundsClipped(parameters, previewSprite);
|
||||||
staticComp.origin = mouseTile;
|
staticComp.origin = mouseTile;
|
||||||
|
@ -7,7 +7,7 @@ import { enumMouseButton } from "../../camera";
|
|||||||
import { StaticMapEntityComponent } from "../../components/static_map_entity";
|
import { StaticMapEntityComponent } from "../../components/static_map_entity";
|
||||||
import { Entity } from "../../entity";
|
import { Entity } from "../../entity";
|
||||||
import { KEYMAPPINGS } from "../../key_action_mapper";
|
import { KEYMAPPINGS } from "../../key_action_mapper";
|
||||||
import { defaultBuildingVariant, MetaBuilding } from "../../meta_building";
|
import { defaultBuildingVariant, MetaBuilding, MetaBuildingVariant } from "../../meta_building";
|
||||||
import { BaseHUDPart } from "../base_hud_part";
|
import { BaseHUDPart } from "../base_hud_part";
|
||||||
import { SOUNDS } from "../../../platform/sound";
|
import { SOUNDS } from "../../../platform/sound";
|
||||||
import { MetaMinerBuilding, enumMinerVariants } from "../../buildings/miner";
|
import { MetaMinerBuilding, enumMinerVariants } from "../../buildings/miner";
|
||||||
@ -64,7 +64,7 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Current building variant
|
* Current building variant
|
||||||
* @type {TypedTrackedState<string>}
|
* @type {TypedTrackedState<typeof MetaBuildingVariant>}
|
||||||
*/
|
*/
|
||||||
this.currentVariant = new TrackedState(() => this.signals.variantChanged.dispatch());
|
this.currentVariant = new TrackedState(() => this.signals.variantChanged.dispatch());
|
||||||
|
|
||||||
@ -77,7 +77,7 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
|
|||||||
/**
|
/**
|
||||||
* Stores which variants for each building we prefer, this is based on what
|
* Stores which variants for each building we prefer, this is based on what
|
||||||
* the user last selected
|
* the user last selected
|
||||||
* @type {Object.<string, string>}
|
* @type {Object.<string, typeof MetaBuildingVariant>}
|
||||||
*/
|
*/
|
||||||
this.preferredVariants = {};
|
this.preferredVariants = {};
|
||||||
|
|
||||||
@ -396,11 +396,13 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const metaBuilding = this.currentMetaBuilding.get();
|
const metaBuilding = this.currentMetaBuilding.get();
|
||||||
const { rotation, rotationVariant } = metaBuilding.computeOptimalDirectionAndRotationVariantAtTile({
|
const {
|
||||||
|
rotation,
|
||||||
|
rotationVariant,
|
||||||
|
} = this.currentVariant.get().computeOptimalDirectionAndRotationVariantAtTile({
|
||||||
root: this.root,
|
root: this.root,
|
||||||
tile,
|
tile,
|
||||||
rotation: this.currentBaseRotation,
|
rotation: this.currentBaseRotation,
|
||||||
variant: this.currentVariant.get(),
|
|
||||||
layer: metaBuilding.getLayer(),
|
layer: metaBuilding.getLayer(),
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -448,13 +450,16 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
|
|||||||
cycleVariants() {
|
cycleVariants() {
|
||||||
const metaBuilding = this.currentMetaBuilding.get();
|
const metaBuilding = this.currentMetaBuilding.get();
|
||||||
if (!metaBuilding) {
|
if (!metaBuilding) {
|
||||||
this.currentVariant.set(defaultBuildingVariant);
|
this.currentVariant.set(null);
|
||||||
} else {
|
} else {
|
||||||
const availableVariants = metaBuilding.getAvailableVariants(this.root);
|
const availableVariants = metaBuilding.getAvailableVariants(this.root);
|
||||||
const index = availableVariants.indexOf(this.currentVariant.get());
|
const index = availableVariants.indexOf(this.currentVariant.get());
|
||||||
assert(
|
assert(
|
||||||
index >= 0,
|
index >= 0,
|
||||||
"Current variant was invalid: " + this.currentVariant.get() + " out of " + availableVariants
|
"Current variant was invalid: " +
|
||||||
|
this.currentVariant.get().getId +
|
||||||
|
" out of " +
|
||||||
|
availableVariants
|
||||||
);
|
);
|
||||||
const newIndex = (index + 1) % availableVariants.length;
|
const newIndex = (index + 1) % availableVariants.length;
|
||||||
const newVariant = availableVariants[newIndex];
|
const newVariant = availableVariants[newIndex];
|
||||||
@ -464,7 +469,7 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the current variant to the given variant
|
* Sets the current variant to the given variant
|
||||||
* @param {string} variant
|
* @param {typeof MetaBuildingVariant} variant
|
||||||
*/
|
*/
|
||||||
setVariant(variant) {
|
setVariant(variant) {
|
||||||
const metaBuilding = this.currentMetaBuilding.get();
|
const metaBuilding = this.currentMetaBuilding.get();
|
||||||
@ -589,7 +594,8 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
|
|||||||
this.abortDragging();
|
this.abortDragging();
|
||||||
this.root.hud.signals.selectedPlacementBuildingChanged.dispatch(metaBuilding);
|
this.root.hud.signals.selectedPlacementBuildingChanged.dispatch(metaBuilding);
|
||||||
if (metaBuilding) {
|
if (metaBuilding) {
|
||||||
const variant = this.preferredVariants[metaBuilding.getId()] || defaultBuildingVariant;
|
const variant =
|
||||||
|
this.preferredVariants[metaBuilding.getId()] || metaBuilding.getDefaultVariant(this.root);
|
||||||
this.currentVariant.set(variant);
|
this.currentVariant.set(variant);
|
||||||
|
|
||||||
this.fakeEntity = new Entity(null);
|
this.fakeEntity = new Entity(null);
|
||||||
@ -599,11 +605,12 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
|
|||||||
new StaticMapEntityComponent({
|
new StaticMapEntityComponent({
|
||||||
origin: new Vector(0, 0),
|
origin: new Vector(0, 0),
|
||||||
rotation: 0,
|
rotation: 0,
|
||||||
tileSize: metaBuilding.getDimensions(this.currentVariant.get()).copy(),
|
tileSize: variant.getDimensions().copy(),
|
||||||
code: getCodeFromBuildingData(metaBuilding, variant, 0),
|
code: getCodeFromBuildingData(metaBuilding, variant, 0),
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
metaBuilding.updateVariants(this.fakeEntity, 0, this.currentVariant.get());
|
variant.updateEntityComponents(this.fakeEntity, 0, this.root);
|
||||||
|
//metaBuilding.updateVariants(this.fakeEntity, 0, this.currentVariant.get());
|
||||||
} else {
|
} else {
|
||||||
this.fakeEntity = null;
|
this.fakeEntity = null;
|
||||||
}
|
}
|
||||||
@ -689,7 +696,7 @@ export class HUDBuildingPlacerLogic extends BaseHUDPart {
|
|||||||
// Automatic Direction
|
// Automatic Direction
|
||||||
if (
|
if (
|
||||||
metaBuilding &&
|
metaBuilding &&
|
||||||
metaBuilding.getRotateAutomaticallyWhilePlacing(this.currentVariant.get()) &&
|
this.currentVariant.get().getRotateAutomaticallyWhilePlacing() &&
|
||||||
!this.root.keyMapper.getBinding(
|
!this.root.keyMapper.getBinding(
|
||||||
KEYMAPPINGS.placementModifiers.placementDisableAutoOrientation
|
KEYMAPPINGS.placementModifiers.placementDisableAutoOrientation
|
||||||
).pressed
|
).pressed
|
||||||
|
@ -4,7 +4,7 @@ import { round2Digits } from "../core/utils";
|
|||||||
import { enumDirection, enumDirectionToVector, enumInvertedDirections, Vector } from "../core/vector";
|
import { enumDirection, enumDirectionToVector, enumInvertedDirections, Vector } from "../core/vector";
|
||||||
import { getBuildingDataFromCode } from "./building_codes";
|
import { getBuildingDataFromCode } from "./building_codes";
|
||||||
import { Entity } from "./entity";
|
import { Entity } from "./entity";
|
||||||
import { MetaBuilding } from "./meta_building";
|
import { MetaBuilding, MetaBuildingVariant } from "./meta_building";
|
||||||
import { GameRoot } from "./root";
|
import { GameRoot } from "./root";
|
||||||
import { WireNetwork } from "./systems/wire";
|
import { WireNetwork } from "./systems/wire";
|
||||||
import { globalConfig } from "../core/config";
|
import { globalConfig } from "../core/config";
|
||||||
@ -98,7 +98,7 @@ export class GameLogic {
|
|||||||
* @param {number} param0.rotation
|
* @param {number} param0.rotation
|
||||||
* @param {number} param0.originalRotation
|
* @param {number} param0.originalRotation
|
||||||
* @param {number} param0.rotationVariant
|
* @param {number} param0.rotationVariant
|
||||||
* @param {string} param0.variant
|
* @param {typeof MetaBuildingVariant} param0.variant
|
||||||
* @param {MetaBuilding} param0.building
|
* @param {MetaBuilding} param0.building
|
||||||
* @returns {Entity}
|
* @returns {Entity}
|
||||||
*/
|
*/
|
||||||
@ -283,10 +283,9 @@ export class GameLogic {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const data = getBuildingDataFromCode(staticComp.code);
|
const data = getBuildingDataFromCode(staticComp.code);
|
||||||
const overlayMatrix = data.metaInstance.getSpecialOverlayRenderMatrix(
|
const overlayMatrix = data.variant.getSpecialOverlayRenderMatrix(
|
||||||
staticComp.rotation,
|
staticComp.rotation,
|
||||||
data.rotationVariant,
|
data.rotationVariant,
|
||||||
data.variant,
|
|
||||||
entity
|
entity
|
||||||
);
|
);
|
||||||
// Always the same
|
// Always the same
|
||||||
|
@ -149,10 +149,9 @@ export class MapChunkView extends MapChunk {
|
|||||||
const data = getBuildingDataFromCode(staticComp.code);
|
const data = getBuildingDataFromCode(staticComp.code);
|
||||||
const metaBuilding = data.metaInstance;
|
const metaBuilding = data.metaInstance;
|
||||||
|
|
||||||
const overlayMatrix = metaBuilding.getSpecialOverlayRenderMatrix(
|
const overlayMatrix = data.variant.getSpecialOverlayRenderMatrix(
|
||||||
staticComp.rotation,
|
staticComp.rotation,
|
||||||
data.rotationVariant,
|
data.rotationVariant,
|
||||||
data.variant,
|
|
||||||
upperContent
|
upperContent
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -249,10 +248,9 @@ export class MapChunkView extends MapChunk {
|
|||||||
const staticComp = entity.components.StaticMapEntity;
|
const staticComp = entity.components.StaticMapEntity;
|
||||||
const data = getBuildingDataFromCode(staticComp.code);
|
const data = getBuildingDataFromCode(staticComp.code);
|
||||||
const metaBuilding = data.metaInstance;
|
const metaBuilding = data.metaInstance;
|
||||||
const overlayMatrix = metaBuilding.getSpecialOverlayRenderMatrix(
|
const overlayMatrix = data.variant.getSpecialOverlayRenderMatrix(
|
||||||
staticComp.rotation,
|
staticComp.rotation,
|
||||||
data.rotationVariant,
|
data.rotationVariant,
|
||||||
data.variant,
|
|
||||||
entity
|
entity
|
||||||
);
|
);
|
||||||
context.fillStyle = overrideColor || metaBuilding.getSilhouetteColor();
|
context.fillStyle = overrideColor || metaBuilding.getSilhouetteColor();
|
||||||
|
@ -33,13 +33,6 @@ export class MetaBuilding {
|
|||||||
return "regular";
|
return "regular";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Should return the dimensions of the building
|
|
||||||
*/
|
|
||||||
getDimensions(variant = defaultBuildingVariant) {
|
|
||||||
return new Vector(1, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns whether the building has the direction lock switch available
|
* Returns whether the building has the direction lock switch available
|
||||||
*/
|
*/
|
||||||
@ -54,28 +47,6 @@ export class MetaBuilding {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Can return a special interlaved 9 elements overlay matrix for rendering
|
|
||||||
* @param {number} rotation
|
|
||||||
* @param {number} rotationVariant
|
|
||||||
* @param {string} variant
|
|
||||||
* @param {Entity} entity
|
|
||||||
* @returns {Array<number>|null}
|
|
||||||
*/
|
|
||||||
getSpecialOverlayRenderMatrix(rotation, rotationVariant, variant, entity) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Should return additional statistics about this building
|
|
||||||
* @param {GameRoot} root
|
|
||||||
* @param {string} variant
|
|
||||||
* @returns {Array<[string, string]>}
|
|
||||||
*/
|
|
||||||
getAdditionalStatistics(root, variant) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns whether this building can get replaced
|
* Returns whether this building can get replaced
|
||||||
*/
|
*/
|
||||||
@ -98,14 +69,6 @@ export class MetaBuilding {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Whether to rotate automatically in the dragging direction while placing
|
|
||||||
* @param {string} variant
|
|
||||||
*/
|
|
||||||
getRotateAutomaticallyWhilePlacing(variant) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns whether this building is removable
|
* Returns whether this building is removable
|
||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
@ -124,44 +87,19 @@ export class MetaBuilding {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {GameRoot} root
|
* @param {GameRoot} root
|
||||||
|
* @returns {typeof MetaBuildingVariant}
|
||||||
|
*/
|
||||||
|
getDefaultVariant(root) {
|
||||||
|
return this.getAvailableVariants(root)[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {GameRoot} root
|
||||||
|
* @returns {Array<typeof MetaBuildingVariant>}
|
||||||
*/
|
*/
|
||||||
getAvailableVariants(root) {
|
getAvailableVariants(root) {
|
||||||
return [defaultBuildingVariant];
|
abstract;
|
||||||
}
|
return [];
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a preview sprite
|
|
||||||
* @returns {AtlasSprite}
|
|
||||||
*/
|
|
||||||
getPreviewSprite(rotationVariant = 0, variant = defaultBuildingVariant) {
|
|
||||||
return Loader.getSprite(
|
|
||||||
"sprites/buildings/" +
|
|
||||||
this.id +
|
|
||||||
(variant === defaultBuildingVariant ? "" : "-" + variant) +
|
|
||||||
".png"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a sprite for blueprints
|
|
||||||
* @returns {AtlasSprite}
|
|
||||||
*/
|
|
||||||
getBlueprintSprite(rotationVariant = 0, variant = defaultBuildingVariant) {
|
|
||||||
return Loader.getSprite(
|
|
||||||
"sprites/blueprints/" +
|
|
||||||
this.id +
|
|
||||||
(variant === defaultBuildingVariant ? "" : "-" + variant) +
|
|
||||||
".png"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns whether this building is rotateable
|
|
||||||
* @param {string} variant
|
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
|
||||||
getIsRotateable(variant) {
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -195,7 +133,7 @@ export class MetaBuilding {
|
|||||||
* @param {number=} param0.rotation Rotation
|
* @param {number=} param0.rotation Rotation
|
||||||
* @param {number} param0.originalRotation Original Rotation
|
* @param {number} param0.originalRotation Original Rotation
|
||||||
* @param {number} param0.rotationVariant Rotation variant
|
* @param {number} param0.rotationVariant Rotation variant
|
||||||
* @param {string} param0.variant
|
* @param {typeof MetaBuildingVariant} param0.variant
|
||||||
*/
|
*/
|
||||||
createEntity({ root, origin, rotation, originalRotation, rotationVariant, variant }) {
|
createEntity({ root, origin, rotation, originalRotation, rotationVariant, variant }) {
|
||||||
const entity = new Entity(root);
|
const entity = new Entity(root);
|
||||||
@ -205,61 +143,15 @@ export class MetaBuilding {
|
|||||||
origin: new Vector(origin.x, origin.y),
|
origin: new Vector(origin.x, origin.y),
|
||||||
rotation,
|
rotation,
|
||||||
originalRotation,
|
originalRotation,
|
||||||
tileSize: this.getDimensions(variant).copy(),
|
tileSize: variant.getDimensions().copy(),
|
||||||
code: getCodeFromBuildingData(this, variant, rotationVariant),
|
code: getCodeFromBuildingData(this, variant, rotationVariant),
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
this.setupEntityComponents(entity, root);
|
this.setupEntityComponents(entity, root);
|
||||||
this.updateVariants(entity, rotationVariant, variant);
|
variant.updateEntityComponents(entity, rotationVariant, root);
|
||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the sprite for a given variant
|
|
||||||
* @param {number} rotationVariant
|
|
||||||
* @param {string} variant
|
|
||||||
* @returns {AtlasSprite}
|
|
||||||
*/
|
|
||||||
getSprite(rotationVariant, variant) {
|
|
||||||
return Loader.getSprite(
|
|
||||||
"sprites/buildings/" +
|
|
||||||
this.id +
|
|
||||||
(variant === defaultBuildingVariant ? "" : "-" + variant) +
|
|
||||||
".png"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Should compute the optimal rotation variant on the given tile
|
|
||||||
* @param {object} param0
|
|
||||||
* @param {GameRoot} param0.root
|
|
||||||
* @param {Vector} param0.tile
|
|
||||||
* @param {number} param0.rotation
|
|
||||||
* @param {string} param0.variant
|
|
||||||
* @param {Layer} param0.layer
|
|
||||||
* @return {{ rotation: number, rotationVariant: number, connectedEntities?: Array<Entity> }}
|
|
||||||
*/
|
|
||||||
computeOptimalDirectionAndRotationVariantAtTile({ root, tile, rotation, variant, layer }) {
|
|
||||||
if (!this.getIsRotateable(variant)) {
|
|
||||||
return {
|
|
||||||
rotation: 0,
|
|
||||||
rotationVariant: 0,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
rotation,
|
|
||||||
rotationVariant: 0,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Should update the entity to match the given variants
|
|
||||||
* @param {Entity} entity
|
|
||||||
* @param {number} rotationVariant
|
|
||||||
* @param {string} variant
|
|
||||||
*/
|
|
||||||
updateVariants(entity, rotationVariant, variant) {}
|
|
||||||
|
|
||||||
// PRIVATE INTERFACE
|
// PRIVATE INTERFACE
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -271,3 +163,136 @@ export class MetaBuilding {
|
|||||||
abstract;
|
abstract;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class MetaBuildingVariant {
|
||||||
|
static toString() {
|
||||||
|
return this.getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns {string} Variant id
|
||||||
|
*/
|
||||||
|
static getId() {
|
||||||
|
abstract;
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Should update the entity components
|
||||||
|
* @param {Entity} entity
|
||||||
|
* @param {number} rotationVariant
|
||||||
|
* @param {GameRoot} root
|
||||||
|
*/
|
||||||
|
static updateEntityComponents(entity, rotationVariant, root) {
|
||||||
|
abstract;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns whether this building is rotateable
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
static getIsRotateable() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Should compute the optimal rotation variant on the given tile
|
||||||
|
* @param {object} param0
|
||||||
|
* @param {GameRoot} param0.root
|
||||||
|
* @param {Vector} param0.tile
|
||||||
|
* @param {number} param0.rotation
|
||||||
|
* @param {Layer} param0.layer
|
||||||
|
* @return {{ rotation: number, rotationVariant: number, connectedEntities?: Array<Entity> }}
|
||||||
|
*/
|
||||||
|
static computeOptimalDirectionAndRotationVariantAtTile({ root, tile, rotation, layer }) {
|
||||||
|
if (!this.getIsRotateable()) {
|
||||||
|
return {
|
||||||
|
rotation: 0,
|
||||||
|
rotationVariant: 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
rotation,
|
||||||
|
rotationVariant: 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Can return a special interlaved 9 elements overlay matrix for rendering
|
||||||
|
* @param {number} rotation
|
||||||
|
* @param {number} rotationVariant
|
||||||
|
* @param {Entity} entity
|
||||||
|
* @returns {Array<number>|null}
|
||||||
|
*/
|
||||||
|
static getSpecialOverlayRenderMatrix(rotation, rotationVariant, entity) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Should return additional statistics about this building
|
||||||
|
* @param {GameRoot} root
|
||||||
|
* @returns {Array<[string, string]>}
|
||||||
|
*/
|
||||||
|
static getAdditionalStatistics(root) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the sprite for a given variant
|
||||||
|
* @param {number} rotationVariant
|
||||||
|
* @param {MetaBuilding} building
|
||||||
|
* @returns {AtlasSprite}
|
||||||
|
*/
|
||||||
|
static getSprite(rotationVariant, building) {
|
||||||
|
return Loader.getSprite(
|
||||||
|
"sprites/buildings/" +
|
||||||
|
building.id +
|
||||||
|
(this.getId() === defaultBuildingVariant ? "" : "-" + this.getId()) +
|
||||||
|
".png"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the sprite for a given variant
|
||||||
|
* @param {number} rotationVariant
|
||||||
|
* @param {MetaBuilding} building
|
||||||
|
* @returns {AtlasSprite}
|
||||||
|
*/
|
||||||
|
static getBlueprintSprite(rotationVariant, building) {
|
||||||
|
return Loader.getSprite(
|
||||||
|
"sprites/blueprints/" +
|
||||||
|
building.id +
|
||||||
|
(this.getId() === defaultBuildingVariant ? "" : "-" + this.getId()) +
|
||||||
|
".png"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the sprite for a given variant
|
||||||
|
* @param {number} rotationVariant
|
||||||
|
* @param {MetaBuilding} building
|
||||||
|
* @returns {AtlasSprite}
|
||||||
|
*/
|
||||||
|
static getPreviewSprite(rotationVariant, building) {
|
||||||
|
return Loader.getSprite(
|
||||||
|
"sprites/buildings/" +
|
||||||
|
building.id +
|
||||||
|
(this.getId() === defaultBuildingVariant ? "" : "-" + this.getId()) +
|
||||||
|
".png"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether to rotate automatically in the dragging direction while placing
|
||||||
|
*/
|
||||||
|
static getRotateAutomaticallyWhilePlacing() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Should return the dimensions of the building
|
||||||
|
*/
|
||||||
|
static getDimensions() {
|
||||||
|
return new Vector(1, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -151,9 +151,9 @@ export function initMetaBuildingRegistry() {
|
|||||||
if (typeof variant.rotationVariant === "undefined") {
|
if (typeof variant.rotationVariant === "undefined") {
|
||||||
variant.rotationVariant = 0;
|
variant.rotationVariant = 0;
|
||||||
}
|
}
|
||||||
if (typeof variant.variant === "undefined") {
|
/*if (typeof variant.variant === "undefined") {
|
||||||
variant.variant = defaultBuildingVariant;
|
variant.variant = defaultBuildingVariant;
|
||||||
}
|
}*/
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.log("Registered", gMetaBuildingRegistry.getNumEntries(), "buildings");
|
logger.log("Registered", gMetaBuildingRegistry.getNumEntries(), "buildings");
|
||||||
@ -168,10 +168,10 @@ export function initBuildingCodesAfterResourcesLoaded() {
|
|||||||
for (const key in gBuildingVariants) {
|
for (const key in gBuildingVariants) {
|
||||||
const variant = gBuildingVariants[key];
|
const variant = gBuildingVariants[key];
|
||||||
|
|
||||||
variant.sprite = variant.metaInstance.getSprite(variant.rotationVariant, variant.variant);
|
variant.sprite = variant.variant.getSprite(variant.rotationVariant, variant.metaInstance);
|
||||||
variant.blueprintSprite = variant.metaInstance.getBlueprintSprite(
|
variant.blueprintSprite = variant.variant.getBlueprintSprite(
|
||||||
variant.rotationVariant,
|
variant.rotationVariant,
|
||||||
variant.variant
|
variant.metaInstance
|
||||||
);
|
);
|
||||||
variant.silhouetteColor = variant.metaInstance.getSilhouetteColor();
|
variant.silhouetteColor = variant.metaInstance.getSilhouetteColor();
|
||||||
}
|
}
|
||||||
|
@ -150,14 +150,15 @@ export class BeltSystem extends GameSystemWithFilter {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const defaultVariant = metaBelt.getDefaultVariant(this.root);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
rotation,
|
rotation,
|
||||||
rotationVariant,
|
rotationVariant,
|
||||||
} = metaBelt.computeOptimalDirectionAndRotationVariantAtTile({
|
} = defaultVariant.computeOptimalDirectionAndRotationVariantAtTile({
|
||||||
root: this.root,
|
root: this.root,
|
||||||
tile: new Vector(x, y),
|
tile: new Vector(x, y),
|
||||||
rotation: targetStaticComp.originalRotation,
|
rotation: targetStaticComp.originalRotation,
|
||||||
variant: defaultBuildingVariant,
|
|
||||||
layer: targetEntity.layer,
|
layer: targetEntity.layer,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -170,12 +171,13 @@ export class BeltSystem extends GameSystemWithFilter {
|
|||||||
|
|
||||||
// Change stuff
|
// Change stuff
|
||||||
targetStaticComp.rotation = rotation;
|
targetStaticComp.rotation = rotation;
|
||||||
metaBelt.updateVariants(targetEntity, rotationVariant, defaultBuildingVariant);
|
//metaBelt.updateVariants(targetEntity, rotationVariant, defaultBuildingVariant);
|
||||||
|
defaultVariant.updateEntityComponents(targetEntity, rotationVariant, this.root);
|
||||||
|
|
||||||
// Update code as well
|
// Update code as well
|
||||||
targetStaticComp.code = getCodeFromBuildingData(
|
targetStaticComp.code = getCodeFromBuildingData(
|
||||||
metaBelt,
|
metaBelt,
|
||||||
defaultBuildingVariant,
|
defaultVariant,
|
||||||
rotationVariant
|
rotationVariant
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -738,14 +738,15 @@ export class WireSystem extends GameSystemWithFilter {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const defaultVariant = metaWire.getDefaultVariant(this.root);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
rotation,
|
rotation,
|
||||||
rotationVariant,
|
rotationVariant,
|
||||||
} = metaWire.computeOptimalDirectionAndRotationVariantAtTile({
|
} = defaultVariant.computeOptimalDirectionAndRotationVariantAtTile({
|
||||||
root: this.root,
|
root: this.root,
|
||||||
tile: new Vector(x, y),
|
tile: new Vector(x, y),
|
||||||
rotation: targetStaticComp.originalRotation,
|
rotation: targetStaticComp.originalRotation,
|
||||||
variant: defaultBuildingVariant,
|
|
||||||
layer: targetEntity.layer,
|
layer: targetEntity.layer,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -755,12 +756,13 @@ export class WireSystem extends GameSystemWithFilter {
|
|||||||
if (targetStaticComp.rotation !== rotation || newType !== targetWireComp.type) {
|
if (targetStaticComp.rotation !== rotation || newType !== targetWireComp.type) {
|
||||||
// Change stuff
|
// Change stuff
|
||||||
targetStaticComp.rotation = rotation;
|
targetStaticComp.rotation = rotation;
|
||||||
metaWire.updateVariants(targetEntity, rotationVariant, defaultBuildingVariant);
|
//metaWire.updateVariants(targetEntity, rotationVariant, defaultBuildingVariant);
|
||||||
|
defaultVariant.updateEntityComponents(targetEntity, rotationVariant, this.root);
|
||||||
|
|
||||||
// Update code as well
|
// Update code as well
|
||||||
targetStaticComp.code = getCodeFromBuildingData(
|
targetStaticComp.code = getCodeFromBuildingData(
|
||||||
metaWire,
|
metaWire,
|
||||||
defaultBuildingVariant,
|
defaultVariant,
|
||||||
rotationVariant
|
rotationVariant
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user