2020-05-09 14:45:23 +00:00
|
|
|
import { createLogger } from "../core/logging";
|
2020-05-27 13:03:36 +00:00
|
|
|
import { round2Digits } from "../core/utils";
|
2020-07-05 15:56:54 +00:00
|
|
|
import { enumDirection, enumDirectionToVector, Vector } from "../core/vector";
|
|
|
|
import { Entity } from "./entity";
|
|
|
|
import { MetaBuilding } from "./meta_building";
|
|
|
|
import { enumLayer, GameRoot } from "./root";
|
|
|
|
import { STOP_PROPAGATION } from "../core/signal";
|
2020-05-09 14:45:23 +00:00
|
|
|
|
|
|
|
const logger = createLogger("ingame/logic");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Typing helper
|
|
|
|
* @typedef {Array<{
|
|
|
|
* entity: Entity,
|
|
|
|
* slot: import("./components/item_ejector").ItemEjectorSlot,
|
|
|
|
* fromTile: Vector,
|
|
|
|
* toDirection: enumDirection
|
|
|
|
* }>} EjectorsAffectingTile
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Typing helper
|
|
|
|
* @typedef {Array<{
|
|
|
|
* entity: Entity,
|
|
|
|
* slot: import("./components/item_acceptor").ItemAcceptorSlot,
|
|
|
|
* toTile: Vector,
|
|
|
|
* fromDirection: enumDirection
|
|
|
|
* }>} AcceptorsAffectingTile
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {{
|
|
|
|
* acceptors: AcceptorsAffectingTile,
|
|
|
|
* ejectors: EjectorsAffectingTile
|
|
|
|
* }} AcceptorsAndEjectorsAffectingTile
|
|
|
|
*/
|
|
|
|
|
|
|
|
export class GameLogic {
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {GameRoot} root
|
|
|
|
*/
|
|
|
|
constructor(root) {
|
|
|
|
this.root = root;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-07-05 15:56:54 +00:00
|
|
|
* Checks if the given entity can be placed
|
|
|
|
* @param {Entity} entity
|
|
|
|
* @param {Vector=} offset Optional, move the entity by the given offset first
|
|
|
|
* @returns {boolean} true if the entity could be placed there
|
2020-05-09 14:45:23 +00:00
|
|
|
*/
|
2020-07-05 15:56:54 +00:00
|
|
|
checkCanPlaceEntity(entity, offset = null) {
|
|
|
|
// Compute area of the building
|
|
|
|
const rect = entity.components.StaticMapEntity.getTileSpaceBounds();
|
|
|
|
if (offset) {
|
|
|
|
rect.x += offset.x;
|
|
|
|
rect.y += offset.y;
|
|
|
|
}
|
2020-05-09 14:45:23 +00:00
|
|
|
|
2020-07-05 15:56:54 +00:00
|
|
|
// Check the whole area of the building
|
2020-05-09 14:45:23 +00:00
|
|
|
for (let x = rect.x; x < rect.x + rect.w; ++x) {
|
|
|
|
for (let y = rect.y; y < rect.y + rect.h; ++y) {
|
2020-07-05 15:56:54 +00:00
|
|
|
// Check if there is any direct collision
|
|
|
|
const otherEntity = this.root.map.getLayerContentXY(x, y, entity.layer);
|
|
|
|
if (otherEntity && !otherEntity.components.ReplaceableMapEntity) {
|
|
|
|
// This one is a direct blocker
|
|
|
|
return false;
|
2020-06-28 17:34:10 +00:00
|
|
|
}
|
2020-05-09 14:45:23 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-10 15:00:02 +00:00
|
|
|
|
2020-07-05 15:56:54 +00:00
|
|
|
// Perform additional placement checks
|
|
|
|
if (this.root.signals.prePlacementCheck.dispatch(entity, offset) === STOP_PROPAGATION) {
|
2020-06-28 17:34:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-10 16:59:42 +00:00
|
|
|
return true;
|
2020-05-10 15:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-07-05 15:56:54 +00:00
|
|
|
* Attempts to place the given building
|
2020-05-10 15:00:02 +00:00
|
|
|
* @param {object} param0
|
|
|
|
* @param {Vector} param0.origin
|
|
|
|
* @param {number} param0.rotation
|
2020-07-05 15:56:54 +00:00
|
|
|
* @param {number} param0.originalRotation
|
2020-05-10 15:00:02 +00:00
|
|
|
* @param {number} param0.rotationVariant
|
2020-05-16 20:45:40 +00:00
|
|
|
* @param {string} param0.variant
|
2020-05-10 15:00:02 +00:00
|
|
|
* @param {MetaBuilding} param0.building
|
2020-07-05 15:56:54 +00:00
|
|
|
* @returns {Entity}
|
2020-05-09 14:45:23 +00:00
|
|
|
*/
|
2020-07-05 15:56:54 +00:00
|
|
|
tryPlaceBuilding({ origin, rotation, rotationVariant, originalRotation, variant, building }) {
|
|
|
|
const entity = building.createEntity({
|
|
|
|
root: this.root,
|
2020-05-10 15:00:02 +00:00
|
|
|
origin,
|
|
|
|
rotation,
|
2020-07-05 15:56:54 +00:00
|
|
|
originalRotation,
|
2020-05-10 15:00:02 +00:00
|
|
|
rotationVariant,
|
2020-05-16 20:45:40 +00:00
|
|
|
variant,
|
2020-05-10 15:00:02 +00:00
|
|
|
});
|
2020-07-05 15:56:54 +00:00
|
|
|
if (this.checkCanPlaceEntity(entity)) {
|
|
|
|
this.freeEntityAreaBeforeBuild(entity);
|
|
|
|
this.root.map.placeStaticEntity(entity);
|
|
|
|
this.root.entityMgr.registerEntity(entity);
|
|
|
|
return entity;
|
|
|
|
}
|
|
|
|
return null;
|
2020-05-09 14:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-07-05 15:56:54 +00:00
|
|
|
* Removes all entities with a RemovableMapEntityComponent which need to get
|
|
|
|
* removed before placing this entity
|
|
|
|
* @param {Entity} entity
|
2020-05-09 14:45:23 +00:00
|
|
|
*/
|
2020-07-05 15:56:54 +00:00
|
|
|
freeEntityAreaBeforeBuild(entity) {
|
|
|
|
const staticComp = entity.components.StaticMapEntity;
|
|
|
|
const rect = staticComp.getTileSpaceBounds();
|
|
|
|
// Remove any removeable colliding entities on the same layer
|
|
|
|
for (let x = rect.x; x < rect.x + rect.w; ++x) {
|
|
|
|
for (let y = rect.y; y < rect.y + rect.h; ++y) {
|
|
|
|
const contents = this.root.map.getLayerContentXY(x, y, entity.layer);
|
|
|
|
if (contents) {
|
|
|
|
assertAlways(
|
|
|
|
contents.components.ReplaceableMapEntity,
|
|
|
|
"Tried to replace non-repleaceable entity"
|
|
|
|
);
|
|
|
|
if (!this.tryDeleteBuilding(contents)) {
|
|
|
|
assertAlways(false, "Tried to replace non-repleaceable entity #2");
|
2020-05-09 14:45:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-05 15:56:54 +00:00
|
|
|
|
|
|
|
// Perform other callbacks
|
|
|
|
this.root.signals.freeEntityAreaBeforeBuild.dispatch(entity);
|
2020-05-09 14:45:23 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 13:03:36 +00:00
|
|
|
/**
|
|
|
|
* Performs a bulk operation, not updating caches in the meantime
|
|
|
|
* @param {function} operation
|
|
|
|
*/
|
|
|
|
performBulkOperation(operation) {
|
2020-06-26 15:02:52 +00:00
|
|
|
logger.warn("Running bulk operation ...");
|
2020-05-27 13:03:36 +00:00
|
|
|
assert(!this.root.bulkOperationRunning, "Can not run two bulk operations twice");
|
|
|
|
this.root.bulkOperationRunning = true;
|
2020-06-27 08:51:52 +00:00
|
|
|
const now = performance.now();
|
2020-05-27 13:03:36 +00:00
|
|
|
const returnValue = operation();
|
2020-06-27 08:51:52 +00:00
|
|
|
const duration = performance.now() - now;
|
2020-05-27 13:03:36 +00:00
|
|
|
logger.log("Done in", round2Digits(duration), "ms");
|
|
|
|
assert(this.root.bulkOperationRunning, "Bulk operation = false while bulk operation was running");
|
|
|
|
this.root.bulkOperationRunning = false;
|
|
|
|
this.root.signals.bulkOperationFinished.dispatch();
|
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
2020-05-09 14:45:23 +00:00
|
|
|
/**
|
|
|
|
* Returns whether the given building can get removed
|
|
|
|
* @param {Entity} building
|
|
|
|
*/
|
|
|
|
canDeleteBuilding(building) {
|
|
|
|
return building.components.StaticMapEntity && !building.components.Unremovable;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tries to delete the given building
|
|
|
|
* @param {Entity} building
|
|
|
|
*/
|
|
|
|
tryDeleteBuilding(building) {
|
|
|
|
if (!this.canDeleteBuilding(building)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this.root.map.removeStaticEntity(building);
|
|
|
|
this.root.entityMgr.destroyEntity(building);
|
2020-06-26 11:57:07 +00:00
|
|
|
this.root.entityMgr.processDestroyList();
|
2020-05-09 14:45:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the acceptors and ejectors which affect the current tile
|
|
|
|
* @param {Vector} tile
|
2020-06-28 17:34:10 +00:00
|
|
|
* @param {enumLayer} layer
|
2020-05-09 14:45:23 +00:00
|
|
|
* @returns {AcceptorsAndEjectorsAffectingTile}
|
|
|
|
*/
|
2020-06-28 17:34:10 +00:00
|
|
|
getEjectorsAndAcceptorsAtTile(tile, layer) {
|
2020-05-09 14:45:23 +00:00
|
|
|
/** @type {EjectorsAffectingTile} */
|
|
|
|
let ejectors = [];
|
|
|
|
/** @type {AcceptorsAffectingTile} */
|
|
|
|
let acceptors = [];
|
|
|
|
|
2020-07-05 15:56:54 +00:00
|
|
|
// Well .. please ignore this code! :D
|
2020-05-09 14:45:23 +00:00
|
|
|
for (let dx = -1; dx <= 1; ++dx) {
|
|
|
|
for (let dy = -1; dy <= 1; ++dy) {
|
2020-06-27 08:51:52 +00:00
|
|
|
if (Math.abs(dx) + Math.abs(dy) !== 1) {
|
2020-05-09 14:45:23 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-06-30 06:23:05 +00:00
|
|
|
const entities = this.root.map.getLayersContentsMultipleXY(tile.x + dx, tile.y + dy);
|
|
|
|
for (let i = 0; i < entities.length; ++i) {
|
|
|
|
const entity = entities[i];
|
|
|
|
|
2020-08-10 13:02:14 +00:00
|
|
|
let ejectorSlots = [];
|
|
|
|
let acceptorSlots = [];
|
|
|
|
|
2020-05-09 14:45:23 +00:00
|
|
|
const staticComp = entity.components.StaticMapEntity;
|
|
|
|
const itemEjector = entity.components.ItemEjector;
|
2020-08-10 13:02:14 +00:00
|
|
|
const itemAcceptor = entity.components.ItemAcceptor;
|
|
|
|
const beltComp = entity.components.Belt;
|
|
|
|
|
2020-05-09 14:45:23 +00:00
|
|
|
if (itemEjector) {
|
2020-08-10 13:02:14 +00:00
|
|
|
ejectorSlots = itemEjector.slots.slice();
|
2020-05-09 14:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (itemAcceptor) {
|
2020-08-10 13:02:14 +00:00
|
|
|
acceptorSlots = itemAcceptor.slots.slice();
|
|
|
|
}
|
2020-06-28 17:34:10 +00:00
|
|
|
|
2020-08-10 13:02:14 +00:00
|
|
|
if (beltComp) {
|
|
|
|
const fakeEjectorSlot = beltComp.getFakeEjectorSlots();
|
|
|
|
const fakeAcceptorSlot = beltComp.getFakeAcceptorSlot();
|
|
|
|
ejectorSlots.push(fakeEjectorSlot);
|
|
|
|
acceptorSlots.push(fakeAcceptorSlot);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let ejectorSlot = 0; ejectorSlot < ejectorSlots.length; ++ejectorSlot) {
|
|
|
|
const slot = ejectorSlots[ejectorSlot];
|
|
|
|
if (slot.layer !== layer) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const wsTile = staticComp.localTileToWorld(slot.pos);
|
|
|
|
const wsDirection = staticComp.localDirectionToWorld(slot.direction);
|
|
|
|
const targetTile = wsTile.add(enumDirectionToVector[wsDirection]);
|
|
|
|
if (targetTile.equals(tile)) {
|
|
|
|
ejectors.push({
|
|
|
|
entity,
|
|
|
|
slot,
|
|
|
|
fromTile: wsTile,
|
|
|
|
toDirection: wsDirection,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let acceptorSlot = 0; acceptorSlot < acceptorSlots.length; ++acceptorSlot) {
|
|
|
|
const slot = acceptorSlots[acceptorSlot];
|
|
|
|
if (slot.layer !== layer) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const wsTile = staticComp.localTileToWorld(slot.pos);
|
|
|
|
for (let k = 0; k < slot.directions.length; ++k) {
|
|
|
|
const direction = slot.directions[k];
|
|
|
|
const wsDirection = staticComp.localDirectionToWorld(direction);
|
|
|
|
|
|
|
|
const sourceTile = wsTile.add(enumDirectionToVector[wsDirection]);
|
|
|
|
if (sourceTile.equals(tile)) {
|
|
|
|
acceptors.push({
|
|
|
|
entity,
|
|
|
|
slot,
|
|
|
|
toTile: wsTile,
|
|
|
|
fromDirection: wsDirection,
|
|
|
|
});
|
2020-05-09 14:45:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return { ejectors, acceptors };
|
|
|
|
}
|
|
|
|
}
|