2020-05-09 14:45:23 +00:00
|
|
|
import { Loader } from "../core/loader";
|
|
|
|
import { AtlasSprite } from "../core/sprites";
|
2020-06-24 20:23:10 +00:00
|
|
|
import { Vector } from "../core/vector";
|
2020-05-14 17:12:58 +00:00
|
|
|
import { SOUNDS } from "../platform/sound";
|
2020-06-24 20:23:10 +00:00
|
|
|
import { StaticMapEntityComponent } from "./components/static_map_entity";
|
|
|
|
import { Entity } from "./entity";
|
2020-06-28 17:34:10 +00:00
|
|
|
import { enumLayer, GameRoot } from "./root";
|
2020-07-27 15:14:29 +00:00
|
|
|
import { getCodeFromBuildingData } from "./building_codes";
|
2020-05-09 14:45:23 +00:00
|
|
|
|
2020-05-16 20:45:40 +00:00
|
|
|
export const defaultBuildingVariant = "default";
|
|
|
|
|
2020-05-09 14:45:23 +00:00
|
|
|
export class MetaBuilding {
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {string} id Building id
|
|
|
|
*/
|
|
|
|
constructor(id) {
|
|
|
|
this.id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the id of this building
|
|
|
|
*/
|
|
|
|
getId() {
|
|
|
|
return this.id;
|
|
|
|
}
|
|
|
|
|
2020-06-24 20:23:10 +00:00
|
|
|
/**
|
|
|
|
* Returns the edit layer of the building
|
2020-06-28 17:34:10 +00:00
|
|
|
* @returns {enumLayer}
|
2020-06-24 20:23:10 +00:00
|
|
|
*/
|
2020-06-28 17:34:10 +00:00
|
|
|
getLayer() {
|
|
|
|
return enumLayer.regular;
|
2020-06-24 20:23:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-09 14:45:23 +00:00
|
|
|
/**
|
|
|
|
* Should return the dimensions of the building
|
|
|
|
*/
|
2020-05-16 20:45:40 +00:00
|
|
|
getDimensions(variant = defaultBuildingVariant) {
|
2020-05-09 14:45:23 +00:00
|
|
|
return new Vector(1, 1);
|
|
|
|
}
|
|
|
|
|
2020-06-16 18:10:00 +00:00
|
|
|
/**
|
|
|
|
* Returns whether the building has the direction lock switch available
|
|
|
|
*/
|
|
|
|
getHasDirectionLockAvailable() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-09 14:45:23 +00:00
|
|
|
/**
|
|
|
|
* Whether to stay in placement mode after having placed a building
|
|
|
|
*/
|
|
|
|
getStayInPlacementMode() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-11 11:17:39 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2020-05-17 13:32:19 +00:00
|
|
|
/**
|
|
|
|
* Should return additional statistics about this building
|
|
|
|
* @param {GameRoot} root
|
|
|
|
* @param {string} variant
|
|
|
|
* @returns {Array<[string, string]>}
|
|
|
|
*/
|
|
|
|
getAdditionalStatistics(root, variant) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-08-10 20:53:02 +00:00
|
|
|
/**
|
|
|
|
* Returns whether this building can get replaced
|
|
|
|
*/
|
|
|
|
getIsReplaceable() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-09 14:45:23 +00:00
|
|
|
/**
|
|
|
|
* Whether to flip the orientation after a building has been placed - useful
|
|
|
|
* for tunnels.
|
|
|
|
*/
|
|
|
|
getFlipOrientationAfterPlacement() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-10 17:01:33 +00:00
|
|
|
/**
|
|
|
|
* Whether to rotate automatically in the dragging direction while placing
|
2020-05-16 20:45:40 +00:00
|
|
|
* @param {string} variant
|
2020-05-10 17:01:33 +00:00
|
|
|
*/
|
2020-05-16 20:45:40 +00:00
|
|
|
getRotateAutomaticallyWhilePlacing(variant) {
|
2020-05-10 17:01:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-10 20:53:02 +00:00
|
|
|
/**
|
|
|
|
* Returns whether this building is removable
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
getIsRemovable() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-14 17:12:58 +00:00
|
|
|
/**
|
|
|
|
* Returns the placement sound
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
getPlacementSound() {
|
|
|
|
return SOUNDS.placeBuilding;
|
|
|
|
}
|
|
|
|
|
2020-05-16 20:45:40 +00:00
|
|
|
/**
|
|
|
|
* @param {GameRoot} root
|
|
|
|
*/
|
|
|
|
getAvailableVariants(root) {
|
|
|
|
return [defaultBuildingVariant];
|
|
|
|
}
|
|
|
|
|
2020-05-09 14:45:23 +00:00
|
|
|
/**
|
|
|
|
* Returns a preview sprite
|
|
|
|
* @returns {AtlasSprite}
|
|
|
|
*/
|
2020-05-16 20:45:40 +00:00
|
|
|
getPreviewSprite(rotationVariant = 0, variant = defaultBuildingVariant) {
|
|
|
|
return Loader.getSprite(
|
|
|
|
"sprites/buildings/" +
|
|
|
|
this.id +
|
|
|
|
(variant === defaultBuildingVariant ? "" : "-" + variant) +
|
|
|
|
".png"
|
|
|
|
);
|
2020-05-09 14:45:23 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 15:00:02 +00:00
|
|
|
/**
|
|
|
|
* Returns a sprite for blueprints
|
|
|
|
* @returns {AtlasSprite}
|
|
|
|
*/
|
2020-05-16 20:45:40 +00:00
|
|
|
getBlueprintSprite(rotationVariant = 0, variant = defaultBuildingVariant) {
|
|
|
|
return Loader.getSprite(
|
|
|
|
"sprites/blueprints/" +
|
|
|
|
this.id +
|
|
|
|
(variant === defaultBuildingVariant ? "" : "-" + variant) +
|
|
|
|
".png"
|
|
|
|
);
|
2020-05-10 15:00:02 +00:00
|
|
|
}
|
|
|
|
|
2020-05-09 14:45:23 +00:00
|
|
|
/**
|
|
|
|
* Returns whether this building is rotateable
|
2020-05-20 13:51:06 +00:00
|
|
|
* @param {string} variant
|
2020-05-09 14:45:23 +00:00
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2020-08-14 06:20:39 +00:00
|
|
|
getIsRotateable(variant) {
|
2020-05-09 14:45:23 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-08-14 06:20:39 +00:00
|
|
|
/**
|
|
|
|
* Should return false if the pins are already included in the sprite of the building
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
getRenderPins() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-21 20:29:23 +00:00
|
|
|
/**
|
|
|
|
* Creates the entity without placing it
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
createEntity({ root, origin, rotation, originalRotation, rotationVariant, variant }) {
|
|
|
|
const entity = new Entity(root);
|
2020-06-28 17:34:10 +00:00
|
|
|
entity.layer = this.getLayer();
|
2020-05-09 14:45:23 +00:00
|
|
|
entity.addComponent(
|
|
|
|
new StaticMapEntityComponent({
|
|
|
|
origin: new Vector(origin.x, origin.y),
|
2020-05-10 15:45:48 +00:00
|
|
|
rotation,
|
2020-05-10 16:50:16 +00:00
|
|
|
originalRotation,
|
2020-05-16 20:45:40 +00:00
|
|
|
tileSize: this.getDimensions(variant).copy(),
|
2020-07-27 15:14:29 +00:00
|
|
|
code: getCodeFromBuildingData(this, variant, rotationVariant),
|
2020-05-09 14:45:23 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
this.setupEntityComponents(entity, root);
|
2020-05-16 21:48:56 +00:00
|
|
|
this.updateVariants(entity, rotationVariant, variant);
|
2020-05-09 14:45:23 +00:00
|
|
|
return entity;
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:14:29 +00:00
|
|
|
/**
|
|
|
|
* 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"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-09 14:45:23 +00:00
|
|
|
/**
|
|
|
|
* Should compute the optimal rotation variant on the given tile
|
2020-06-30 06:23:05 +00:00
|
|
|
* @param {object} param0
|
|
|
|
* @param {GameRoot} param0.root
|
|
|
|
* @param {Vector} param0.tile
|
|
|
|
* @param {number} param0.rotation
|
|
|
|
* @param {string} param0.variant
|
|
|
|
* @param {string} param0.layer
|
2020-05-09 14:45:23 +00:00
|
|
|
* @return {{ rotation: number, rotationVariant: number, connectedEntities?: Array<Entity> }}
|
|
|
|
*/
|
2020-06-30 06:23:05 +00:00
|
|
|
computeOptimalDirectionAndRotationVariantAtTile({ root, tile, rotation, variant, layer }) {
|
2020-08-14 06:20:39 +00:00
|
|
|
if (!this.getIsRotateable(variant)) {
|
2020-05-09 14:45:23 +00:00
|
|
|
return {
|
|
|
|
rotation: 0,
|
|
|
|
rotationVariant: 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
rotation,
|
|
|
|
rotationVariant: 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-16 21:48:56 +00:00
|
|
|
* Should update the entity to match the given variants
|
2020-05-09 14:45:23 +00:00
|
|
|
* @param {Entity} entity
|
|
|
|
* @param {number} rotationVariant
|
2020-05-16 20:45:40 +00:00
|
|
|
* @param {string} variant
|
|
|
|
*/
|
2020-05-16 21:48:56 +00:00
|
|
|
updateVariants(entity, rotationVariant, variant) {}
|
2020-05-16 20:45:40 +00:00
|
|
|
|
2020-05-09 14:45:23 +00:00
|
|
|
// PRIVATE INTERFACE
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Should setup the entity components
|
|
|
|
* @param {Entity} entity
|
|
|
|
* @param {GameRoot} root
|
|
|
|
*/
|
|
|
|
setupEntityComponents(entity, root) {
|
|
|
|
abstract;
|
|
|
|
}
|
|
|
|
}
|