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:
59
src/js/game/components/energy_generator.js
Normal file
59
src/js/game/components/energy_generator.js
Normal 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;
|
||||
}
|
||||
}
|
||||
65
src/js/game/components/wired_pins.js
Normal file
65
src/js/game/components/wired_pins.js
Normal 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,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user