Skip to content

Instantly share code, notes, and snippets.

@marlibs3-zz
Created July 16, 2018 10:36
Show Gist options
  • Save marlibs3-zz/cd85a628135679c1c947b7ed41fc8127 to your computer and use it in GitHub Desktop.
Save marlibs3-zz/cd85a628135679c1c947b7ed41fc8127 to your computer and use it in GitHub Desktop.
R(edux)PG - Run on JSFiddle
const { createStore, combineReducers } = Redux;
// The Hero
const initialState = {
xp: 0,
level: 1,
position: {
x: 0,
y: 0,
},
stats: {
health: 50,
maxHealth: 50,
},
inventory: {
potions: 1,
}
};
// Actions
const Actions = {
GAIN_XP: 'GAIN_XP',
LEVEL_UP: 'LEVEL_UP',
MOVE: 'MOVE',
DRINK_POTION: 'DRINK_POTION',
TAKE_DAMAGE: 'TAKE_DAMAGE',
};
// Action Creators
const gainXp = (xp) => ({
type: Actions.GAIN_XP,
payload: xp
});
const levelUp = () => ({
type: Actions.LEVEL_UP
});
const move = (x, y) => ({
type: Actions.MOVE,
payload: { x, y }
});
const drinkPotion = () => ({
type: Actions.DRINK_POTION
});
const takeDamage = (amount) => ({
type: Actions.TAKE_DAMAGE,
payload: amount
});
// Reducers
const xpReducer = (state = 0, action) => {
switch (action.type) {
case Actions.GAIN_XP:
return state + action.payload;
}
return state;
}
const levelReducer = (state = 1, action) => {
switch (action.type) {
case Actions.LEVEL_UP:
return state + 1;
}
return state;
};
const positionReducer = (state = initialState.position, action) => {
switch (action.type) {
case Actions.MOVE:
let { x, y } = action.payload;
x += state.x;
y += state.y;
return { x, y };
}
return state;
};
const statsReducer = (state = initialState.stats, action) => {
let { health, maxHealth } = state;
switch (action.type) {
case Actions.DRINK_POTION:
health = Math.min(health + 20, maxHealth);
return { ...state, health, maxHealth };
case Actions.TAKE_DAMAGE:
health = Math.max(0, health - action.payload);
return { ...state, health };
}
return state;
};
const inventoryReducer = (state = initialState.inventory, action) => {
let { potions } = state;
switch (action.type) {
case Actions.DRINK_POTION:
potions = Math.max(0, potions - 1);
return { ...state, potions };
}
return state;
};
// Bootstrapping
const reducer = combineReducers({
xp: xpReducer,
level: levelReducer,
position: positionReducer,
stats: statsReducer,
inventory: inventoryReducer,
});
const store = createStore(reducer);
store.subscribe(() => {
console.log(JSON.stringify(store.getState()));
});
// Run!
store.dispatch(move(1, 0));
store.dispatch(move(0, 1));
store.dispatch(takeDamage(13));
store.dispatch(drinkPotion());
store.dispatch(gainXp(100));
store.dispatch(levelUp());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment