You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tobspr_shapez.io/src/js/game/logic.js

260 lines
9.2 KiB

4 years ago
import { createLogger } from "../core/logging";
import { round2Digits } from "../core/utils";
import { enumDirection, enumDirectionToVector, Vector } from "../core/vector";
import { Entity } from "./entity";
import { MetaBuilding } from "./meta_building";
import { GameRoot, enumLayer } from "./root";
import { STOP_PROPAGATION } from "../core/signal";
4 years ago
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;
}
/**
* 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
4 years ago
*/
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;
}
4 years ago
// Check the whole area of the building
4 years ago
for (let x = rect.x; x < rect.x + rect.w; ++x) {
for (let y = rect.y; y < rect.y + rect.h; ++y) {
// 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;
}
4 years ago
}
}
// Perform additional placement checks
if (this.root.signals.prePlacementCheck.dispatch(entity, offset) === STOP_PROPAGATION) {
return false;
}
return true;
}
/**
* Attempts to place the given building
* @param {object} param0
* @param {Vector} param0.origin
* @param {number} param0.rotation
* @param {number} param0.originalRotation
* @param {number} param0.rotationVariant
* @param {string} param0.variant
* @param {MetaBuilding} param0.building
* @returns {Entity}
4 years ago
*/
tryPlaceBuilding({ origin, rotation, rotationVariant, originalRotation, variant, building }) {
const entity = building.createEntity({
root: this.root,
origin,
rotation,
originalRotation,
rotationVariant,
variant,
});
if (this.checkCanPlaceEntity(entity)) {
this.freeEntityAreaBeforeBuild(entity);
this.root.map.placeStaticEntity(entity);
this.root.entityMgr.registerEntity(entity);
return entity;
}
return null;
4 years ago
}
/**
* Removes all entities with a RemovableMapEntityComponent which need to get
* removed before placing this entity
* @param {Entity} entity
4 years ago
*/
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");
4 years ago
}
}
}
}
// Perform other callbacks
this.root.signals.freeEntityAreaBeforeBuild.dispatch(entity);
4 years ago
}
/**
* Performs a bulk operation, not updating caches in the meantime
* @param {function} operation
*/
performBulkOperation(operation) {
logger.warn("Running bulk operation ...");
assert(!this.root.bulkOperationRunning, "Can not run two bulk operations twice");
this.root.bulkOperationRunning = true;
const now = performance.now();
const returnValue = operation();
const duration = performance.now() - now;
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;
}
4 years ago
/**
* 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);
this.root.entityMgr.processDestroyList();
4 years ago
return true;
}
/**
* Returns the acceptors and ejectors which affect the current tile
* @param {Vector} tile
* @returns {AcceptorsAndEjectorsAffectingTile}
*/
getEjectorsAndAcceptorsAtTile(tile) {
4 years ago
/** @type {EjectorsAffectingTile} */
let ejectors = [];
/** @type {AcceptorsAffectingTile} */
let acceptors = [];
// Well .. please ignore this code! :D
4 years ago
for (let dx = -1; dx <= 1; ++dx) {
for (let dy = -1; dy <= 1; ++dy) {
if (Math.abs(dx) + Math.abs(dy) !== 1) {
4 years ago
continue;
}
const entity = this.root.map.getLayerContentXY(tile.x + dx, tile.y + dy, enumLayer.regular);
if (entity) {
let ejectorSlots = [];
let acceptorSlots = [];
4 years ago
const staticComp = entity.components.StaticMapEntity;
const itemEjector = entity.components.ItemEjector;
const itemAcceptor = entity.components.ItemAcceptor;
const beltComp = entity.components.Belt;
4 years ago
if (itemEjector) {
ejectorSlots = itemEjector.slots.slice();
4 years ago
}
if (itemAcceptor) {
acceptorSlots = itemAcceptor.slots.slice();
}
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];
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];
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,
});
4 years ago
}
}
}
}
}
}
return { ejectors, acceptors };
}
}