1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2026-03-02 03:39:21 +00:00

Initial take on wires

This commit is contained in:
tobspr
2020-06-24 22:23:10 +02:00
parent 97ef46bd52
commit 6677ff0a44
40 changed files with 1204 additions and 518 deletions

View File

@@ -0,0 +1,59 @@
import { types } from "../../savegame/serialization";
import { BaseItem } from "../base_item";
import { Component } from "../component";
import { ShapeItem } from "../items/shape_item";
const maxQueueSize = 10;
export class EnergyGeneratorComponent extends Component {
static getId() {
return "EnergyGenerator";
}
static getSchema() {
return {
requiredKey: types.string,
};
}
/**
*
* @param {object} param0
* @param {string} param0.requiredKey Which shape this generator needs, can be null if not computed yet
*/
constructor({ requiredKey }) {
super();
this.requiredKey = requiredKey;
/**
* Stores how many items are ready to be converted to energy
* @type {number}
*/
this.itemsInQueue = 0;
}
/**
*
* @param {BaseItem} item
*/
tryTakeItem(item) {
if (!(item instanceof ShapeItem)) {
// Not a shape
return false;
}
if (item.definition.getHash() !== this.requiredKey) {
// Not our shape
return false;
}
if (this.itemsInQueue >= maxQueueSize) {
// Queue is full
return false;
}
// Take item and put it into the queue
++this.itemsInQueue;
return true;
}
}

View File

@@ -0,0 +1,65 @@
import { Component } from "../component";
import { Vector } from "../../core/vector";
import { types } from "../../savegame/serialization";
/** @enum {string} */
export const enumPinSlotType = {
energyEjector: "energyEjector",
};
/** @typedef {{
* pos: Vector,
* type: enumPinSlotType
* }} WirePinSlotDefinition */
/** @typedef {{
* pos: Vector,
* type: enumPinSlotType,
* value: number
* }} WirePinSlot */
export class WiredPinsComponent extends Component {
static getId() {
return "WiredPins";
}
static getSchema() {
return {
slots: types.array(
types.structured({
pos: types.vector,
type: types.enum(enumPinSlotType),
value: types.float,
})
),
};
}
/**
*
* @param {object} param0
* @param {Array<WirePinSlotDefinition>} param0.slots
*/
constructor({ slots }) {
super();
this.setSlots(slots);
}
/**
* Sets the slots of this building
* @param {Array<WirePinSlotDefinition>} slots
*/
setSlots(slots) {
/** @type {Array<WirePinSlot>} */
this.slots = [];
for (let i = 0; i < slots.length; ++i) {
const slotData = slots[i];
this.slots.push({
pos: slotData.pos,
type: slotData.type,
value: 0.0,
});
}
}
}