Skip to content

Commit 7043621

Browse files
committed
Improve apply and revert flow
1 parent c14fb91 commit 7043621

12 files changed

Lines changed: 509 additions & 433 deletions

File tree

core/lib/actions/Centered.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,36 @@ import Typed from 'typed.js';
55

66
export class Centered extends Action {
77

8+
static canProceed () {
9+
if (!Monogatari.global ('finishedTyping') && Monogatari.global ('textObject') !== null) {
10+
const str = Monogatari.global ('textObject').strings [0];
11+
const element = $_(Monogatari.global ('textObject').el).data ('ui');
12+
13+
if (element == 'centered') {
14+
Monogatari.global ('textObject').destroy ();
15+
$_('[data-ui="centered"]').html (str);
16+
Monogatari.global ('finishedTyping', true);
17+
}
18+
19+
return Promise.reject ();
20+
} else if (Monogatari.global ('finishedTyping') && $_(`${Monogatari.selector} [data-ui="centered"]`).isVisible ()) {
21+
$_(`${Monogatari.selector} [data-ui="centered"]`).remove ();
22+
}
23+
$_(`${Monogatari.selector} [data-ui="text"]`).show ();
24+
return Promise.resolve (Monogatari.global ('finishedTyping'));
25+
}
26+
27+
static canRevert () {
28+
if ($_(`${Monogatari.selector} [data-ui="centered"]`).isVisible ()) {
29+
$_(`${Monogatari.selector} [data-ui="centered"]`).remove ();
30+
Monogatari.global ('finishedTyping', true);
31+
Monogatari.global ('textObject').destroy ();
32+
Monogatari.global ('_CurrentChoice', null);
33+
$_(`${Monogatari.selector} [data-ui="text"]`).show ();
34+
}
35+
return Promise.resolve ();
36+
}
37+
838
static matchString ([ action ]) {
939
return action === 'centered';
1040
}

core/lib/actions/Choice.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,17 @@ export class Choice extends Action {
1616
return Promise.resolve ();
1717
}
1818

19+
static canRevert () {
20+
if ($_(`${Monogatari.selector} [data-ui="choices"]`).isVisible ()) {
21+
$_(`${Monogatari.selector} [data-ui="choices"]`).hide ();
22+
$_(`${Monogatari.selector} [data-ui="choices"]`).html ('');
23+
Monogatari.global ('_CurrentChoice', null);
24+
}
25+
return Promise.resolve ();
26+
}
27+
1928
static bind (selector) {
2029
$_(`${selector}`).on('click', '[data-do]', function () {
21-
Monogatari.action ('Centered').hide ();
22-
Monogatari.shutUp ();
2330
if ($_(this).data('do') != 'null' && $_(this).data('do') != '') {
2431
$_(`${selector} [data-ui="choices"]`).hide ();
2532
$_(`${selector} [data-ui="choices"]`).html ('');

core/lib/actions/Dialog.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ import { $_ } from '@aegis-framework/artemis';
66
export class Dialog extends Action {
77

88
static canProceed () {
9+
if (!Monogatari.global ('finishedTyping') && Monogatari.global ('textObject') !== null) {
10+
const str = Monogatari.global ('textObject').strings [0];
11+
const element = $_(Monogatari.global ('textObject').el).data ('ui');
12+
13+
if (element !== 'centered') {
14+
Monogatari.global ('textObject').destroy ();
15+
$_('[data-ui="say"]').html (str);
16+
Monogatari.global ('finishedTyping', true);
17+
}
18+
19+
return Promise.reject ();
20+
}
921
return Promise.resolve (Monogatari.global ('finishedTyping'));
1022
}
1123

@@ -136,8 +148,6 @@ export class Dialog extends Action {
136148
if (Monogatari.global ('autoPlay') !== null) {
137149
Monogatari.global ('autoPlay', setTimeout (() => {
138150
if (Monogatari.canProceed () && Monogatari.global ('finishedTyping')) {
139-
Monogatari.hideCentered ();
140-
Monogatari.shutUp ();
141151
Monogatari.next ();
142152
}
143153
}, Monogatari.preference ('AutoPlaySpeed') * 1000));
@@ -149,8 +159,6 @@ export class Dialog extends Action {
149159
if (Monogatari.global ('autoPlay') !== null) {
150160
Monogatari.global ('autoPlay', setTimeout (() => {
151161
if (Monogatari.canProceed() && Monogatari.global ('finishedTyping')) {
152-
Monogatari.hideCentered();
153-
Monogatari.shutUp();
154162
Monogatari.next ();
155163
}
156164
}, Monogatari.preference ('AutoPlaySpeed') * 1000));

core/lib/actions/InputModal.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ export class InputModal extends Action {
2828
return Promise.resolve ();
2929
}
3030

31+
static canProceed () {
32+
if ($_(`${Monogatari.selector} [data-ui="input"]`).isVisible ()) {
33+
return Promise.reject ();
34+
}
35+
return Promise.resolve ();
36+
}
37+
38+
static canRevert () {
39+
if ($_(`${Monogatari.selector} [data-ui="input"]`).isVisible ()) {
40+
$_(`${Monogatari.selector} [data-ui="input"]`).removeClass ('active');
41+
$_(`${Monogatari.selector} [data-ui="input"] [data-ui="warning"]`).text ('');
42+
$_(`${Monogatari.selector} [data-ui="input"] input`).value ('');
43+
$_(`${Monogatari.selector} [data-ui="input"]`).get (0).removeEventListener ('submit', Monogatari.global ('_inputListener'));
44+
Monogatari.global ('block', false);
45+
}
46+
return Promise.resolve ();
47+
}
48+
3149
constructor ({ Input }) {
3250
super ();
3351
this.statement = Input;

core/lib/actions/Message.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,23 @@ export class Message extends Action {
2929
}
3030

3131
static reset () {
32-
$_(`${Monogatari.selector} [data-ui="messages"]`).removeClass ('modal--active');
32+
$_(`${Monogatari.selector} [data-ui="messages"]`).removeClass ('active');
33+
return Promise.resolve ();
34+
}
35+
36+
static canProceed () {
37+
if ($_(`${Monogatari.selector} [data-ui="messages"]`).isVisible ()) {
38+
return Promise.reject ();
39+
}
40+
return Promise.resolve ();
41+
}
42+
43+
static canRevert () {
44+
if ($_(`${Monogatari.selector} [data-ui="messages"]`).isVisible ()) {
45+
$_(`${Monogatari.selector} [data-ui="messages"]`).removeClass ('active');
46+
$_(`${Monogatari.selector} [data-ui="message-content"]`).html ('');
47+
Monogatari.global ('block', false);
48+
}
3349
return Promise.resolve ();
3450
}
3551

core/lib/actions/Next.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ export class Next extends Action {
1515
Monogatari.next ();
1616
return Promise.resolve ();
1717
}
18+
19+
didRevert () {
20+
return Promise.resolve (true);
21+
}
1822
}
1923

2024
Next.id = 'Next';

core/lib/actions/PlayMedia.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ export class Play extends Action {
88
if ($_(`${Monogatari.selector} [data-component="video"]`).isVisible ()) {
99
return Promise.reject ();
1010
}
11+
Play.shutUp ();
12+
return Promise.resolve ();
13+
}
14+
15+
static canRevert () {
16+
if ($_(`${Monogatari.selector} [data-component="video"]`).isVisible ()) {
17+
Play.stopVideo ();
18+
}
19+
Play.shutUp ();
1120
return Promise.resolve ();
1221
}
1322

@@ -118,6 +127,14 @@ export class Play extends Action {
118127
$_(`${Monogatari.selector} [data-component="video"]`).removeClass ('active');
119128
}
120129

130+
// Stop the voice player
131+
static shutUp () {
132+
if (!Monogatari.voicePlayer.paused && typeof Monogatari.voicePlayer.src !== 'undefined' && Monogatari.voicePlayer.src != '') {
133+
Monogatari.voicePlayer.pause ();
134+
Monogatari.voicePlayer.currentTime = 0;
135+
}
136+
}
137+
121138
constructor ([ action, type, media, ...props ]) {
122139
super ();
123140
this.type = type;
@@ -171,7 +188,10 @@ export class Play extends Action {
171188
}
172189

173190
didApply () {
174-
return Promise.resolve (true);
191+
if (this.type !== 'video') {
192+
return Promise.resolve (true);
193+
}
194+
return Promise.resolve ();
175195
}
176196

177197
revert () {

core/lib/actions/Stop.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,24 @@ export class Stop extends Action {
7575
Monogatari.state ({
7676
music: ''
7777
});
78-
last = Monogatari.history ('music').pop ().split (' ');
78+
last = Monogatari.history ('music')[Monogatari.history ('music').length - 1].split (' ');
7979
} else if (this.type === 'sound') {
8080
Monogatari.state ({
8181
sound: ''
8282
});
83-
last = Monogatari.history ('sound').pop ().split (' ');
83+
last = Monogatari.history ('sound')[Monogatari.history ('sound').length - 1].split (' ');
8484
}
8585

86-
if ('loop' in this.props) {
86+
const [ , , media, ...props ] = last;
87+
88+
if (props.indexOf ('loop') > -1) {
8789
this.player.setAttribute ('loop', '');
8890
}
8991

90-
if (typeof Monogatari.asset (this.type, last[2]) !== 'undefined') {
91-
Monogatari.musicPlayer.setAttribute('src', `assets/${this.type}/${Monogatari.asset (this.type, last[2])}`);
92+
if (typeof Monogatari.asset (this.type, media) !== 'undefined') {
93+
this.player.setAttribute('src', `assets/${this.type}/${Monogatari.asset (this.type, media)}`);
9294
} else {
93-
Monogatari.musicPlayer.setAttribute('src', `assets/${this.type}/` + last[2]);
95+
this.player.setAttribute('src', `assets/${this.type}/${media}`);
9496
}
9597

9698
if (this.type == 'music') {

core/lib/monogatari.js

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -823,17 +823,6 @@ class Monogatari {
823823
}
824824
}
825825

826-
// Stop the voice player
827-
static shutUp () {
828-
if (!Monogatari.voicePlayer.paused && typeof Monogatari.voicePlayer.src !== 'undefined' && Monogatari.voicePlayer.src != '') {
829-
Monogatari.voicePlayer.pause ();
830-
Monogatari.voicePlayer.currentTime = 0;
831-
}
832-
}
833-
834-
835-
836-
837826
static resetGame () {
838827

839828
$_('[data-component="modal"]').removeClass ('modal--active');
@@ -888,29 +877,6 @@ class Monogatari {
888877
}
889878
}
890879

891-
static continue () {
892-
Monogatari.canProceed ().then (() => {
893-
if (!Monogatari.global ('finishedTyping') && Monogatari.global ('textObject') !== null) {
894-
const str = Monogatari.global ('textObject').strings [0];
895-
const element = $_(Monogatari.global ('textObject').el).data ('ui');
896-
Monogatari.global ('textObject').destroy ();
897-
if (element == 'centered') {
898-
$_('[data-ui="centered"]').html (str);
899-
} else {
900-
$_('[data-ui="say"]').html (str);
901-
}
902-
Monogatari.global ('finishedTyping', true);
903-
} else {
904-
Monogatari.action ('Centered').hide();
905-
Monogatari.shutUp();
906-
Monogatari.next ();
907-
}
908-
}).catch (() => {
909-
// An action waiting for user interaction or something else
910-
// is blocking the game.
911-
});
912-
}
913-
914880
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
915881
//
916882

@@ -926,8 +892,6 @@ class Monogatari {
926892
// possibly special handling to avoid futile "catch up" run
927893
}
928894
Monogatari.canProceed ().then (() => {
929-
Monogatari.action ('Centered').hide();
930-
Monogatari.shutUp();
931895
Monogatari.next ();
932896
expected += interval;
933897
setTimeout (Monogatari.global ('_AutoPlayTimer'), Math.max (0, interval - now)); // take into account drift
@@ -954,9 +918,6 @@ class Monogatari {
954918
// Function to execute the previous statement in the script.
955919
static revert () {
956920

957-
Monogatari.action ('Centered').hide ();
958-
Monogatari.shutUp ();
959-
960921
if (Monogatari.state ('step') >= 1) {
961922

962923
for (const action of Monogatari.actions ()) {
@@ -1122,7 +1083,12 @@ class Monogatari {
11221083
});
11231084

11241085
$_('#game').click (function () {
1125-
Monogatari.continue ();
1086+
Monogatari.canProceed ().then (() => {
1087+
Monogatari.next ();
1088+
}).catch (() => {
1089+
// An action waiting for user interaction or something else
1090+
// is blocking the game.
1091+
});
11261092
});
11271093

11281094
$_('[data-action], [data-action] *').click(function () {
@@ -1243,7 +1209,12 @@ class Monogatari {
12431209
// Spacebar and Right Arrow
12441210
case 32:
12451211
case 39:
1246-
Monogatari.continue ();
1212+
Monogatari.canProceed ().then (() => {
1213+
Monogatari.next ();
1214+
}).catch (() => {
1215+
// An action waiting for user interaction or something else
1216+
// is blocking the game.
1217+
});
12471218
break;
12481219

12491220
// Left Arrow

dist/assets/images/.gitignore

Whitespace-only changes.

0 commit comments

Comments
 (0)