mirror of
https://github.com/tobspr/shapez.io.git
synced 2026-03-02 03:39:21 +00:00
Refactor belt underlay feature into seperate component
This commit is contained in:
44
src/js/game/components/belt_underlays.js
Normal file
44
src/js/game/components/belt_underlays.js
Normal file
@@ -0,0 +1,44 @@
|
||||
import { Component } from "../component";
|
||||
import { types } from "../../savegame/serialization";
|
||||
import { enumDirection, Vector } from "../../core/vector";
|
||||
|
||||
export class BeltUnderlaysComponent extends Component {
|
||||
static getId() {
|
||||
return "BeltUnderlays";
|
||||
}
|
||||
|
||||
static getSchema() {
|
||||
return {
|
||||
underlays: types.array(
|
||||
types.structured({
|
||||
pos: types.vector,
|
||||
direction: types.enum(enumDirection),
|
||||
})
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
duplicateWithoutContents() {
|
||||
const beltUnderlaysCopy = [];
|
||||
for (let i = 0; i < this.underlays.length; ++i) {
|
||||
const underlay = this.underlays[i];
|
||||
beltUnderlaysCopy.push({
|
||||
pos: underlay.pos.copy(),
|
||||
direction: underlay.direction,
|
||||
});
|
||||
}
|
||||
|
||||
return new BeltUnderlaysComponent({
|
||||
underlays: beltUnderlaysCopy,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {object} param0
|
||||
* @param {Array<{pos: Vector, direction: enumDirection}>=} param0.underlays Where to render belt underlays
|
||||
*/
|
||||
constructor({ underlays }) {
|
||||
super();
|
||||
this.underlays = underlays;
|
||||
}
|
||||
}
|
||||
@@ -39,16 +39,6 @@ export class ItemAcceptorComponent extends Component {
|
||||
directions: types.array(types.enum(enumDirection)),
|
||||
filter: types.nullable(types.enum(enumItemType)),
|
||||
|
||||
// TODO: MIGRATE
|
||||
layer: types.enum(enumLayer),
|
||||
})
|
||||
),
|
||||
animated: types.bool,
|
||||
beltUnderlays: types.array(
|
||||
types.structured({
|
||||
pos: types.vector,
|
||||
direction: types.enum(enumDirection),
|
||||
|
||||
// TODO: MIGRATE
|
||||
layer: types.enum(enumLayer),
|
||||
})
|
||||
@@ -68,20 +58,8 @@ export class ItemAcceptorComponent extends Component {
|
||||
});
|
||||
}
|
||||
|
||||
const beltUnderlaysCopy = [];
|
||||
for (let i = 0; i < this.beltUnderlays.length; ++i) {
|
||||
const underlay = this.beltUnderlays[i];
|
||||
beltUnderlaysCopy.push({
|
||||
pos: underlay.pos.copy(),
|
||||
direction: underlay.direction,
|
||||
layer: underlay.layer,
|
||||
});
|
||||
}
|
||||
|
||||
return new ItemAcceptorComponent({
|
||||
slots: slotsCopy,
|
||||
beltUnderlays: beltUnderlaysCopy,
|
||||
animated: this.animated,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -89,23 +67,16 @@ export class ItemAcceptorComponent extends Component {
|
||||
*
|
||||
* @param {object} param0
|
||||
* @param {Array<ItemAcceptorSlotConfig>} param0.slots The slots from which we accept items
|
||||
* @param {boolean=} param0.animated Whether to animate item consumption
|
||||
* @param {Array<{pos: Vector, direction: enumDirection, layer: enumLayer}>=} param0.beltUnderlays Where to render belt underlays
|
||||
*/
|
||||
constructor({ slots = [], beltUnderlays = [], animated = true }) {
|
||||
constructor({ slots = [] }) {
|
||||
super();
|
||||
|
||||
this.animated = animated;
|
||||
|
||||
/**
|
||||
* Fixes belt animations
|
||||
* @type {Array<{ item: BaseItem, slotIndex: number, animProgress: number, direction: enumDirection }>}
|
||||
*/
|
||||
this.itemConsumptionAnimations = [];
|
||||
|
||||
/* Which belt underlays to render */
|
||||
this.beltUnderlays = beltUnderlays;
|
||||
|
||||
this.setSlots(slots);
|
||||
}
|
||||
|
||||
@@ -164,14 +135,12 @@ export class ItemAcceptorComponent extends Component {
|
||||
* @param {BaseItem} item
|
||||
*/
|
||||
onItemAccepted(slotIndex, direction, item) {
|
||||
if (this.animated) {
|
||||
this.itemConsumptionAnimations.push({
|
||||
item,
|
||||
slotIndex,
|
||||
direction,
|
||||
animProgress: 0.0,
|
||||
});
|
||||
}
|
||||
this.itemConsumptionAnimations.push({
|
||||
item,
|
||||
slotIndex,
|
||||
direction,
|
||||
animProgress: 0.0,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user