Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

2.8 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
import { describe, it, expect, beforeEach, vi } from 'vitest';
import {
    SpriteAnimationAPI,
    type SpriteAnimClip,
    type SpriteAnimEvent,
} from '../src/animation/SpriteAnimator';

import { shouldFireEvent } from '../src/animation/SpriteAnimator';

describe('SpriteAnimator Frame Events', () => {
    let anim: SpriteAnimationAPI;

    beforeEach(() => {
        anim = new SpriteAnimationAPI();
    });

    describe('shouldFireEvent', () => {
        it('fires when frame advances past event frame', () => {
            expect(shouldFireEvent(2, 1, 3, 5, false)).toBe(true);
        });

        it('fires on exact frame', () => {
            expect(shouldFireEvent(2, 1, 2, 5, false)).toBe(true);
        });

        it('does not fire when event frame not in range', () => {
            expect(shouldFireEvent(4, 1, 3, 5, false)).toBe(false);
        });

        it('fires across loop boundary', () => {
            expect(shouldFireEvent(1, 4, 1, 5, true)).toBe(true);
        });

        it('fires for frame 0 on loop wrap', () => {
            expect(shouldFireEvent(0, 3, 0, 4, true)).toBe(true);
        });

        it('does not fire across boundary without loop', () => {
            expect(shouldFireEvent(1, 4, 1, 5, false)).toBe(false);
        });
    });

    describe('onAnimEvent', () => {
        it('registers and removes listener', () => {
            const handler = vi.fn();
            const unsub = anim.onEvent(1 as any, handler);
            unsub();
        });
    });

    describe('onAnimEventGlobal', () => {
        it('registers and removes global listener', () => {
            const handler = vi.fn();
            const unsub = anim.onEventGlobal(handler);
            unsub();
        });
    });

    describe('removeAnimEventListeners', () => {
        it('removes all listeners for entity', () => {
            const handler = vi.fn();
            anim.onEvent(1 as any, handler);
            anim.onEvent(1 as any, handler);
            anim.removeEntityListeners(1 as any);
        });
    });

    describe('clip events definition', () => {
        it('stores events on clip', () => {
            const clip: SpriteAnimClip = {
                name: 'attack',
                frames: [{ texture: 1 as any }, { texture: 2 as any }, { texture: 3 as any }],
                fps: 10,
                loop: false,
                events: [
                    { frame: 1, name: 'hit' },
                    { frame: 2, name: 'end', data: { sound: 'swoosh' } },
                ],
            };
            anim.registerClip(clip);
            expect(clip.events).toHaveLength(2);
            expect(clip.events![0].name).toBe('hit');
            expect(clip.events![1].data).toEqual({ sound: 'swoosh' });
        });
    });
});