@@ -8,35 +8,37 @@ export class AnimatedSprite extends Renderer {
88 spritesheetData ?: SpriteSheetData ; // Parsed spritesheet data
99 loadedSheet ?: HTMLImageElement ; // Loaded image for the spritesheet
1010 frames : Record < string , HTMLImageElement [ ] > = { } ; // Object to hold individual frame images for each animation
11- lastFrameTime : number = 0 ; // Timestamp of the last frame update
1211 currentFrameIndex : number = 0 ; // Index of the current frame being displayed
1312 hasWarnedAboutTransform : boolean = false ; // Flag to prevent multiple warnings about missing Transform part
1413 width : number ; // Width of the animated sprite
1514 height : number ; // Height of the animated sprite
1615 bouncing : boolean = false ; // Flag to indicate if the sprite animation is in reverse (bouncing)
1716 currentAnimation : string = "default" ; // Current animation name
1817 disableAntiAliasing : boolean = false ; // Option to disable anti-aliasing
18+ webEngine : boolean = false ; // Flag to indicate if this is running in a web engine context
1919 onAnimationComplete ?: ( animationName : string , sprite : AnimatedSprite ) => void ; // Callback for when an animation completes
20- constructor ( { spritesheet, spritesheetImage, width, height, startingAnimation, disableAntiAliasing = false , onAnimationComplete } : { spritesheet : string , spritesheetImage ?: string , width : number , height : number , startingAnimation ?: string , disableAntiAliasing ?: boolean , onAnimationComplete ?: ( animationName : string , sprite : AnimatedSprite ) => void } ) {
20+ constructor ( { spritesheet, spritesheetImage, width, height, startingAnimation, disableAntiAliasing = false , onAnimationComplete, webEngine = false } : { spritesheet : string , spritesheetImage ?: string , width : number , height : number , startingAnimation ?: string , disableAntiAliasing ?: boolean , onAnimationComplete ?: ( animationName : string , sprite : AnimatedSprite ) => void , webEngine ?: boolean } ) {
2121 super ( { width, height } ) ; // Call the parent constructor with empty imageSource
2222 this . name = "AnimatedSprite" ;
2323 this . debugEmoji = "🎞️" ; // Default emoji for debugging the animated sprite
2424 this . spritesheet = spritesheet ;
2525 this . width = width ;
2626 this . height = height ;
2727 this . ready = false ;
28+ this . spritesheetImage = spritesheetImage ; // Optional image for the spritesheet
2829 this . currentAnimation = startingAnimation || "default" ;
2930 this . disableAntiAliasing = disableAntiAliasing ;
3031 this . onAnimationComplete = onAnimationComplete ; // Set the callback for animation completion
31-
32+ this . webEngine = webEngine ; // Set the web engine flag
3233 }
3334
3435 async onMount ( parent : Part ) {
3536 super . onMount ( parent ) ;
3637 parent . setSuperficialDimensions ( this . width , this . height ) ; // Set dimensions for the parent part
37- console . log ( this . width , this . height ) ;
38- console . log ( this . parent )
3938 let spritesheetData : SpriteSheetData ;
39+ if ( ! this . spritesheet ) {
40+ return ;
41+ }
4042 if ( this . spritesheet . startsWith ( "data:application/json" ) ) {
4143 spritesheetData = JSON . parse ( atob ( this . spritesheet . split ( ',' ) [ 1 ] ) ) ;
4244 } else {
@@ -46,7 +48,6 @@ export class AnimatedSprite extends Renderer {
4648 }
4749 spritesheetData = await response . json ( ) as SpriteSheetData ; // Assuming the spritesheet is a JSON file
4850 }
49-
5051 // Validate the data structure
5152 if ( ! spritesheetData . frames || ! Array . isArray ( spritesheetData . frames ) ) {
5253 throw new Error ( "Invalid spritesheet format: 'frames' array is missing or not an array." ) ;
@@ -62,12 +63,15 @@ export class AnimatedSprite extends Renderer {
6263 }
6364
6465 const image = new Image ( ) ;
65- if ( spritesheetImage ) {
66- image . src = spritesheetImage ;
66+ // If spritesheetImage is provided, use it directly. Otherwise, try to resolve from spritesheet data.
67+ if ( this . spritesheetImage ) {
68+ image . src = this . spritesheetImage ;
6769 } else {
68- const relativeToSpritesheet = this . spritesheet . startsWith ( "data:" ) ? "" : this . spritesheet . split ( "/" ) . slice ( 0 , - 1 ) . join ( "/" ) ;
69- const path = spritesheetData . meta . image . startsWith ( "http" ) ? spritesheetData . meta . image : new URL ( relativeToSpritesheet + "/" + spritesheetData . meta . image , window . location . href ) . href ; // Handle relative paths
70- image . src = path ; // Set the image source to the spritesheet image path
70+ if ( ! this . webEngine ) {
71+ const relativeToSpritesheet = this . spritesheet . startsWith ( "data:" ) ? "" : this . spritesheet . split ( "/" ) . slice ( 0 , - 1 ) . join ( "/" ) ;
72+ const path = spritesheetData . meta . image . startsWith ( "http" ) ? spritesheetData . meta . image : new URL ( relativeToSpritesheet + "/" + spritesheetData . meta . image , window . location . href ) . href ; // Handle relative paths
73+ image . src = path ; // Set the image source to the spritesheet image path
74+ }
7175 }
7276
7377 image . onerror = ( err ) => {
@@ -91,9 +95,9 @@ export class AnimatedSprite extends Renderer {
9195 for ( const animation of Object . keys ( this . frames ) ) {
9296 this . frames [ animation ] = new Array ( this . frames [ animation ] . length ) ;
9397 for ( let i = 0 ; i < this . frames [ animation ] . length ; i ++ ) {
94- const frameIndex = this . spritesheetData ?. frames . findIndex ( frame => frame . filename === data . meta . animations [ animation ] . frames [ i ] ) ;
98+ const frameIndex = this . spritesheetData ?. frames . findIndex ( frame => frame . filename === this . spritesheetData ! . meta . animations [ animation ] . frames [ i ] ) ;
9599 if ( frameIndex === - 1 ) {
96- throw new Error ( `Frame '${ data . meta . animations [ animation ] . frames [ i ] } ' does not exist in spritesheet for animated sprite <${ this . name } > attached to ${ this . parent ?. name } .` ) ;
100+ throw new Error ( `Frame '${ this . spritesheetData ! . meta . animations [ animation ] . frames [ i ] } ' does not exist in spritesheet for animated sprite <${ this . name } > attached to ${ this . parent ?. name } .` ) ;
97101 }
98102 const frame : HTMLImageElement | null = this . frame ( frameIndex ! ) ;
99103 if ( frame ) {
@@ -171,7 +175,6 @@ export class AnimatedSprite extends Renderer {
171175 this . currentAnimation = animationName ;
172176 this . currentFrameIndex = 0 ; // Reset to the first frame of the new animation
173177 this . bouncing = bounce ?? this . spritesheetData . meta . animations [ animationName ] . bounce ?? false ; // Reset bouncing state
174- this . lastFrameTime = performance . now ( ) ; // Reset frame timing
175178
176179 if ( loop !== undefined ) {
177180 this . spritesheetData . meta . animations [ this . currentAnimation ] . loop = loop ;
@@ -180,10 +183,11 @@ export class AnimatedSprite extends Renderer {
180183 console . warn ( `Animation '${ animationName } ' does not exist in spritesheet for animated sprite <${ this . name } > attached to ${ this . parent ?. name } .` ) ;
181184 }
182185 }
183- act ( ) {
184- super . act ( ) ;
185- const now = performance . now ( ) ;
186- const delta = now - this . lastFrameTime ;
186+ act ( delta : number ) {
187+ super . act ( delta ) ;
188+ if ( ! this . ready ) {
189+ return ;
190+ }
187191 const duration = ( this . spritesheetData ?. frames [ this . currentFrameIndex ] . duration || 100 )
188192 if ( this . ready && this . spritesheetData ) {
189193 if ( delta > duration ) {
@@ -220,7 +224,6 @@ export class AnimatedSprite extends Renderer {
220224 // Stay at the last frame if we've reached it
221225 }
222226 }
223- this . lastFrameTime = now ;
224227 }
225228 const transform = this . sibling < Transform > ( "Transform" ) ;
226229 if ( ! transform ) {
0 commit comments