paricafe/packages/frontend/src/components/MkClickerGame.vue

93 lines
1.8 KiB
Vue
Raw Normal View History

2023-01-08 02:41:09 -06:00
<template>
<div>
<div v-if="game.ready" :class="$style.game">
2023-01-08 05:30:19 -06:00
<div :class="$style.cps" class="">{{ number(cps) }}cps</div>
2023-01-08 02:41:09 -06:00
<div :class="$style.count" class=""><i class="ti ti-cookie" style="font-size: 70%;"></i> {{ number(cookies) }}</div>
<button v-click-anime class="_button" :class="$style.button" @click="onClick">
<img src="/client-assets/cookie.png" :class="$style.img">
</button>
</div>
<div v-else>
<MkLoading/>
</div>
</div>
</template>
<script lang="ts" setup>
import { computed, defineAsyncComponent, onMounted, onUnmounted } from 'vue';
import MkPlusOneEffect from '@/components/MkPlusOneEffect.vue';
import * as os from '@/os';
import { useInterval } from '@/scripts/use-interval';
import * as game from '@/scripts/clicker-game';
import number from '@/filters/number';
defineProps<{
}>();
const saveData = game.saveData;
const cookies = computed(() => saveData.value?.cookies);
2023-01-08 05:30:19 -06:00
let cps = $ref(0);
let prevCookies = $ref(0);
2023-01-08 02:41:09 -06:00
function onClick(ev: MouseEvent) {
saveData.value!.cookies++;
2023-01-08 05:42:45 -06:00
saveData.value!.totalCookies++;
saveData.value!.totalHandmadeCookies++;
2023-01-08 02:41:09 -06:00
saveData.value!.clicked++;
const x = ev.clientX;
const y = ev.clientY;
os.popup(MkPlusOneEffect, { x, y }, {}, 'end');
}
2023-01-08 05:30:19 -06:00
useInterval(() => {
const diff = saveData.value!.cookies - prevCookies;
cps = diff;
prevCookies = saveData.value!.cookies;
}, 1000, {
immediate: false,
afterMounted: true,
});
2023-01-08 02:41:09 -06:00
useInterval(game.save, 1000 * 5, {
immediate: false,
afterMounted: true,
});
onMounted(async () => {
await game.load();
2023-01-08 05:30:19 -06:00
prevCookies = saveData.value!.cookies;
2023-01-08 02:41:09 -06:00
});
onUnmounted(() => {
game.save();
});
</script>
<style lang="scss" module>
.game {
padding: 16px;
text-align: center;
}
2023-01-08 05:30:19 -06:00
.cps {
position: absolute;
top: 12px;
left: 12px;
opacity: 0.5;
}
2023-01-08 02:41:09 -06:00
.count {
font-size: 1.3em;
margin-bottom: 6px;
}
.button {
}
.img {
max-width: 90px;
}
</style>