parent
a19dd163ad
commit
44620c2a3f
12 changed files with 231 additions and 107 deletions
@ -1,13 +1,7 @@ |
||||
export const SCOREBAR_ACTION = 'SCOREBAR_ACTION'; |
||||
export const MUNCH_SUCCEEDED = 'SCOREBAR_MUNCH_SUCCEEDED'; |
||||
export const MUNCH_FAILED = 'SCOREBAR_MUNCH_FAILED'; |
||||
export const UPDATE = 'SCOREBAR_UPDATE'; |
||||
|
||||
export const munchSucceeded = () => ({ |
||||
export const update = () => ({ |
||||
type: SCOREBAR_ACTION, |
||||
action: MUNCH_SUCCEEDED, |
||||
}); |
||||
|
||||
export const munchFailed = () => ({ |
||||
type: SCOREBAR_ACTION, |
||||
action: MUNCH_FAILED |
||||
action: UPDATE |
||||
}); |
||||
|
@ -0,0 +1,43 @@ |
||||
import * as ScorebarActions from '../actions/board/scorebar.actions'; |
||||
import { SETTINGS } from '../App'; |
||||
|
||||
let dispatch; |
||||
let lives = 3; |
||||
let currentScore = 0; |
||||
let highScore = 7; |
||||
|
||||
const ScorebarCtrl = { |
||||
setDispatch: d => dispatch = d, |
||||
|
||||
getCurrentScore: () => currentScore, |
||||
getHighScore: () => highScore, |
||||
getLives: () => lives, |
||||
|
||||
munchSucceeded: () => { |
||||
currentScore += 10; |
||||
dispatch(ScorebarActions.update()); |
||||
}, |
||||
|
||||
munchFailed: () => { |
||||
lives--; |
||||
currentScore -= 5; |
||||
dispatch(ScorebarActions.update()); |
||||
}, |
||||
|
||||
eatenByTroggle: () => { |
||||
lives--; |
||||
dispatch(ScorebarActions.update()); |
||||
}, |
||||
|
||||
levelUp: (level) => { |
||||
currentScore += 25; |
||||
dispatch(ScorebarActions.update()); |
||||
}, |
||||
|
||||
reset: () => { |
||||
lives = SETTINGS.LIVES; |
||||
currentScore = 0; |
||||
} |
||||
}; |
||||
|
||||
export default ScorebarCtrl; |
@ -1,24 +1,24 @@ |
||||
const Immutable = require('immutable'); |
||||
|
||||
import * as ScorebarActions from '../../actions/board/scorebar.actions'; |
||||
import { SETTINGS } from '../../App'; |
||||
import ScorebarCtrl from '../../controllers/scorebar.controller.js'; |
||||
|
||||
const initial = { current: 0, high: 999, lives: SETTINGS.LIVES }; |
||||
const initial = { |
||||
current: ScorebarCtrl.getCurrentScore(), |
||||
high: ScorebarCtrl.getHighScore(), |
||||
lives: ScorebarCtrl.getLives() |
||||
}; |
||||
|
||||
const reducer = (state = initial, action) => { |
||||
if (action.type !== ScorebarActions.SCOREBAR_ACTION) { |
||||
return state; |
||||
} |
||||
|
||||
switch (action.action) { |
||||
case ScorebarActions.MUNCH_SUCCEEDED: |
||||
return Immutable.Map(state).set('current', state.current + 10).toObject(); |
||||
|
||||
case ScorebarActions.MUNCH_FAILED: |
||||
return Immutable.Map(state).set('lives', state.lives - 1).toObject(); |
||||
} |
||||
|
||||
return state; |
||||
return Immutable.Map(state) |
||||
.set('current', ScorebarCtrl.getCurrentScore()) |
||||
.set('high', ScorebarCtrl.getHighScore()) |
||||
.set('lives', ScorebarCtrl.getLives()) |
||||
.toObject(); |
||||
}; |
||||
|
||||
export default reducer; |
||||
|
Loading…
Reference in new issue