官术网_书友最值得收藏!

Food and health bubbles

This component contains an image and a text that displays the current amount for either the food or health of the castle. Its position will change depending on this amount--it will go up as the amount diminishes and will go down when it replenishes.

We will need three props for this component:

  • type is either food or health; it will used for the CSS class and for the image path
  • value is the amount displayed in the bubble
  • ratio is the amount pided by the maximum amount

We also need a computed property to calculate the vertical position of the bubble with the ratio prop. The position will range from 40 pixels to 260 pixels. So, the position value will be given by this expression:

(this.ratio * 220 + 40) * state.worldRatio + 'px'

Remember to multiply every position or size with the worldRatio value, so the game takes into account the window size (it gets bigger if the window is bigger, or vice versa).

  1. Let's write our new bubble component:
      Vue.component('bubble', {
template: `<p class="stat-bubble" :class="type + '-bubble'"
:style="bubbleStyle">
<img :src="'svg/' + type + '-bubble.svg'" />
<p class="counter">{{ value }}</p>
</p>`,
props: ['type', 'value', 'ratio'],
computed: {
bubbleStyle () {
return {
top: (this.ratio * 220 + 40) * state.worldRatio + 'px',
}
},
},
})

It has a root p element with the stat-bubble CSS class, a dynamic class (either 'food-bubble' or 'health-bubble', depending on the type prop value) plus a dynamic CSS style we set with the bubbleStyle computed property.

It contains an SVG image, which is not the same for food and health, and a p element with the counter class that displays the amount.

  1. Add a food and an health bubble to the castle-banners component:
      template: `<p class="banners">
<!-- Food -->
<img class="food-icon" src="svg/food-icon.svg" />
<bubble type="food" :value="player.food" :ratio="foodRatio" />
<!-- Banner bar here -->

<!-- Health -->
<img class="health-icon" src="svg/health-icon.svg" />
<bubble type="health" :value="player.health"
:ratio="healthRatio" />
<!-- Banner bar here -->
</p>`,
主站蜘蛛池模板: 阿克| 延津县| 吴堡县| 武强县| 名山县| 广丰县| 满城县| 昭苏县| 渝中区| 濉溪县| 五家渠市| 陆川县| 康乐县| 仪征市| 伊宁县| 阿瓦提县| 射阳县| 丹江口市| 翁牛特旗| 太仆寺旗| 宜黄县| 横峰县| 淅川县| 聂拉木县| 芷江| 固阳县| 新蔡县| 资兴市| 濮阳县| 巴林左旗| 沙湾县| 雅江县| 平阳县| 乐平市| 丰台区| 姜堰市| 伊金霍洛旗| 拜泉县| 芮城县| 高碑店市| 天峻县|