Moved links into resources objects

This commit is contained in:
thepaperpilot 2021-08-26 00:57:33 -05:00
parent 7890c1c298
commit 0e4cf7ebbc

View file

@ -36,16 +36,33 @@ type ActionNodeData = {
log: LogEntry[]; log: LogEntry[];
}; };
enum LinkType {
LossOnly,
GainOnly,
Both
}
// Links cause gain/loss of one resource to also affect other resources
type ResourceLink = {
resource: string;
amount: DecimalSource;
linkType: LinkType;
};
type Resource = { type Resource = {
readonly name: string; readonly name: string;
readonly color: string; readonly color: string;
readonly node: BoardNode; readonly node: BoardNode;
readonly links?: ResourceLink[];
readonly maxAmount: DecimalSource; readonly maxAmount: DecimalSource;
amount: DecimalSource; amount: DecimalSource;
}; };
const resources = { const resources = {
time: createResource("time", "#3EB489", 24 * 60 * 60, 24 * 60 * 60), time: createResource("time", "#3EB489", 24 * 60 * 60, 24 * 60 * 60, [
{ resource: "social", amount: 1 / (60 * 60), linkType: LinkType.LossOnly },
{ resource: "mental", amount: 1 / (120 * 60), linkType: LinkType.LossOnly }
]),
energy: createResource("energy", "#FFA500", 100, 100), energy: createResource("energy", "#FFA500", 100, 100),
social: createResource("social", "#800080", 100, 100), social: createResource("social", "#800080", 100, 100),
mental: createResource("mental", "#32CD32", 100, 100), mental: createResource("mental", "#32CD32", 100, 100),
@ -56,7 +73,8 @@ function createResource(
name: string, name: string,
color: string, color: string,
maxAmount: DecimalSource, maxAmount: DecimalSource,
defaultAmount: DecimalSource defaultAmount: DecimalSource,
links?: ResourceLink[]
): Resource { ): Resource {
const node = computed(() => const node = computed(() =>
player.layers.main?.boards.main.nodes.find( player.layers.main?.boards.main.nodes.find(
@ -67,6 +85,7 @@ function createResource(
return { return {
name, name,
color, color,
links,
get node() { get node() {
// Should only run once, but this tricks TS into knowing node.value exists // Should only run once, but this tricks TS into knowing node.value exists
while (node.value == null) { while (node.value == null) {
@ -319,37 +338,16 @@ function getRandomEvent(events: WeightedEvent[]): LogEntry | null {
return null; return null;
} }
enum LinkType { for (const id in resources) {
LossOnly, const resource = resources[id];
GainOnly,
Both
}
// Links cause gain/loss of one resource to also affect other resources
const links = {
time: [
{ resource: "social", amount: 1 / (60 * 60), linkType: LinkType.LossOnly },
{ resource: "mental", amount: 1 / (120 * 60), linkType: LinkType.LossOnly }
]
} as Record<
string,
{
resource: string;
amount: DecimalSource;
linkType: LinkType;
}[]
>;
for (const resource in links) {
const resourceLinks = links[resource];
watch( watch(
() => resources[resource].amount, () => resource.amount,
(amount, oldAmount) => { (amount, oldAmount) => {
if (amount == null || oldAmount == null) { if (amount == null || oldAmount == null) {
return; return;
} }
const resourceGain = Decimal.sub(amount, oldAmount); const resourceGain = Decimal.sub(amount, oldAmount);
resourceLinks.forEach(link => { resource.links?.forEach(link => {
if (link.linkType === LinkType.LossOnly && Decimal.gt(amount, oldAmount)) { if (link.linkType === LinkType.LossOnly && Decimal.gt(amount, oldAmount)) {
return; return;
} }
@ -412,10 +410,8 @@ const resourceNodeType = {
selectedNode.value.type === "resource" selectedNode.value.type === "resource"
) { ) {
const selectedResource = getResource(selectedNode.value); const selectedResource = getResource(selectedNode.value);
if (selectedResource.name in links) { if (selectedResource.links) {
const link = links[selectedResource.name].find( const link = selectedResource.links.find(link => link.resource === resource.name);
link => link.resource === resource.name
);
if (link) { if (link) {
let text; let text;
if (resource.name === "time") { if (resource.name === "time") {
@ -691,8 +687,8 @@ export default {
} }
if (selectedNode.value.type === "resource") { if (selectedNode.value.type === "resource") {
const resource = getResource(selectedNode.value); const resource = getResource(selectedNode.value);
if (resource.name in links) { if (resource.links) {
return links[resource.name].map(link => { return resource.links.map(link => {
const linkResource = resources[link.resource]; const linkResource = resources[link.resource];
let negativeLink = Decimal.lt(link.amount, 0); let negativeLink = Decimal.lt(link.amount, 0);
if (link.linkType === LinkType.LossOnly) { if (link.linkType === LinkType.LossOnly) {