Proper belt underlays for splitters
Before Width: | Height: | Size: 1.3 MiB After Width: | Height: | Size: 1.3 MiB |
@ -1471,6 +1471,6 @@
|
|||||||
"format": "RGBA8888",
|
"format": "RGBA8888",
|
||||||
"size": {"w":512,"h":1024},
|
"size": {"w":512,"h":1024},
|
||||||
"scale": "0.25",
|
"scale": "0.25",
|
||||||
"smartupdate": "$TexturePacker:SmartUpdate:d21082eda6f288e04b0739186004794d:0912211652d1c400e2846013f9de057b:908b89f5ca8ff73e331a35a3b14d0604$"
|
"smartupdate": "$TexturePacker:SmartUpdate:64d35c8d649d4bb41c7539e7e89c0865:2a9399f9a7c16dc686a4fb0941b02e6b:908b89f5ca8ff73e331a35a3b14d0604$"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Before Width: | Height: | Size: 280 KiB After Width: | Height: | Size: 281 KiB |
Before Width: | Height: | Size: 677 KiB After Width: | Height: | Size: 661 KiB |
Before Width: | Height: | Size: 23 KiB After Width: | Height: | Size: 22 KiB |
Before Width: | Height: | Size: 19 KiB After Width: | Height: | Size: 16 KiB |
@ -2,9 +2,8 @@ import { DrawParameters } from "./draw_parameters";
|
|||||||
import { Rectangle } from "./rectangle";
|
import { Rectangle } from "./rectangle";
|
||||||
import { round3Digits } from "./utils";
|
import { round3Digits } from "./utils";
|
||||||
|
|
||||||
const floorSpriteCoordinates = false;
|
|
||||||
|
|
||||||
export const ORIGINAL_SPRITE_SCALE = "0.75";
|
export const ORIGINAL_SPRITE_SCALE = "0.75";
|
||||||
|
export const FULL_CLIP_RECT = new Rectangle(0, 0, 1, 1);
|
||||||
|
|
||||||
export class BaseSprite {
|
export class BaseSprite {
|
||||||
/**
|
/**
|
||||||
@ -200,45 +199,79 @@ export class AtlasSprite extends BaseSprite {
|
|||||||
destH = intersection.h;
|
destH = intersection.h;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (floorSpriteCoordinates) {
|
parameters.context.drawImage(
|
||||||
parameters.context.drawImage(
|
link.atlas,
|
||||||
link.atlas,
|
|
||||||
|
|
||||||
// atlas src pos
|
// atlas src pos
|
||||||
Math.floor(srcX),
|
srcX,
|
||||||
Math.floor(srcY),
|
srcY,
|
||||||
|
|
||||||
// atlas src size
|
// atlas src siize
|
||||||
Math.floor(srcW),
|
srcW,
|
||||||
Math.floor(srcH),
|
srcH,
|
||||||
|
|
||||||
// dest pos
|
// dest pos and size
|
||||||
Math.floor(destX),
|
destX,
|
||||||
Math.floor(destY),
|
destY,
|
||||||
|
destW,
|
||||||
|
destH
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// dest size
|
/**
|
||||||
Math.floor(destW),
|
* Draws a subset of the sprite. Does NO culling
|
||||||
Math.floor(destH)
|
* @param {DrawParameters} parameters
|
||||||
);
|
* @param {number} x
|
||||||
} else {
|
* @param {number} y
|
||||||
parameters.context.drawImage(
|
* @param {number} w
|
||||||
link.atlas,
|
* @param {number} h
|
||||||
|
* @param {Rectangle=} clipRect The rectangle in local space (0 ... 1) to draw of the image
|
||||||
// atlas src pos
|
*/
|
||||||
srcX,
|
drawCachedWithClipRect(parameters, x, y, w = null, h = null, clipRect = FULL_CLIP_RECT) {
|
||||||
srcY,
|
if (G_IS_DEV) {
|
||||||
|
assert(parameters instanceof DrawParameters, "Not a valid context");
|
||||||
// atlas src siize
|
assert(!!w && w > 0, "Not a valid width:" + w);
|
||||||
srcW,
|
assert(!!h && h > 0, "Not a valid height:" + h);
|
||||||
srcH,
|
assert(clipRect, "No clip rect given!");
|
||||||
|
|
||||||
// dest pos and size
|
|
||||||
destX,
|
|
||||||
destY,
|
|
||||||
destW,
|
|
||||||
destH
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const scale = parameters.desiredAtlasScale;
|
||||||
|
const link = this.linksByResolution[scale];
|
||||||
|
|
||||||
|
if (!link) {
|
||||||
|
assert(false, `Link not known: ${scale} (having ${Object.keys(this.linksByResolution)})`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const scaleW = w / link.w;
|
||||||
|
const scaleH = h / link.h;
|
||||||
|
|
||||||
|
let destX = x + link.packOffsetX * scaleW + clipRect.x * w;
|
||||||
|
let destY = y + link.packOffsetY * scaleH + clipRect.y * h;
|
||||||
|
let destW = link.packedW * scaleW * clipRect.w;
|
||||||
|
let destH = link.packedH * scaleH * clipRect.h;
|
||||||
|
|
||||||
|
let srcX = link.packedX + clipRect.x * link.packedW;
|
||||||
|
let srcY = link.packedY + clipRect.y * link.packedH;
|
||||||
|
let srcW = link.packedW * clipRect.w;
|
||||||
|
let srcH = link.packedH * clipRect.h;
|
||||||
|
|
||||||
|
parameters.context.drawImage(
|
||||||
|
link.atlas,
|
||||||
|
|
||||||
|
// atlas src pos
|
||||||
|
srcX,
|
||||||
|
srcY,
|
||||||
|
|
||||||
|
// atlas src siize
|
||||||
|
srcW,
|
||||||
|
srcH,
|
||||||
|
|
||||||
|
// dest pos and size
|
||||||
|
destX,
|
||||||
|
destY,
|
||||||
|
destW,
|
||||||
|
destH
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
|
import { Component } from "../game/component";
|
||||||
|
import { Entity } from "../game/entity";
|
||||||
|
import { globalConfig } from "./config";
|
||||||
import { createLogger } from "./logging";
|
import { createLogger } from "./logging";
|
||||||
import { Rectangle } from "./rectangle";
|
import { Rectangle } from "./rectangle";
|
||||||
import { globalConfig } from "./config";
|
|
||||||
|
|
||||||
const logger = createLogger("stale_areas");
|
const logger = createLogger("stale_areas");
|
||||||
|
|
||||||
@ -34,6 +36,40 @@ export class StaleAreaDetector {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Makes this detector recompute the area of an entity whenever
|
||||||
|
* it changes in any way
|
||||||
|
* @param {Array<typeof Component>} components
|
||||||
|
* @param {number} tilesAround
|
||||||
|
*/
|
||||||
|
recomputeOnComponentsChanged(components, tilesAround = 1) {
|
||||||
|
const componentIds = components.map(component => component.getId());
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Internal checker method
|
||||||
|
* @param {Entity} entity
|
||||||
|
*/
|
||||||
|
const checker = entity => {
|
||||||
|
// Check for all components
|
||||||
|
for (let i = 0; i < componentIds.length; ++i) {
|
||||||
|
if (entity.components[componentIds[i]]) {
|
||||||
|
// Entity is relevant, compute affected area
|
||||||
|
const area = entity.components.StaticMapEntity.getTileSpaceBounds().expandedInAllDirections(
|
||||||
|
tilesAround
|
||||||
|
);
|
||||||
|
this.invalidate(area);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
this.root.signals.entityAdded.add(checker);
|
||||||
|
this.root.signals.entityChanged.add(checker);
|
||||||
|
this.root.signals.entityComponentRemoved.add(checker);
|
||||||
|
this.root.signals.entityGotNewComponent.add(checker);
|
||||||
|
this.root.signals.entityDestroyed.add(checker);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the stale area
|
* Updates the stale area
|
||||||
*/
|
*/
|
||||||
|
@ -96,6 +96,7 @@ export class MetaSplitterBuilding extends MetaBuilding {
|
|||||||
entity.addComponent(
|
entity.addComponent(
|
||||||
new ItemEjectorComponent({
|
new ItemEjectorComponent({
|
||||||
slots: [], // set later
|
slots: [], // set later
|
||||||
|
renderFloatingItems: false,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -1,6 +1,29 @@
|
|||||||
import { Component } from "../component";
|
|
||||||
import { types } from "../../savegame/serialization";
|
|
||||||
import { enumDirection, Vector } from "../../core/vector";
|
import { enumDirection, Vector } from "../../core/vector";
|
||||||
|
import { Component } from "../component";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store which type an underlay is, this is cached so we can easily
|
||||||
|
* render it.
|
||||||
|
*
|
||||||
|
* Full: Render underlay at top and bottom of tile
|
||||||
|
* Bottom Only: Only render underlay at the bottom half
|
||||||
|
* Top Only:
|
||||||
|
* @enum {string}
|
||||||
|
*/
|
||||||
|
export const enumClippedBeltUnderlayType = {
|
||||||
|
full: "full",
|
||||||
|
bottomOnly: "bottomOnly",
|
||||||
|
topOnly: "topOnly",
|
||||||
|
none: "none",
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {{
|
||||||
|
* pos: Vector,
|
||||||
|
* direction: enumDirection,
|
||||||
|
* cachedType?: enumClippedBeltUnderlayType
|
||||||
|
* }} BeltUnderlayTile
|
||||||
|
*/
|
||||||
|
|
||||||
export class BeltUnderlaysComponent extends Component {
|
export class BeltUnderlaysComponent extends Component {
|
||||||
static getId() {
|
static getId() {
|
||||||
@ -24,9 +47,9 @@ export class BeltUnderlaysComponent extends Component {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {object} param0
|
* @param {object} param0
|
||||||
* @param {Array<{pos: Vector, direction: enumDirection}>=} param0.underlays Where to render belt underlays
|
* @param {Array<BeltUnderlayTile>=} param0.underlays Where to render belt underlays
|
||||||
*/
|
*/
|
||||||
constructor({ underlays }) {
|
constructor({ underlays = [] }) {
|
||||||
super();
|
super();
|
||||||
this.underlays = underlays;
|
this.underlays = underlays;
|
||||||
}
|
}
|
||||||
|
@ -47,6 +47,7 @@ export class ItemEjectorComponent extends Component {
|
|||||||
|
|
||||||
return new ItemEjectorComponent({
|
return new ItemEjectorComponent({
|
||||||
slots: slotsCopy,
|
slots: slotsCopy,
|
||||||
|
renderFloatingItems: this.renderFloatingItems,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,16 +55,13 @@ export class ItemEjectorComponent extends Component {
|
|||||||
*
|
*
|
||||||
* @param {object} param0
|
* @param {object} param0
|
||||||
* @param {Array<{pos: Vector, direction: enumDirection }>=} param0.slots The slots to eject on
|
* @param {Array<{pos: Vector, direction: enumDirection }>=} param0.slots The slots to eject on
|
||||||
|
* @param {boolean=} param0.renderFloatingItems Whether to render items even if they are not connected
|
||||||
*/
|
*/
|
||||||
constructor({ slots = [] }) {
|
constructor({ slots = [], renderFloatingItems = true }) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.setSlots(slots);
|
this.setSlots(slots);
|
||||||
|
this.renderFloatingItems = renderFloatingItems;
|
||||||
/**
|
|
||||||
* Whether this ejector slot is enabled
|
|
||||||
*/
|
|
||||||
this.enabled = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,12 +1,35 @@
|
|||||||
import { globalConfig } from "../../core/config";
|
import { globalConfig } from "../../core/config";
|
||||||
import { drawRotatedSprite } from "../../core/draw_utils";
|
|
||||||
import { Loader } from "../../core/loader";
|
|
||||||
import { enumDirectionToAngle } from "../../core/vector";
|
|
||||||
import { BeltUnderlaysComponent } from "../components/belt_underlays";
|
|
||||||
import { GameSystemWithFilter } from "../game_system_with_filter";
|
|
||||||
import { BELT_ANIM_COUNT } from "./belt";
|
|
||||||
import { MapChunkView } from "../map_chunk_view";
|
|
||||||
import { DrawParameters } from "../../core/draw_parameters";
|
import { DrawParameters } from "../../core/draw_parameters";
|
||||||
|
import { Loader } from "../../core/loader";
|
||||||
|
import { Rectangle } from "../../core/rectangle";
|
||||||
|
import { FULL_CLIP_RECT } from "../../core/sprites";
|
||||||
|
import { StaleAreaDetector } from "../../core/stale_area_detector";
|
||||||
|
import {
|
||||||
|
enumDirection,
|
||||||
|
enumDirectionToAngle,
|
||||||
|
enumDirectionToVector,
|
||||||
|
enumInvertedDirections,
|
||||||
|
Vector,
|
||||||
|
} from "../../core/vector";
|
||||||
|
import { BeltComponent } from "../components/belt";
|
||||||
|
import { BeltUnderlaysComponent, enumClippedBeltUnderlayType } from "../components/belt_underlays";
|
||||||
|
import { ItemAcceptorComponent } from "../components/item_acceptor";
|
||||||
|
import { ItemEjectorComponent } from "../components/item_ejector";
|
||||||
|
import { Entity } from "../entity";
|
||||||
|
import { GameSystemWithFilter } from "../game_system_with_filter";
|
||||||
|
import { MapChunkView } from "../map_chunk_view";
|
||||||
|
import { BELT_ANIM_COUNT } from "./belt";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mapping from underlay type to clip rect
|
||||||
|
* @type {Object<enumClippedBeltUnderlayType, Rectangle>}
|
||||||
|
*/
|
||||||
|
const enumUnderlayTypeToClipRect = {
|
||||||
|
[enumClippedBeltUnderlayType.none]: null,
|
||||||
|
[enumClippedBeltUnderlayType.full]: FULL_CLIP_RECT,
|
||||||
|
[enumClippedBeltUnderlayType.topOnly]: new Rectangle(0, 0, 1, 0.5),
|
||||||
|
[enumClippedBeltUnderlayType.bottomOnly]: new Rectangle(0, 0.5, 1, 0.5),
|
||||||
|
};
|
||||||
|
|
||||||
export class BeltUnderlaysSystem extends GameSystemWithFilter {
|
export class BeltUnderlaysSystem extends GameSystemWithFilter {
|
||||||
constructor(root) {
|
constructor(root) {
|
||||||
@ -17,6 +40,180 @@ export class BeltUnderlaysSystem extends GameSystemWithFilter {
|
|||||||
for (let i = 0; i < BELT_ANIM_COUNT; ++i) {
|
for (let i = 0; i < BELT_ANIM_COUNT; ++i) {
|
||||||
this.underlayBeltSprites.push(Loader.getSprite("sprites/belt/built/forward_" + i + ".png"));
|
this.underlayBeltSprites.push(Loader.getSprite("sprites/belt/built/forward_" + i + ".png"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Automatically recompute areas
|
||||||
|
this.staleArea = new StaleAreaDetector({
|
||||||
|
root,
|
||||||
|
name: "belt-underlay",
|
||||||
|
recomputeMethod: this.recomputeStaleArea.bind(this),
|
||||||
|
});
|
||||||
|
|
||||||
|
this.staleArea.recomputeOnComponentsChanged(
|
||||||
|
[BeltUnderlaysComponent, BeltComponent, ItemAcceptorComponent, ItemEjectorComponent],
|
||||||
|
1
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
update() {
|
||||||
|
this.staleArea.update();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when an area changed - Resets all caches in the given area
|
||||||
|
* @param {Rectangle} area
|
||||||
|
*/
|
||||||
|
recomputeStaleArea(area) {
|
||||||
|
for (let x = 0; x < area.w; ++x) {
|
||||||
|
for (let y = 0; y < area.h; ++y) {
|
||||||
|
const tileX = area.x + x;
|
||||||
|
const tileY = area.y + y;
|
||||||
|
const entity = this.root.map.getLayerContentXY(tileX, tileY, "regular");
|
||||||
|
if (entity) {
|
||||||
|
const underlayComp = entity.components.BeltUnderlays;
|
||||||
|
if (underlayComp) {
|
||||||
|
for (let i = 0; i < underlayComp.underlays.length; ++i) {
|
||||||
|
underlayComp.underlays[i].cachedType = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a given tile is connected and has an acceptor
|
||||||
|
* @param {Vector} tile
|
||||||
|
* @param {enumDirection} fromDirection
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
checkIsAcceptorConnected(tile, fromDirection) {
|
||||||
|
const contents = this.root.map.getLayerContentXY(tile.x, tile.y, "regular");
|
||||||
|
if (!contents) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const staticComp = contents.components.StaticMapEntity;
|
||||||
|
|
||||||
|
// Check if its a belt, since then its simple
|
||||||
|
const beltComp = contents.components.Belt;
|
||||||
|
if (beltComp) {
|
||||||
|
return staticComp.localDirectionToWorld(enumDirection.bottom) === fromDirection;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if there's an item acceptor
|
||||||
|
const acceptorComp = contents.components.ItemAcceptor;
|
||||||
|
if (acceptorComp) {
|
||||||
|
// Check each slot to see if its connected
|
||||||
|
for (let i = 0; i < acceptorComp.slots.length; ++i) {
|
||||||
|
const slot = acceptorComp.slots[i];
|
||||||
|
const slotTile = staticComp.localTileToWorld(slot.pos);
|
||||||
|
|
||||||
|
// Step 1: Check if the tile matches
|
||||||
|
if (!slotTile.equals(tile)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 2: Check if any of the directions matches
|
||||||
|
for (let j = 0; j < slot.directions.length; ++j) {
|
||||||
|
const slotDirection = staticComp.localDirectionToWorld(slot.directions[j]);
|
||||||
|
if (slotDirection === fromDirection) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a given tile is connected and has an ejector
|
||||||
|
* @param {Vector} tile
|
||||||
|
* @param {enumDirection} toDirection
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
checkIsEjectorConnected(tile, toDirection) {
|
||||||
|
const contents = this.root.map.getLayerContentXY(tile.x, tile.y, "regular");
|
||||||
|
if (!contents) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const staticComp = contents.components.StaticMapEntity;
|
||||||
|
|
||||||
|
// Check if its a belt, since then its simple
|
||||||
|
const beltComp = contents.components.Belt;
|
||||||
|
if (beltComp) {
|
||||||
|
return staticComp.localDirectionToWorld(beltComp.direction) === toDirection;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for an ejector
|
||||||
|
const ejectorComp = contents.components.ItemEjector;
|
||||||
|
if (ejectorComp) {
|
||||||
|
// Check each slot to see if its connected
|
||||||
|
for (let i = 0; i < ejectorComp.slots.length; ++i) {
|
||||||
|
const slot = ejectorComp.slots[i];
|
||||||
|
const slotTile = staticComp.localTileToWorld(slot.pos);
|
||||||
|
|
||||||
|
// Step 1: Check if the tile matches
|
||||||
|
if (!slotTile.equals(tile)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 2: Check if the direction matches
|
||||||
|
const slotDirection = staticComp.localDirectionToWorld(slot.direction);
|
||||||
|
if (slotDirection === toDirection) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Computes the flag for a given tile
|
||||||
|
* @param {Entity} entity
|
||||||
|
* @param {import("../components/belt_underlays").BeltUnderlayTile} underlayTile
|
||||||
|
* @returns {enumClippedBeltUnderlayType} The type of the underlay
|
||||||
|
*/
|
||||||
|
computeBeltUnderlayType(entity, underlayTile) {
|
||||||
|
if (underlayTile.cachedType) {
|
||||||
|
return underlayTile.cachedType;
|
||||||
|
}
|
||||||
|
|
||||||
|
const staticComp = entity.components.StaticMapEntity;
|
||||||
|
|
||||||
|
const transformedPos = staticComp.localTileToWorld(underlayTile.pos);
|
||||||
|
const destX = transformedPos.x * globalConfig.tileSize;
|
||||||
|
const destY = transformedPos.y * globalConfig.tileSize;
|
||||||
|
|
||||||
|
// Extract direction and angle
|
||||||
|
const worldDirection = staticComp.localDirectionToWorld(underlayTile.direction);
|
||||||
|
const worldDirectionVector = enumDirectionToVector[worldDirection];
|
||||||
|
|
||||||
|
// Figure out if there is anything connected at the top
|
||||||
|
const connectedTop = this.checkIsAcceptorConnected(
|
||||||
|
transformedPos.add(worldDirectionVector),
|
||||||
|
enumInvertedDirections[worldDirection]
|
||||||
|
);
|
||||||
|
|
||||||
|
// Figure out if there is anything connected at the bottom
|
||||||
|
const connectedBottom = this.checkIsEjectorConnected(
|
||||||
|
transformedPos.sub(worldDirectionVector),
|
||||||
|
worldDirection
|
||||||
|
);
|
||||||
|
|
||||||
|
let flag = enumClippedBeltUnderlayType.none;
|
||||||
|
|
||||||
|
if (connectedTop && connectedBottom) {
|
||||||
|
flag = enumClippedBeltUnderlayType.full;
|
||||||
|
} else if (connectedTop) {
|
||||||
|
flag = enumClippedBeltUnderlayType.topOnly;
|
||||||
|
} else if (connectedBottom) {
|
||||||
|
flag = enumClippedBeltUnderlayType.bottomOnly;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (underlayTile.cachedType = flag);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -39,18 +236,18 @@ export class BeltUnderlaysSystem extends GameSystemWithFilter {
|
|||||||
const staticComp = entity.components.StaticMapEntity;
|
const staticComp = entity.components.StaticMapEntity;
|
||||||
const underlays = underlayComp.underlays;
|
const underlays = underlayComp.underlays;
|
||||||
for (let i = 0; i < underlays.length; ++i) {
|
for (let i = 0; i < underlays.length; ++i) {
|
||||||
|
// Extract underlay parameters
|
||||||
const { pos, direction } = underlays[i];
|
const { pos, direction } = underlays[i];
|
||||||
const transformedPos = staticComp.localTileToWorld(pos);
|
const transformedPos = staticComp.localTileToWorld(pos);
|
||||||
|
const destX = transformedPos.x * globalConfig.tileSize;
|
||||||
|
const destY = transformedPos.y * globalConfig.tileSize;
|
||||||
|
|
||||||
// Culling
|
// Culling, Part 1: Check if the chunk contains the tile
|
||||||
if (!chunk.tileSpaceRectangle.containsPoint(transformedPos.x, transformedPos.y)) {
|
if (!chunk.tileSpaceRectangle.containsPoint(transformedPos.x, transformedPos.y)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const destX = transformedPos.x * globalConfig.tileSize;
|
// Culling, Part 2: Check if the overlay is visible
|
||||||
const destY = transformedPos.y * globalConfig.tileSize;
|
|
||||||
|
|
||||||
// Culling, #2
|
|
||||||
if (
|
if (
|
||||||
!parameters.visibleRect.containsRect4Params(
|
!parameters.visibleRect.containsRect4Params(
|
||||||
destX,
|
destX,
|
||||||
@ -62,22 +259,41 @@ export class BeltUnderlaysSystem extends GameSystemWithFilter {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const angle = enumDirectionToAngle[staticComp.localDirectionToWorld(direction)];
|
// Extract direction and angle
|
||||||
|
const worldDirection = staticComp.localDirectionToWorld(direction);
|
||||||
|
const angle = enumDirectionToAngle[worldDirection];
|
||||||
|
|
||||||
|
const underlayType = this.computeBeltUnderlayType(entity, underlays[i]);
|
||||||
|
const clipRect = enumUnderlayTypeToClipRect[underlayType];
|
||||||
|
if (!clipRect) {
|
||||||
|
// Empty
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actually draw the sprite
|
||||||
|
const x = destX + globalConfig.halfTileSize;
|
||||||
|
const y = destY + globalConfig.halfTileSize;
|
||||||
|
const angleRadians = Math.radians(angle);
|
||||||
|
|
||||||
// SYNC with systems/belt.js:drawSingleEntity!
|
// SYNC with systems/belt.js:drawSingleEntity!
|
||||||
const animationIndex = Math.floor(
|
const animationIndex = Math.floor(
|
||||||
((this.root.time.realtimeNow() * speedMultiplier * BELT_ANIM_COUNT * 126) / 42) *
|
((this.root.time.realtimeNow() * speedMultiplier * BELT_ANIM_COUNT * 126) / 42) *
|
||||||
globalConfig.itemSpacingOnBelts
|
globalConfig.itemSpacingOnBelts
|
||||||
);
|
);
|
||||||
|
parameters.context.translate(x, y);
|
||||||
drawRotatedSprite({
|
parameters.context.rotate(angleRadians);
|
||||||
|
this.underlayBeltSprites[
|
||||||
|
animationIndex % this.underlayBeltSprites.length
|
||||||
|
].drawCachedWithClipRect(
|
||||||
parameters,
|
parameters,
|
||||||
sprite: this.underlayBeltSprites[animationIndex % this.underlayBeltSprites.length],
|
-globalConfig.halfTileSize,
|
||||||
x: destX + globalConfig.halfTileSize,
|
-globalConfig.halfTileSize,
|
||||||
y: destY + globalConfig.halfTileSize,
|
globalConfig.tileSize,
|
||||||
angle: Math.radians(angle),
|
globalConfig.tileSize,
|
||||||
size: globalConfig.tileSize,
|
clipRect
|
||||||
});
|
);
|
||||||
|
parameters.context.rotate(-angleRadians);
|
||||||
|
parameters.context.translate(-x, -y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -198,9 +198,6 @@ export class ItemEjectorSystem extends GameSystemWithFilter {
|
|||||||
for (let i = 0; i < this.allEntities.length; ++i) {
|
for (let i = 0; i < this.allEntities.length; ++i) {
|
||||||
const sourceEntity = this.allEntities[i];
|
const sourceEntity = this.allEntities[i];
|
||||||
const sourceEjectorComp = sourceEntity.components.ItemEjector;
|
const sourceEjectorComp = sourceEntity.components.ItemEjector;
|
||||||
if (!sourceEjectorComp.enabled) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const slots = sourceEjectorComp.slots;
|
const slots = sourceEjectorComp.slots;
|
||||||
for (let j = 0; j < slots.length; ++j) {
|
for (let j = 0; j < slots.length; ++j) {
|
||||||
@ -211,8 +208,6 @@ export class ItemEjectorSystem extends GameSystemWithFilter {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const targetEntity = sourceSlot.cachedTargetEntity;
|
|
||||||
|
|
||||||
// Advance items on the slot
|
// Advance items on the slot
|
||||||
sourceSlot.progress = Math.min(
|
sourceSlot.progress = Math.min(
|
||||||
1,
|
1,
|
||||||
@ -245,15 +240,16 @@ export class ItemEjectorSystem extends GameSystemWithFilter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check if the target acceptor can actually accept this item
|
// Check if the target acceptor can actually accept this item
|
||||||
|
const destEntity = sourceSlot.cachedTargetEntity;
|
||||||
const destSlot = sourceSlot.cachedDestSlot;
|
const destSlot = sourceSlot.cachedDestSlot;
|
||||||
if (destSlot) {
|
if (destSlot) {
|
||||||
const targetAcceptorComp = targetEntity.components.ItemAcceptor;
|
const targetAcceptorComp = destEntity.components.ItemAcceptor;
|
||||||
if (!targetAcceptorComp.canAcceptItem(destSlot.index, item)) {
|
if (!targetAcceptorComp.canAcceptItem(destSlot.index, item)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to hand over the item
|
// Try to hand over the item
|
||||||
if (this.tryPassOverItem(item, targetEntity, destSlot.index)) {
|
if (this.tryPassOverItem(item, destEntity, destSlot.index)) {
|
||||||
// Handover successful, clear slot
|
// Handover successful, clear slot
|
||||||
targetAcceptorComp.onItemAccepted(destSlot.index, destSlot.acceptedDirection, item);
|
targetAcceptorComp.onItemAccepted(destSlot.index, destSlot.acceptedDirection, item);
|
||||||
sourceSlot.item = null;
|
sourceSlot.item = null;
|
||||||
@ -357,6 +353,11 @@ export class ItemEjectorSystem extends GameSystemWithFilter {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!ejectorComp.renderFloatingItems && !slot.cachedTargetEntity) {
|
||||||
|
// Not connected to any building
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
const realPosition = staticComp.localTileToWorld(slot.pos);
|
const realPosition = staticComp.localTileToWorld(slot.pos);
|
||||||
if (!chunk.tileSpaceRectangle.containsPoint(realPosition.x, realPosition.y)) {
|
if (!chunk.tileSpaceRectangle.containsPoint(realPosition.x, realPosition.y)) {
|
||||||
// Not within this chunk
|
// Not within this chunk
|
||||||
|