chromatic-lattice/src/features/upgrades/Upgrade.vue

145 lines
4.4 KiB
Vue
Raw Normal View History

2021-06-12 04:38:16 +00:00
<template>
<button
v-if="unref(visibility) !== Visibility.None"
:style="[
{
visibility: unref(visibility) === Visibility.Hidden ? 'hidden' : undefined
},
unref(style) ?? {}
]"
2022-01-14 04:25:47 +00:00
@click="purchase"
:class="{
feature: true,
upgrade: true,
can: unref(canPurchase),
locked: !unref(canPurchase),
bought: unref(bought),
...unref(classes)
}"
:disabled="!unref(canPurchase)"
>
<component v-if="unref(component)" :is="unref(component)" />
<MarkNode :mark="unref(mark)" />
2022-01-14 04:25:47 +00:00
<LinkNode :id="id" />
</button>
2021-06-12 04:38:16 +00:00
</template>
2022-01-25 04:25:34 +00:00
<script lang="tsx">
2022-02-27 22:04:56 +00:00
import "@/components/common/features.css";
import LinkNode from "@/components/links/LinkNode.vue";
import MarkNode from "@/components/MarkNode.vue";
import { jsx, StyleValue, Visibility } from "@/features/feature";
2022-02-27 22:04:56 +00:00
import { displayResource, Resource } from "@/features/resources/resource";
import { GenericUpgrade } from "@/features/upgrades/upgrade";
2022-01-25 04:25:34 +00:00
import { DecimalSource } from "@/lib/break_eternity";
import { coerceComponent, isCoercableComponent, processedPropType, unwrapRef } from "@/util/vue";
import {
Component,
defineComponent,
PropType,
shallowRef,
toRefs,
unref,
UnwrapRef,
watchEffect
} from "vue";
2021-06-12 04:38:16 +00:00
2022-01-25 04:25:34 +00:00
export default defineComponent({
props: {
display: {
type: processedPropType<UnwrapRef<GenericUpgrade["display"]>>(String, Object, Function),
2022-01-25 04:25:34 +00:00
required: true
},
visibility: {
type: processedPropType<Visibility>(Number),
2022-01-25 04:25:34 +00:00
required: true
},
style: processedPropType<StyleValue>(String, Object, Array),
classes: processedPropType<Record<string, boolean>>(Object),
resource: Object as PropType<Resource>,
cost: processedPropType<DecimalSource>(String, Object, Number),
2022-01-25 04:25:34 +00:00
canPurchase: {
type: processedPropType<boolean>(Boolean),
2022-01-25 04:25:34 +00:00
required: true
},
bought: {
type: processedPropType<boolean>(Boolean),
2022-01-25 04:25:34 +00:00
required: true
},
mark: processedPropType<boolean | string>(Boolean, String),
2022-01-25 04:25:34 +00:00
id: {
type: String,
required: true
},
purchase: {
type: Function as PropType<VoidFunction>,
required: true
}
},
components: {
LinkNode,
MarkNode
},
2022-01-25 04:25:34 +00:00
setup(props) {
const { display, cost } = toRefs(props);
2022-01-14 04:25:47 +00:00
const component = shallowRef<Component | string>("");
watchEffect(() => {
const currDisplay = unwrapRef(display);
2022-01-25 04:25:34 +00:00
if (currDisplay == null) {
component.value = "";
return;
2022-01-25 04:25:34 +00:00
}
if (isCoercableComponent(currDisplay)) {
component.value = coerceComponent(currDisplay);
return;
2022-01-25 04:25:34 +00:00
}
const currCost = unwrapRef(cost);
const Title = coerceComponent(currDisplay.title || "", "h3");
const Description = coerceComponent(currDisplay.description, "div");
const EffectDisplay = coerceComponent(currDisplay.effectDisplay || "");
component.value = coerceComponent(
jsx(() => (
<span>
{currDisplay.title ? (
<div>
<Title />
</div>
) : null}
<Description />
{currDisplay.effectDisplay ? (
<div>
Currently: <EffectDisplay />
</div>
) : null}
{props.resource != null ? (
<>
<br />
Cost: {props.resource &&
displayResource(props.resource, currCost)}{" "}
{props.resource?.displayName}
</>
) : null}
</span>
))
2022-01-25 04:25:34 +00:00
);
});
return {
component,
unref,
2022-01-25 04:25:34 +00:00
Visibility
};
}
});
2021-06-12 04:38:16 +00:00
</script>
<style scoped>
.upgrade {
min-height: 120px;
width: 120px;
font-size: 10px;
}
</style>