From f3deb8977d8181f11eb110439d3a65130704bd4a Mon Sep 17 00:00:00 2001 From: pemcne Date: Mon, 18 Jun 2018 23:38:31 -0400 Subject: [PATCH] Replaced the EventBBus with custom lib, moved income to Engine and tied to ticker --- src/Engine.js | 47 +++++++++++++++---- src/EventBus.js | 4 -- src/components/Market.vue | 8 ++-- src/components/ResourceList.vue | 34 -------------- src/main.js | 17 ++----- src/modules/EventBus.js | 82 +++++++++++++++++++++++++++++++++ src/store/index.js | 8 +++- 7 files changed, 137 insertions(+), 63 deletions(-) delete mode 100644 src/EventBus.js create mode 100644 src/modules/EventBus.js diff --git a/src/Engine.js b/src/Engine.js index 8479132..00f6fb0 100644 --- a/src/Engine.js +++ b/src/Engine.js @@ -1,9 +1,40 @@ -const buyItem = (store, item, count, cost) => { - store.dispatch('buy', { - item: item, - amount: count, - cost: cost - }) -} +// Set up the event bus +import EventBus from '@/modules/EventBus.js' + +export default { + store: null, + eventBus: EventBus, + buyItem (item, amount, cost) { + this.store.dispatch('buy', { + item, + amount, + cost + }) + }, + tick () { + const timestamp = Math.floor(new Date() / 1000) + EventBus.emit('tick', {current: timestamp, previous: this.store.getters.timestamp}) + this.store.dispatch('setTime', {timestamp}) + }, + start () { + this.interval = setInterval(() => this.tick(), 1000) -export {buyItem} + // Set up the other listeners + EventBus.on('tick', () => this.income()) + }, + stop () { + clearInterval(this.interval) + EventBus.reset() + }, + income () { + let income = 0 + for (const key in this.store.getters.inventory) { + const resource = this.store.getters.resource(key) + const amount = this.store.getters.inventory[key] + income += amount * resource.income + } + this.store.dispatch('income', { + amount: income + }) + } +} diff --git a/src/EventBus.js b/src/EventBus.js deleted file mode 100644 index f003e83..0000000 --- a/src/EventBus.js +++ /dev/null @@ -1,4 +0,0 @@ -import Vue from 'vue' - -// Idea grabbed from https://alligator.io/vuejs/global-event-bus/ -export default new Vue() diff --git a/src/components/Market.vue b/src/components/Market.vue index 995b1f4..b38da71 100644 --- a/src/components/Market.vue +++ b/src/components/Market.vue @@ -1,12 +1,12 @@ diff --git a/src/main.js b/src/main.js index bb66bdb..01cf5de 100644 --- a/src/main.js +++ b/src/main.js @@ -3,7 +3,8 @@ import Vue from 'vue' import App from './App' import store from './store' -import EventBus from './EventBus.js' +// import EventBus from './EventBus.js' +import Engine from '@/Engine.js' Vue.config.productionTip = false @@ -17,20 +18,12 @@ new Vue({ counter: 0 }, mounted () { - // Call tick first for inital state - this.tick() - // Now start the clock - this.$options.interval = setInterval(this.tick, 1000) + Engine.store = this.$store + Engine.start() }, beforeDestroy () { - clearInterval(this.$options.interval) + Engine.stop() }, methods: { - // This fires an event on the EventBus every second - // Primary driver for the game, should probably move to Engine.js - tick () { - const timestamp = new Date() / 1000 - EventBus.$emit('tick', timestamp) - } } }) diff --git a/src/modules/EventBus.js b/src/modules/EventBus.js new file mode 100644 index 0000000..005799d --- /dev/null +++ b/src/modules/EventBus.js @@ -0,0 +1,82 @@ +// Originally borrowed from https://gist.github.com/zangue/7384f593df908d4a7ee2 + +class EventBus { + constructor () { + this.listeners = {} + } + + _getListenerIdx (eventName, callback, scope) { + let eventListeners = this.listeners[eventName] + let i + let idx = -1 + + if (!eventListeners || eventListeners.length === 0) { + return idx + } + for (i = 0; i < eventListeners.length; i++) { + if (eventListeners[i].callback === callback && + (!scope || scope === eventListeners[i].scope)) { + idx = i + break + } + } + return idx + } + + on (eventName, callback, scope) { + let listener, + idx + + if (!eventName) { + throw new Error('Event name cannot be null or undefined') + } + + if (!callback || typeof (callback) !== 'function') { + throw new Error('Listener must be of type function.') + } + + idx = this._getListenerIdx(eventName, callback, scope) + + if (idx >= 0) return + + listener = { + callback: callback, + scope: scope + } + + this.listeners[eventName] = this.listeners[eventName] || [] + this.listeners[eventName].push(listener) + } + + unsubscribe (eventName, callback, scope) { + let idx + if (!eventName || !callback || !this.listeners[eventName]) { + return + } + idx = this._getListenerIdx(eventName, callback, scope) + if (idx === -1) return + this.listeners[eventName].splice(idx, 1) + } + + emit (eventName, args) { + let eventListeners = this.listeners[eventName] + + if (!eventName || !this.listeners[eventName]) { + return + } + + args = args || {} + + eventListeners.forEach((listener) => { + listener.callback.call(listener.scope, args) + }) + } + + reset () { + this.listeners = {} + } +} + +// Singleton +let eventBus = new EventBus() +export default eventBus diff --git a/src/store/index.js b/src/store/index.js index 9ca676e..43c45cf 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -25,7 +25,7 @@ const state = { cost: 10, rate: 0.2 }], - timestamp: new Date() / 1000 + timestamp: Math.floor(new Date() / 1000) } // Define getters here @@ -56,6 +56,9 @@ const actions = { }, income ({commit}, {amount}) { commit('INCOME', {amount}) + }, + setTime ({commit}, {timestamp}) { + commit('SETTIME', {timestamp}) } } @@ -73,6 +76,9 @@ const mutations = { }, INCOME (state, {amount}) { state.money += amount + }, + SETTIME (state, {timestamp}) { + state.timestamp = timestamp } }