learnin-some-vue/monster_game/app.js

76 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

2018-04-14 04:08:16 +00:00
PLAYER = 0;
MONSTER = 1;
var app = new Vue({
el: '#app',
data: {
pHP: 100,
mHP: 100,
gameOn: false,
logs: [],
},
watch: {
pHP(value){
if (value <= 0){
alert("You lose");
this.giveUp();
}
},
mHP(value){
if (value <= 0){
alert("You Win!");
this.giveUp();
}
}
},
methods:{
start(){
this.gameOn = true;
this.mHP = 100;
this.pHP = 100;
this.logs = [];
},
playerAttack(){
2018-04-14 04:55:51 +00:00
var dmg = this.getRandomInt(3, 10);
2018-04-14 04:08:16 +00:00
this.mHP -= dmg;
2018-04-14 04:55:51 +00:00
this.logs.unshift({isPlayer: true, text: 'Player hits monster for ' + dmg});
2018-04-14 04:08:16 +00:00
this.monsterAttack();
},
specialAttack(){
2018-04-14 04:55:51 +00:00
var dmg = this.getRandomInt(10, 20);
2018-04-14 04:08:16 +00:00
this.mHP -= dmg;
2018-04-14 04:55:51 +00:00
this.logs.unshift({isPlayer: true, text: 'Player hits monster hard for ' + dmg});
2018-04-14 04:08:16 +00:00
this.monsterAttack();
},
heal(){
2018-04-14 04:55:51 +00:00
var hp = 10
this.pHP = Math.min(100, this.pHP + 10 );
var log =
this.logs.unshift({isPlayer: true, text: 'Player heals for 10'});
2018-04-14 04:08:16 +00:00
this.monsterAttack();
},
giveUp(){
this.gameOn = false;
this.logs = [];
},
monsterAttack(){
2018-04-14 04:55:51 +00:00
var dmg = this.getRandomInt(5, 12);
var log = { isPlayer: false, text: 'monster hits player for ' + dmg}
2018-04-14 04:08:16 +00:00
this.pHP -= dmg;
this.logs.unshift(log);
},
getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
},
computed: {
mHPStyle(){
return {width: this.mHP + "%"};
},
pHPStyle(){
return {width: this.pHP + "%"};
},
}
})