1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2024-10-27 20:34:29 +00:00
tobspr_shapez.io/src/js/game/meta_building.js

245 lines
6.5 KiB
JavaScript
Raw Normal View History

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-05-09 14:45:23 +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
*/
getDimensions(variant = defaultBuildingVariant) {
2020-05-09 14:45:23 +00:00
return new Vector(1, 1);
}
/**
* 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;
}
/**
* 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;
}
/**
* Whether to rotate automatically in the dragging direction while placing
* @param {string} variant
*/
getRotateAutomaticallyWhilePlacing(variant) {
return false;
}
2020-05-14 17:12:58 +00:00
/**
* Returns the placement sound
* @returns {string}
*/
getPlacementSound() {
return SOUNDS.placeBuilding;
}
/**
* @param {GameRoot} root
*/
getAvailableVariants(root) {
return [defaultBuildingVariant];
}
2020-05-09 14:45:23 +00: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 14:45:23 +00:00
}
2020-05-10 15:00:02 +00: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 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
* @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 14:45:23 +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-06-28 17:34:10 +00:00
entity.layer = this.getLayer();
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({
spriteKey:
"sprites/buildings/" +
this.id +
(variant === defaultBuildingVariant ? "" : "-" + variant) +
".png",
2020-05-09 14:45:23 +00:00
origin: new Vector(origin.x, origin.y),
rotation,
2020-05-10 16:50:16 +00:00
originalRotation,
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
* @param {string} variant
*/
2020-05-16 21:48:56 +00:00
updateVariants(entity, rotationVariant, variant) {}
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;
}
}