Skip to content

Instantly share code, notes, and snippets.

@headwinds
Created April 20, 2020 12:34
Show Gist options
  • Save headwinds/0925e64051cb9e047be6f0c25568e3dd to your computer and use it in GitHub Desktop.
Save headwinds/0925e64051cb9e047be6f0c25568e3dd to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const updateContext = () => {};
const reduceEnemy = () => {};
const reduceHero = () => {};
const ffMachine = Machine(
{
id: "finalfantasy",
initial: "battle",
context: {
turnCount: 0,
enemies: [],
party: [],
phase: "start",
selectedAction: "PARTY_FIGHT",
selectedEnemy: null,
selectedHero: null,
experience: 0,
round: 0,
hereosActedCount: 0,
hereosTotal: 4,
},
states: {
battle: {
on: {
INCREMENT_TURN: {
actions: updateContext((context) => context.turnCount++),
},
DECREMENT_TURN: {
actions: updateContext((context) => context.turnCount--),
},
ENEMY_ATTACK: {
actions: updateContext((context) => context.turnCount--),
},
PARTY_FIGHT: {
actions: updateContext(reduceEnemy),
},
PARTY_RUN: {
actions: updateContext(reduceEnemy),
},
MOVE_HERO: {
actions: updateContext(reduceHero),
},
UPDATE_PHASE: {
actions: updateContext(
(context, action) => (context.phase = action.phase)
),
},
SELECTED_ACTION: {
actions: updateContext(
(context, action) =>
(context.selectedAction = action.selectedAction)
),
},
SELECTED_ENEMY: {
actions: updateContext(
(context, action) =>
(context.selectedEnemy = action.selectedEnemy)
),
},
SELECTED_HERO: {
actions: updateContext(
(context, action) =>
(context.selectedHero = action.selectedHero)
),
},
REWARD_EXPERIENCE: {
actions: assign({
experience: 100
})
}
}
},
results: { type: "final" },
gameover: { type: "final" },
},
},
{
guards: {
didPlayerWin: (context, event) => {
// check if player won
return context.experience > 99;
},
didPlayerLose: (context, event) => {
// check if player lost
return context.experience < 0;
},
},
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment