@@ -8,35 +8,37 @@ export class AnimatedSprite extends Renderer {
8
8
spritesheetData ?: SpriteSheetData ; // Parsed spritesheet data
9
9
loadedSheet ?: HTMLImageElement ; // Loaded image for the spritesheet
10
10
frames : Record < string , HTMLImageElement [ ] > = { } ; // Object to hold individual frame images for each animation
11
- lastFrameTime : number = 0 ; // Timestamp of the last frame update
12
11
currentFrameIndex : number = 0 ; // Index of the current frame being displayed
13
12
hasWarnedAboutTransform : boolean = false ; // Flag to prevent multiple warnings about missing Transform part
14
13
width : number ; // Width of the animated sprite
15
14
height : number ; // Height of the animated sprite
16
15
bouncing : boolean = false ; // Flag to indicate if the sprite animation is in reverse (bouncing)
17
16
currentAnimation : string = "default" ; // Current animation name
18
17
disableAntiAliasing : boolean = false ; // Option to disable anti-aliasing
18
+ webEngine : boolean = false ; // Flag to indicate if this is running in a web engine context
19
19
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 } ) {
21
21
super ( { width, height } ) ; // Call the parent constructor with empty imageSource
22
22
this . name = "AnimatedSprite" ;
23
23
this . debugEmoji = "🎞️" ; // Default emoji for debugging the animated sprite
24
24
this . spritesheet = spritesheet ;
25
25
this . width = width ;
26
26
this . height = height ;
27
27
this . ready = false ;
28
+ this . spritesheetImage = spritesheetImage ; // Optional image for the spritesheet
28
29
this . currentAnimation = startingAnimation || "default" ;
29
30
this . disableAntiAliasing = disableAntiAliasing ;
30
31
this . onAnimationComplete = onAnimationComplete ; // Set the callback for animation completion
31
-
32
+ this . webEngine = webEngine ; // Set the web engine flag
32
33
}
33
34
34
35
async onMount ( parent : Part ) {
35
36
super . onMount ( parent ) ;
36
37
parent . setSuperficialDimensions ( this . width , this . height ) ; // Set dimensions for the parent part
37
- console . log ( this . width , this . height ) ;
38
- console . log ( this . parent )
39
38
let spritesheetData : SpriteSheetData ;
39
+ if ( ! this . spritesheet ) {
40
+ return ;
41
+ }
40
42
if ( this . spritesheet . startsWith ( "data:application/json" ) ) {
41
43
spritesheetData = JSON . parse ( atob ( this . spritesheet . split ( ',' ) [ 1 ] ) ) ;
42
44
} else {
@@ -46,7 +48,6 @@ export class AnimatedSprite extends Renderer {
46
48
}
47
49
spritesheetData = await response . json ( ) as SpriteSheetData ; // Assuming the spritesheet is a JSON file
48
50
}
49
-
50
51
// Validate the data structure
51
52
if ( ! spritesheetData . frames || ! Array . isArray ( spritesheetData . frames ) ) {
52
53
throw new Error ( "Invalid spritesheet format: 'frames' array is missing or not an array." ) ;
@@ -62,12 +63,15 @@ export class AnimatedSprite extends Renderer {
62
63
}
63
64
64
65
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 ;
67
69
} 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
+ }
71
75
}
72
76
73
77
image . onerror = ( err ) => {
@@ -91,9 +95,9 @@ export class AnimatedSprite extends Renderer {
91
95
for ( const animation of Object . keys ( this . frames ) ) {
92
96
this . frames [ animation ] = new Array ( this . frames [ animation ] . length ) ;
93
97
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 ] ) ;
95
99
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 } .` ) ;
97
101
}
98
102
const frame : HTMLImageElement | null = this . frame ( frameIndex ! ) ;
99
103
if ( frame ) {
@@ -171,7 +175,6 @@ export class AnimatedSprite extends Renderer {
171
175
this . currentAnimation = animationName ;
172
176
this . currentFrameIndex = 0 ; // Reset to the first frame of the new animation
173
177
this . bouncing = bounce ?? this . spritesheetData . meta . animations [ animationName ] . bounce ?? false ; // Reset bouncing state
174
- this . lastFrameTime = performance . now ( ) ; // Reset frame timing
175
178
176
179
if ( loop !== undefined ) {
177
180
this . spritesheetData . meta . animations [ this . currentAnimation ] . loop = loop ;
@@ -180,10 +183,11 @@ export class AnimatedSprite extends Renderer {
180
183
console . warn ( `Animation '${ animationName } ' does not exist in spritesheet for animated sprite <${ this . name } > attached to ${ this . parent ?. name } .` ) ;
181
184
}
182
185
}
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
+ }
187
191
const duration = ( this . spritesheetData ?. frames [ this . currentFrameIndex ] . duration || 100 )
188
192
if ( this . ready && this . spritesheetData ) {
189
193
if ( delta > duration ) {
@@ -220,7 +224,6 @@ export class AnimatedSprite extends Renderer {
220
224
// Stay at the last frame if we've reached it
221
225
}
222
226
}
223
- this . lastFrameTime = now ;
224
227
}
225
228
const transform = this . sibling < Transform > ( "Transform" ) ;
226
229
if ( ! transform ) {
0 commit comments