import {Sprite as PixiSprite} from '@pixi/react';

import {useAsset} from '@/context/assets.js';

export default function Sprite({entity, ...rest}) {
  const asset = useAsset(entity.Sprite.source);
  if (!asset) {
    return false;
  }
  let texture;
  if (asset.data.animations) {
    texture = asset.animations[entity.Sprite.animation][entity.Sprite.frame];
  }
  else {
    texture = asset.textures[''];
  }
  return (
    <PixiSprite
      anchor={entity.Sprite.anchor}
      scale={entity.Sprite.scale}
      texture={texture}
      x={Math.round(entity.Position.x)}
      y={Math.round(entity.Position.y)}
      {...rest}
    />
  );
}