1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2024-10-27 20:34:29 +00:00
tobspr_shapez.io/src/js/game/systems/belt_underlays.js

85 lines
3.2 KiB
JavaScript
Raw Normal View History

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";
2020-08-15 17:43:03 +00:00
import { MapChunkView } from "../map_chunk_view";
import { DrawParameters } from "../../core/draw_parameters";
export class BeltUnderlaysSystem extends GameSystemWithFilter {
constructor(root) {
super(root, [BeltUnderlaysComponent]);
this.underlayBeltSprites = [];
for (let i = 0; i < BELT_ANIM_COUNT; ++i) {
2020-08-14 10:14:15 +00:00
this.underlayBeltSprites.push(Loader.getSprite("sprites/belt/built/forward_" + i + ".png"));
}
}
/**
2020-08-15 17:43:03 +00:00
* Draws a given chunk
* @param {DrawParameters} parameters
* @param {MapChunkView} chunk
*/
2020-08-15 17:43:03 +00:00
drawChunk(parameters, chunk) {
// Limit speed to avoid belts going backwards
const speedMultiplier = Math.min(this.root.hubGoals.getBeltBaseSpeed(), 10);
2020-08-15 17:50:22 +00:00
const contents = chunk.containedEntitiesByLayer.regular;
2020-08-15 17:43:03 +00:00
for (let i = 0; i < contents.length; ++i) {
const entity = contents[i];
const underlayComp = entity.components.BeltUnderlays;
2020-08-15 20:52:16 +00:00
if (!underlayComp) {
continue;
}
2020-08-15 17:43:03 +00:00
2020-08-15 20:52:16 +00:00
const staticComp = entity.components.StaticMapEntity;
const underlays = underlayComp.underlays;
for (let i = 0; i < underlays.length; ++i) {
const { pos, direction } = underlays[i];
const transformedPos = staticComp.localTileToWorld(pos);
2020-08-15 20:52:16 +00:00
// Culling
if (!chunk.tileSpaceRectangle.containsPoint(transformedPos.x, transformedPos.y)) {
continue;
}
2020-08-15 20:52:16 +00:00
const destX = transformedPos.x * globalConfig.tileSize;
const destY = transformedPos.y * globalConfig.tileSize;
2020-08-15 20:52:16 +00:00
// Culling, #2
if (
!parameters.visibleRect.containsRect4Params(
destX,
destY,
globalConfig.tileSize,
globalConfig.tileSize
)
) {
continue;
}
2020-08-15 20:52:16 +00:00
const angle = enumDirectionToAngle[staticComp.localDirectionToWorld(direction)];
2020-08-15 20:52:16 +00:00
// SYNC with systems/belt.js:drawSingleEntity!
const animationIndex = Math.floor(
((this.root.time.realtimeNow() * speedMultiplier * BELT_ANIM_COUNT * 126) / 42) *
globalConfig.itemSpacingOnBelts
);
drawRotatedSprite({
parameters,
sprite: this.underlayBeltSprites[animationIndex % this.underlayBeltSprites.length],
x: destX + globalConfig.halfTileSize,
y: destY + globalConfig.halfTileSize,
angle: Math.radians(angle),
size: globalConfig.tileSize,
});
2020-08-15 17:43:03 +00:00
}
}
}
}