I'm trying to do a little game with Vuejs. The problem is that I want an inventory, and add objects to it. Each object must have its own action, for example potions heal. So I'm trying to do the following, but I think this cannot be done with Vue, but I do not find a solution.
new Vue({
el: '#app',
data: {
playerHealth: 100,
enemyHealth: 100,
invClicked: false,
gameStarted: false,
logs: [],
specialAttacks: [],
inventory: {
potion: {
amount: 1,
img: 'images/potion.svg',
action: function(){
this.playerHealth = this.playerHealth + 25;
}
}
}
},
computed: {
eHealthColor: function() {
if (this.enemyHealth < 25){
return 'red';
}
},
pHealthColor: function() {
if (this.playerHealth < 25){
return 'red';
}
},
numberOfPlayers: function() {
return 2;
}
},
methods: {
attack: function(event) {
let dealedDamage = (Math.floor(Math.random() * (5 - 1)) + 1);
let damageTaken = (Math.floor(Math.random() * (3 - 1)) + 1);
if(this.enemyHealth > dealedDamage){
this.enemyHealth = this.enemyHealth - dealedDamage;
this.logs.unshift(`PLAYER HITS ENEMY FOR ${dealedDamage}`);
} else {
this.enemyHealth = 0;
alert('YOU WON!')
}
if(this.playerHealth > damageTaken){
this.playerHealth = this.playerHealth - damageTaken;
this.logs.unshift(`ENEMY HITS PLAYER FOR ${damageTaken}`);
} else{
this.playerHealth = 0;
alert('YOU LOST!');
}
},
findItem: function() {
var item = this.inventory.potion;
return item;
}
}
});
The action method is not working, I've tried with this keyword and with double quotes and do notwork either. In html I'm calling by:
<div class="inv-item position-relative d-flex justify-content-center" @click="findItem().action">
Another problem is the amount, I would like that the function action would reduce the amount, but I can't refer to that object. Thanks!