Skip to content

Commit 9d3dd94

Browse files
committed
Improve revert functionality
1 parent e80b452 commit 9d3dd94

35 files changed

Lines changed: 217 additions & 123 deletions

core/lib/Action.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ class Action {
286286
}
287287

288288
/**
289-
* _setCycle - This simple method is used to set what cycle the action is
289+
* _setCycle - This simple method is used to set what cycle the action is
290290
* currently performing. This is useful to know on those actions that may
291291
* use the apply or revert methods on any situation but that have slight
292292
* differences on the logic.
@@ -329,7 +329,9 @@ class Action {
329329
* next statement right away, false if it should wait for user's interaction.
330330
*/
331331
didApply () {
332-
return Promise.resolve ();
332+
return Promise.resolve ({
333+
advance: false
334+
});
333335
}
334336

335337
/**
@@ -374,7 +376,10 @@ class Action {
374376
* previous statement right away, false if it should wait for user's interaction.
375377
*/
376378
didRevert () {
377-
return Promise.resolve ();
379+
return Promise.resolve ({
380+
advance: false,
381+
step: true
382+
});
378383
}
379384
}
380385

core/lib/actions/Canvas.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export class Canvas extends Action {
152152
didApply () {
153153
Monogatari.history ('canvas').push (this._statement);
154154
Monogatari.state ('canvas').push (this._statement);
155-
return Promise.resolve (true);
155+
return Promise.resolve ({ advance: true });
156156
}
157157

158158
revert () {
@@ -179,7 +179,7 @@ export class Canvas extends Action {
179179
break;
180180
}
181181
}
182-
return Promise.resolve (true);
182+
return Promise.resolve ({ advance: true, step: true });
183183
}
184184
}
185185

core/lib/actions/Centered.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ export class Centered extends Action {
7171
this.apply ();
7272
return Promise.resolve ();
7373
}
74+
75+
didRevert () {
76+
return Promise.resolve ({ advance: false, step: true });
77+
}
7478
}
7579

7680
Centered.id = 'Centered';

core/lib/actions/Choice.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export class Choice extends Action {
66

77
static setup () {
88
Monogatari.global ('_CurrentChoice', null);
9+
Monogatari.history ('choice');
910
return Promise.resolve ();
1011
}
1112

@@ -52,6 +53,7 @@ export class Choice extends Action {
5253
Monogatari.run (Monogatari.global ('_CurrentChoice')[$_(this).data ('choice')].Do, false);
5354
Monogatari.global ('_CurrentChoice', null);
5455
}
56+
Monogatari.history ('choice').push ($_(this).data ('choice'));
5557
} else {
5658
Monogatari.run ($_(this).data ('do'), false);
5759
Monogatari.global ('_CurrentChoice', null);
@@ -155,8 +157,36 @@ export class Choice extends Action {
155157
// Revert is disabled for choices since we still don't have a way to know what
156158
// a choice did
157159
willRevert () {
160+
if (Monogatari.history ('choice').length > 0) {
161+
const choice = Monogatari.history ('choice')[Monogatari.history ('choice').length - 1];
162+
if (this.statement[choice] !== 'undefined') {
163+
164+
// Check if the choice had an onChosen function with it's matching
165+
// onRevert functionality, or if no onChosen function was provided
166+
// which are the only cases where it can be reverted.
167+
const functionReversible = (typeof this.statement[choice]['onRevert'] === 'function' && typeof this.statement[choice]['onChosen'] === 'function') || typeof this.statement[choice]['onChosen'] !== 'function';
168+
169+
if (functionReversible) {
170+
return Promise.resolve ();
171+
}
172+
}
173+
}
158174
return Promise.reject ();
159175
}
176+
177+
revert () {
178+
const choice = Monogatari.history ('choice')[Monogatari.history ('choice').length - 1];
179+
return Monogatari.revert (this.statement[choice].Do, false).then (() => {
180+
return Monogatari.run (this.statement);
181+
}).catch (() => {
182+
});
183+
return Promise.resolve ();
184+
}
185+
186+
didRevert () {
187+
Monogatari.history ('choice').pop ();
188+
return Promise.resolve ({ advance: false, step: false });
189+
}
160190
}
161191

162192
Choice.id = 'Choice';

core/lib/actions/Clear.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class Clear extends Action {
1717
}
1818

1919
didApply () {
20-
return Promise.resolve (true);
20+
return Promise.resolve ({ advance: true });
2121
}
2222

2323
willRevert () {

core/lib/actions/Conditional.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { Util } from '@aegis-framework/artemis';
44

55
export class Conditional extends Action {
66

7+
static setup (selector) {
8+
Monogatari.history ('conditional');
9+
}
10+
711
static matchObject (statement) {
812
return typeof statement.Conditional !== 'undefined';
913
}
@@ -16,7 +20,7 @@ export class Conditional extends Action {
1620
apply () {
1721
return new Promise ((resolve) => {
1822

19-
// Call the condition function. Since the function might use a
23+
// Call the condition function. Since the function might use a
2024
// Promise.reject () to return as false, we also define a catch
2125
// block to run the False branch of the condition.
2226
Util.callAsync (this.statement.Condition, Monogatari).then ((returnValue) => {
@@ -25,7 +29,7 @@ export class Conditional extends Action {
2529

2630
// Check if the function returned true so we run the True branch
2731
// of the conditional. If false is returned, we run the False
28-
// branch of the conditional and if a string is returned, we use
32+
// branch of the conditional and if a string is returned, we use
2933
// it as a key so we run the branch that has that key
3034
if (returnValue === true) {
3135
Monogatari.run (this.statement.True, false);

core/lib/actions/Dialog.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,10 @@ export class Dialog extends Action {
327327
}
328328
}
329329

330+
didRevert () {
331+
return Promise.resolve ({ advance: false, step: true });
332+
}
333+
330334
}
331335

332336
Dialog.id = 'Dialog';

core/lib/actions/Function.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class ReversibleFunction extends Action {
1919
// The function will be run asynchronously (No matter if its code isn't)
2020
// if the function returns false, the next statement will not be run
2121
// automatically and the game will wait for user interaction or some other
22-
// code inside the function to keep going. Any other returnValue will
22+
// code inside the function to keep going. Any other returnValue will
2323
// allow the game to keep going right away.
2424
return Util.callAsync (this.statement.Apply, Monogatari).then ((returnValue) => {
2525
Monogatari.global ('block', false);
@@ -52,7 +52,7 @@ export class ReversibleFunction extends Action {
5252
// The function will be run asynchronously (No matter if its code isn't)
5353
// if the function returns false, the previous statement will not be run
5454
// automatically and the game will wait for user interaction or some other
55-
// code inside the function to keep going. Any other returnValue will
55+
// code inside the function to keep going. Any other returnValue will
5656
// allow the game to keep going right away.
5757
return Util.callAsync (this.statement.Reverse, Monogatari).then ((returnValue) => {
5858
Monogatari.global ('block', false);
@@ -78,7 +78,7 @@ export class ReversibleFunction extends Action {
7878
}
7979

8080
didRevert () {
81-
return Promise.resolve (this.shouldContinue);
81+
return Promise.resolve ({ advance: this.shouldContinue, step: true });
8282
}
8383
}
8484

core/lib/actions/Gallery.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export class Gallery extends Action {
4242
}
4343
return Promise.resolve ();
4444
}
45+
46+
didRevert () {
47+
return Promise.resolve ({ advance: true, step: true });
48+
}
4549
}
4650

4751
Gallery.id = 'Gallery';

core/lib/actions/HideCanvas.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class HideCanvas extends Action {
4848
break;
4949
}
5050
}
51-
return Promise.resolve (true);
51+
return Promise.resolve ({ advance: true });
5252
}
5353

5454
revert () {
@@ -65,7 +65,7 @@ export class HideCanvas extends Action {
6565
}
6666

6767
didRevert () {
68-
return Promise.resolve (true);
68+
return Promise.resolve ({ advance: true, step: true });
6969
}
7070
}
7171

0 commit comments

Comments
 (0)