mirror of
https://github.com/tobspr/shapez.io.git
synced 2025-06-13 13:04:03 +00:00
Implement chainable splitter system
This commit is contained in:
parent
bfa9e98050
commit
db30dccf4c
@ -1,10 +1,12 @@
|
|||||||
import { GameSystemWithFilter } from "../game_system_with_filter";
|
import { GameSystemWithFilter } from "../game_system_with_filter";
|
||||||
import { ChainableSplitterComponent } from "../components/chainable_splitter";
|
import { ChainableSplitterComponent } from "../components/chainable_splitter";
|
||||||
|
import { enumDirectionToVector, enumDirection } from "../../core/vector";
|
||||||
import { Entity } from "../entity";
|
import { Entity } from "../entity";
|
||||||
import { DrawParameters } from "../../core/draw_parameters";
|
import { DrawParameters } from "../../core/draw_parameters";
|
||||||
import { formatBigNumber, lerp } from "../../core/utils";
|
import { formatBigNumber, lerp } from "../../core/utils";
|
||||||
import { Loader } from "../../core/loader";
|
import { Loader } from "../../core/loader";
|
||||||
import { enumLayer } from "../root";
|
import { enumLayer } from "../root";
|
||||||
|
import { BaseItem } from "../base_item";
|
||||||
|
|
||||||
export class ChainableSplitterSystem extends GameSystemWithFilter {
|
export class ChainableSplitterSystem extends GameSystemWithFilter {
|
||||||
constructor(root) {
|
constructor(root) {
|
||||||
@ -15,19 +17,64 @@ export class ChainableSplitterSystem extends GameSystemWithFilter {
|
|||||||
for (let i = 0; i < this.allEntities.length; ++i) {
|
for (let i = 0; i < this.allEntities.length; ++i) {
|
||||||
const entity = this.allEntities[i];
|
const entity = this.allEntities[i];
|
||||||
const splitterComp = entity.components.ChainableSplitter;
|
const splitterComp = entity.components.ChainableSplitter;
|
||||||
|
|
||||||
if (splitterComp.inputItem === null) {
|
if (splitterComp.inputItem === null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const ejectorComp = entity.components.ItemEjector;
|
const leftEdgeEntity = this.getLeftEdgeEntity(entity);
|
||||||
|
this.reset = true;
|
||||||
const nextSlot = ejectorComp.getFirstFreeSlot(enumLayer.regular);
|
if (this.tryEject(leftEdgeEntity, splitterComp.inputItem)) {
|
||||||
if (nextSlot !== null) {
|
splitterComp.inputItem = null;
|
||||||
if (ejectorComp.tryEject(nextSlot, splitterComp.inputItem)) {
|
|
||||||
splitterComp.inputItem = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {Entity} entity
|
||||||
|
* @returns {Entity}
|
||||||
|
*/
|
||||||
|
getLeftEdgeEntity(entity) {
|
||||||
|
const leftEntity = this.getAdjacentEntity(entity, enumDirection.left);
|
||||||
|
if (leftEntity === null || !leftEntity.components.ChainableSplitter) {
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.getLeftEdgeEntity(leftEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {Entity} entity
|
||||||
|
* @param {BaseItem} item
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
tryEject(entity, item) {
|
||||||
|
const splitterComp = entity.components.ChainableSplitter;
|
||||||
|
if (!splitterComp.ejected) {
|
||||||
|
this.reset = false;
|
||||||
|
const ejectComp = entity.components.ItemEjector;
|
||||||
|
|
||||||
|
if (ejectComp.canEjectOnSlot(0)) {
|
||||||
|
if (ejectComp.tryEject(0, item)) {
|
||||||
|
splitterComp.ejected = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const rightEntity = this.getAdjacentEntity(entity, enumDirection.right);
|
||||||
|
if (
|
||||||
|
rightEntity !== null &&
|
||||||
|
rightEntity.components.ChainableSplitter &&
|
||||||
|
this.tryEject(rightEntity, item)
|
||||||
|
) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.reset) {
|
||||||
|
splitterComp.ejected = false;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user