11import Action from './../lib/Action' ;
22import { $_ } from '@aegis-framework/artemis' ;
3+ import { FancyError } from './../lib/FancyError' ;
34import { ActionApplyResult , ActionRevertResult , ActionInstance , CharacterHistoryItem } from '../lib/types' ;
45
56export class ShowCharacter extends Action {
@@ -81,12 +82,36 @@ export class ShowCharacter extends Action {
8182 }
8283
8384 } else {
84- // TODO: Add Fancy Error when the specified character does not exist
8585 this . sprite = '' ;
8686 this . classes = [ ] ;
8787 }
8888 }
8989
90+ override async willApply ( ) : Promise < void > {
91+ if ( typeof this . character === 'undefined' ) {
92+ FancyError . show ( 'action:show_character:character_not_found' , {
93+ asset : this . asset ,
94+ availableCharacters : Object . keys ( this . engine . characters ( ) ) ,
95+ statement : `<code class='language=javascript'>"${ this . _statement } "</code>` ,
96+ label : this . engine . state ( 'label' ) ,
97+ step : this . engine . state ( 'step' )
98+ } ) ;
99+ throw new Error ( `Character "${ this . asset } " not found.` ) ;
100+ }
101+
102+ if ( typeof this . image === 'undefined' ) {
103+ FancyError . show ( 'action:show_character:sprite_not_found' , {
104+ asset : this . asset ,
105+ sprite : this . sprite ,
106+ availableSprites : this . character . sprites ? Object . keys ( this . character . sprites ) : [ ] ,
107+ statement : `<code class='language=javascript'>"${ this . _statement } "</code>` ,
108+ label : this . engine . state ( 'label' ) ,
109+ step : this . engine . state ( 'step' )
110+ } ) ;
111+ throw new Error ( `Sprite "${ this . sprite } " not found for character "${ this . asset } ".` ) ;
112+ }
113+ }
114+
90115 override async apply ( ) : Promise < void > {
91116 // show [character] [expression] at [position] with [animation] [infinite]
92117 // 0 1 2 3 4 5 6 7
@@ -467,16 +492,14 @@ export class ShowCharacter extends Action {
467492 }
468493 return ;
469494 } else {
470- if ( typeof this . image === 'object' ) {
471- for ( let j = characterLayerHistory . length - 1 ; j >= 0 ; j -- ) {
472- const { parent } = characterLayerHistory [ j ] ;
473- if ( typeof parent === 'string' ) {
474- const [ , , _asset ] = parent . split ( ' ' ) ;
475-
476- if ( _asset === this . asset ) {
477- characterLayerHistory . splice ( j , 1 ) ;
478- break ;
479- }
495+ for ( let j = characterLayerHistory . length - 1 ; j >= 0 ; j -- ) {
496+ const { parent } = characterLayerHistory [ j ] ;
497+ if ( typeof parent === 'string' ) {
498+ const [ , , _asset ] = parent . split ( ' ' ) ;
499+
500+ if ( _asset === this . asset ) {
501+ characterLayerHistory . splice ( j , 1 ) ;
502+ break ;
480503 }
481504 }
482505 }
@@ -498,27 +521,32 @@ export class ShowCharacter extends Action {
498521 // If the script didn't return on the for cycle above, it means either the
499522 // history didn't have any items left or, the character was not found.
500523 // In that case, we simply remove the character from the state.
501- this . engine . state ( {
524+ const updatedState : Record < string , unknown > = {
502525 characters : [
503526 ...this . engine . state ( 'characters' ) . filter ( ( item : any ) => {
504527 if ( typeof item === 'string' ) {
505- const [ show , character , asset , sprite ] = item . split ( ' ' ) ;
528+ const [ , , asset ] = item . split ( ' ' ) ;
506529 return asset !== this . asset ;
507530 }
508531 return false ;
509532 } ) ,
510533 ] ,
511- characterLayers : [
534+ } ;
535+
536+ if ( this . engine . setting ( 'ExperimentalFeatures' ) === true ) {
537+ updatedState . characterLayers = [
512538 ...this . engine . state ( 'characterLayers' ) . filter ( ( item : any ) => {
513539 if ( typeof item === 'string' ) {
514- const [ show , character , asset , sprite ] = item . split ( ' ' ) ;
515- const [ id , layer ] = asset . split ( ':' ) ;
540+ const [ , , asset ] = item . split ( ' ' ) ;
541+ const [ id ] = asset . split ( ':' ) ;
516542 return id !== this . asset ;
517543 }
518544 return false ;
519545 } ) ,
520- ]
521- } ) ;
546+ ] ;
547+ }
548+
549+ this . engine . state ( updatedState ) ;
522550 }
523551
524552 override async didRevert ( ) : Promise < ActionRevertResult > {
0 commit comments