You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
451 lines
10 KiB
451 lines
10 KiB
<script setup lang="ts">
|
|
import { ref, computed, onMounted, onUnmounted } from 'vue'
|
|
import { useRoute, useRouter } from 'vue-router'
|
|
import { useMonsterStore } from '@/stores/monster'
|
|
import { useCharacterStore } from '@/stores/character'
|
|
import { ElMessage } from 'element-plus'
|
|
import { ArrowLeft } from '@element-plus/icons-vue'
|
|
|
|
const route = useRoute()
|
|
const router = useRouter()
|
|
const monsterStore = useMonsterStore()
|
|
const characterStore = useCharacterStore()
|
|
|
|
const monsterId = computed(() => parseInt(route.query.id as string))
|
|
|
|
const playerHealth = ref(0)
|
|
const playerMaxHealth = ref(0)
|
|
const monsterHealth = ref(0)
|
|
const monsterMaxHealth = ref(0)
|
|
const battleLog = ref<{ round: number; text: string; isPlayer: boolean }[]>([])
|
|
const isFighting = ref(false)
|
|
const battleResult = ref<'win' | 'lose' | null>(null)
|
|
const rewards = ref<{ type: string; itemName: string; count: number }[]>([])
|
|
|
|
const playerHealthPercent = computed(() => {
|
|
if (playerMaxHealth.value === 0) return 0
|
|
return (playerHealth.value / playerMaxHealth.value) * 100
|
|
})
|
|
|
|
const monsterHealthPercent = computed(() => {
|
|
if (monsterMaxHealth.value === 0) return 0
|
|
return (monsterHealth.value / monsterMaxHealth.value) * 100
|
|
})
|
|
|
|
const currentCharacter = computed(() => characterStore.currentCharacter)
|
|
const currentMonster = computed(() => monsterStore.currentMonster)
|
|
|
|
const getMonsterIcon = (iconName?: string) => {
|
|
if (!iconName) return '/src/assets/images/item-default.svg'
|
|
return `/src/assets/images/monster/${iconName}`
|
|
}
|
|
|
|
const calculateDamage = (attack: number, defend: number, criticalRate: number): { damage: number; isCritical: boolean } => {
|
|
const baseDamage = Math.max(1, attack - defend)
|
|
const random = Math.random()
|
|
const isCritical = random < criticalRate / 100
|
|
const damage = isCritical ? Math.floor(baseDamage * 1.5) : baseDamage
|
|
const variance = (90 + Math.floor(Math.random() * 21)) / 100
|
|
return { damage: Math.floor(damage * variance), isCritical }
|
|
}
|
|
|
|
const startBattle = async () => {
|
|
if (!currentMonster.value || !currentCharacter.value) {
|
|
ElMessage.error('数据加载中...')
|
|
return
|
|
}
|
|
|
|
isFighting.value = true
|
|
battleResult.value = null
|
|
battleLog.value = []
|
|
rewards.value = []
|
|
|
|
playerMaxHealth.value = currentCharacter.value.maxHP
|
|
playerHealth.value = currentCharacter.value.currentHP
|
|
monsterMaxHealth.value = currentMonster.value.health
|
|
monsterHealth.value = currentMonster.value.health
|
|
|
|
const playerAttack = currentCharacter.value.attack
|
|
const playerDefend = currentCharacter.value.defend
|
|
const playerCriticalRate = currentCharacter.value.criticalRate
|
|
const monsterAttack = currentMonster.value.attack
|
|
const monsterDefend = currentMonster.value.defense
|
|
|
|
let round = 0
|
|
const maxRounds = 100
|
|
|
|
const fight = () => {
|
|
if (battleResult.value !== null) return
|
|
|
|
round++
|
|
|
|
// 玩家攻击
|
|
const playerResult = calculateDamage(playerAttack, monsterDefend, playerCriticalRate)
|
|
monsterHealth.value = Math.max(0, monsterHealth.value - playerResult.damage)
|
|
battleLog.value.push({
|
|
round,
|
|
text: `你${playerResult.isCritical ? '暴击' : '攻击'}造成 ${playerResult.damage} 伤害`,
|
|
isPlayer: true
|
|
})
|
|
|
|
if (monsterHealth.value <= 0) {
|
|
battleResult.value = 'win'
|
|
finishBattle()
|
|
return
|
|
}
|
|
|
|
// 怪物攻击
|
|
const monsterResult = calculateDamage(monsterAttack, playerDefend, currentMonster.value.criticalRate)
|
|
playerHealth.value = Math.max(0, playerHealth.value - monsterResult.damage)
|
|
battleLog.value.push({
|
|
round,
|
|
text: `怪物攻击造成 ${monsterResult.damage} 伤害`,
|
|
isPlayer: false
|
|
})
|
|
|
|
if (playerHealth.value <= 0) {
|
|
battleResult.value = 'lose'
|
|
finishBattle()
|
|
return
|
|
}
|
|
|
|
if (round < maxRounds) {
|
|
setTimeout(fight, 800)
|
|
}
|
|
}
|
|
|
|
setTimeout(fight, 500)
|
|
}
|
|
|
|
const finishBattle = async () => {
|
|
if (!currentMonster.value) return
|
|
|
|
const result = await monsterStore.challengeMonster(currentMonster.value.id)
|
|
|
|
if (result) {
|
|
rewards.value = result.rewards || []
|
|
|
|
if (result.success) {
|
|
ElMessage.success('战斗胜利!')
|
|
} else {
|
|
ElMessage.warning('战斗失败')
|
|
}
|
|
|
|
// 刷新角色数据
|
|
await characterStore.fetchCharacters()
|
|
}
|
|
|
|
isFighting.value = false
|
|
}
|
|
|
|
const goBack = () => {
|
|
router.push('/monster-list')
|
|
}
|
|
|
|
const retry = () => {
|
|
battleResult.value = null
|
|
battleLog.value = []
|
|
rewards.value = []
|
|
startBattle()
|
|
}
|
|
|
|
onMounted(async () => {
|
|
if (monsterId.value) {
|
|
await monsterStore.fetchMonster(monsterId.value)
|
|
|
|
if (currentCharacter.value) {
|
|
playerMaxHealth.value = currentCharacter.value.maxHP
|
|
playerHealth.value = currentCharacter.value.currentHP
|
|
}
|
|
|
|
if (currentMonster.value) {
|
|
monsterMaxHealth.value = currentMonster.value.health
|
|
monsterHealth.value = currentMonster.value.health
|
|
}
|
|
}
|
|
})
|
|
|
|
onUnmounted(() => {
|
|
monsterStore.clearBattleResult()
|
|
})
|
|
</script>
|
|
|
|
<template>
|
|
<div class="battle-container">
|
|
<div class="battle-header">
|
|
<el-button :icon="ArrowLeft" circle @click="goBack" />
|
|
<h2>战斗</h2>
|
|
</div>
|
|
|
|
<div v-if="!currentMonster || !currentCharacter" class="loading">
|
|
加载中...
|
|
</div>
|
|
|
|
<template v-else>
|
|
<!-- 战斗区域 -->
|
|
<div class="battle-area">
|
|
<!-- 怪物 -->
|
|
<div class="combatant monster">
|
|
<div class="combatant-icon">
|
|
<img :src="getMonsterIcon(currentMonster.icon)" :alt="currentMonster.name" />
|
|
</div>
|
|
<div class="combatant-name">{{ currentMonster.name }}</div>
|
|
<el-progress
|
|
:percentage="monsterHealthPercent"
|
|
:stroke-width="12"
|
|
:color="monsterHealthPercent > 30 ? '#ef4444' : '#dc2626'"
|
|
/>
|
|
<div class="health-text">
|
|
{{ monsterHealth }} / {{ monsterMaxHealth }}
|
|
</div>
|
|
</div>
|
|
|
|
<!-- VS -->
|
|
<div class="vs">VS</div>
|
|
|
|
<!-- 玩家 -->
|
|
<div class="combatant player">
|
|
<div class="combatant-icon">
|
|
<img src="/src/assets/images/character.svg" :alt="currentCharacter.name" />
|
|
</div>
|
|
<div class="combatant-name">{{ currentCharacter.name }}</div>
|
|
<el-progress
|
|
:percentage="playerHealthPercent"
|
|
:stroke-width="12"
|
|
:color="playerHealthPercent > 30 ? '#22c55e' : '#ef4444'"
|
|
/>
|
|
<div class="health-text">
|
|
{{ playerHealth }} / {{ playerMaxHealth }}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- 战斗日志 -->
|
|
<div class="battle-log">
|
|
<div
|
|
v-for="(log, index) in battleLog"
|
|
:key="index"
|
|
class="log-item"
|
|
:class="{ player: log.isPlayer }"
|
|
>
|
|
<span class="round">第{{ log.round }}回合</span>
|
|
<span class="text">{{ log.text }}</span>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- 结果区域 -->
|
|
<div v-if="battleResult" class="battle-result">
|
|
<div class="result-title" :class="{ win: battleResult === 'win', lose: battleResult === 'lose' }">
|
|
{{ battleResult === 'win' ? '战斗胜利!' : '战斗失败' }}
|
|
</div>
|
|
|
|
<div v-if="battleResult === 'win' && rewards.length > 0" class="rewards">
|
|
<h3>获得奖励</h3>
|
|
<div class="reward-list">
|
|
<div v-for="reward in rewards" :key="reward.itemName" class="reward-item">
|
|
<span class="reward-type">{{ reward.type }}</span>
|
|
<span class="reward-name">{{ reward.itemName }}</span>
|
|
<span class="reward-count">×{{ reward.count }}</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="result-actions">
|
|
<el-button @click="goBack">返回</el-button>
|
|
<el-button type="primary" @click="retry">再战</el-button>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- 开始战斗按钮 -->
|
|
<div v-if="!battleResult && !isFighting" class="start-battle">
|
|
<el-button type="danger" size="large" @click="startBattle">
|
|
开始战斗
|
|
</el-button>
|
|
</div>
|
|
</template>
|
|
</div>
|
|
</template>
|
|
|
|
<style scoped lang="css">
|
|
.battle-container {
|
|
padding: 20px;
|
|
min-height: 100vh;
|
|
background: linear-gradient(180deg, #0f172a 0%, #1e293b 100%);
|
|
}
|
|
|
|
.battle-header {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 16px;
|
|
margin-bottom: 24px;
|
|
}
|
|
|
|
.battle-header h2 {
|
|
color: #f8fafc;
|
|
margin: 0;
|
|
}
|
|
|
|
.loading {
|
|
color: #64748b;
|
|
text-align: center;
|
|
padding: 60px 20px;
|
|
}
|
|
|
|
.battle-area {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
padding: 40px 20px;
|
|
background: rgba(0, 0, 0, 0.2);
|
|
border-radius: 16px;
|
|
margin-bottom: 20px;
|
|
}
|
|
|
|
.combatant {
|
|
text-align: center;
|
|
flex: 1;
|
|
}
|
|
|
|
.combatant-icon {
|
|
width: 100px;
|
|
height: 100px;
|
|
margin: 0 auto 16px;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
background: rgba(255, 255, 255, 0.08);
|
|
border-radius: 16px;
|
|
}
|
|
|
|
.combatant-icon img {
|
|
width: 70px;
|
|
height: 70px;
|
|
object-fit: contain;
|
|
}
|
|
|
|
.combatant-name {
|
|
color: #f8fafc;
|
|
font-size: 18px;
|
|
font-weight: 600;
|
|
margin-bottom: 12px;
|
|
}
|
|
|
|
.health-text {
|
|
color: #94a3b8;
|
|
font-size: 14px;
|
|
margin-top: 8px;
|
|
}
|
|
|
|
.vs {
|
|
color: #f59e0b;
|
|
font-size: 32px;
|
|
font-weight: 800;
|
|
padding: 0 24px;
|
|
}
|
|
|
|
.battle-log {
|
|
background: rgba(0, 0, 0, 0.3);
|
|
border-radius: 12px;
|
|
padding: 16px;
|
|
max-height: 200px;
|
|
overflow-y: auto;
|
|
margin-bottom: 20px;
|
|
}
|
|
|
|
.log-item {
|
|
display: flex;
|
|
gap: 12px;
|
|
padding: 8px 0;
|
|
border-bottom: 1px solid rgba(255, 255, 255, 0.04);
|
|
}
|
|
|
|
.log-item:last-child {
|
|
border-bottom: none;
|
|
}
|
|
|
|
.log-item .round {
|
|
color: #64748b;
|
|
font-size: 12px;
|
|
min-width: 70px;
|
|
}
|
|
|
|
.log-item .text {
|
|
color: #f8fafc;
|
|
font-size: 14px;
|
|
}
|
|
|
|
.log-item.player .text {
|
|
color: #22c55e;
|
|
}
|
|
|
|
.battle-result {
|
|
text-align: center;
|
|
padding: 24px;
|
|
background: rgba(0, 0, 0, 0.3);
|
|
border-radius: 12px;
|
|
margin-bottom: 20px;
|
|
}
|
|
|
|
.result-title {
|
|
font-size: 28px;
|
|
font-weight: 700;
|
|
margin-bottom: 20px;
|
|
}
|
|
|
|
.result-title.win {
|
|
color: #22c55e;
|
|
}
|
|
|
|
.result-title.lose {
|
|
color: #ef4444;
|
|
}
|
|
|
|
.rewards {
|
|
margin-bottom: 20px;
|
|
}
|
|
|
|
.rewards h3 {
|
|
color: #f8fafc;
|
|
margin: 0 0 12px;
|
|
}
|
|
|
|
.reward-list {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 8px;
|
|
}
|
|
|
|
.reward-item {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
gap: 8px;
|
|
padding: 12px;
|
|
background: rgba(34, 197, 94, 0.1);
|
|
border-radius: 8px;
|
|
}
|
|
|
|
.reward-type {
|
|
color: #22c55e;
|
|
font-size: 12px;
|
|
}
|
|
|
|
.reward-name {
|
|
color: #f8fafc;
|
|
}
|
|
|
|
.reward-count {
|
|
color: #22c55e;
|
|
font-weight: 600;
|
|
}
|
|
|
|
.result-actions {
|
|
display: flex;
|
|
gap: 12px;
|
|
justify-content: center;
|
|
}
|
|
|
|
.start-battle {
|
|
text-align: center;
|
|
}
|
|
</style>
|