1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2026-03-02 03:39:21 +00:00

Improve placement of belts

This commit is contained in:
Tobias Springer
2020-05-10 17:00:02 +02:00
parent 61bda596b6
commit 72476486b7
22 changed files with 335 additions and 94 deletions

View File

@@ -8,6 +8,8 @@ import { Entity } from "../entity";
import { GameSystemWithFilter } from "../game_system_with_filter";
import { enumDirection, enumDirectionToVector, Vector } from "../../core/vector";
import { MapChunkView } from "../map_chunk_view";
import { gMetaBuildingRegistry } from "../../core/global_registries";
import { MetaBeltBaseBuilding } from "../buildings/belt_base";
const BELT_ANIM_COUNT = 6;
@@ -51,6 +53,49 @@ export class BeltSystem extends GameSystemWithFilter {
Loader.getSprite("sprites/belt/right_5.png"),
],
};
this.root.signals.entityAdded.add(this.updateSurroundingBeltPlacement, this);
this.root.signals.entityDestroyed.add(this.updateSurroundingBeltPlacement, this);
}
/**
* Updates the belt placement after an entity has been added / deleted
* @param {Entity} entity
*/
updateSurroundingBeltPlacement(entity) {
const staticComp = entity.components.StaticMapEntity;
if (!staticComp) {
return;
}
const metaBelt = gMetaBuildingRegistry.findByClass(MetaBeltBaseBuilding);
// Compute affected area
const originalRect = staticComp.getTileSpaceBounds();
const affectedArea = originalRect.expandedInAllDirections(1);
for (let x = affectedArea.x; x < affectedArea.right(); ++x) {
for (let y = affectedArea.y; y < affectedArea.bottom(); ++y) {
if (!originalRect.containsPoint(x, y)) {
const targetEntity = this.root.map.getTileContentXY(x, y);
if (targetEntity) {
const targetBeltComp = targetEntity.components.Belt;
if (targetBeltComp) {
const targetStaticComp = targetEntity.components.StaticMapEntity;
const {
rotation,
rotationVariant,
} = metaBelt.computeOptimalDirectionAndRotationVariantAtTile(
this.root,
new Vector(x, y),
targetStaticComp.rotationDegrees
);
targetStaticComp.rotationDegrees = rotation;
metaBelt.updateRotationVariant(targetEntity, rotationVariant);
}
}
}
}
}
}
draw(parameters) {