2020-06-24 18:25:43 +00:00
|
|
|
import { Math_min } from "../../core/builtins";
|
2020-05-09 14:45:23 +00:00
|
|
|
import { globalConfig } from "../../core/config";
|
|
|
|
import { DrawParameters } from "../../core/draw_parameters";
|
2020-06-24 18:25:43 +00:00
|
|
|
import { createLogger } from "../../core/logging";
|
|
|
|
import { Rectangle } from "../../core/rectangle";
|
2020-05-09 14:45:23 +00:00
|
|
|
import { enumDirectionToVector, Vector } from "../../core/vector";
|
|
|
|
import { BaseItem } from "../base_item";
|
|
|
|
import { ItemEjectorComponent } from "../components/item_ejector";
|
|
|
|
import { Entity } from "../entity";
|
|
|
|
import { GameSystemWithFilter } from "../game_system_with_filter";
|
2020-06-14 12:37:13 +00:00
|
|
|
|
|
|
|
const logger = createLogger("systems/ejector");
|
2020-05-09 14:45:23 +00:00
|
|
|
|
|
|
|
export class ItemEjectorSystem extends GameSystemWithFilter {
|
|
|
|
constructor(root) {
|
|
|
|
super(root, [ItemEjectorComponent]);
|
2020-06-14 12:37:13 +00:00
|
|
|
|
2020-06-24 18:25:43 +00:00
|
|
|
this.root.signals.entityAdded.add(this.checkForCacheInvalidation, this);
|
|
|
|
this.root.signals.entityDestroyed.add(this.checkForCacheInvalidation, this);
|
|
|
|
this.root.signals.postLoadHook.add(this.recomputeCache, this);
|
2020-06-17 01:50:16 +00:00
|
|
|
|
|
|
|
/**
|
2020-06-24 18:25:43 +00:00
|
|
|
* @type {Rectangle}
|
2020-06-17 01:50:16 +00:00
|
|
|
*/
|
2020-06-24 18:25:43 +00:00
|
|
|
this.areaToRecompute = null;
|
2020-05-09 14:45:23 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 01:50:16 +00:00
|
|
|
/**
|
2020-06-17 02:48:29 +00:00
|
|
|
*
|
|
|
|
* @param {Entity} entity
|
2020-06-17 01:50:16 +00:00
|
|
|
*/
|
2020-06-24 18:25:43 +00:00
|
|
|
checkForCacheInvalidation(entity) {
|
|
|
|
if (!this.root.gameInitialized) {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-17 01:50:16 +00:00
|
|
|
if (!entity.components.StaticMapEntity) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optimize for the common case: adding or removing one building at a time. Clicking
|
|
|
|
// and dragging can cause up to 4 add/remove signals.
|
|
|
|
const staticComp = entity.components.StaticMapEntity;
|
|
|
|
const bounds = staticComp.getTileSpaceBounds();
|
|
|
|
const expandedBounds = bounds.expandedInAllDirections(2);
|
2020-06-24 18:25:43 +00:00
|
|
|
|
|
|
|
if (this.areaToRecompute) {
|
|
|
|
this.areaToRecompute = this.areaToRecompute.getUnion(expandedBounds);
|
|
|
|
} else {
|
|
|
|
this.areaToRecompute = expandedBounds;
|
|
|
|
}
|
2020-06-14 12:37:13 +00:00
|
|
|
}
|
2020-05-30 15:50:29 +00:00
|
|
|
|
2020-06-14 12:37:13 +00:00
|
|
|
/**
|
|
|
|
* Precomputes the cache, which makes up for a huge performance improvement
|
|
|
|
*/
|
|
|
|
recomputeCache() {
|
2020-06-24 18:25:43 +00:00
|
|
|
if (this.areaToRecompute) {
|
|
|
|
logger.log("Recomputing cache using rectangle");
|
|
|
|
this.recomputeAreaCache(this.areaToRecompute);
|
|
|
|
this.areaToRecompute = null;
|
2020-06-17 01:50:16 +00:00
|
|
|
} else {
|
2020-06-24 18:25:43 +00:00
|
|
|
logger.log("Full cache recompute");
|
2020-06-17 01:50:16 +00:00
|
|
|
// Try to find acceptors for every ejector
|
|
|
|
for (let i = 0; i < this.allEntities.length; ++i) {
|
|
|
|
const entity = this.allEntities[i];
|
2020-06-24 18:25:43 +00:00
|
|
|
this.recomputeSingleEntityCache(entity);
|
2020-06-17 01:50:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-09 14:45:23 +00:00
|
|
|
|
2020-06-17 01:50:16 +00:00
|
|
|
/**
|
2020-06-17 02:48:29 +00:00
|
|
|
*
|
|
|
|
* @param {Rectangle} area
|
2020-06-17 01:50:16 +00:00
|
|
|
*/
|
2020-06-24 18:25:43 +00:00
|
|
|
recomputeAreaCache(area) {
|
2020-06-17 01:50:16 +00:00
|
|
|
let entryCount = 0;
|
2020-06-24 18:25:43 +00:00
|
|
|
|
|
|
|
logger.log("Recomputing area:", area.x, area.y, "/", area.w, area.h);
|
|
|
|
|
|
|
|
// Store the entities we already recomputed, so we don't do work twice
|
|
|
|
const recomputedEntities = new Set();
|
|
|
|
|
2020-06-17 01:50:16 +00:00
|
|
|
for (let x = area.x; x < area.right(); ++x) {
|
|
|
|
for (let y = area.y; y < area.bottom(); ++y) {
|
|
|
|
const entity = this.root.map.getTileContentXY(x, y);
|
2020-06-24 18:25:43 +00:00
|
|
|
if (entity) {
|
|
|
|
// Recompute the entity in case its relevant for this system and it
|
|
|
|
// hasn't already been computed
|
|
|
|
if (!recomputedEntities.has(entity.uid) && entity.components.ItemEjector) {
|
|
|
|
recomputedEntities.add(entity.uid);
|
|
|
|
this.recomputeSingleEntityCache(entity);
|
|
|
|
}
|
2020-05-09 14:45:23 +00:00
|
|
|
}
|
2020-06-17 01:50:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return entryCount;
|
|
|
|
}
|
2020-05-09 14:45:23 +00:00
|
|
|
|
2020-06-17 01:50:16 +00:00
|
|
|
/**
|
2020-06-17 02:48:29 +00:00
|
|
|
* @param {Entity} entity
|
2020-06-17 01:50:16 +00:00
|
|
|
*/
|
|
|
|
recomputeSingleEntityCache(entity) {
|
|
|
|
const ejectorComp = entity.components.ItemEjector;
|
|
|
|
const staticComp = entity.components.StaticMapEntity;
|
2020-05-09 14:45:23 +00:00
|
|
|
|
2020-06-17 01:50:16 +00:00
|
|
|
// Clear the old cache.
|
|
|
|
ejectorComp.cachedConnectedSlots = null;
|
2020-05-09 14:45:23 +00:00
|
|
|
|
2020-06-17 01:50:16 +00:00
|
|
|
for (let ejectorSlotIndex = 0; ejectorSlotIndex < ejectorComp.slots.length; ++ejectorSlotIndex) {
|
|
|
|
const ejectorSlot = ejectorComp.slots[ejectorSlotIndex];
|
|
|
|
|
|
|
|
// Clear the old cache.
|
|
|
|
ejectorSlot.cachedDestSlot = null;
|
|
|
|
ejectorSlot.cachedTargetEntity = null;
|
|
|
|
|
|
|
|
// Figure out where and into which direction we eject items
|
|
|
|
const ejectSlotWsTile = staticComp.localTileToWorld(ejectorSlot.pos);
|
|
|
|
const ejectSlotWsDirection = staticComp.localDirectionToWorld(ejectorSlot.direction);
|
|
|
|
const ejectSlotWsDirectionVector = enumDirectionToVector[ejectSlotWsDirection];
|
|
|
|
const ejectSlotTargetWsTile = ejectSlotWsTile.add(ejectSlotWsDirectionVector);
|
|
|
|
|
|
|
|
// Try to find the given acceptor component to take the item
|
|
|
|
const targetEntity = this.root.map.getTileContent(ejectSlotTargetWsTile);
|
|
|
|
if (!targetEntity) {
|
|
|
|
// No consumer for item
|
|
|
|
continue;
|
2020-06-14 12:37:13 +00:00
|
|
|
}
|
2020-05-09 14:45:23 +00:00
|
|
|
|
2020-06-17 01:50:16 +00:00
|
|
|
const targetAcceptorComp = targetEntity.components.ItemAcceptor;
|
|
|
|
const targetStaticComp = targetEntity.components.StaticMapEntity;
|
|
|
|
if (!targetAcceptorComp) {
|
|
|
|
// Entity doesn't accept items
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const matchingSlot = targetAcceptorComp.findMatchingSlot(
|
|
|
|
targetStaticComp.worldToLocalTile(ejectSlotTargetWsTile),
|
|
|
|
targetStaticComp.worldDirectionToLocal(ejectSlotWsDirection)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!matchingSlot) {
|
|
|
|
// No matching slot found
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ok we found a connection
|
|
|
|
if (ejectorComp.cachedConnectedSlots) {
|
|
|
|
ejectorComp.cachedConnectedSlots.push(ejectorSlot);
|
|
|
|
} else {
|
|
|
|
ejectorComp.cachedConnectedSlots = [ejectorSlot];
|
|
|
|
}
|
|
|
|
ejectorSlot.cachedTargetEntity = targetEntity;
|
|
|
|
ejectorSlot.cachedDestSlot = matchingSlot;
|
|
|
|
}
|
2020-06-14 12:37:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
update() {
|
2020-06-24 18:25:43 +00:00
|
|
|
if (this.areaToRecompute) {
|
2020-06-14 12:37:13 +00:00
|
|
|
this.recomputeCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Precompute effective belt speed
|
|
|
|
const effectiveBeltSpeed = this.root.hubGoals.getBeltBaseSpeed() * globalConfig.itemSpacingOnBelts;
|
|
|
|
let progressGrowth = (effectiveBeltSpeed / 0.5) * this.root.dynamicTickrate.deltaSeconds;
|
|
|
|
|
|
|
|
if (G_IS_DEV && globalConfig.debug.instantBelts) {
|
|
|
|
progressGrowth = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go over all cache entries
|
2020-06-17 01:11:26 +00:00
|
|
|
for (let i = 0; i < this.allEntities.length; ++i) {
|
|
|
|
const sourceEntity = this.allEntities[i];
|
|
|
|
const sourceEjectorComp = sourceEntity.components.ItemEjector;
|
|
|
|
if (!sourceEjectorComp.cachedConnectedSlots) {
|
2020-06-14 12:37:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-06-17 01:11:26 +00:00
|
|
|
for (let j = 0; j < sourceEjectorComp.cachedConnectedSlots.length; j++) {
|
|
|
|
const sourceSlot = sourceEjectorComp.cachedConnectedSlots[j];
|
|
|
|
const destSlot = sourceSlot.cachedDestSlot;
|
|
|
|
const targetEntity = sourceSlot.cachedTargetEntity;
|
2020-06-14 12:37:13 +00:00
|
|
|
|
2020-06-17 01:11:26 +00:00
|
|
|
const item = sourceSlot.item;
|
2020-06-14 12:37:13 +00:00
|
|
|
|
2020-06-17 01:11:26 +00:00
|
|
|
if (!item) {
|
|
|
|
// No item available to be ejected
|
|
|
|
continue;
|
|
|
|
}
|
2020-06-14 12:37:13 +00:00
|
|
|
|
2020-06-17 01:11:26 +00:00
|
|
|
// Advance items on the slot
|
|
|
|
sourceSlot.progress = Math_min(1, sourceSlot.progress + progressGrowth);
|
2020-06-14 12:37:13 +00:00
|
|
|
|
2020-06-17 01:11:26 +00:00
|
|
|
// Check if we are still in the process of ejecting, can't proceed then
|
|
|
|
if (sourceSlot.progress < 1.0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the target acceptor can actually accept this item
|
|
|
|
const targetAcceptorComp = targetEntity.components.ItemAcceptor;
|
|
|
|
if (!targetAcceptorComp.canAcceptItem(destSlot.index, item)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to hand over the item
|
|
|
|
if (this.tryPassOverItem(item, targetEntity, destSlot.index)) {
|
|
|
|
// Handover successful, clear slot
|
|
|
|
targetAcceptorComp.onItemAccepted(destSlot.index, destSlot.acceptedDirection, item);
|
|
|
|
sourceSlot.item = null;
|
|
|
|
continue;
|
|
|
|
}
|
2020-05-09 14:45:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {BaseItem} item
|
|
|
|
* @param {Entity} receiver
|
|
|
|
* @param {number} slotIndex
|
|
|
|
*/
|
2020-05-17 08:07:20 +00:00
|
|
|
tryPassOverItem(item, receiver, slotIndex) {
|
2020-05-09 14:45:23 +00:00
|
|
|
// Try figuring out how what to do with the item
|
|
|
|
// TODO: Kinda hacky. How to solve this properly? Don't want to go through inheritance hell.
|
|
|
|
// Also its just a few cases (hope it stays like this .. :x).
|
|
|
|
|
|
|
|
const beltComp = receiver.components.Belt;
|
|
|
|
if (beltComp) {
|
|
|
|
// Ayy, its a belt!
|
2020-05-20 13:51:06 +00:00
|
|
|
if (beltComp.canAcceptItem()) {
|
|
|
|
beltComp.takeItem(item);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const storageComp = receiver.components.Storage;
|
|
|
|
if (storageComp) {
|
|
|
|
// It's a storage
|
|
|
|
if (storageComp.canAcceptItem(item)) {
|
|
|
|
storageComp.takeItem(item);
|
2020-05-09 14:45:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const itemProcessorComp = receiver.components.ItemProcessor;
|
|
|
|
if (itemProcessorComp) {
|
|
|
|
// Its an item processor ..
|
2020-05-17 08:07:20 +00:00
|
|
|
if (itemProcessorComp.tryTakeItem(item, slotIndex)) {
|
2020-05-09 14:45:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:58:30 +00:00
|
|
|
const undergroundBeltComp = receiver.components.UndergroundBelt;
|
|
|
|
if (undergroundBeltComp) {
|
2020-05-09 14:45:23 +00:00
|
|
|
// Its an underground belt. yay.
|
|
|
|
if (
|
2020-05-18 21:58:30 +00:00
|
|
|
undergroundBeltComp.tryAcceptExternalItem(
|
2020-05-09 14:45:23 +00:00
|
|
|
item,
|
|
|
|
this.root.hubGoals.getUndergroundBeltBaseSpeed()
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
draw(parameters) {
|
|
|
|
this.forEachMatchingEntityOnScreen(parameters, this.drawSingleEntity.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {DrawParameters} parameters
|
|
|
|
* @param {Entity} entity
|
|
|
|
*/
|
|
|
|
drawSingleEntity(parameters, entity) {
|
|
|
|
const ejectorComp = entity.components.ItemEjector;
|
|
|
|
const staticComp = entity.components.StaticMapEntity;
|
|
|
|
|
2020-05-18 15:40:20 +00:00
|
|
|
if (!staticComp.shouldBeDrawn(parameters)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-09 14:45:23 +00:00
|
|
|
for (let i = 0; i < ejectorComp.slots.length; ++i) {
|
|
|
|
const slot = ejectorComp.slots[i];
|
|
|
|
const ejectedItem = slot.item;
|
|
|
|
if (!ejectedItem) {
|
|
|
|
// No item
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-05-10 15:51:54 +00:00
|
|
|
const realPosition = slot.pos.rotateFastMultipleOf90(staticComp.rotation);
|
2020-05-09 14:45:23 +00:00
|
|
|
const realDirection = Vector.transformDirectionFromMultipleOf90(
|
|
|
|
slot.direction,
|
2020-05-10 15:51:54 +00:00
|
|
|
staticComp.rotation
|
2020-05-09 14:45:23 +00:00
|
|
|
);
|
|
|
|
const realDirectionVector = enumDirectionToVector[realDirection];
|
|
|
|
|
|
|
|
const tileX =
|
|
|
|
staticComp.origin.x + realPosition.x + 0.5 + realDirectionVector.x * 0.5 * slot.progress;
|
|
|
|
const tileY =
|
|
|
|
staticComp.origin.y + realPosition.y + 0.5 + realDirectionVector.y * 0.5 * slot.progress;
|
|
|
|
|
|
|
|
const worldX = tileX * globalConfig.tileSize;
|
|
|
|
const worldY = tileY * globalConfig.tileSize;
|
|
|
|
|
|
|
|
ejectedItem.draw(worldX, worldY, parameters);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|