shapez/src/js/game/meta_building.js

229 lines
5.9 KiB
JavaScript
Raw Normal View History

2020-05-09 16:45:23 +02:00
import { Vector, enumDirection, enumAngleToDirection } from "../core/vector";
import { Loader } from "../core/loader";
import { GameRoot } from "./root";
import { AtlasSprite } from "../core/sprites";
import { Entity } from "./entity";
import { StaticMapEntityComponent } from "./components/static_map_entity";
2020-05-14 19:12:58 +02:00
import { SOUNDS } from "../platform/sound";
2020-05-09 16:45:23 +02:00
export const defaultBuildingVariant = "default";
2020-05-09 16:45:23 +02:00
export class MetaBuilding {
/**
*
* @param {string} id Building id
*/
constructor(id) {
this.id = id;
}
/**
* Returns the id of this building
*/
getId() {
return this.id;
}
/**
* Should return the dimensions of the building
*/
getDimensions(variant = defaultBuildingVariant) {
2020-05-09 16:45:23 +02:00
return new Vector(1, 1);
}
/**
* Should return the name of this building
*/
getName() {
return this.id;
}
/**
* Should return the description of this building
*/
getDescription() {
return "No Description";
}
/**
* Whether to stay in placement mode after having placed a building
*/
getStayInPlacementMode() {
return false;
}
/**
* Whether to flip the orientation after a building has been placed - useful
* for tunnels.
*/
getFlipOrientationAfterPlacement() {
return false;
}
/**
* Whether to rotate automatically in the dragging direction while placing
* @param {string} variant
*/
getRotateAutomaticallyWhilePlacing(variant) {
return false;
}
2020-05-14 19:12:58 +02:00
/**
* Returns the placement sound
* @returns {string}
*/
getPlacementSound() {
return SOUNDS.placeBuilding;
}
/**
* @param {GameRoot} root
*/
getAvailableVariants(root) {
return [defaultBuildingVariant];
}
2020-05-09 16:45:23 +02:00
/**
* Returns a preview sprite
* @returns {AtlasSprite}
*/
getPreviewSprite(rotationVariant = 0, variant = defaultBuildingVariant) {
return Loader.getSprite(
"sprites/buildings/" +
this.id +
(variant === defaultBuildingVariant ? "" : "-" + variant) +
".png"
);
2020-05-09 16:45:23 +02:00
}
2020-05-10 17:00:02 +02:00
/**
* Returns a sprite for blueprints
* @returns {AtlasSprite}
*/
getBlueprintSprite(rotationVariant = 0, variant = defaultBuildingVariant) {
return Loader.getSprite(
"sprites/blueprints/" +
this.id +
(variant === defaultBuildingVariant ? "" : "-" + variant) +
".png"
);
2020-05-10 17:00:02 +02:00
}
2020-05-09 16:45:23 +02:00
/**
* Returns whether this building is rotateable
* @returns {boolean}
*/
isRotateable() {
return true;
}
/**
* Returns whether this building is unlocked for the given game
* @param {GameRoot} root
*/
getIsUnlocked(root) {
return true;
}
/**
* Should return a silhouette color for the map overview or null if not set
*/
getSilhouetteColor() {
return null;
}
/**
* Should perform additional placement checks
* @param {GameRoot} root
* @param {object} param0
* @param {Vector} param0.origin
* @param {number} param0.rotation
* @param {number} param0.rotationVariant
* @param {string} param0.variant
*/
performAdditionalPlacementChecks(root, { origin, rotation, rotationVariant, variant }) {
return true;
}
2020-05-09 16:45:23 +02:00
/**
* Creates the entity at the given location
* @param {object} param0
* @param {GameRoot} param0.root
* @param {Vector} param0.origin Origin tile
* @param {number=} param0.rotation Rotation
* @param {number} param0.originalRotation Original Rotation
* @param {number} param0.rotationVariant Rotation variant
* @param {string} param0.variant
2020-05-09 16:45:23 +02:00
*/
createAndPlaceEntity({ root, origin, rotation, originalRotation, rotationVariant, variant }) {
2020-05-09 16:45:23 +02:00
const entity = new Entity(root);
entity.addComponent(
new StaticMapEntityComponent({
spriteKey:
"sprites/buildings/" +
this.id +
(variant === defaultBuildingVariant ? "" : "-" + variant) +
".png",
2020-05-09 16:45:23 +02:00
origin: new Vector(origin.x, origin.y),
rotation,
2020-05-10 18:50:16 +02:00
originalRotation,
tileSize: this.getDimensions(variant).copy(),
2020-05-09 16:45:23 +02:00
silhouetteColor: this.getSilhouetteColor(),
})
);
this.setupEntityComponents(entity, root);
this.updateRotationVariant(entity, rotationVariant);
this.updateVariant(entity, variant);
2020-05-09 16:45:23 +02:00
root.map.placeStaticEntity(entity);
2020-05-10 17:00:02 +02:00
root.entityMgr.registerEntity(entity);
2020-05-09 16:45:23 +02:00
return entity;
}
/**
* Should compute the optimal rotation variant on the given tile
* @param {GameRoot} root
* @param {Vector} tile
* @param {number} rotation
* @return {{ rotation: number, rotationVariant: number, connectedEntities?: Array<Entity> }}
*/
computeOptimalDirectionAndRotationVariantAtTile(root, tile, rotation) {
if (!this.isRotateable()) {
return {
rotation: 0,
rotationVariant: 0,
};
}
return {
rotation,
rotationVariant: 0,
};
}
/**
* Should update the entity to match the given rotation variant
* @param {Entity} entity
* @param {number} rotationVariant
*/
updateRotationVariant(entity, rotationVariant) {}
/**
* Should update the entity to match the given variant
* @param {Entity} entity
* @param {string} variant
*/
updateVariant(entity, variant) {}
2020-05-09 16:45:23 +02:00
// PRIVATE INTERFACE
/**
* Should setup the entity components
* @param {Entity} entity
* @param {GameRoot} root
*/
setupEntityComponents(entity, root) {
abstract;
}
}