2020-05-09 14:45:23 +00: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 17:12:58 +00:00
|
|
|
import { SOUNDS } from "../platform/sound";
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-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-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-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-05-20 13:51:06 +00:00
|
|
|
isRotateable(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the entity at the given location
|
2020-05-10 15:45:48 +00:00
|
|
|
* @param {object} param0
|
|
|
|
* @param {GameRoot} param0.root
|
|
|
|
* @param {Vector} param0.origin Origin tile
|
|
|
|
* @param {number=} param0.rotation Rotation
|
2020-05-16 20:45:40 +00:00
|
|
|
* @param {number} param0.originalRotation Original Rotation
|
|
|
|
* @param {number} param0.rotationVariant Rotation variant
|
|
|
|
* @param {string} param0.variant
|
2020-05-09 14:45:23 +00:00
|
|
|
*/
|
2020-05-16 20:45:40 +00:00
|
|
|
createAndPlaceEntity({ root, origin, rotation, originalRotation, rotationVariant, variant }) {
|
2020-06-21 20:29:23 +00:00
|
|
|
const entity = this.createEntity({
|
|
|
|
root,
|
|
|
|
origin,
|
|
|
|
rotation,
|
|
|
|
originalRotation,
|
|
|
|
rotationVariant,
|
|
|
|
variant,
|
|
|
|
});
|
|
|
|
root.map.placeStaticEntity(entity);
|
|
|
|
root.entityMgr.registerEntity(entity);
|
|
|
|
return entity;
|
|
|
|
}
|
2020-05-27 12:30:59 +00:00
|
|
|
|
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-05-27 12:30:59 +00:00
|
|
|
const blueprintSprite = this.getBlueprintSprite(rotationVariant, variant);
|
2020-05-09 14:45:23 +00:00
|
|
|
entity.addComponent(
|
|
|
|
new StaticMapEntityComponent({
|
2020-05-16 20:45:40 +00:00
|
|
|
spriteKey:
|
|
|
|
"sprites/buildings/" +
|
|
|
|
this.id +
|
|
|
|
(variant === defaultBuildingVariant ? "" : "-" + variant) +
|
|
|
|
".png",
|
2020-05-09 14:45:23 +00:00
|
|
|
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-05-09 14:45:23 +00:00
|
|
|
silhouetteColor: this.getSilhouetteColor(),
|
2020-05-27 12:30:59 +00:00
|
|
|
blueprintSpriteKey: blueprintSprite ? blueprintSprite.spriteName : "",
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Should compute the optimal rotation variant on the given tile
|
|
|
|
* @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-20 13:51:06 +00:00
|
|
|
if (!this.isRotateable(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;
|
|
|
|
}
|
|
|
|
}
|