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/item_acceptor.js

94 lines
3.5 KiB
JavaScript
Raw Normal View History

2020-05-17 08:07:20 +00:00
import { GameSystemWithFilter } from "../game_system_with_filter";
import { globalConfig } from "../../core/config";
import { DrawParameters } from "../../core/draw_parameters";
import { Entity } from "../entity";
import { enumDirectionToVector, enumDirectionToAngle } from "../../core/vector";
import { ItemAcceptorComponent } from "../components/item_acceptor";
import { Loader } from "../../core/loader";
import { drawRotatedSprite } from "../../core/draw_utils";
2020-06-25 10:18:48 +00:00
import { BELT_ANIM_COUNT } from "./belt";
2020-06-27 09:03:34 +00:00
import { fastArrayDelete } from "../../core/utils";
2020-06-28 17:34:10 +00:00
import { enumLayer } from "../root";
2020-05-17 08:07:20 +00:00
export class ItemAcceptorSystem extends GameSystemWithFilter {
constructor(root) {
super(root, [ItemAcceptorComponent]);
}
update() {
const progress = this.root.dynamicTickrate.deltaSeconds * 2; // * 2 because its only a half tile
2020-05-17 08:07:20 +00:00
for (let i = 0; i < this.allEntities.length; ++i) {
const entity = this.allEntities[i];
const aceptorComp = entity.components.ItemAcceptor;
const animations = aceptorComp.itemConsumptionAnimations;
2020-05-17 08:07:20 +00:00
// Process item consumption animations to avoid items popping from the belts
for (let animIndex = 0; animIndex < animations.length; ++animIndex) {
const anim = animations[animIndex];
const layer = aceptorComp.slots[anim.slotIndex].layer;
anim.animProgress +=
progress *
this.root.hubGoals.getBeltBaseSpeed(layer) *
globalConfig.beltItemSpacingByLayer[layer];
2020-05-17 08:07:20 +00:00
if (anim.animProgress > 1) {
2020-06-27 09:03:34 +00:00
// Original
// animations.splice(animIndex, 1);
// Faster variant
fastArrayDelete(animations, animIndex);
2020-05-17 08:07:20 +00:00
animIndex -= 1;
}
}
}
}
2020-06-28 17:34:10 +00:00
/**
* Draws the acceptor items
* @param {DrawParameters} parameters
* @param {enumLayer} layer
*/
drawLayer(parameters, layer) {
this.forEachMatchingEntityOnScreen(parameters, this.drawEntityRegularLayer.bind(this, layer));
2020-05-17 08:07:20 +00:00
}
/**
2020-06-28 17:34:10 +00:00
* @param {enumLayer} layer
2020-05-17 08:07:20 +00:00
* @param {DrawParameters} parameters
* @param {Entity} entity
*/
2020-06-28 17:34:10 +00:00
drawEntityRegularLayer(layer, parameters, entity) {
2020-05-17 08:07:20 +00:00
const staticComp = entity.components.StaticMapEntity;
const acceptorComp = entity.components.ItemAcceptor;
2020-05-18 15:40:20 +00:00
if (!staticComp.shouldBeDrawn(parameters)) {
return;
}
2020-05-17 08:07:20 +00:00
for (let animIndex = 0; animIndex < acceptorComp.itemConsumptionAnimations.length; ++animIndex) {
const { item, slotIndex, animProgress, direction } = acceptorComp.itemConsumptionAnimations[
animIndex
];
const slotData = acceptorComp.slots[slotIndex];
2020-06-28 17:34:10 +00:00
if (slotData.layer !== layer) {
// Don't draw non-regular slots for now
continue;
}
2020-05-17 08:07:20 +00:00
2020-06-28 17:34:10 +00:00
const slotWorldPos = staticComp.applyRotationToVector(slotData.pos).add(staticComp.origin);
2020-05-17 08:07:20 +00:00
const fadeOutDirection = enumDirectionToVector[staticComp.localDirectionToWorld(direction)];
const finalTile = slotWorldPos.subScalars(
fadeOutDirection.x * (animProgress / 2 - 0.5),
fadeOutDirection.y * (animProgress / 2 - 0.5)
);
item.draw(
(finalTile.x + 0.5) * globalConfig.tileSize,
(finalTile.y + 0.5) * globalConfig.tileSize,
parameters
);
}
}
}