Skip to content

Instantly share code, notes, and snippets.

@Abeyy Abeyy/store.js Secret
Created Aug 29, 2018

Embed
What would you like to do?
store in Vuex
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
currentView: 'homeScreen',
currentViewOptions: ['homeScreen', 'howToPlay', 'storyIntro',
'enemySelectionScreen', 'playGame', 'enemyDefeated'],
currentActionMessagesFirst: 'Begin Fight',
currentActionMessagesSecond: 'Good luck!',
// Hero Stats
currentHeroHealth: '',
currentHeroMaxHealth: '',
currentHeroLevel: '',
currentHeroExperience: '',
experienceToNextLevel: '',
currentHeroState: '',
currentHeroStateOptions: ['idle', 'attacking1', 'attacking2', 'poisoned', 'slain'],
heroAnimations: []
// Enemy Stats
currentEnemy: {},
},
getters: {
},
mutations: {
changeView(state, view) {
return state.currentView = view
},
updateCurrentActionMessages(state, action) {
console.log('msg updated in state')
let powerAmt = state.currentHeroLevel * 10
state.currentActionMessagesFirst = `You ${action} for ${powerAmt}!`
state.currentActionMessagesSecond = `${state.currentEnemy.name} attacks for ${state.currentEnemy.damage}!`
},
initializeEnemy(state, enemy) {
state.currentEnemy = enemy
},
damageEnemy(state, damage) {
return state.currentEnemy.health = state.currentEnemy.health - damage
},
damageHero(state) {
state.currentHeroHealth = state.currentHeroHealth - state.currentEnemy.damage
if (state.currentHeroHealth <= 0) {
state.currentHeroState = 'Dead'
}
},
healHero(state, heal) {
state.currentHeroHealth = state.currentHeroHealth + heal
console.log('healed for', heal)
if (state.currentHeroHealth > state.currentHeroMaxHealth) {
state.currentHeroHealth = state.currentHeroMaxHealth
}
},
updateHeroStatus(state, status) {
state.currentHeroState = status
if (status == 'Attacking') {
setTimeout(function(){
state.currentHeroState = 'Idle';
}, 500);
}
},
grantExperience(state, exp) {
state.experienceToNextLevel = state.experienceToNextLevel - exp
if (state.experienceToNextLevel <= 0) {
state.currentHeroLevel += 1
state.currentHeroMaxHealth += 50
state.currentHeroHealth = state.currentHeroMaxHealth
state.experienceToNextLevel = (state.currentHeroLevel * 50)
}
}
}
})
export default store
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.