Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reducer solution memo #25

Closed
TyrealGray opened this issue Aug 28, 2019 · 0 comments
Closed

Reducer solution memo #25

TyrealGray opened this issue Aug 28, 2019 · 0 comments
Labels

Comments

@TyrealGray
Copy link
Owner

TyrealGray commented Aug 28, 2019

One Dispatch example

dispatch({type: KILL_ACTION, payload:{triggerBy: houyi, triggerTo: npc1});

const npcInfo = (state = initState, action) =>
	produce(state, draft => {
		const { type: actionType, triggerBy, triggerTo } = action;
		draft.npcs.forEach( (npc,index) => if( npc[actionType] ){
		const { onlyDirect, isAny, types, triggers, blockers } = npc[actionType];
		if((onlyDirect && triggerTo.id !== npc.id)
		|| blockers.includes(triggerBy.id))
		|| (!isAny && !types.includes(triggerBy.type) && !triggers.includes(triggerBy.id))){
			return;
		}
		/* reaction -> e.g: [
			{type:ADD, props:'hp', value: 100 },
			{type:MAYBE_ADD, props:'mp', rate: 0.9 , value: 0 },
		] */
		const reactions = npc[actionType].reactions;
		// processReaction is a function in common util 
		const updatedNpc = processReaction(reactions, npc);
		draft.npcs[index] = updatedNpc;
	})
}

const processReaction = (reactions, target) => {
	for (const reaction of reactions){
		switch(reaction.type){
			case ADD:
				target[reaction.props] += reaction.value;
				break;
			case MAYBE_ADD:
				if(randomCheck(seed)){
					target[reaction.props] += reaction.value;
				}
				break;
				//...
		}
	}
	return target;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant