import { PluginPureHook, BlockPair } from '@gedit/runtime-compiler'; import { FramesGameObject, FrameAnimation } from '@gedit/runtime-render'; import { transform } from './transform'; import { addGameObjectToMap, code } from '../common'; import { LocalComponent } from './utils'; function createAnimation( ctx: PluginPureHook, component: FramesGameObject, animation: FrameAnimation, textureSet: Set, ): void { const { animFrames: data } = component; const { texture } = data; const needConcat = texture.split('/')[0] === 'image'; ctx.print(`${code.createAnimation()}(`); ctx.push(); ctx.println(`key: '${texture}/${animation.name}',`); ctx.print('frames: '); ctx.push(BlockPair.bracket); animation.frames.forEach(frame => { ctx.push(); if (needConcat) { ctx.println(`key: '${texture}/${frame.name}',`); textureSet.add(`${texture}/${frame.name}`); } else { ctx.println(`frame: '${frame.name}',`); } ctx.pop(false); ctx.println(','); }); ctx.pop(false); ctx.println(','); if (!needConcat) { ctx.println(`defaultTextureKey: '${texture}',`); textureSet.add(`${texture}`); } if (animation.frameRate) { ctx.println(`frameRate: ${animation.frameRate},`); } if (animation.repeat) { ctx.println('repeat: -1,'); } // todo repeat ctx.pop(false); ctx.println(');'); } export function frames(ctx: PluginPureHook, component: FramesGameObject, textureSet: Set): void { const { animFrames: data, name, displayType } = component; const { assetAnimFrames, texture, isAutoPlay } = data; const { animations, animationIndex } = assetAnimFrames[texture]; const defaultAnimation = animations[animationIndex]; ctx.comment(`@type ${displayType}\n@name ${name}`); animations.forEach(animation => { createAnimation(ctx, component, animation, textureSet); }); const blk = new LocalComponent(ctx); blk.in(); ctx.println(`${code.create(component)}(0, 0, '')`); transform(ctx, component); if (isAutoPlay) { ctx.println(` .play('${texture}/${defaultAnimation.name}');`); } addGameObjectToMap(ctx, component); blk.out(); }