Advertisement
Guest User

Untitled

a guest
Jul 9th, 2016
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { EventEmitter } from 'events'
  2. import Dispatcher from '../dispatcher/dispatcher.js'
  3. import { ActionTypes } from '../constants/actions'
  4.  
  5. const state = {
  6.     health: 100,
  7.     fatigue: 100
  8. }
  9.  
  10. const stat = {
  11.     strength: 1, // affects rof and damage
  12.     agility: 1, // affects speed
  13.     endurance: 1 // affects rate of fatigue
  14. }
  15.  
  16. /**
  17.  * A store registers itself with the dispatcher and provides it with a callback.
  18.  * This callback receives the action as a parameter.
  19.  * Within the store's registered callback, a switch statement based on the action's
  20.  * type is used to interpret the action and to provide the proper hooks into the store's internal methods.
  21.  * This allows an action to result in an update to the state of the store, via the dispatcher.
  22.  * After the stores are updated, they broadcast an event declaring that their state has changed,
  23.  * so the views may query the new state and update themselves.
  24.  *
  25.  * Stores contain the application state and logic.
  26.  * Their role is somewhat similar to a model in a traditional MVC,
  27.  * but they manage the state of many objects:
  28.  * they do not represent a single record of data like ORM models do.
  29.  * Nor are they the same as Backbone's collections.
  30.  * More than simply managing a collection of ORM-style objects,
  31.  * ##stores manage the application state for a particular domain within the application##.
  32.  */
  33. class PlayerStore extends EventEmitter {
  34.  
  35.     constructor() {
  36.         super()
  37.  
  38.         // register this store with the dispatcher
  39.         this.dispatchToken = Dispatcher.register(this.onAction.bind(this))
  40.     }
  41.  
  42.     onAction(payload) {
  43.  
  44.         // provide hooks to each respective action
  45.         switch(payload.type) {
  46.             case ActionTypes.PLAYER_HEALED:
  47.                 this.increaseHealth()
  48.                 break
  49.             case ActionTypes.PLAYER_INJURED:
  50.                 this.noIHaveNoMadeThisBitYet()
  51.                 break
  52.         }
  53.     }
  54.  
  55.     get health() {
  56.         return state.health
  57.     }
  58.  
  59.     increaseHealth() {
  60.         state.health += 6
  61.  
  62.         // broadcast an event declaring that the state has changed
  63.         // so that whoever is listening to this store can update themself
  64.         this.emit('change')
  65.     }
  66. }
  67.  
  68. module.exports = new PlayerStore();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement