Created
March 4, 2017 08:37
-
-
Save happyDemon/bf2c17f6c902743ab3da7672c974b5e4 to your computer and use it in GitHub Desktop.
Vue.js explained through Pokemon #5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Consult the type chart and build a multiplier | |
calculateTypeEffect(otherPokemonType) { | |
// If we haven't defined the type in the typeChart return normal damage | |
if (typeof this.typeChart[this.attack.type] == 'undefined') | |
return 1; | |
// Here we'll build the multiplier in | |
let effectTotal = 1; | |
// In case of immune we'll just set this to true | |
let noEffect = false; | |
// Let's loop over every type the defending pokemon has | |
this.defending.type.forEach((type) => { | |
// Let's loop offer the effects that attack type has on other types | |
Object.keys(this.typeChart[this.attack.type]).forEach((effect) => { | |
const otherTypes = this.typeChart[this.attack.type][effect]; | |
// If the effect has types defined and the defending pokemon's type is listed | |
if(otherTypes.length > 0 && otherTypes.indexOf(type) > -1) | |
{ | |
// Figure out what effect to apply | |
switch(effect) | |
{ | |
case 'immunes': | |
noEffect = true; | |
effectTotal = 0; | |
break; | |
// Reduce the attack's damage | |
case 'weaknesses': | |
effectTotal = effectTotal * 0.5; | |
break; | |
// Increase the attack's effect | |
case 'strengths': | |
if(effectTotal < 2) | |
effectTotal = effectTotal * 2; | |
break; | |
} | |
} | |
}, this); | |
}); | |
// Defending pokemon is immune | |
if (noEffect || effectTotal == 0) | |
{ | |
Vuemit.fire(otherPokemonType+'.attack.effective', 'Had no effect'); | |
return 0; | |
} | |
if(effectTotal == 0.5) | |
{ | |
Vuemit.fire(otherPokemonType+'.attack.effective', 'Not very effective'); | |
} | |
else if(effectTotal > 1) | |
{ | |
Vuemit.fire(otherPokemonType+'.attack.effective', 'Very effective'); | |
} | |
// Return the multiplier | |
return effectTotal; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment