<template>
<div class="inventory-panel" v-propagate>
<div class="header">
<h3>Inventory</h3>
<button @click="closeInventory">×</button>
</div>
<!-- Player Information -->
<div v-if="currentPlayer" class="player-info">
<p>Name: {{ currentPlayer.object.name }}</p>
<p>Level: {{ currentPlayer.object.level }}</p>
<p>HP: {{ currentPlayer.object.hp }}/{{ currentPlayer.object.maxHp }}</p>
</div>
<!-- Inventory Items -->
<div class="items-grid">
<div
v-for="item in items"
:key="item.id"
class="item-slot"
@click="useItem(item)"
>
<img :src="item.icon" :alt="item.name" />
<span class="quantity">{{ item.quantity }}</span>
</div>
</div>
<!-- Sound Controls -->
<div class="controls">
<button @click="playSound('click')">Play Click Sound</button>
<button @click="testInteraction">Send Data to Server</button>
</div>
</div>
</template>
<script setup>
import { ref, onMounted, onUnmounted, inject } from 'vue';
// Define props
const props = defineProps({
items: {
type: Array,
default: () => []
}
});
// Inject RPGJS services
const rpgEngine = inject('rpgEngine');
const rpgSocket = inject('rpgSocket');
const rpgGui = inject('rpgGui');
const rpgScene = inject('rpgScene');
const rpgResource = inject('rpgResource');
const rpgObjects = inject('rpgObjects');
const rpgCurrentPlayer = inject('rpgCurrentPlayer');
const rpgGuiClose = inject('rpgGuiClose');
const rpgGuiInteraction = inject('rpgGuiInteraction');
const rpgKeypress = inject('rpgKeypress');
const rpgSound = inject('rpgSound');
// Reactive state
const currentPlayer = ref(null);
const playerSubscription = ref(null);
const keypressSubscription = ref(null);
// Component lifecycle
onMounted(() => {
// Subscribe to current player changes
if (rpgCurrentPlayer) {
playerSubscription.value = rpgCurrentPlayer.subscribe((player) => {
currentPlayer.value = player;
});
}
// Subscribe to keypress events
if (rpgKeypress) {
keypressSubscription.value = rpgKeypress.subscribe((keyData) => {
if (keyData.control?.actionName === 'escape') {
closeInventory();
}
});
}
});
onUnmounted(() => {
// Clean up subscriptions
if (playerSubscription.value) {
playerSubscription.value.unsubscribe();
}
if (keypressSubscription.value) {
keypressSubscription.value.unsubscribe();
}
});
// Methods
const closeInventory = () => {
rpgGuiClose('inventory', {
closedBy: 'user',
timestamp: Date.now()
});
};
const useItem = (item) => {
rpgGuiInteraction('inventory', 'use-item', {
itemId: item.id,
playerId: currentPlayer.value?.object?.id
});
};
const playSound = (soundId) => {
rpgSound.play(soundId);
};
const testInteraction = () => {
rpgGuiInteraction('inventory', 'test-action', {
message: 'Hello from Vue 3 component!',
playerData: currentPlayer.value
});
};
</script>
<style scoped>
.inventory-panel {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
color: white;
padding: 24px;
border-radius: 12px;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
min-width: 400px;
max-height: 80vh;
overflow-y: auto;
}
.header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 16px;
padding-bottom: 12px;
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}
.player-info {
background: rgba(0, 0, 0, 0.2);
padding: 12px;
border-radius: 8px;
margin-bottom: 16px;
}
.items-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(60px, 1fr));
gap: 8px;
margin-bottom: 16px;
}
.item-slot {
position: relative;
aspect-ratio: 1;
background: rgba(0, 0, 0, 0.3);
border: 2px solid rgba(255, 255, 255, 0.1);
border-radius: 6px;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: all 0.2s ease;
}
.item-slot:hover {
border-color: #3498db;
background: rgba(52, 152, 219, 0.1);
}
.quantity {
position: absolute;
bottom: 2px;
right: 2px;
background: #e74c3c;
color: white;
font-size: 10px;
padding: 2px 4px;
border-radius: 10px;
min-width: 16px;
text-align: center;
}
.controls {
display: flex;
gap: 8px;
flex-wrap: wrap;
}
button {
background: #3498db;
color: white;
border: none;
padding: 8px 16px;
border-radius: 6px;
cursor: pointer;
transition: background-color 0.2s ease;
}
button:hover {
background: #2980b9;
}
.close-btn {
background: #e74c3c;
width: 32px;
height: 32px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 18px;
font-weight: bold;
}
.close-btn:hover {
background: #c0392b;
}
</style>