-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from pemcne/eventbus
EventBus and Engine overhaul
- Loading branch information
Showing
7 changed files
with
137 additions
and
63 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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