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";
|
|
|
|
|
|
|
|
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() {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a preview sprite
|
|
|
|
* @returns {AtlasSprite}
|
|
|
|
*/
|
|
|
|
getPreviewSprite(rotationVariant = 0) {
|
|
|
|
return Loader.getSprite("sprites/buildings/" + this.id + ".png");
|
|
|
|
}
|
|
|
|
|
2020-05-10 15:00:02 +00:00
|
|
|
/**
|
|
|
|
* Returns a sprite for blueprints
|
|
|
|
* @returns {AtlasSprite}
|
|
|
|
*/
|
|
|
|
getBlueprintSprite(rotationVariant = 0) {
|
|
|
|
return Loader.getSprite("sprites/blueprints/" + this.id + ".png");
|
|
|
|
}
|
|
|
|
|
2020-05-09 14:45:23 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the entity at the given location
|
|
|
|
* @param {GameRoot} root
|
|
|
|
* @param {Vector} origin Origin tile
|
|
|
|
* @param {number=} rotation Rotation
|
|
|
|
* @param {number=} rotationVariant Rotation variant
|
|
|
|
*/
|
|
|
|
createAndPlaceEntity(root, origin, rotation = 0, rotationVariant = 0) {
|
|
|
|
const entity = new Entity(root);
|
|
|
|
entity.addComponent(
|
|
|
|
new StaticMapEntityComponent({
|
|
|
|
spriteKey: "sprites/buildings/" + this.id + ".png",
|
|
|
|
origin: new Vector(origin.x, origin.y),
|
|
|
|
rotationDegrees: rotation,
|
|
|
|
tileSize: this.getDimensions().copy(),
|
|
|
|
silhouetteColor: this.getSilhouetteColor(),
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
this.setupEntityComponents(entity, root);
|
|
|
|
this.updateRotationVariant(entity, rotationVariant);
|
|
|
|
|
|
|
|
root.map.placeStaticEntity(entity);
|
2020-05-10 15:00:02 +00:00
|
|
|
root.entityMgr.registerEntity(entity);
|
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
|
|
|
|
* @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) {}
|
|
|
|
|
|
|
|
// PRIVATE INTERFACE
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Should setup the entity components
|
|
|
|
* @param {Entity} entity
|
|
|
|
* @param {GameRoot} root
|
|
|
|
*/
|
|
|
|
setupEntityComponents(entity, root) {
|
|
|
|
abstract;
|
|
|
|
}
|
|
|
|
}
|