Fix ts errors

This commit is contained in:
ducdat0507 2022-12-21 17:00:56 +07:00
parent 67b093a7b0
commit 0f2ae44de0
2 changed files with 58 additions and 45 deletions

View file

@ -1,7 +1,9 @@
import { Application } from "@pixi/app"; import { Application } from "@pixi/app";
import { Assets } from "@pixi/assets"; import { Assets } from "@pixi/assets";
import { Resource, Texture } from "@pixi/core";
import { Container } from "@pixi/display"; import { Container } from "@pixi/display";
import { Graphics } from "@pixi/graphics"; import { Graphics } from "@pixi/graphics";
import { Matrix } from "@pixi/math";
import { Sprite } from "@pixi/sprite"; import { Sprite } from "@pixi/sprite";
import { jsx } from "features/feature"; import { jsx } from "features/feature";
import { globalBus } from "game/events"; import { globalBus } from "game/events";
@ -94,6 +96,8 @@ const factory = createLayer(id, () => {
const name = "The Factory"; const name = "The Factory";
const color = "grey"; const color = "grey";
// ---------------------------------------------- Components
const FACTORY_COMPONENTS = { const FACTORY_COMPONENTS = {
cursor: { cursor: {
imageSrc: cursor, imageSrc: cursor,
@ -147,17 +151,19 @@ const factory = createLayer(id, () => {
consumption: {}, consumption: {},
consumptionStock: {} consumptionStock: {}
} }
} as const; } as Record<string, FactoryComponentDeclaration>;
const RESOURCES = { const RESOURCES = {
square: square square: square
} as Record<string, string>; } as Record<string, string>;
type FactoryCompNames = keyof typeof FACTORY_COMPONENTS; type FactoryCompNames = keyof typeof FACTORY_COMPONENTS;
type BuildableCompName = Exclude<FactoryCompNames, "cursor">; type BuildableCompName = Exclude<FactoryCompNames, "cursor">;
interface FactoryComponentBase extends Record<string, State> { interface FactoryComponentBase extends Record<string, State> {
direction: Direction; direction: Direction;
} }
interface FactoryComponentProducers extends FactoryComponentBase {
interface FactoryComponentProducer extends FactoryComponentBase {
type: Exclude<BuildableCompName, "conveyor">; type: Exclude<BuildableCompName, "conveyor">;
consumptionStock: Record<string, number>; consumptionStock: Record<string, number>;
@ -165,11 +171,14 @@ const factory = createLayer(id, () => {
productionStock: Record<string, number>; productionStock: Record<string, number>;
ticksDone: number; ticksDone: number;
} }
interface FactoryComponentConveyor extends FactoryComponentBase { interface FactoryComponentConveyor extends FactoryComponentBase {
type: "conveyor"; type: "conveyor";
} }
type FactoryComponent = FactoryComponentBase & type FactoryComponent = FactoryComponentBase &
(FactoryComponentConveyor | FactoryComponentProducers); (FactoryComponentConveyor | FactoryComponentProducer);
interface FactoryComponentDeclaration { interface FactoryComponentDeclaration {
tick: number; tick: number;
imageSrc: string; imageSrc: string;
@ -263,8 +272,8 @@ const factory = createLayer(id, () => {
const floorGraphics = new Graphics(); const floorGraphics = new Graphics();
floorGraphics.beginFill(0x70645d); floorGraphics.beginFill(0x70645d);
floorGraphics.drawRect( floorGraphics.drawRect(
-factorySize.width * blockSize, (-factorySize.width - 0.5) * blockSize,
-factorySize.height * blockSize, (-factorySize.height - 0.5) * blockSize,
factorySize.width * 2 * blockSize, factorySize.width * 2 * blockSize,
factorySize.height * 2 * blockSize factorySize.height * 2 * blockSize
); );
@ -305,13 +314,14 @@ const factory = createLayer(id, () => {
// make them produce // make them produce
for (const id in components.value) { for (const id in components.value) {
const [x, y] = id.split("x").map(p => +p); const [x, y] = id.split("x").map(p => +p);
const data = components.value[id]; const _data = components.value[id];
const compData = compInternalData[id]; const _compData = compInternalData[id];
//console.log(compData, data) if (_data === undefined || _compData === undefined) continue;
if (data === undefined || compData === undefined) continue; const factoryData = FACTORY_COMPONENTS[_data.type];
const factoryData = FACTORY_COMPONENTS[data.type];
//debugger; //debugger;
if (data.type === "conveyor") { if (_data.type === "conveyor") {
const data = _data as FactoryComponentConveyor;
const compData = _compData as FactoryInternalConveyor;
if (compData.type !== "conveyor") throw new TypeError("this should not happen"); if (compData.type !== "conveyor") throw new TypeError("this should not happen");
// conveyor part // conveyor part
// use a copy // use a copy
@ -336,14 +346,13 @@ const factory = createLayer(id, () => {
// push it to the next conveyor, kill it from the // push it to the next conveyor, kill it from the
// curent conveyor // curent conveyor
block.lastX += dirAmt; block.lastX += dirAmt;
compBehind.nextPackages.push(block); (compBehind as FactoryInternalConveyor).nextPackages.push(block);
compData.packages.splice(key, 1); compData.packages.splice(key, 1);
} else { } else {
// send it to the factory // send it to the factory
// destory its sprite and data // destory its sprite and data
(storedComp as FactoryComponentProducers).consumptionStock[ const factoryData = storedComp as FactoryComponentProducer;
block.type factoryData.consumptionStock[block.type]++;
]++;
movingBlocks.removeChild(block.sprite); movingBlocks.removeChild(block.sprite);
compData.packages.splice(key, 1); compData.packages.splice(key, 1);
} }
@ -367,12 +376,12 @@ const factory = createLayer(id, () => {
// push it to the next conveyor, kill it from the // push it to the next conveyor, kill it from the
// curent conveyor // curent conveyor
block.lastY += dirAmt; block.lastY += dirAmt;
compBehind.nextPackages.push(block); (compBehind as FactoryInternalConveyor).nextPackages.push(block);
compData.packages.splice(key, 1); compData.packages.splice(key, 1);
} else { } else {
// send it to the factory // send it to the factory
// destory its sprite and data // destory its sprite and data
const factoryData = storedComp as FactoryComponentProducers; const factoryData = storedComp as FactoryComponentProducer;
factoryData.consumptionStock[block.type]++; factoryData.consumptionStock[block.type]++;
movingBlocks.removeChild(block.sprite); movingBlocks.removeChild(block.sprite);
compData.packages.splice(key, 1); compData.packages.splice(key, 1);
@ -385,6 +394,8 @@ const factory = createLayer(id, () => {
} }
} }
} else { } else {
const data = _data as FactoryComponentProducer;
const compData = _compData as FactoryInternalProducer;
// factory part // factory part
// PRODUCTION // PRODUCTION
if (data.ticksDone >= factoryData.tick) { if (data.ticksDone >= factoryData.tick) {
@ -406,27 +417,16 @@ const factory = createLayer(id, () => {
let yInc = 0; let yInc = 0;
let xInc = 0; let xInc = 0;
//debugger; if (components.value[x + "x" + (y + 1)]?.type === "conveyor") {
if ( if (components.value[x + "x" + (y + 1)].direction === Direction.Up) {
components.value[x + "x" + (y + 1)]?.type === "conveyor" && yInc = 1;
components.value[x + "x" + (y + 1)].direction === Direction.Up } else if (components.value[x + "x" + (y - 1)].direction === Direction.Down) {
) { yInc = -1;
yInc = 1; } else if (components.value[x + 1 + "x" + y].direction === Direction.Right) {
} else if ( xInc = 1;
components.value[x + "x" + (y - 1)]?.type === "conveyor" && } else if (components.value[x - 1 + "x" + y].direction === Direction.Left) {
components.value[x + "x" + (y + 1)].direction === Direction.Down xInc = -1;
) { }
yInc = -1;
} else if (
components.value[x + 1 + "x" + y]?.type === "conveyor" &&
components.value[x + "x" + (y + 1)].direction === Direction.Right
) {
xInc = 1;
} else if (
components.value[x - 1 + "x" + y]?.type === "conveyor" &&
components.value[x + "x" + (y + 1)].direction === Direction.Left
) {
xInc = -1;
} }
// no suitable location to dump stuff in // no suitable location to dump stuff in
//console.log(x, y) //console.log(x, y)
@ -535,7 +535,7 @@ const factory = createLayer(id, () => {
if (data.type === "conveyor") return true; if (data.type === "conveyor") return true;
if (!(factoryBaseData.canProduce?.value ?? true)) return false; if (!(factoryBaseData.canProduce?.value ?? true)) return false;
// this should NEVER be null // this should NEVER be null
const compData = components.value[x + "x" + y] as FactoryComponentProducers; const compData = components.value[x + "x" + y] as FactoryComponentProducer;
for (const [key, res] of Object.entries(compData.productionStock)) { for (const [key, res] of Object.entries(compData.productionStock)) {
// if the current stock + production is more than you can handle // if the current stock + production is more than you can handle
if ( if (
@ -551,7 +551,7 @@ const factory = createLayer(id, () => {
return true; return true;
}), }),
sprite sprite
} as FactoryInternal; } as FactoryInternalProducer;
spriteContainer.addChild(sprite); spriteContainer.addChild(sprite);
} }
@ -572,7 +572,7 @@ const factory = createLayer(id, () => {
compSelected.value !== "rotate" compSelected.value !== "rotate"
) { ) {
const { tx, ty } = spriteContainer.localTransform; const { tx, ty } = spriteContainer.localTransform;
graphicContainer.beginFill(0x808080); graphicContainer.lineStyle(4, 0x808080, 1);
graphicContainer.drawRect( graphicContainer.drawRect(
roundDownTo(mouseCoords.x - tx, blockSize) + tx - blockSize / 2, roundDownTo(mouseCoords.x - tx, blockSize) + tx - blockSize / 2,
roundDownTo(mouseCoords.y - ty, blockSize) + ty - blockSize / 2, roundDownTo(mouseCoords.y - ty, blockSize) + ty - blockSize / 2,
@ -603,8 +603,14 @@ const factory = createLayer(id, () => {
mapOffset.y += e.movementY / blockSize; mapOffset.y += e.movementY / blockSize;
// the maximum you can see currently // the maximum you can see currently
// total size of blocks - current size = amount you should move // total size of blocks - current size = amount you should move
mapOffset.x = Math.min(Math.max(mapOffset.x, -factorySize.width), factorySize.width); mapOffset.x = Math.min(
mapOffset.y = Math.min(Math.max(mapOffset.y, -factorySize.height), factorySize.height); Math.max(mapOffset.x, -factorySize.width + 0.5),
factorySize.width + 0.5
);
mapOffset.y = Math.min(
Math.max(mapOffset.y, -factorySize.height + 0.5),
factorySize.height + 0.5
);
} }
if (!pointerDown.value && !pointerDrag.value) { if (!pointerDown.value && !pointerDrag.value) {
const { tx, ty } = spriteContainer.localTransform; const { tx, ty } = spriteContainer.localTransform;
@ -721,8 +727,14 @@ const factory = createLayer(id, () => {
<> <>
Stock:{" "} Stock:{" "}
{Object.entries({ {Object.entries({
...compHovered.value.productionStock, ...(compHovered.value.productionStock as Record<
...compHovered.value.consumptionStock string,
number
>),
...(compHovered.value.consumptionStock as Record<
string,
number
>)
}).map(i => { }).map(i => {
return `${i[0]}: ${i[1]}/${ return `${i[0]}: ${i[1]}/${
FACTORY_COMPONENTS[compHovered.value?.type ?? "cursor"] FACTORY_COMPONENTS[compHovered.value?.type ?? "cursor"]

View file

@ -17,6 +17,7 @@
.info-container { .info-container {
position: absolute; position: absolute;
width: max-content;
max-width: 300px; max-width: 300px;
margin: 20px 0 0 10px; margin: 20px 0 0 10px;