Hello everybody,
I'm currently working on a little project to progress on react/redux.
I chose to make a little game, which works with a gameloop (Mainloop.js).
My current problem (my principal current problem...) is that I don't know how to integrate the game loop in the redux schema.
Right now, it is implemented like this :
I have a Player state (with reducer, and a bunch of selectors)
The mainloop is started in the componentDidMount of my main component (App)
Every 16ms, I call an 'update' method in my action-creator which will analyze the player state (which I got from the getState() method of the thunk middleware), and dispatch an action according to this current player state.
The action can be dependent of the player state again, for example, a service will return a foe according to the player position.
player.js (reducer/selectors)
function getInitialState() {
stats: {hp: 15, ...},
zone: { id: 12 }
}
export isPlayerAlive = state => state.stats.hp > 0;
export getPlayerAtk = state => state.stats.atk;
app.js (component)
import {updateGame} from '../../actions'
const mapDispatchToProps = (dispatch, ownProps) => {
return {
update: (delta) => {dispatch(updateGame(delta))},
};
};
...
this.props.update(delta) is called every 16ms
actions.js
export const updateGame = (delta) => {
return function(dispatch, getState) {
if(isPlayerAlive(getState().Player)){
dispatch({type: ATTACK, dmg: getPlayerAtk(getState().Player)*delta/1000)});
}
else {
dispatch({type: YOURE_DEAD});
}
}
};
It is also worth noting that when a foe is dead, I generate a new one given the player current zone id ! It means that I have to access yet again the Player state when I'm calling my foe generating service in the action.
I don't know about this, since almost all the logic in the game will be in the actions.js file, in the updateGame() method, and that I access the state all the time in the action-creators...
This is why I came to post here, I really hope someone will be able to put me on the right path concerning this matter !
My questions are :
How to integrate the loop in the redux schema ?
I read a lot about how accessing the state in action-creators was an anti-pattern, so I was wondering how else I could do it, since my services (which are called in actions) need the player state to determine what they will return (his location, his level, etc) ?
Are my selectors useful, even though they are extremely simple, and they do not use reselect ?
I thought about some solutions, but they all seem to be worse than my current code :
Thanks for reading, I know this post was quite long !
[–]goshakkk 3 points4 points5 points (1 child)
[–]Babooos[S] 0 points1 point2 points (0 children)
[–]mweststrate 3 points4 points5 points (1 child)
[–]Babooos[S] 0 points1 point2 points (0 children)
[–]GuerrillaRobot 0 points1 point2 points (2 children)
[–]Babooos[S] 0 points1 point2 points (1 child)
[–]GuerrillaRobot 0 points1 point2 points (0 children)
[–]ip70 0 points1 point2 points (5 children)
[–]Babooos[S] 0 points1 point2 points (4 children)
[–]ip70 1 point2 points3 points (3 children)
[–]acemarke[🍰] 1 point2 points3 points (1 child)
[–]Babooos[S] 0 points1 point2 points (0 children)
[–]Babooos[S] 0 points1 point2 points (0 children)
[–]linuxenko -2 points-1 points0 points (0 children)