You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is in preparation for an upcoming feature to quickly switch
to a temporary phase (and back after it is done).
OLD:
phases: [
{ name: 'A', ... }
{ name: 'B', ... }
]
NEW:
phases: {
A: { ... },
B: { ... },
}
This allows creation of different phase orderings that aren't
simple lists. Each phase can now point to a `next` phase:
phases: {
A: { next: 'B' },
}
A 'default' phase is the catch-all next phase for phases that don't
specify `next`.
In order to emulate the old behavior, you can do something like:
phases: {
A: { next: 'B' },
B: { next: 'C' },
C: { next: 'A' },
}
Copy file name to clipboardExpand all lines: docs/phases.md
+54-28Lines changed: 54 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,19 +42,14 @@ We'll ignore the rendering component of this game, but this is how it might look
42
42
43
43
Now let's say we want the game to work in two phases:
44
44
45
-
*the first phase where the player can only draw cards (until the deck is empty).
45
+
*a first phase where the player can only draw cards (until the deck is empty).
46
46
* a second phase where the player can only play cards (until their hand is empty).
47
47
48
-
In order to do this, we add a `flow` section to the `Game`
49
-
constructor (you should already be familiar with this as the location
48
+
In order to do this, we add a `flow` section to the game
49
+
spec (you should already be familiar with this as the location
50
50
where you placed `endGameIf` in the
51
-
[tutorial](#/tutorial?id=add-victory-condition)).
52
-
53
-
It can also contain a `phases` array, which defines different
54
-
phases in the game. Each phase can specify a list of `allowedMoves`,
51
+
[tutorial](#/tutorial?id=add-victory-condition)). It can also contain a `phases` object, which defines different phases in the game. Each phase can specify a list of `allowedMoves`,
55
52
which allows only those moves to be played during that phase.
56
-
They can also contain a `endPhaseIf` function that terminates
57
-
the phase automatically if a particular condition is met.
58
53
59
54
```js
60
55
constgame=Game({
@@ -66,24 +61,58 @@ const game = Game({
66
61
},
67
62
68
63
flow: {
69
-
phases: [
70
-
{
71
-
name:'draw phase',
64
+
startingPhase:'draw',
65
+
66
+
phases: {
67
+
draw: {
72
68
allowedMoves: ['drawCard'],
73
69
endPhaseIf:G=>G.deck<=0,
70
+
next:'play',
74
71
},
75
-
{
76
-
name:'play phase',
72
+
73
+
play: {
77
74
allowedMoves: ['playCard'],
78
75
endPhaseIf:G=>G.hand<=0,
76
+
next:'draw',
79
77
},
80
-
],
78
+
},
81
79
},
82
80
});
83
81
```
84
82
85
-
!> Phases can also be terminated manually by calling `props.events.endPhase()` from the
86
-
board React component (in response to a user action like clicking a button, for example).
83
+
!> Every game also has an implicit `default` phase, which is just
84
+
a phase with no specific options, so the example above actually
85
+
has three phases, even though we never use the `default` phase.
86
+
87
+
### Terminating a phase
88
+
89
+
A phase ends when one of the following happens:
90
+
91
+
###### 1. `endPhaseIf` triggers:
92
+
93
+
This is a simple boolean function that terminates the phase when
94
+
it returns `true` (see the example above).
95
+
96
+
###### 2. The `endPhase` event is dispatched:
97
+
98
+
This can happen either in the game logic or from the client
99
+
directly. See the [Events API](events.md) for more details
100
+
on how to dispatch events.
101
+
102
+
###### What happens when a phase terminates?
103
+
104
+
The game moves on to the "next" phase. This phase is determined by the
105
+
following in increasing order of precedence (i.e. if [2] and [4] are both
106
+
relevant, the result of [4] is used):
107
+
108
+
1. The `default` phase is chosen as the next phase if no other option is present.
109
+
110
+
2. If a phase specifies the `next` option (like our example above does), then that is
111
+
chosen as the next phase.
112
+
113
+
3.`endPhaseIf` can return the name of the next phase.
114
+
115
+
4. The `endPhase` event accepts the name of the next phase as an argument.
87
116
88
117
Watch our game in action now with phases. Notice that you can only draw cards in the first
89
118
phase, and you can only play cards in the second phase.
@@ -101,13 +130,12 @@ end of a phase. These are specified just like normal moves in `onPhaseBegin` and
101
130
`onPhaseEnd`.
102
131
103
132
```js
104
-
phases: [
105
-
{
106
-
name:'...',
133
+
phases: {
134
+
phaseA: {
107
135
onPhaseBegin: (G, ctx) =>G,
108
136
onPhaseEnd: (G, ctx) =>G,
109
137
},
110
-
];
138
+
};
111
139
```
112
140
113
141
#### Triggers / Hooks
@@ -116,7 +144,7 @@ The `flow` section can specify a number of automatic behaviors when a move is ma
116
144
or when the turn or phase is ended. These can also be overridden at the phase level.
117
145
Let's take a look at some of these:
118
146
119
-
!> For a more complete set of options, take a look
147
+
!> For an authoritative set of options, take a look
0 commit comments