Implement energy that affects factory tick rate

This commit is contained in:
thepaperpilot 2022-12-21 20:37:11 -06:00
parent ef4c0b606d
commit 7cbbf732fc

View file

@ -8,7 +8,7 @@ import { globalBus } from "game/events";
import { createLayer } from "game/layers"; import { createLayer } from "game/layers";
import { Persistent, persistent, State } from "game/persistence"; import { Persistent, persistent, State } from "game/persistence";
import player from "game/player"; import player from "game/player";
import { formatWhole } from "util/bignum"; import Decimal, { formatWhole } from "util/bignum";
import { Direction } from "util/common"; import { Direction } from "util/common";
import { computed, ComputedRef, reactive, ref, watchEffect } from "vue"; import { computed, ComputedRef, reactive, ref, watchEffect } from "vue";
import conveyor from "./factory-components/conveyor.png"; import conveyor from "./factory-components/conveyor.png";
@ -75,6 +75,16 @@ const factory = createLayer(id, () => {
const name = "The Factory"; const name = "The Factory";
const color = "grey"; const color = "grey";
const energy = computed(() => 100);
const energyConsumption = computed(() =>
Object.values(components.value)
.map(c => FACTORY_COMPONENTS[c.type].energyCost ?? 0)
.reduce((a, b) => a + b, 0)
);
const tickRate = computed(() =>
Decimal.div(energyConsumption.value, energy.value).pow(2).min(1)
);
// ---------------------------------------------- Components // ---------------------------------------------- Components
const FACTORY_COMPONENTS = { const FACTORY_COMPONENTS = {
@ -94,6 +104,7 @@ const factory = createLayer(id, () => {
imageSrc: conveyor, imageSrc: conveyor,
name: "Conveyor", name: "Conveyor",
description: "Moves items at 1 block per second.", description: "Moves items at 1 block per second.",
energyCost: 1,
tick: 1, tick: 1,
ports: { ports: {
[Direction.Left]: { [Direction.Left]: {
@ -108,6 +119,7 @@ const factory = createLayer(id, () => {
imageSrc: square, imageSrc: square,
name: "Producer", name: "Producer",
description: "Produces 1 square every 1 second.", description: "Produces 1 square every 1 second.",
energyCost: 10,
tick: 1, tick: 1,
outputs: { outputs: {
square: { square: {
@ -131,6 +143,7 @@ const factory = createLayer(id, () => {
name: "Shrinker", name: "Shrinker",
description: description:
"Converts 100 squares to 1 square. I don't know why you would want to do this but here you go anyways.", "Converts 100 squares to 1 square. I don't know why you would want to do this but here you go anyways.",
energyCost: 20,
tick: 1, tick: 1,
inputs: { inputs: {
square: { square: {
@ -178,6 +191,7 @@ const factory = createLayer(id, () => {
imageSrc: string; imageSrc: string;
name: string; name: string;
description: string; description: string;
energyCost?: number;
/** amount it consumes */ /** amount it consumes */
inputs?: Record< inputs?: Record<
@ -308,6 +322,9 @@ const factory = createLayer(id, () => {
globalBus.on("update", diff => { globalBus.on("update", diff => {
if (!loaded) return; if (!loaded) return;
const factoryTicks = tickRate.value.times(diff).toNumber();
//debugger //debugger
// make them produce // make them produce
for (const id in components.value) { for (const id in components.value) {
@ -361,7 +378,8 @@ const factory = createLayer(id, () => {
key--; key--;
} else { } else {
const change = const change =
dirAmt * Math.min(Math.abs(x + 1.3 * dirAmt - block.x), diff); dirAmt *
Math.min(Math.abs(x + 1.3 * dirAmt - block.x), factoryTicks);
block.x += change; block.x += change;
block.sprite.x += change * blockSize; block.sprite.x += change * blockSize;
} }
@ -395,7 +413,8 @@ const factory = createLayer(id, () => {
key--; key--;
} else { } else {
const change = const change =
dirAmt * Math.min(Math.abs(y + 1.3 * dirAmt - block.y), diff); dirAmt *
Math.min(Math.abs(y + 1.3 * dirAmt - block.y), factoryTicks);
block.y += change; block.y += change;
block.sprite.y += change * blockSize; block.sprite.y += change * blockSize;
} }
@ -425,7 +444,7 @@ const factory = createLayer(id, () => {
data.ticksDone -= cyclesDone * factoryData.tick; data.ticksDone -= cyclesDone * factoryData.tick;
} }
} else { } else {
data.ticksDone += diff; data.ticksDone += factoryTicks;
} }
// now look at each component direction and see if it accepts items coming in // now look at each component direction and see if it accepts items coming in
// components are 1x1 so simple math for now // components are 1x1 so simple math for now