1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-06-06 09:34:05 +00:00
tobspr_shapez.io/src/js/game/systems/miner.js

149 lines
5.4 KiB
JavaScript
Raw Normal View History

2020-05-09 14:45:23 +00:00
import { globalConfig } from "../../core/config";
import { DrawParameters } from "../../core/draw_parameters";
import { enumDirectionToVector } from "../../core/vector";
import { BaseItem } from "../base_item";
2020-05-09 14:45:23 +00:00
import { MinerComponent } from "../components/miner";
import { Entity } from "../entity";
2020-05-09 14:45:23 +00:00
import { GameSystemWithFilter } from "../game_system_with_filter";
import { MapChunkView } from "../map_chunk_view";
export class MinerSystem extends GameSystemWithFilter {
constructor(root) {
super(root, [MinerComponent]);
}
update() {
2020-05-30 15:50:29 +00:00
let miningSpeed = this.root.hubGoals.getMinerBaseSpeed();
if (G_IS_DEV && globalConfig.debug.instantMiners) {
miningSpeed *= 100;
}
2020-05-09 14:45:23 +00:00
for (let i = 0; i < this.allEntities.length; ++i) {
const entity = this.allEntities[i];
2020-05-27 13:03:36 +00:00
// Check if miner is above an actual tile
2020-05-09 14:45:23 +00:00
const minerComp = entity.components.Miner;
const staticComp = entity.components.StaticMapEntity;
2020-05-27 13:03:36 +00:00
const tileBelow = this.root.map.getLowerLayerContentXY(staticComp.origin.x, staticComp.origin.y);
if (!tileBelow) {
continue;
}
// First, try to get rid of chained items
if (minerComp.itemChainBuffer.length > 0) {
if (this.tryPerformMinerEject(entity, minerComp.itemChainBuffer[0])) {
minerComp.itemChainBuffer.shift();
2020-05-09 14:45:23 +00:00
continue;
}
}
2020-05-09 14:45:23 +00:00
if (this.root.time.isIngameTimerExpired(minerComp.lastMiningTime, 1 / miningSpeed)) {
2020-05-09 14:45:23 +00:00
const lowerLayerItem = this.root.map.getLowerLayerContentXY(
staticComp.origin.x,
staticComp.origin.y
);
// TODO: Should not be required actually
2020-05-09 14:45:23 +00:00
if (!lowerLayerItem) {
// Nothing below;
continue;
}
if (this.tryPerformMinerEject(entity, lowerLayerItem)) {
// Analytics hook
this.root.signals.itemProduced.dispatch(lowerLayerItem);
// Actually mine
minerComp.lastMiningTime = this.root.time.now();
}
}
}
}
/**
*
* @param {Entity} entity
* @param {BaseItem} item
*/
tryPerformMinerEject(entity, item) {
const minerComp = entity.components.Miner;
const ejectComp = entity.components.ItemEjector;
const staticComp = entity.components.StaticMapEntity;
2020-05-13 16:04:51 +00:00
// Check if we are a chained miner
if (minerComp.chainable) {
const ejectingSlot = ejectComp.slots[0];
const ejectingPos = staticComp.localTileToWorld(ejectingSlot.pos);
const ejectingDirection = staticComp.localDirectionToWorld(ejectingSlot.direction);
const targetTile = ejectingPos.add(enumDirectionToVector[ejectingDirection]);
const targetContents = this.root.map.getTileContent(targetTile);
// Check if we are connected to another miner and thus do not eject directly
if (targetContents) {
const targetMinerComp = targetContents.components.Miner;
if (targetMinerComp) {
if (targetMinerComp.tryAcceptChainedItem(item)) {
return true;
} else {
return false;
}
2020-05-09 14:45:23 +00:00
}
}
}
// Seems we are a regular miner or at the end of a row, try actually ejecting
if (ejectComp.tryEject(0, item)) {
return true;
}
return false;
2020-05-09 14:45:23 +00:00
}
/**
*
* @param {DrawParameters} parameters
* @param {MapChunkView} chunk
*/
drawChunk(parameters, chunk) {
const contents = chunk.contents;
for (let y = 0; y < globalConfig.mapChunkSize; ++y) {
for (let x = 0; x < globalConfig.mapChunkSize; ++x) {
const entity = contents[x][y];
if (entity && entity.components.Miner) {
const staticComp = entity.components.StaticMapEntity;
2020-05-18 15:40:20 +00:00
if (!staticComp.shouldBeDrawn(parameters)) {
continue;
2020-05-18 15:40:20 +00:00
}
2020-05-09 14:45:23 +00:00
const lowerLayerItem = this.root.map.getLowerLayerContentXY(
staticComp.origin.x,
staticComp.origin.y
);
if (lowerLayerItem) {
const padding = 3;
parameters.context.fillStyle = lowerLayerItem.getBackgroundColorAsResource();
parameters.context.fillRect(
staticComp.origin.x * globalConfig.tileSize + padding,
staticComp.origin.y * globalConfig.tileSize + padding,
globalConfig.tileSize - 2 * padding,
globalConfig.tileSize - 2 * padding
);
}
if (lowerLayerItem) {
lowerLayerItem.draw(
(0.5 + staticComp.origin.x) * globalConfig.tileSize,
(0.5 + staticComp.origin.y) * globalConfig.tileSize,
parameters
);
}
}
}
}
}
}