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/buildings/underground_belt.js

195 lines
6.6 KiB
JavaScript
Raw Normal View History

2020-05-09 14:45:23 +00:00
import { Loader } from "../../core/loader";
import { enumDirection, Vector, enumAngleToDirection, enumDirectionToVector } from "../../core/vector";
import { ItemAcceptorComponent } from "../components/item_acceptor";
import { ItemEjectorComponent } from "../components/item_ejector";
import { enumUndergroundBeltMode, UndergroundBeltComponent } from "../components/underground_belt";
import { Entity } from "../entity";
2020-05-16 21:48:56 +00:00
import { MetaBuilding, defaultBuildingVariant } from "../meta_building";
2020-05-09 14:45:23 +00:00
import { GameRoot } from "../root";
import { globalConfig } from "../../core/config";
import { enumHubGoalRewards } from "../tutorial_goals";
/** @enum {string} */
export const arrayUndergroundRotationVariantToMode = [
enumUndergroundBeltMode.sender,
enumUndergroundBeltMode.receiver,
];
2020-05-16 21:48:56 +00:00
/** @enum {string} */
export const enumUndergroundBeltVariants = { tier2: "tier2" };
export const enumUndergroundBeltVariantToTier = {
[defaultBuildingVariant]: 0,
[enumUndergroundBeltVariants.tier2]: 1,
};
2020-05-09 14:45:23 +00:00
export class MetaUndergroundBeltBuilding extends MetaBuilding {
constructor() {
super("underground_belt");
}
getSilhouetteColor() {
return "#555";
}
getFlipOrientationAfterPlacement() {
return true;
}
getStayInPlacementMode() {
return true;
}
2020-05-16 21:48:56 +00:00
getAvailableVariants(root) {
return [defaultBuildingVariant, enumUndergroundBeltVariants.tier2];
}
getPreviewSprite(rotationVariant, variant) {
let suffix = "";
if (variant !== defaultBuildingVariant) {
suffix = "-" + variant;
}
2020-05-09 14:45:23 +00:00
switch (arrayUndergroundRotationVariantToMode[rotationVariant]) {
case enumUndergroundBeltMode.sender:
2020-05-16 21:48:56 +00:00
return Loader.getSprite("sprites/buildings/underground_belt_entry" + suffix + ".png");
2020-05-09 14:45:23 +00:00
case enumUndergroundBeltMode.receiver:
2020-05-16 21:48:56 +00:00
return Loader.getSprite("sprites/buildings/underground_belt_exit" + suffix + ".png");
2020-05-09 14:45:23 +00:00
default:
assertAlways(false, "Invalid rotation variant");
}
}
2020-05-16 21:48:56 +00:00
getBlueprintSprite(rotationVariant, variant) {
let suffix = "";
if (variant !== defaultBuildingVariant) {
suffix = "-" + variant;
}
2020-05-10 15:00:02 +00:00
switch (arrayUndergroundRotationVariantToMode[rotationVariant]) {
case enumUndergroundBeltMode.sender:
2020-05-16 21:48:56 +00:00
return Loader.getSprite("sprites/blueprints/underground_belt_entry" + suffix + ".png");
2020-05-10 15:00:02 +00:00
case enumUndergroundBeltMode.receiver:
2020-05-16 21:48:56 +00:00
return Loader.getSprite("sprites/blueprints/underground_belt_exit" + suffix + ".png");
2020-05-10 15:00:02 +00:00
default:
assertAlways(false, "Invalid rotation variant");
}
}
2020-05-09 14:45:23 +00:00
/**
* @param {GameRoot} root
*/
getIsUnlocked(root) {
return root.hubGoals.isRewardUnlocked(enumHubGoalRewards.reward_tunnel);
}
/**
* Creates the entity at the given location
* @param {Entity} entity
*/
setupEntityComponents(entity) {
// Required, since the item processor needs this.
entity.addComponent(
new ItemEjectorComponent({
slots: [],
})
);
entity.addComponent(new UndergroundBeltComponent({}));
entity.addComponent(
new ItemAcceptorComponent({
slots: [],
})
);
}
/**
* @param {GameRoot} root
* @param {Vector} tile
* @param {number} rotation
2020-05-16 21:48:56 +00:00
* @param {string} variant
2020-05-09 14:45:23 +00:00
* @return {{ rotation: number, rotationVariant: number, connectedEntities?: Array<Entity> }}
*/
2020-05-16 21:48:56 +00:00
computeOptimalDirectionAndRotationVariantAtTile(root, tile, rotation, variant) {
2020-05-09 14:45:23 +00:00
const searchDirection = enumAngleToDirection[rotation];
const searchVector = enumDirectionToVector[searchDirection];
2020-05-16 21:48:56 +00:00
const tier = enumUndergroundBeltVariantToTier[variant];
2020-05-09 14:45:23 +00:00
const targetRotation = (rotation + 180) % 360;
2020-05-16 21:48:56 +00:00
for (
let searchOffset = 1;
searchOffset <= globalConfig.undergroundBeltMaxTilesByTier[tier];
++searchOffset
) {
2020-05-09 14:45:23 +00:00
tile = tile.addScalars(searchVector.x, searchVector.y);
const contents = root.map.getTileContent(tile);
if (contents) {
const undergroundComp = contents.components.UndergroundBelt;
2020-05-16 21:48:56 +00:00
if (undergroundComp && undergroundComp.tier === tier) {
2020-05-09 14:45:23 +00:00
const staticComp = contents.components.StaticMapEntity;
if (staticComp.rotation === targetRotation) {
2020-05-09 14:45:23 +00:00
if (undergroundComp.mode !== enumUndergroundBeltMode.sender) {
// If we encounter an underground receiver on our way which is also faced in our direction, we don't accept that
break;
}
return {
rotation: targetRotation,
rotationVariant: 1,
connectedEntities: [contents],
};
}
}
}
}
return {
rotation,
rotationVariant: 0,
};
}
/**
2020-05-16 21:48:56 +00:00
*
2020-05-09 14:45:23 +00:00
* @param {Entity} entity
* @param {number} rotationVariant
2020-05-16 21:48:56 +00:00
* @param {string} variant
2020-05-09 14:45:23 +00:00
*/
2020-05-16 21:48:56 +00:00
updateVariants(entity, rotationVariant, variant) {
entity.components.UndergroundBelt.tier = enumUndergroundBeltVariantToTier[variant];
entity.components.StaticMapEntity.spriteKey = this.getPreviewSprite(
rotationVariant,
variant
).spriteName;
2020-05-09 14:45:23 +00:00
switch (arrayUndergroundRotationVariantToMode[rotationVariant]) {
case enumUndergroundBeltMode.sender: {
entity.components.UndergroundBelt.mode = enumUndergroundBeltMode.sender;
entity.components.ItemEjector.setSlots([]);
entity.components.ItemAcceptor.setSlots([
{
pos: new Vector(0, 0),
directions: [enumDirection.bottom],
},
]);
return;
}
case enumUndergroundBeltMode.receiver: {
entity.components.UndergroundBelt.mode = enumUndergroundBeltMode.receiver;
entity.components.ItemAcceptor.setSlots([]);
entity.components.ItemEjector.setSlots([
{
pos: new Vector(0, 0),
direction: enumDirection.top,
},
]);
return;
}
default:
assertAlways(false, "Invalid rotation variant");
}
}
}