Create Hud with Vue.js
Description
Create Hud
Structure
Files
player.ts
import { RpgPlayer, type RpgPlayerHooks, Control } from '@rpgjs/server'
const player: RpgPlayerHooks = {
onJoinMap(player: RpgPlayer) {
player.gui('my-hud').open() // Here, open the HUD after loading the map
},
onInput(player: RpgPlayer, { input }) {
if (input === Control.Action) {
player.hp -= 100
}
}
}
export default player
hud.vue
<template>
<div class="health-bar">
<p>{{ hp }} / {{ maxHp }}</p>
<div class="bar">
<div class="inner-bar" :style="{ width }"></div>
</div>
</div>
</template>
<script>
export default {
name: 'my-hud',
inject: ['rpgCurrentPlayer'],
data() {
return {
hp: 0,
maxHp: 0
}
},
mounted() {
this.obsCurrentPlayer = this.rpgCurrentPlayer
.subscribe(({ object }) => {
this.hp = object.hp
this.maxHp = object.param.maxHp
})
},
computed: {
width() {
return ((this.hp / this.maxHp) * 100) + '%'
}
},
unmounted() {
this.obsCurrentPlayer.unsubscribe()
}
}
</script>
<style>
.health-bar {
width: 200px;
margin-top: 10px;
margin-left: 10px;
background: rgba(0, 0, 0, 0.3);
position: absolute;
}
.health-bar p {
margin: 5px;
color: white;
font-size: 21px;
font-weight: bold;
font-family: sans-serif;
}
.bar {
border: 2px solid black;
border-radius: 5px;
position: relative;
}
.inner-bar {
background: #c54;
height: 10px;
position: relative;
transition: width .5s linear;
}
</style>