about summary refs log blame commit diff stats
path: root/html/rogue/js/animation.js
blob: d682b01e1add1630f74306c1b282e2f914a3fea3 (plain) (tree)




















































                                                                                     
const Animation = {
    // Track loaded images with their paths as keys
    images: new Map(),
    
    // Load an image and return a promise
    loadImage(path) {
        if (this.images.has(path)) {
            return Promise.resolve(this.images.get(path));
        }
        
        return new Promise((resolve, reject) => {
            const img = new Image();
            img.onload = () => {
                this.images.set(path, img);
                resolve(img);
            };
            img.onerror = () => reject(new Error(`Failed to load image: ${path}`));
            img.src = path;
        });
    },
    
    // Animation class to handle sprite animations
    createAnimation(frames, frameTime) {
        return {
            frames,
            frameTime,
            currentFrame: 0,
            lastFrameTime: 0,
            
            update(currentTime) {
                if (currentTime - this.lastFrameTime >= this.frameTime) {
                    this.currentFrame = (this.currentFrame + 1) % this.frames.length;
                    this.lastFrameTime = currentTime;
                }
                return this.frames[this.currentFrame];
            }
        };
    },
    
    // Add this new method to scale sprites to fit within bounds
    scaleToFit(image, maxWidth, maxHeight) {
        const scale = Math.min(
            maxWidth / image.width,
            maxHeight / image.height
        );
        
        return {
            width: image.width * scale,
            height: image.height * scale,
            scale
        };
    }
};